Module: Assay::TestUnit::Assertions

Included in:
Assertions
Defined in:
lib/assay-testunit/assertions.rb

Overview

TODO:

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

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.

Instance Method Summary collapse

Instance Method Details

#assert_alike(exp, act, msg = nil) ⇒ Object

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.



27
28
29
# 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) ⇒ Object



42
43
44
# 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) ⇒ Object

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



49
50
51
# 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) ⇒ Object

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Object

Passes if object is empty.

assert_empty(object)


95
96
97
# 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) ⇒ Object

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


115
116
117
# 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) ⇒ Object

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


336
337
338
# 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) ⇒ Object

Passed if object is false.

assert_false(false)


136
137
138
# 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) ⇒ Object

Passes if expected and actual are equal within delta tolerance.

assert_in_delta 0.05, (50000.0 / 10**6), 0.00001


154
155
156
# 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) ⇒ Object

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



170
171
172
173
174
175
176
177
178
# 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) ⇒ Object

Passes if ‘collection` contains `member`.



196
197
198
# 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) ⇒ Object

Passes if object is an instance of class.

assert_instance_of(String, 'foo')


212
213
214
# 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) ⇒ Object

Passes if object .kind_of? klass

assert_kind_of(Object, 'foo')


230
231
232
# 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) ⇒ Object

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

assert_match(/\d+/, 'five, 6, seven')


247
248
249
# 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) ⇒ Object

Passed if object is nil.

assert_nil(nil)


279
280
281
# 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) ⇒ Object

Passes if object has no match pattern using ‘#!~` method.

assert_no_match(/two/, 'one 2 three')


263
264
265
# 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) ⇒ Object

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



35
36
37
# 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) ⇒ Object

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



56
57
58
# 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) ⇒ Object

Passes if object is not empty.

refute_empty(object)


103
104
105
# 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) ⇒ Object

Passes if expected != actual

assert_not_equal 'some string', 5


123
124
125
# 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) ⇒ Object

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

assert_not_equivalent 1, 1.0


344
345
346
# 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) ⇒ Object

Passed if object is not false.

assert_not_false(false)


145
146
147
# 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) ⇒ Object

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

assert_not_in_delta 0.05, (50000.0 / 10**6), 0.00001


163
164
165
# 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) ⇒ Object

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



183
184
185
186
187
188
189
190
191
# 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) ⇒ Object

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



203
204
205
# 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) ⇒ Object

Passes if object is not an instance of class.

assert_not_instance_of(String, 500)


221
222
223
# 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) ⇒ Object

Passes if object .kind_of? klass

assert_not_kind_of(Object, 'foo')


239
240
241
# 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) ⇒ Object

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

assert_no_match(/two/, 'one 2 three')


255
256
257
# 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) ⇒ Object

Passed if object is not nil.

assert_not_nil(true)


287
288
289
# 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) ⇒ Object



303
304
305
306
307
# 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) ⇒ Object

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

assert_not_raised IOError do
  raise 'Boom!!!'
end


391
392
393
# 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) ⇒ Object

Passes if object does not respond_to? methods.

assert_not_respond_to 'bugbear', :slice


324
325
326
# 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) ⇒ Object

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

assert_not_same(object, other)


435
436
437
# 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) ⇒ Object

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

assert_not_thrown :done do
  throw :chimp
end


465
466
467
# 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) ⇒ Object

Passed if object is not true.

assert_not_true(false)


488
489
490
# 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) ⇒ Object

Passes if the block yields successfully.

assert_nothing_raised "Couldn't do the thing" do
  do_the_thing
end


402
403
404
# 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) ⇒ Object



472
473
474
# 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) ⇒ Object

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



351
352
353
354
355
# 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) ⇒ Object



360
361
362
# 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) ⇒ Object



367
368
369
# 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) ⇒ Object



294
295
296
297
298
# 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) ⇒ Object Also known as: assert_raises

Passes if the block raises a given exception.

assert_raise RuntimeError do
  raise 'Boom!!!'
end


378
379
380
# File 'lib/assay-testunit/assertions.rb', line 378

def assert_raise(exception, message=nil, &block)
  RaiseAssay.assert!(exception, :message=>message, :backtrace=>caller, &block)
end

#assert_raise_kind_of(exception_class, message = nil, &block) ⇒ Object

Passes if the block raises a given exception.

assert_raise_kind_of RuntimeError do
  raise 'Boom!!!'
end


413
414
415
# 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_respond_to(reciever, method, msg = nil) ⇒ Object Also known as: assert_responds_to

Passes if object respond_to? methods.

assert_respond_to 'bugbear', :slice


314
315
316
# 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

#assert_same(exp, act, msg = nil) ⇒ Object

Passes if actual is the same exact object as expected.

assert_same(object, object)


426
427
428
# 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) ⇒ Object Also known as: assert_throws

Passes if the block throws ‘expected` object.

assert_throw :done do
  throw :done
end


452
453
454
# File 'lib/assay-testunit/assertions.rb', line 452

def assert_throw(expected, msg=nil, &blk)
  ThrowAssay.assert!(expected, :message=>msg, :backtrace=>caller, &blk)
end

#assert_true(exp, msg = nil) ⇒ Object

Passed if object is true.



479
480
481
# File 'lib/assay-testunit/assertions.rb', line 479

def assert_true(exp, msg=nil)
  TrueAssay.assert!(exp, :message=>msg, :backtrace=>caller)
end