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
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, 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
-
#devaluate(actual) ⇒ Object
Supports negative/converse assertion testing.
- #error(e) ⇒ Object
-
#evaluate(actual) ⇒ Object
Supports positive assertion testing.
- #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.
34 35 36 |
# File 'lib/riot/assertion_macro.rb', line 34 def expects_exception @expects_exception end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
54 55 56 |
# File 'lib/riot/assertion_macro.rb', line 54 def file @file end |
#line ⇒ Object
Returns the value of attribute line.
54 55 56 |
# File 'lib/riot/assertion_macro.rb', line 54 def line @line end |
Class Method Details
.default ⇒ Object
The default macro.
37 38 39 |
# File 'lib/riot/assertion_macro.rb', line 37 def default @default_macro ||= new end |
.expects_exception! ⇒ Object
Specify that the macro expects an exception to be thrown by the assertion.
42 43 44 |
# File 'lib/riot/assertion_macro.rb', line 42 def expects_exception! @expects_exception = true end |
.register(name) ⇒ Object
Register the macro under the given name.
49 50 51 |
# File 'lib/riot/assertion_macro.rb', line 49 def register(name) Assertion.register_macro name, self end |
Instance Method Details
#devaluate(actual) ⇒ Object
Supports negative/converse assertion testing
68 69 70 |
# File 'lib/riot/assertion_macro.rb', line 68 def devaluate(actual) !actual ? pass : fail("Expected non-true but got #{actual.inspect} instead") end |
#error(e) ⇒ Object
58 |
# File 'lib/riot/assertion_macro.rb', line 58 def error(e) [:error, e]; end |
#evaluate(actual) ⇒ Object
Supports positive assertion testing
63 64 65 |
# File 'lib/riot/assertion_macro.rb', line 63 def evaluate(actual) actual ? pass : fail("Expected non-false but got #{actual.inspect} instead") end |
#expected_message(*phrases) ⇒ Object
76 |
# File 'lib/riot/assertion_macro.rb', line 76 def (*phrases) .expected(*phrases); end |
#expects_exception? ⇒ Boolean
60 |
# File 'lib/riot/assertion_macro.rb', line 60 def expects_exception?; self.class.expects_exception; end |
#fail(message) ⇒ Object
57 |
# File 'lib/riot/assertion_macro.rb', line 57 def fail() [:fail, .to_s, line, file]; end |
#new_message(*phrases) ⇒ Object
Messaging
74 |
# File 'lib/riot/assertion_macro.rb', line 74 def (*phrases) Message.new(*phrases); end |
#pass(message = nil) ⇒ Object
56 |
# File 'lib/riot/assertion_macro.rb', line 56 def pass(=nil) [:pass, .to_s]; end |
#should_have_message(*phrases) ⇒ Object
75 |
# File 'lib/riot/assertion_macro.rb', line 75 def (*phrases) .should_have(*phrases); end |