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



30
31
32
# File 'lib/riot/assertion_macro.rb', line 30

def expects_exception
  @expects_exception
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



50
51
52
# File 'lib/riot/assertion_macro.rb', line 50

def file
  @file
end

#lineObject

Returns the value of attribute line.



50
51
52
# File 'lib/riot/assertion_macro.rb', line 50

def line
  @line
end

Class Method Details

.defaultObject

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.

Parameters:

  • name (Symbol)

    the name of the macro



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

#expects_exception?Boolean

Returns:

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

#new_message(*phrases) ⇒ Object

Messaging



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

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

#pass(message = nil) ⇒ Object



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

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

#should_have_message(*phrases) ⇒ Object



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

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