Module: Junkfood::Assert

Defined in:
lib/junkfood/assert.rb

Overview

A dumbed down implementation for Assertions based on the ‘wrong’ gem, but without any of wrong’s awesomeness. This is just for the future in case ‘wrong’ disappears from the scene.

Examples:

class MyClass
  include Junkfood::Assert
  def my_method
    assert { true }
    assert('Custom Error Message') { false }
  rescue Junkfood::Assert::AssertionFailedError => e
    puts e
  end
end

Defined Under Namespace

Classes: AssertionFailedError

Instance Method Summary collapse

Instance Method Details

#assert { ... } ⇒ Object #assert(message) { ... } ⇒ Object

Tests an assertion claim.

Examples:

x = 3
assert { x == 2 }
assert('failure message') { x == 1 }

Overloads:

  • #assert { ... } ⇒ Object

    Yields:

    • block with predicates to determine whether or not to raise an AssertionFailedError.

    Yield Returns:

    • (Boolean, nil)
  • #assert(message) { ... } ⇒ Object

    Parameters:

    • message (String)

      the custom error message of for the AssertionFailedError.

    Yields:

    • block with predicates to determine whether or not to raise an AssertionFailedError.

    Yield Returns:

    • (Boolean, nil)

Raises:

  • AssertionFailedError when the block yields a false/nil value.

See Also:



66
67
68
69
70
71
72
73
# File 'lib/junkfood/assert.rb', line 66

def assert(*args, &block)
  begin
    return super if block.nil?
  rescue NoMethodError
    raise 'You must pass a block for assertion check'
  end
  raise AssertionFailedError, args.first unless yield
end