Class: Riot::AssertionMacro
Overview
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((actual).to_be_empty)
end
def devaluate(actual)
actual.empty? ? fail((actual).to_not_be_empty) : pass(.is_empty)
end
end
Direct Known Subclasses
AnyMacro, AssignsMacro, EmptyMacro, EqualsMacro, EquivalentToMacro, ExistsMacro, IncludesMacro, KindOfMacro, MatchesMacro, NilMacro, RaisesKindOfMacro, RaisesMacro, RespondToMacro, SameElementsMacro, SizeMacro
Class Attribute Summary collapse
-
.expects_exception ⇒ Boolean
readonly
Whether the macro expects an exception to be thrown.
Instance Attribute Summary collapse
-
#file ⇒ String
During failure reporting, what file did the failure occur in.
-
#line ⇒ Number
During failure reporting, what line number did the failure occur at.
Class Method Summary collapse
-
.expects_exception! ⇒ Object
Specify that the macro expects an exception to be thrown by the assertion.
-
.register(name) ⇒ Object
Register the macro under the given name.
Instance Method Summary collapse
-
#devaluate(actual) ⇒ Array
Supports negative/converse assertion testing.
-
#error(ex) ⇒ Array[Symbol, Exception]
Returns a status tuple indicating the assertion had an unexpected error.
-
#evaluate(actual) ⇒ Array
Supports positive assertion testing.
-
#expected_message(*phrases) ⇒ Riot::Message
Creates a new message for use in any macro response that will start as “expected ”.
-
#expects_exception? ⇒ boolean
Returns
true
if this macro expects to handle Exceptions during evaluation. -
#fail(message) ⇒ Array[Symbol, String, Number, String]
Returns a status tuple indicating the assertion failed and where it failed it if that can be determined.
-
#new_message(*phrases) ⇒ Riot::Message
Creates a new message for use in any macro response that is initially empty.
-
#pass(message = nil) ⇒ Array[Symbol, String]
Returns a status tuple indicating the assertion passed.
-
#should_have_message(*phrases) ⇒ Riot::Message
Creates a new message for use in any macro response that will start as “should have ”.
Class Attribute Details
.expects_exception ⇒ Boolean (readonly)
Whether the macro expects an exception to be thrown.
36 37 38 |
# File 'lib/riot/assertion_macro.rb', line 36 def expects_exception @expects_exception end |
Instance Attribute Details
#file ⇒ String
During failure reporting, what file did the failure occur in
65 66 67 |
# File 'lib/riot/assertion_macro.rb', line 65 def file @file end |
#line ⇒ Number
During failure reporting, what line number did the failure occur at
61 62 63 |
# File 'lib/riot/assertion_macro.rb', line 61 def line @line end |
Class Method Details
.expects_exception! ⇒ Object
Specify that the macro expects an exception to be thrown by the assertion.
47 48 49 |
# File 'lib/riot/assertion_macro.rb', line 47 def expects_exception! @expects_exception = true end |
.register(name) ⇒ Object
Register the macro under the given name.
54 55 56 |
# File 'lib/riot/assertion_macro.rb', line 54 def register(name) Assertion.register_macro name, self end |
Instance Method Details
#devaluate(actual) ⇒ Array
Supports negative/converse assertion testing. This is also where magic happens.
103 104 105 |
# 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) ⇒ Array[Symbol, Exception]
Returns a status tuple indicating the assertion had an unexpected error.
84 |
# File 'lib/riot/assertion_macro.rb', line 84 def error(ex) [:error, ex]; end |
#evaluate(actual) ⇒ Array
Supports positive assertion testing. This is where magic happens.
95 96 97 |
# 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) ⇒ Riot::Message
Creates a new message for use in any macro response that will start as “expected ”.
123 |
# File 'lib/riot/assertion_macro.rb', line 123 def (*phrases) .expected(*phrases); end |
#expects_exception? ⇒ boolean
Returns true
if this macro expects to handle Exceptions during evaluation.
89 |
# File 'lib/riot/assertion_macro.rb', line 89 def expects_exception?; self.class.expects_exception; end |
#fail(message) ⇒ Array[Symbol, String, Number, String]
Returns a status tuple indicating the assertion failed and where it failed it if that can be determined.
78 |
# File 'lib/riot/assertion_macro.rb', line 78 def fail() [:fail, .to_s, line, file]; end |
#new_message(*phrases) ⇒ Riot::Message
Creates a new message for use in any macro response that is initially empty.
111 |
# File 'lib/riot/assertion_macro.rb', line 111 def (*phrases) Message.new(*phrases); end |
#pass(message = nil) ⇒ Array[Symbol, String]
Returns a status tuple indicating the assertion passed.
71 |
# File 'lib/riot/assertion_macro.rb', line 71 def pass(=nil) [:pass, .to_s]; end |
#should_have_message(*phrases) ⇒ Riot::Message
Creates a new message for use in any macro response that will start as “should have ”.
117 |
# File 'lib/riot/assertion_macro.rb', line 117 def (*phrases) .should_have(*phrases); end |