Class: Tiramisu::Assert

Inherits:
Object
  • Object
show all
Defined in:
lib/tiramisu/assert.rb

Instance Method Summary collapse

Constructor Details

#initialize(object, action = :assert, caller = nil) ⇒ Assert

Returns a new instance of Assert.



4
5
6
7
8
9
10
# File 'lib/tiramisu/assert.rb', line 4

def initialize object, action = :assert, caller = nil
  @object = object
  @caller = caller
  @assert = action == :assert
  @refute = action == :refute
  @assert || @refute || Kernel.raise(ArgumentError, 'action should be either :assert or :refute')
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a, &b) ⇒ Object

forward any missing method to tested object.

Examples:

assert(some_array).include? x
# object returned by `assert` does not respond to `include?`
# so `include?` is passed to `some_array`


31
32
33
# File 'lib/tiramisu/assert.rb', line 31

def method_missing m, *a, &b
  __assert__(m, a, b)
end

Instance Method Details

#__validate_expectations__Object



127
128
129
130
# File 'lib/tiramisu/assert.rb', line 127

def __validate_expectations__
  return unless @__expectation__
  @__expectation__.validate
end

#raise(type = nil, message = nil, &block) ⇒ Object Also known as: to_raise

Note:

if block given it will have precedence over arguments

ensure the given block raises as expected

Examples:

assertion pass if block raises whatever

assert {some code}.raise

assertion pass if block raises NameError

assert {some code}.raise NameError

assertion pass if block raises NameError and error message matches /blah/

assert {some code}.raise NameError, /blah/

assertion pass if block raises whatever error that matches /blah/

assert {some code}.raise nil, /blah/

assertion pass if validation block returns a positive value

assert {some code}.raise {|e| e.is_a?(NameError) && e.message =~ /blah/}

assertion pass if nothing raised

refute {some code}.raise
# same
fail_if {some code}.raise

assertion fails only if block raises a NameError.

it may raise whatever but NameError. if nothing raised assertion will fail.

fail_if {some code}.raise NameError

assertion pass if raised error does not match /blah/

if nothing raised assertion will fail.

fail_if {some code}.raise nil, /blah/

assertion will pass if raised error is not a NameError

and error message does not match /blah/
if nothing raised assertion will fail as well.

fail_if {some code}.raise NameError, /blah/


76
77
78
79
80
81
82
# File 'lib/tiramisu/assert.rb', line 76

def raise type = nil, message = nil, &block
  failure = Tiramisu.__send__(
    @assert ? :assert_raised_as_expected : :refute_raised_as_expected,
    @object, type, message, block
  )
  Tiramisu.fail(failure, caller[0]) if failure
end

#receive(expected_message) ⇒ Expectation Also known as: to_receive

ensure given mock will receive expected message by the end of test

Examples:

test :auth do
  user = mock(User.new)
  expect(user).to_receive(:password)
  user.authenticate
  # by the end of test user should receive :password message,
  # otherwise the test will fail
end

Parameters:

  • Symbol

    expected message

Returns:



121
122
123
124
# File 'lib/tiramisu/assert.rb', line 121

def receive expected_message
  Kernel.throw(:__tiramisu_status__, @object[:raised]) if @object[:raised]
  @__expectation__ = Expectation.new(@object[:returned], expected_message.to_sym, @assert, caller[0])
end

#throw(expected_symbol = nil, &block) ⇒ Object Also known as: to_throw

Note:

if block given it will have precedence over arguments

ensure given block thrown as expected

Examples:

assertion pass if any symbol thrown

assert {some code}.throw

assertion pass only if :x symbol thrown

assert {some code}.throw(:x)

assertion pass only if given block validates thrown symbol

assert {some code}.throw {|sym| sym == :x}


98
99
100
101
102
103
104
# File 'lib/tiramisu/assert.rb', line 98

def throw expected_symbol = nil, &block
  failure = Tiramisu.__send__(
    @assert ? :assert_thrown_as_expected : :refute_thrown_as_expected,
    @object, expected_symbol ? expected_symbol.to_sym : nil, block
  )
  Tiramisu.fail(failure, caller[0]) if failure
end