module Assay::MiniTest::Assertions

This module holds the MiniTest assertion methods for MiniTest compatibility.

While it does not provide 100% of MiniTest’s assertions at the moment, compatibility is very close and will improved with upcoming releases.

TODO: Should we adjust error messages to be like MiniTests ?

Public Instance Methods

assert(truth, msg=nil) click to toggle source
# File lib/assay-minitest/assertions.rb, line 15
def assert(truth, msg=nil)
  Assertion.assert!(truth, :message=>msg)
end
assert_alike(exp, act, msg=nil) click to toggle source

Passes if actual is like expected, where ‘like` means satisfyin any one of `#===`, `#==`, `#eql?` or `#equal?` calls.

This is not strictly a Test::Unit assertion but is added here to cover all of Assay’s availabe assertion classes.

# File lib/assay-minitest/assertions.rb, line 35
def assert_alike(exp, act, msg=nil)
  LikeAssay.assert!(act, exp, :message=>msg, :backtrace=>caller)
end
assert_block(message="assert_block failed.", &block) click to toggle source
# File lib/assay-minitest/assertions.rb, line 50
def assert_block(message="assert_block failed.", &block)
  ExecutionAssay.assert!(:message=>message, &block)
end
assert_boolean(boolean, message=nil) click to toggle source

Passes if ‘boolean` is either `true` or `false`.

# File lib/assay-minitest/assertions.rb, line 57
def assert_boolean(boolean, message=nil)
  BooleanAssay.assert!(boolean, :message=>message)
end
assert_empty(exp, msg=nil) click to toggle source

Passes if object is empty.

assert_empty(object)
# File lib/assay-minitest/assertions.rb, line 79
def assert_empty(exp, msg=nil)
  EmptyAssay.assert!(exp, :message=>msg, :backtrace=>caller)
end
assert_equal(exp, act, msg=nil) click to toggle source

Passes if expected == +actual.

Note that the ordering of arguments is important, since a helpful error message is generated when this one fails that tells you the values of expected and actual.

assert_equal 'MY STRING', 'my string'.upcase
# File lib/assay-minitest/assertions.rb, line 99
def assert_equal(exp, act, msg=nil)
  EqualAssay.assert!(act, exp, :message=>msg, :backtrace=>caller)
end
assert_equivalent(exp, act, msg=nil) click to toggle source

Passes if expected .eql? actual.

Note that the ordering of arguments is important, since a helpful error message is generated when this one fails that tells you the values of expected and actual.

assert_equivalent 'MY STRING', 'my string'.upcase
# File lib/assay-minitest/assertions.rb, line 356
def assert_equivalent(exp, act, msg=nil)
  EqualityAssay.assert!(act, exp, :message=>msg, :backtrace=>caller)
end
assert_false(exp, msg=nil) click to toggle source

Passed if object is false.

assert_false(false)
# File lib/assay-minitest/assertions.rb, line 120
def assert_false(exp, msg=nil)
  FalseAssay.assert!(exp, :message=>msg, :backtrace=>caller)
end
assert_in_delta(exp, act, delta, msg=nil) click to toggle source

Passes if expected and actual are equal within delta tolerance.

assert_in_delta 0.05, (50000.0 / 10**6), 0.00001
# File lib/assay-minitest/assertions.rb, line 138
def assert_in_delta(exp, act, delta, msg=nil)
  WithinAssay.assert!(act, exp, delta, :message=>msg, :backtrace=>caller)
end
assert_in_epsilon(exp, act, epsilon=0.001, message=nil) click to toggle source

Passes if ‘expected_float` and `actual_float` are within `epsilon`.

# File lib/assay-minitest/assertions.rb, line 154
def assert_in_epsilon(exp, act, epsilon=0.001, message=nil) 
  delta = [exp, act].min * epsilon
  WithinAssay.assert!(act, exp, delta, :message=>message, :backtrace=>caller)
end
assert_includes(collection, member, message=nil) click to toggle source

Passes if ‘collection` contains `member`.

# File lib/assay-minitest/assertions.rb, line 170
def assert_includes(collection, member, message=nil) 
  IncludeAssay.assert!(collection, member, :message=>message, :backtrace=>caller)
end
assert_instance_of(cls, obj, msg=nil) click to toggle source

Passes if object is an instance of class.

assert_instance_of(String, 'foo')
# File lib/assay-minitest/assertions.rb, line 186
def assert_instance_of(cls, obj, msg=nil)
  InstanceAssay.assert!(obj, cls, :message=>msg, :backtrace=>caller)
end
assert_kind_of(cls, obj, msg=nil) click to toggle source

Passes if object .kind_of? klass

assert_kind_of(Object, 'foo')
# File lib/assay-minitest/assertions.rb, line 204
def assert_kind_of(cls, obj, msg=nil)
  KindAssay.assert!(obj, cls, :message=>msg, :backtrace=>caller)
end
assert_match(pattern, string, msg=nil) click to toggle source

Passes if object matches pattern using ‘#=~` method.

assert_match(/\d+/, 'five, 6, seven')
# File lib/assay-minitest/assertions.rb, line 222
def assert_match(pattern, string, msg=nil)
  MatchAssay.assert!(string, pattern, :message=>msg, :backtrace=>caller)
end
assert_nil(exp, msg=nil) click to toggle source

Passes if object is nil.

assert_nil(nil)
# File lib/assay-minitest/assertions.rb, line 249
def assert_nil(exp, msg=nil)
  NilAssay.assert!(exp, :message=>msg, :backtrace=>caller)
end
assert_no_match(pattern, string, msg=nil) click to toggle source

Passes if object does not match pattern using ‘#!~` method.

assert_no_match(/two/, 'one 2 three')
# File lib/assay-minitest/assertions.rb, line 240
def assert_no_match(pattern, string, msg=nil)
  NoMatchAssay.assert!(string, pattern, :message=>msg, :backtrace=>caller)
end
assert_nothing_raised(msg=nil, &block) click to toggle source

Passes if the block yields successfully.

refute_nothing_raised "Couldn't do the thing" do
  do_the_thing
end
# File lib/assay-minitest/assertions.rb, line 418
def assert_nothing_raised(msg=nil, &block)
  RescueAssay.refute!(Exception, :message=>msg, :backtrace=>caller, &block)
end
assert_operator(receiver, operator, operand, message=nil) click to toggle source
# File lib/assay-minitest/assertions.rb, line 372
def assert_operator(receiver, operator, operand, message=nil) 
  ExecutionAssay.assert!(:message=>message, :backtrace=>caller) do
    receiver.__send__(operator, operand)
  end
end
assert_output(stdout=nil, stderr=nil, &block) click to toggle source

Passes if a block outputs matching test to ‘stdout` or `staderr`.

This does not work exactly like the original MiniTest assertion in that it is an and condition between the two stdout and stderr, whereas the original is and or condition.

Note that this assertion translates into two separate underlying assertions, so counts for it may be double of what one might expect.

# File lib/assay-minitest/assertions.rb, line 272
def assert_output(stdout=nil, stderr=nil, &block)
  StdoutAssay.assert!(stdout, :backtrace=>caller, &block) if stdout
  StderrAssay.assert!(stderr, :backtrace=>caller, &block) if stderr
end
assert_predicate(object, predicate, message=nil) click to toggle source

Passes it predicate sent to object returns postively.

assert_predicate(10, :even?)
# File lib/assay-minitest/assertions.rb, line 311
def assert_predicate(object, predicate, message=nil) 
  ExecutionAssay.assert!(:message=>message, :backtrace=>caller) do
    object.__send__(predicate)
  end
end
assert_raises(*exceptions, &block) click to toggle source

Passes if the block raises given exception(s).

assert_raises RuntimeError do
  raise 'Boom!!!'
end
# File lib/assay-minitest/assertions.rb, line 394
def assert_raises(*exceptions, &block)
  msg = (Exception === exceptions.last ? exceptions.pop : nil)
  RaiseAssay.assert!(*exceptions, :message=>msg, :backtrace=>caller, &block)
end
assert_respond_to(reciever, method, msg=nil) click to toggle source

Passes if object respond_to? methods.

assert_respond_to 'bugbear', :slice
# File lib/assay-minitest/assertions.rb, line 333
def assert_respond_to(reciever, method, msg=nil)
  RespondAssay.assert!(reciever, method, :message=>msg, :backtrace=>caller)
end
Also aliased as: assert_responds_to
assert_responds_to(reciever, method, msg=nil)
Alias for: assert_respond_to
assert_same(exp, act, msg=nil) click to toggle source

Passes if actual is the same exact object as expected.

assert_same(object, object)
# File lib/assay-minitest/assertions.rb, line 438
def assert_same(exp, act, msg=nil)
  IdentityAssay.assert!(act, exp, :message=>msg, :backtrace=>caller)
end
assert_silent(msg=nil, &block) click to toggle source

Like {#assert_output} but ensures no output.

# File lib/assay-minitest/assertions.rb, line 295
def assert_silent(msg=nil, &block)
  SilentAssay.assert!(:message=>msg, :backtrace=>caller, &block)
end
assert_throws(expected, msg=nil, &blk) click to toggle source

Passes if the block throws ‘expected` object.

assert_throw :done do
  throw :done
end
# File lib/assay-minitest/assertions.rb, line 462
def assert_throws(expected, msg=nil, &blk)
  ThrowAssay.assert!(expected, :message=>msg, :backtrace=>caller, &blk)
end
assert_true(exp, msg=nil) click to toggle source

Passed if object is true.

# File lib/assay-minitest/assertions.rb, line 480
def assert_true(exp, msg=nil)
  TrueAssay.assert!(exp, :message=>msg, :backtrace=>caller)
end
refute(untruth, msg=nil) click to toggle source
# File lib/assay-minitest/assertions.rb, line 20
def refute(untruth, msg=nil)
  Assertion.refute!(untruth, :message=>msg)
end
refute_alike(exp, act, msg=nil) click to toggle source

Passes if actual is NOT like expected, where ‘like` means satisfyin any one of `#===`, `#==`, `#eql?` or `#equal?` calls.

# File lib/assay-minitest/assertions.rb, line 43
def refute_alike(exp, act, msg=nil)
  LikeAssay.refute!(act, exp, :message=>msg, :backtrace=>caller)
end
refute_boolean(boolean, message=nil) click to toggle source

Passes if ‘boolean` is neither `true` or `false`.

# File lib/assay-minitest/assertions.rb, line 64
def refute_boolean(boolean, message=nil)
  BooleanAssay.refute!(boolean, :message=>message)
end
refute_empty(exp, msg=nil) click to toggle source

Passes if object is not empty.

refute_empty(object)
# File lib/assay-minitest/assertions.rb, line 87
def refute_empty(exp, msg=nil)
  EmptyAssay.refute!(exp, :message=>msg, :backtrace=>caller)
end
refute_equal(exp, act, msg=nil) click to toggle source

Passes if expected != actual

refute_equal 'some string', 5
# File lib/assay-minitest/assertions.rb, line 107
def refute_equal(exp, act, msg=nil)
   EqualAssay.refute!(act, exp, :message=>msg, :backtrace=>caller)
end
refute_equivalent(criterion, act, msg=nil) click to toggle source

Passes if criterion is NOT equivalent to actual as tested using ‘#eql?`.

refute_equivalent 'some string', 5
# File lib/assay-minitest/assertions.rb, line 365
def refute_equivalent(criterion, act, msg=nil)
  EqualityAssay.refute!(act, criterion, :message=>msg, :backtrace=>caller)
end
refute_false(exp, msg=nil) click to toggle source

Passed if object is not false.

refute_false(false)
# File lib/assay-minitest/assertions.rb, line 129
def refute_false(exp, msg=nil)
  FalseAssay.refute!(exp, :message=>msg, :backtrace=>caller)
end
refute_in_delta(exp, act, delta, msg=nil) click to toggle source

Passes if expected and actual are equal not within delta tolerance.

refute_in_delta 0.05, (50000.0 / 10**6), 0.00001
# File lib/assay-minitest/assertions.rb, line 147
def refute_in_delta(exp, act, delta, msg=nil)
  WithinAssay.refute!(act, exp, delta, :message=>msg, :backtrace=>caller)
end
refute_in_epsilon(exp, act, epsilon=0.001, message=nil) click to toggle source

Passes if ‘expected_float` and `actual_float` are NOT within `epsilon`.

# File lib/assay-minitest/assertions.rb, line 162
def refute_in_epsilon(exp, act, epsilon=0.001, message=nil)
  delta = [exp, act].min * epsilon
  WithinAssay.refute!(act, exp, delta, :message=>message, :backtrace=>caller)
end
refute_includes(collection, member, message=nil) click to toggle source

Passes if ‘collection` does not contain `member`.

# File lib/assay-minitest/assertions.rb, line 177
def refute_includes(collection, member, message=nil)
  IncludeAssay.refute!(collection, member, :message=>message, :backtrace=>caller) 
end
refute_instance_of(cls, obj, msg=nil) click to toggle source

Passes if object is not an instance of class.

refute_instance_of(String, 500)
# File lib/assay-minitest/assertions.rb, line 195
def refute_instance_of(cls, obj, msg=nil)
  InstanceAssay.refute!(obj, cls, :message=>msg, :backtrace=>caller)
end
refute_kind_of(cls, obj, msg=nil) click to toggle source

Passes if object .kind_of? klass

refute_kind_of(Object, 'foo')
# File lib/assay-minitest/assertions.rb, line 213
def refute_kind_of(cls, obj, msg=nil)
  KindAssay.refute!(obj, cls, :message=>msg, :backtrace=>caller)
end
refute_match(pattern, string, msg=nil) click to toggle source

Passes if object does not match pattern using ‘#=~` method.

assert_no_match(/two/, 'one 2 three')
# File lib/assay-minitest/assertions.rb, line 231
def refute_match(pattern, string, msg=nil)
  MatchAssay.refute!(string, pattern, :message=>msg, :backtrace=>caller)
end
refute_nil(exp, msg=nil) click to toggle source

Passes if object is not nil.

refute_nil(true)
# File lib/assay-minitest/assertions.rb, line 258
def refute_nil(exp, msg=nil)
  NilAssay.refute!(exp, :message=>msg, :backtrace=>caller)
end
refute_nothing_raised(msg=nil, &block) click to toggle source

Passes if the block yields successfully.

refute_nothing_raised "Couldn't do the thing" do
  do_the_thing
end
# File lib/assay-minitest/assertions.rb, line 429
def refute_nothing_raised(msg=nil, &block)
  RescueAssay.assert!(Exception, :message=>msg, :backtrace=>caller, &block)
end
refute_operator(receiver, operator, operand, message=nil) click to toggle source
# File lib/assay-minitest/assertions.rb, line 381
def refute_operator(receiver, operator, operand, message=nil) 
  ExecutionAssay.refute!(:message=>message, :backtrace=>caller) do
    receiver.__send__(operator, operand)
  end
end
refute_output(stdout=nil, stderr=nil, &block) click to toggle source

Passes if a block outputs matching test to ‘stdout` or `staderr`.

This does not work exactly like the original MiniTest assertion in that it is an and condition between the two stdout and stderr, whereas the original is and or condition.

Note that this assertion translates into two separate underlying assertions, so counts for it may be double of what one might expect.

# File lib/assay-minitest/assertions.rb, line 287
def refute_output(stdout=nil, stderr=nil, &block)
  StdoutAssay.refute!(stdout, :backtrace=>caller, &block) if stdout
  StderrAssay.refute!(stderr, :backtrace=>caller, &block) if stderr
end
refute_predicate(object, predicate, message=nil) click to toggle source

Passes it predicate sent to object returns negatively.

refute_predicate(10, :odd?)
# File lib/assay-minitest/assertions.rb, line 322
def refute_predicate(object, predicate, message=nil) 
  ExecutionAssay.refute!(:message=>message, :backtrace=>caller) do
    object.__send__(predicate)
  end
end
refute_raises(*exceptions, &block) click to toggle source

Passes if the block *does not* raise given exception(s).

refute_raises RuntimeError do
  raise 'Boom!!!'
end
# File lib/assay-minitest/assertions.rb, line 406
def refute_raises(*exceptions, &block)
  msg = (Exception === exceptions.last ? exceptions.pop : nil)
  RaiseAssay.refute!(*exceptions, :message=>msg, :backtrace=>caller, &block)
end
refute_respond_to(reciever, method, msg=nil) click to toggle source

Passes if object does not respond_to? methods.

refute_respond_to 'bugbear', :slice
# File lib/assay-minitest/assertions.rb, line 343
def refute_respond_to(reciever, method, msg=nil)
  RespondAssay.refute!(reciever, method, :message=>msg, :backtrace=>caller)
end
refute_same(exp, act, msg=nil) click to toggle source

Passes if actual is not the same exact object as expected.

refute_same(object, other)
# File lib/assay-minitest/assertions.rb, line 447
def refute_same(exp, act, msg=nil)
  IdentityAssay.refute!(act, exp, :message=>msg, :backtrace=>caller)
end
refute_silent(msg=nil, &block) click to toggle source

Like {#refute_output} but ensures some output.

# File lib/assay-minitest/assertions.rb, line 302
def refute_silent(msg=nil, &block)
  SilentAssay.refute!(:message=>msg, :backtrace=>caller, &block)
end
refute_throws(expected, msg=nil, &blk) click to toggle source

Passes if the block does not throws ‘expected` object.

refute_throws :done do
  throw :chimp
end
# File lib/assay-minitest/assertions.rb, line 473
def refute_throws(expected, msg=nil, &blk)
  ThrowAssay.refute!(expected, :message=>msg, :backtrace=>caller, &blk)
end
refute_true(exp, msg=nil) click to toggle source

Passed if object is not true.

refute_true(false)
# File lib/assay-minitest/assertions.rb, line 489
def refute_true(exp, msg=nil)
  TrueAssay.refute!(exp, :message=>msg, :backtrace=>caller)
end