Class: Riot::AssertionMacro

Inherits:
Object show all
Defined in:
lib/riot/assertion_macro.rb

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(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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.expects_exceptionObject (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

#fileObject

Returns the value of attribute file.



54
55
56
# File 'lib/riot/assertion_macro.rb', line 54

def file
  @file
end

#lineObject

Returns the value of attribute line.



54
55
56
# File 'lib/riot/assertion_macro.rb', line 54

def line
  @line
end

Class Method Details

.defaultObject

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.

Parameters:

  • name (Symbol)

    the name of the macro



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 expected_message(*phrases) new_message.expected(*phrases); end

#expects_exception?Boolean

Returns:

  • (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(message) [:fail, message.to_s, line, file]; end

#new_message(*phrases) ⇒ Object

Messaging



74
# File 'lib/riot/assertion_macro.rb', line 74

def new_message(*phrases) Message.new(*phrases); end

#pass(message = nil) ⇒ Object



56
# File 'lib/riot/assertion_macro.rb', line 56

def pass(message=nil) [:pass, message.to_s]; end

#should_have_message(*phrases) ⇒ Object



75
# File 'lib/riot/assertion_macro.rb', line 75

def should_have_message(*phrases) new_message.should_have(*phrases); end