class Riot::AssertionMacro

The base class for all assertion macros.

Using macros

Macros are applied to the return value of assertions. For example, the empty macro asserts that the value is empty or denies that it is empty e.g.

asserts(:comments).empty?
denies(:comments).empty?

Writing your own macros

Macros are added by subclassing {AssertionMacro}. For example, here's the implementation of empty:

class EmptyMacro < AssertionMacro
  register :empty

  def evaluate(actual)
    actual.length == 0 ? pass : fail(expected_message(actual).to_be_empty)
  end

  def devaluate(actual)
    actual.empty? ? fail(expected_message(actual).to_not_be_empty) : pass(new_message.is_empty)
  end
end

Attributes

expects_exception[R]

Whether the macro expects an exception to be thrown.

@return [Boolean]

file[RW]

During failure reporting, what file did the failure occur in @return [String]

line[RW]

During failure reporting, what line number did the failure occur at @return [Number]

Public Instance Methods

default() click to toggle source

@private The default macro.

@return [Riot::AssertionMacro]

# File lib/riot/assertion_macro.rb, line 42
def default
  @default_macro ||= new
end
devaluate(actual) click to toggle source

Supports negative/converse assertion testing. This is also where magic happens.

@param [Object] actual the value returned from evaling the {Riot::Assertion Assertion} block @return [Array] response from either {#pass}, {#fail} or {#error}

# File lib/riot/assertion_macro.rb, line 103
def devaluate(actual)
  !actual ? pass : fail("Expected non-true but got #{actual.inspect} instead")
end
error(ex) click to toggle source

Returns a status tuple indicating the assertion had an unexpected error.

@param [Exception] ex the Exception that was captured @return [Array[Symbol, Exception]]

# File lib/riot/assertion_macro.rb, line 84
def error(ex) [:error, ex]; end
evaluate(actual) click to toggle source

Supports positive assertion testing. This is where magic happens.

@param [Object] actual the value returned from evaling the {Riot::Assertion Assertion} block @return [Array] response from either {#pass}, {#fail} or {#error}

# File lib/riot/assertion_macro.rb, line 95
def evaluate(actual)
  actual ? pass : fail("Expected non-false but got #{actual.inspect} instead")
end
expected_message(*phrases) click to toggle source

Creates a new message for use in any macro response that will start as “expected ”.

@param [Array<Object>] *phrases array of object whose values will be inspected and added to message @return [Riot::Message]

# File lib/riot/assertion_macro.rb, line 123
def expected_message(*phrases) new_message.expected(*phrases); end
expects_exception!() click to toggle source

Specify that the macro expects an exception to be thrown by the assertion.

# File lib/riot/assertion_macro.rb, line 47
def expects_exception!
  @expects_exception = true
end
expects_exception?() click to toggle source

Returns true if this macro expects to handle Exceptions during evaluation.

@return [boolean]

# File lib/riot/assertion_macro.rb, line 89
def expects_exception?; self.class.expects_exception; end
fail(message) click to toggle source

Returns a status tuple indicating the assertion failed and where it failed it if that can be determined.

@param [String] message the message to report with @return [Array[Symbol, String, Number, String]]

# File lib/riot/assertion_macro.rb, line 78
def fail(message) [:fail, message.to_s, line, file]; end
new_message(*phrases) click to toggle source

Creates a new message for use in any macro response that is initially empty.

@param [Array<Object>] *phrases array of object whose values will be inspected and added to message @return [Riot::Message]

# File lib/riot/assertion_macro.rb, line 111
def new_message(*phrases) Message.new(*phrases); end
pass(message=nil) click to toggle source

Returns a status tuple indicating the assertion passed.

@param [String] message the message to report with @return [Array[Symbol, String]]

# File lib/riot/assertion_macro.rb, line 71
def pass(message=nil) [:pass, message.to_s]; end
register(name) click to toggle source

Register the macro under the given name.

@param [String, Symbol] name the name of the macro

# File lib/riot/assertion_macro.rb, line 54
def register(name)
  Assertion.register_macro name, self
end
should_have_message(*phrases) click to toggle source

Creates a new message for use in any macro response that will start as “should have ”.

@param [Array<Object>] *phrases array of object whose values will be inspected and added to message @return [Riot::Message]

# File lib/riot/assertion_macro.rb, line 117
def should_have_message(*phrases) new_message.should_have(*phrases); end