Puppet Function: assert::true

Defined in:
lib/puppet/functions/assert/true.rb
Function type:
Ruby 4.x API

Overview

When given one parameter, returns true if the parameter is a boolean true, or raise a Puppet::Error if the parameter is false.

When the parameter is a boolean value, ‘assert::true` either returns true or raises a `Puppet::Error` if the parameter is not a boolean true.

~~~ puppet

([1,3,2].sort == [1,2,3]).assert::true

~~~

~~~ puppet

assert::true(2 + 2 == 1)

~~~

When a lambda and any number of parameters are provided it passes all parameters to the block and validates that the block returns boolean true. It returns true if the block returned true, or raises a Puppet::Error if the block returns anything other than boolean true.

~~~ puppet

[1,3,2]).assert::true |$x| {
  sort($x) == [1,2,3]
}

~~~

Examples:

expected to succeed

expected to raise an exception

lambda expected to return true

Signatures:

  • assert::true(Boolean $arg)Any

    Parameters:

    • arg (Boolean)

    Returns:

    • (Any)
  • assert::true(Any *$arg, Callable &$block)Any

    Parameters:

    • *arg (Any)
    • &block (Callable)

    Returns:

    • (Any)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/puppet/functions/assert/true.rb', line 30

Puppet::Functions.create_function(:'assert::true') do
  dispatch :assert do
    param 'Boolean', :arg
  end

  dispatch :assert_block do
    repeated_param 'Any', :arg
    block_param
  end

  def assert(arg)
    raise Puppet::Error, "assert::true failed because argument is #{arg} instead of true" unless arg == true
    return true if arg == true
  end

  def assert_block(*args)
    output = yield(*args)
    raise Puppet::Error, "assert::true failed because the block's return value is #{output} instead of true" unless output == true
    return true if output == true
  end
end