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, well, empty, e.g.
asserts(: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
end
Direct Known Subclasses
AnyMacro, AssignsMacro, EmptyMacro, EqualsMacro, EquivalentToMacro, ExistsMacro, IncludesMacro, KindOfMacro, MatchesMacro, NilMacro, NotMacro, RaisesMacro, RespondToMacro, SameElementsMacro, SizeMacro
Class Attribute Summary collapse
-
.expects_exception ⇒ Object
readonly
Whether the macro expects an exception to be thrown.
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#line ⇒ Object
Returns the value of attribute line.
Class Method Summary collapse
-
.default ⇒ Object
The default macro.
-
.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
- #error(e) ⇒ Object
- #evaluate(actual) ⇒ Object
- #expected_message(*phrases) ⇒ Object
- #expects_exception? ⇒ Boolean
- #fail(message) ⇒ Object
-
#new_message(*phrases) ⇒ Object
Messaging.
- #pass(message = nil) ⇒ Object
- #should_have_message(*phrases) ⇒ Object
Class Attribute Details
.expects_exception ⇒ Object (readonly)
Whether the macro expects an exception to be thrown.
30 31 32 |
# File 'lib/riot/assertion_macro.rb', line 30 def expects_exception @expects_exception end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
50 51 52 |
# File 'lib/riot/assertion_macro.rb', line 50 def file @file end |
#line ⇒ Object
Returns the value of attribute line.
50 51 52 |
# File 'lib/riot/assertion_macro.rb', line 50 def line @line end |
Class Method Details
.default ⇒ Object
The default macro.
33 34 35 |
# File 'lib/riot/assertion_macro.rb', line 33 def default @default_macro ||= new end |
.expects_exception! ⇒ Object
Specify that the macro expects an exception to be thrown by the assertion.
38 39 40 |
# File 'lib/riot/assertion_macro.rb', line 38 def expects_exception! @expects_exception = true end |
.register(name) ⇒ Object
Register the macro under the given name.
45 46 47 |
# File 'lib/riot/assertion_macro.rb', line 45 def register(name) Assertion.register_macro name, self end |
Instance Method Details
#error(e) ⇒ Object
54 |
# File 'lib/riot/assertion_macro.rb', line 54 def error(e) [:error, e]; end |
#evaluate(actual) ⇒ Object
58 59 60 |
# File 'lib/riot/assertion_macro.rb', line 58 def evaluate(actual) actual ? pass : fail("Expected non-false but got #{actual.inspect} instead") end |
#expected_message(*phrases) ⇒ Object
66 |
# File 'lib/riot/assertion_macro.rb', line 66 def (*phrases) .expected(*phrases); end |
#expects_exception? ⇒ Boolean
56 |
# File 'lib/riot/assertion_macro.rb', line 56 def expects_exception?; self.class.expects_exception; end |
#fail(message) ⇒ Object
53 |
# File 'lib/riot/assertion_macro.rb', line 53 def fail() [:fail, .to_s, line, file]; end |
#new_message(*phrases) ⇒ Object
Messaging
64 |
# File 'lib/riot/assertion_macro.rb', line 64 def (*phrases) Message.new(*phrases); end |
#pass(message = nil) ⇒ Object
52 |
# File 'lib/riot/assertion_macro.rb', line 52 def pass(=nil) [:pass, .to_s]; end |
#should_have_message(*phrases) ⇒ Object
65 |
# File 'lib/riot/assertion_macro.rb', line 65 def (*phrases) .should_have(*phrases); end |