module Assay::TestUnit::Assertions

This module holds the Test::Unit assertion methods for Test::Unit compatibility.

While it does not provide 100% of Test::Unit assertions at the moment, compatibility will improved with upcoming releases.

@see test-unit.rubyforge.org/test-unit/en/

@todo Should we adjust error message to be like Test::Units ?

Public Instance Methods

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-testunit/assertions.rb, line 27
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-testunit/assertions.rb, line 42
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-testunit/assertions.rb, line 49
def assert_boolean(boolean, message=nil)
  BooleanAssay.assert!(boolean, :message=>message)
end
assert_compare(receiver, operator, operand, message=nil) click to toggle source

Passes if ‘object` satisify compaision by `operator`.

# File lib/assay-testunit/assertions.rb, line 62
def assert_compare(receiver, operator, operand, message=nil)
  case operator.to_sym
  when :<
    LessAssay.assert!(receiver, operand, :message=>message, :backtrace=>caller)
  when :<=
    LessEqualAssay.assert!(receiver, operand, :message=>message, :backtrace=>caller)
  when :>
    MoreAssay.assert!(receiver, operand, :message=>message, :backtrace=>caller)
  when :>=
    MoreEqualAssay.assert!(receiver, operand, :message=>message, :backtrace=>caller)
  when :==
    EqualAssay.assert!(operand, receiver, :message=>message, :backtrace=>caller)
  else
    raise ArgumentError, "comparision operator must be one of <, <=, >, >= or '=='"
  end
end
assert_empty(exp, msg=nil) click to toggle source

Passes if object is empty.

assert_empty(object)
# File lib/assay-testunit/assertions.rb, line 95
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-testunit/assertions.rb, line 115
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-testunit/assertions.rb, line 336
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-testunit/assertions.rb, line 136
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-testunit/assertions.rb, line 154
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 ‘exp` and `act` are within `epsilon`.

# File lib/assay-testunit/assertions.rb, line 170
def assert_in_epsilon(exp, act, epsilon=0.001, message=nil)
  exp = exp.to_f
  if exp.zero?
    delta = epsilon.to_f ** 2
  else
    delta = exp * epsilon.to_f
  end
  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-testunit/assertions.rb, line 196
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-testunit/assertions.rb, line 212
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-testunit/assertions.rb, line 230
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-testunit/assertions.rb, line 247
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

Passed if object is nil.

assert_nil(nil)
# File lib/assay-testunit/assertions.rb, line 279
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 has no match pattern using ‘#!~` method.

assert_no_match(/two/, 'one 2 three')
# File lib/assay-testunit/assertions.rb, line 263
def assert_no_match(pattern, string, msg=nil)
  NoMatchAssay.assert!(string, pattern, :message=>msg, :backtrace=>caller)
end
assert_not_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-testunit/assertions.rb, line 35
def assert_not_alike(exp, act, msg=nil)
  LikeAssay.refute!(act, exp, :message=>msg, :backtrace=>caller)
end
assert_not_boolean(boolean, message=nil) click to toggle source

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

# File lib/assay-testunit/assertions.rb, line 56
def assert_not_boolean(boolean, message=nil)
  BooleanAssay.refute!(boolean, :message=>message)
end
assert_not_empty(exp, msg=nil) click to toggle source

Passes if object is not empty.

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

Passes if expected != actual

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

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

assert_not_equivalent 1, 1.0
# File lib/assay-testunit/assertions.rb, line 344
def assert_not_equivalent(criterion, act, msg=nil)
  EqualityAssay.refute!(act, criterion, :message=>msg, :backtrace=>caller)
end
assert_not_false(exp, msg=nil) click to toggle source

Passed if object is not false.

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

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

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

Passes if ‘exp` and `act` are NOT within `epsilon`.

# File lib/assay-testunit/assertions.rb, line 183
def assert_not_in_epsilon(exp, act, epsilon=0.001, message=nil) 
  exp = exp.to_f
  if exp.zero?
    delta = epsilon.to_f ** 2
  else
    delta = exp * epsilon.to_f
  end
  WithinAssay.refute!(act, exp, delta, :message=>message, :backtrace=>caller)
end
assert_not_includes(collection, member, message=nil) click to toggle source

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

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

Passes if object is not an instance of class.

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

Passes if object .kind_of? klass

assert_not_kind_of(Object, 'foo')
# File lib/assay-testunit/assertions.rb, line 239
def assert_not_kind_of(cls, obj, msg=nil)
  KindAssay.refute!(obj, cls, :message=>msg, :backtrace=>caller)
end
assert_not_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-testunit/assertions.rb, line 255
def assert_not_match(pattern, string, msg=nil)
  MatchAssay.refute!(string, pattern, :message=>msg, :backtrace=>caller)
end
assert_not_nil(exp, msg=nil) click to toggle source

Passed if object is not nil.

assert_not_nil(true)
# File lib/assay-testunit/assertions.rb, line 287
def assert_not_nil(exp, msg=nil)
  NilAssay.refute!(exp, :message=>msg, :backtrace=>caller)
end
assert_not_predicate(object, predicate, message = nil) click to toggle source
# File lib/assay-testunit/assertions.rb, line 303
def assert_not_predicate(object, predicate, message = nil) 
  ExecutionAssay.refute!(:message=>message) do
    object.__send__(predicate)
  end
end
assert_not_raised(exception, message=nil, &block) click to toggle source

Passes if the block *does not* raise a given exceptions.

assert_not_raised IOError do
  raise 'Boom!!!'
end
# File lib/assay-testunit/assertions.rb, line 391
def assert_not_raised(exception, message=nil, &block) #:yeild:
  RaiseAssay.refute!(exception, :message=>message, :backtrace=>caller, &block)
end
assert_not_respond_to(reciever, method, msg=nil) click to toggle source

Passes if object does not respond_to? methods.

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

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

assert_not_same(object, other)
# File lib/assay-testunit/assertions.rb, line 435
def assert_not_same(exp, act, msg=nil)
  IdentityAssay.refute!(act, exp, :message=>msg, :backtrace=>caller)
end
assert_not_thrown(expected, msg=nil, &blk) click to toggle source

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

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

Passed if object is not true.

assert_not_true(false)
# File lib/assay-testunit/assertions.rb, line 488
def assert_not_true(exp, msg=nil)
  TrueAssay.refute!(exp, :message=>msg, :backtrace=>caller)
end
assert_nothing_raised(message=nil, &block) click to toggle source

Passes if the block yields successfully.

assert_nothing_raised "Couldn't do the thing" do
  do_the_thing
end
# File lib/assay-testunit/assertions.rb, line 402
def assert_nothing_raised(message=nil, &block)
  RescueAssay.refute!(Exception, :message=>message, :backtrace=>caller, &block)
end
assert_nothing_thrown(message=nil, &blk) click to toggle source
# File lib/assay-testunit/assertions.rb, line 472
def assert_nothing_thrown(message=nil, &blk)
  ThrowAssay.refute!(nil, :message=>message, &blk)
end
assert_operator(receiver, operator, operand, message=nil) click to toggle source

TODO: Is this supposed to be restricted in some way?

# File lib/assay-testunit/assertions.rb, line 351
def assert_operator(receiver, operator, operand, message=nil) 
  ExecutionAssay.assert!(:message=>message, :backtrace=>caller) do
    receiver.__send__(operator, operand)
  end
end
assert_path_exist(path, message=nil) click to toggle source
# File lib/assay-testunit/assertions.rb, line 360
def assert_path_exist(path, message=nil)
  PathAssay.assert!(path, :message=>message, :backtrace=>caller)
end
assert_path_not_exist(path, message=nil) click to toggle source
# File lib/assay-testunit/assertions.rb, line 367
def assert_path_not_exist(path, message=nil) 
  PathAssay.refute!(path, :message=>message, :backtrace=>caller)
end
assert_predicate(object, predicate, message = nil) click to toggle source
# File lib/assay-testunit/assertions.rb, line 294
def assert_predicate(object, predicate, message = nil) 
  ExecutionAssay.assert!(:message=>message) do
    object.__send__(predicate)
  end
end
assert_raise(exception, message=nil, &block) click to toggle source

Passes if the block raises a given exception.

assert_raise RuntimeError do
  raise 'Boom!!!'
end
# File lib/assay-testunit/assertions.rb, line 378
def assert_raise(exception, message=nil, &block)
  RaiseAssay.assert!(exception, :message=>message, :backtrace=>caller, &block)
end
Also aliased as: assert_raises
assert_raise_kind_of(exception_class, message=nil, &block) click to toggle source

Passes if the block raises a given exception.

assert_raise_kind_of RuntimeError do
  raise 'Boom!!!'
end
# File lib/assay-testunit/assertions.rb, line 413
def assert_raise_kind_of(exception_class, message=nil, &block)
  RescueAssay.assert!(exception_class, :message=>message, :backtrace=>caller, &block)
end
assert_raises(exception, message=nil, &block)
Alias for: assert_raise
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-testunit/assertions.rb, line 314
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-testunit/assertions.rb, line 426
def assert_same(exp, act, msg=nil)
  IdentityAssay.assert!(act, exp, :message=>msg, :backtrace=>caller)
end
assert_throw(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-testunit/assertions.rb, line 452
def assert_throw(expected, msg=nil, &blk)
  ThrowAssay.assert!(expected, :message=>msg, :backtrace=>caller, &blk)
end
Also aliased as: assert_throws
assert_throws(expected, msg=nil, &blk)
Alias for: assert_throw
assert_true(exp, msg=nil) click to toggle source

Passed if object is true.

# File lib/assay-testunit/assertions.rb, line 479
def assert_true(exp, msg=nil)
  TrueAssay.assert!(exp, :message=>msg, :backtrace=>caller)
end