class Tokyo::Assert
Public Class Methods
# File lib/tokyo/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
Public Instance Methods
# File lib/tokyo/assert.rb, line 127 def __validate_expectations__ return unless @__expectation__ @__expectation__.validate end
forward any missing method to tested object.
@example
assert(some_array).include? x # object returned by `assert` does not respond to `include?` # so `include?` is passed to `some_array`
# File lib/tokyo/assert.rb, line 31 def method_missing m, *a, &b __assert__(m, a, b) end
ensure the given block raises as expected
@note if block given it will have precedence over arguments
@example assertion pass if block raises whatever
assert {some code}.raise
@example assertion pass if block raises NameError
assert {some code}.raise NameError
@example assertion pass if block raises NameError and error message matches /blah/
assert {some code}.raise NameError, /blah/
@example assertion pass if block raises whatever error that matches /blah/
assert {some code}.raise nil, /blah/
@example assertion pass if validation block returns a positive value
assert {some code}.raise {|e| e.is_a?(NameError) && e.message =~ /blah/}
@example assertion pass if nothing raised
refute {some code}.raise # same fail_if {some code}.raise
@example 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
@example assertion pass if raised error does not match /blah/
if nothing raised assertion will fail. fail_if {some code}.raise nil, /blah/
@example 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/
# File lib/tokyo/assert.rb, line 76 def raise type = nil, message = nil, &block failure = Tokyo.__send__( @assert ? :assert_raised_as_expected : :refute_raised_as_expected, @object, type, message, block ) Tokyo.fail(failure, caller[0]) if failure end
ensure given mock will receive expected message by the end of test
@example
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
@param Symbol expected message @return [Expectation]
# File lib/tokyo/assert.rb, line 121 def receive expected_message Kernel.throw(:__tokyo_status__, @object[:raised]) if @object[:raised] @__expectation__ = Expectation.new(@object[:returned], expected_message.to_sym, @assert, caller[0]) end
ensure given block thrown as expected
@note if block given it will have precedence over arguments
@example assertion pass if any symbol thrown
assert {some code}.throw
@example assertion pass only if :x symbol thrown
assert {some code}.throw(:x)
@example assertion pass only if given block validates thrown symbol
assert {some code}.throw {|sym| sym == :x}
# File lib/tokyo/assert.rb, line 98 def throw expected_symbol = nil, &block failure = Tokyo.__send__( @assert ? :assert_thrown_as_expected : :refute_thrown_as_expected, @object, expected_symbol ? expected_symbol.to_sym : nil, block ) Tokyo.fail(failure, caller[0]) if failure end
Private Instance Methods
# File lib/tokyo/assert.rb, line 133 def __assert__ message, arguments, block Kernel.throw(:__tokyo_status__, @object[:raised]) if @object[:raised] result = __send_message__(@object[:returned], message, arguments, block) return true if (@assert && result) || (@refute && !result) Kernel.throw(:__tokyo_status__, AssertionFailure.new(@object[:returned], arguments, @caller)) end
# File lib/tokyo/assert.rb, line 140 def __send_message__ object, message, arguments, block if assertion = Tokyo.assertions[message.to_sym] return assertion.call(object, *arguments, &block) end object.__send__(message, *arguments, &block) end