Module: Assay::MiniTest::Assertions

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

Overview

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 ?

Instance Method Summary collapse

Instance Method Details

#assert(truth, msg = nil) ⇒ Object



15
16
17
# 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) ⇒ 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.



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



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

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



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

Passes if object is empty.

assert_empty(object)


79
80
81
# 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) ⇒ 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


99
100
101
# 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) ⇒ 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


356
357
358
# 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) ⇒ Object

Passed if object is false.

assert_false(false)


120
121
122
# 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) ⇒ Object

Passes if expected and actual are equal within delta tolerance.

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


138
139
140
# 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) ⇒ Object

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



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

Passes if ‘collection` contains `member`.



170
171
172
# 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) ⇒ Object

Passes if object is an instance of class.

assert_instance_of(String, 'foo')


186
187
188
# 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) ⇒ Object

Passes if object .kind_of? klass

assert_kind_of(Object, 'foo')


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

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

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


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

Passes if object is nil.

assert_nil(nil)


249
250
251
# 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) ⇒ Object

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

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


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

Passes if the block yields successfully.

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


418
419
420
# 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) ⇒ Object



372
373
374
375
376
# 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) ⇒ Object

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.



272
273
274
275
# 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) ⇒ Object

Passes it predicate sent to object returns postively.

assert_predicate(10, :even?)


311
312
313
314
315
# 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) ⇒ Object

Passes if the block raises given exception(s).

assert_raises RuntimeError do
  raise 'Boom!!!'
end


394
395
396
397
# 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) ⇒ Object Also known as: assert_responds_to

Passes if object respond_to? methods.

assert_respond_to 'bugbear', :slice


333
334
335
# 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

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

Passes if actual is the same exact object as expected.

assert_same(object, object)


438
439
440
# 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) ⇒ Object

Like #assert_output but ensures no output.



295
296
297
# 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) ⇒ Object

Passes if the block throws ‘expected` object.

assert_throw :done do
  throw :done
end


462
463
464
# 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) ⇒ Object

Passed if object is true.



480
481
482
# 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) ⇒ Object



20
21
22
# 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) ⇒ Object

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



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

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



64
65
66
# 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) ⇒ Object

Passes if object is not empty.

refute_empty(object)


87
88
89
# 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) ⇒ Object

Passes if expected != actual

refute_equal 'some string', 5


107
108
109
# 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) ⇒ Object

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

refute_equivalent 'some string', 5


365
366
367
# 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) ⇒ Object

Passed if object is not false.

refute_false(false)


129
130
131
# 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) ⇒ Object

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

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


147
148
149
# 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) ⇒ Object

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



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

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



177
178
179
# 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) ⇒ Object

Passes if object is not an instance of class.

refute_instance_of(String, 500)


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

Passes if object .kind_of? klass

refute_kind_of(Object, 'foo')


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

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

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


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

Passes if object is not nil.

refute_nil(true)


258
259
260
# 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) ⇒ Object

Passes if the block yields successfully.

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


429
430
431
# 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) ⇒ Object



381
382
383
384
385
# 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) ⇒ Object

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.



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

Passes it predicate sent to object returns negatively.

refute_predicate(10, :odd?)


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

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

refute_raises RuntimeError do
  raise 'Boom!!!'
end


406
407
408
409
# 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) ⇒ Object

Passes if object does not respond_to? methods.

refute_respond_to 'bugbear', :slice


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

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

refute_same(object, other)


447
448
449
# 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) ⇒ Object

Like #refute_output but ensures some output.



302
303
304
# 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) ⇒ Object

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

refute_throws :done do
  throw :chimp
end


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

Passed if object is not true.

refute_true(false)


489
490
491
# File 'lib/assay-minitest/assertions.rb', line 489

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