Module: MicroTest::Assertions
- Included in:
- World
- Defined in:
- lib/microtest/assertions.rb
Overview
Legacy Assertions
This module provides a compatibility layer for Test::Unit/MiniTest. This is an optional module and must be loaded on it’s own.
Note that two methods are not provided, #assert_nothing_raised
, and #assert_nothing_thrown
.
Instance Method Summary collapse
-
#assert(test, msg = nil) ⇒ Assertor
The assertion upon which all other assertions are based.
-
#assert_block(msg = nil) ⇒ Object
Passes if the block yields true.
-
#assert_equal(exp, act, msg = nil) ⇒ Object
Passes if expected == +actual.
-
#assert_in_delta(exp, act, delta, msg = nil) ⇒ Object
Passes if expected_float and actual_float are equal within delta tolerance.
-
#assert_instance_of(cls, obj, msg = nil) ⇒ Object
Passes if object .instance_of? klass.
-
#assert_kind_of(cls, obj, msg = nil) ⇒ Object
Passes if object .kind_of? klass.
-
#assert_match(exp, act, msg = nil) ⇒ Object
Passes if string =~ pattern.
-
#assert_nil(obj, msg = nil) ⇒ Object
Passes if object is nil.
-
#assert_no_match(exp, act, msg = nil) ⇒ Object
Passes if regexp !~ string.
-
#assert_not_equal(exp, act, msg = nil) ⇒ Object
Passes if expected != actual.
-
#assert_not_nil(obj, msg = nil) ⇒ Object
Passes if ! object .nil?.
-
#assert_not_same(exp, act, msg = nil) ⇒ Object
Passes if ! actual .equal? expected.
-
#assert_operator(o1, op, o2, msg = "") ⇒ Object
Compares the
object1
withobject2
using operator. -
#assert_raises(*args) ⇒ Object
(also: #assert_raise)
Passes if the block raises one of the given exceptions.
-
#assert_respond_to(obj, meth, msg = nil) ⇒ Object
Passes if
object
respond_to?method
. -
#assert_same(exp, act, msg = nil) ⇒ Object
Passes if
actual
.equal?expected
(i.e. they are the same instance). -
#assert_send(send_array, msg = nil) ⇒ Object
Passes if the method send returns a true value.
-
#assert_throws(sym, msg = nil) ⇒ Object
Passes if the block throws expected_symbol.
-
#flunk(msg = nil) ⇒ Object
Flunk always fails.
Instance Method Details
#assert(test, msg = nil) ⇒ Assertor
The assertion upon which all other assertions are based.
32 33 34 35 |
# File 'lib/microtest/assertions.rb', line 32 def assert(test, msg=nil) msg = "failed assertion (no message given)" unless msg raise Assertion.new(msg, :backtrace=>caller) unless test end |
#assert_block(msg = nil) ⇒ Object
Passes if the block yields true.
47 48 49 50 51 |
# File 'lib/microtest/assertions.rb', line 47 def assert_block(msg=nil) # :yields: test = ! yield msg = "assertion failed" unless msg __assert__(test, msg) 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.
65 66 67 68 69 |
# File 'lib/microtest/assertions.rb', line 65 def assert_equal(exp, act, msg=nil) test = (exp == act) msg = "Expected #{act.inspect} to be equal to #{exp.inspect}" unless msg __assert__(test, msg) end |
#assert_in_delta(exp, act, delta, msg = nil) ⇒ Object
Passes if expected_float and actual_float are equal within delta tolerance.
79 80 81 82 83 |
# File 'lib/microtest/assertions.rb', line 79 def assert_in_delta(exp, act, delta, msg=nil) test = (exp.to_f - act.to_f).abs <= delta.to_f msg = "Expected #{exp} to be within #{delta} of #{act}" unless msg __assert__(test, msg) end |
#assert_instance_of(cls, obj, msg = nil) ⇒ Object
Passes if object .instance_of? klass
93 94 95 96 97 |
# File 'lib/microtest/assertions.rb', line 93 def assert_instance_of(cls, obj, msg=nil) test = (cls === obj) msg = "Expected #{obj} to be a #{cls}" unless msg __assert__(test, msg) end |
#assert_kind_of(cls, obj, msg = nil) ⇒ Object
Passes if object .kind_of? klass
107 108 109 110 111 |
# File 'lib/microtest/assertions.rb', line 107 def assert_kind_of(cls, obj, msg=nil) test = obj.kind_of?(cls) msg = "Expected #{obj.inspect} to be a kind of #{cls}" unless msg __assert__(test, msg) end |
#assert_match(exp, act, msg = nil) ⇒ Object
Passes if string =~ pattern.
121 122 123 124 125 |
# File 'lib/microtest/assertions.rb', line 121 def assert_match(exp, act, msg=nil) test = (act =~ exp) msg = "Expected #{act.inspect} to match #{exp.inspect}" unless msg __assert__(test, msg) end |
#assert_nil(obj, msg = nil) ⇒ Object
Passes if object is nil.
135 136 137 138 139 |
# File 'lib/microtest/assertions.rb', line 135 def assert_nil(obj, msg=nil) test = obj.nil? msg = "Expected #{obj.inspect} to be nil" unless msg __assert__(test, msg) end |
#assert_no_match(exp, act, msg = nil) ⇒ Object
Passes if regexp !~ string
149 150 151 152 153 |
# File 'lib/microtest/assertions.rb', line 149 def assert_no_match(exp, act, msg=nil) test = (act !~ exp) msg = "Expected #{act.inspect} to match #{exp.inspect}" unless msg __assert__(test, msg) end |
#assert_not_equal(exp, act, msg = nil) ⇒ Object
Passes if expected != actual
163 164 165 166 167 |
# File 'lib/microtest/assertions.rb', line 163 def assert_not_equal(exp, act, msg=nil) test = (exp != act) msg = "Expected #{act.inspect} to not be equal to #{exp.inspect}" unless msg __assert__(test, msg) end |
#assert_not_nil(obj, msg = nil) ⇒ Object
Passes if ! object .nil?
177 178 179 180 181 |
# File 'lib/microtest/assertions.rb', line 177 def assert_not_nil(obj, msg=nil) test = ! obj.nil? msg = "Expected #{obj.inspect} to not be nil" unless msg __assert__(test, msg) end |
#assert_not_same(exp, act, msg = nil) ⇒ Object
Passes if ! actual .equal? expected
191 192 193 194 195 |
# File 'lib/microtest/assertions.rb', line 191 def assert_not_same(exp, act, msg=nil) test = ! exp.equal?(act) msg = "Expected #{act.inspect} to not be the same as #{exp.inspect}" unless msg __assert__(test, msg) end |
#assert_operator(o1, op, o2, msg = "") ⇒ Object
Compares the object1
with object2
using operator.
Passes if object1.send(operator, object2) is true.
207 208 209 210 211 |
# File 'lib/microtest/assertions.rb', line 207 def assert_operator(o1, op, o2, msg="") test = o1.__send__(op, o2) msg = "Expected #{o1}.#{op}(#{o2}) to be true" unless msg __assert__(test, msg) end |
#assert_raises(*args) ⇒ Object Also known as: assert_raise
Passes if the block raises one of the given exceptions.
223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/microtest/assertions.rb', line 223 def assert_raises(*args) msg = (Module === args.last ? nil : args.pop) begin yield msg = "Expected one of #{args.join(', ')} to be raised" unless msg __assert__(false, msg) rescue Exception => err test = args.any?{ |e| e === err } msg = "Expected one of #{args.join(', ')} to be raised, but got #{err.class}" unless msg __assert__(test, msg) return err end end |
#assert_respond_to(obj, meth, msg = nil) ⇒ Object
Passes if object
respond_to? method
.
262 263 264 265 266 267 268 |
# File 'lib/microtest/assertions.rb', line 262 def assert_respond_to(obj, meth, msg=nil) msg = "Expected #{obj} (#{obj.class}) to respond to ##{meth}" unless msg #flip = (Symbol === obj) && ! (Symbol === meth) # HACK for specs #obj, meth = meth, obj if flip test = obj.respond_to?(meth) __assert__(test, msg) end |
#assert_same(exp, act, msg = nil) ⇒ Object
Passes if actual
.equal? expected
(i.e. they are the same instance).
279 280 281 282 283 |
# File 'lib/microtest/assertions.rb', line 279 def assert_same(exp, act, msg=nil) msg = "Expected #{act.inspect} to be the same as #{exp.inspect}" unless msg test = exp.equal?(act) __assert__(test, msg) end |
#assert_send(send_array, msg = nil) ⇒ Object
Passes if the method send returns a true value. The parameter send_array
is composed of:
-
A receiver
-
A method
-
Arguments to the method
298 299 300 301 302 303 |
# File 'lib/microtest/assertions.rb', line 298 def assert_send(send_array, msg=nil) r, m, *args = *send_array test = r.__send__(m, *args) msg = "Expected #{r}.#{m}(*#{args.inspect}) to return true" unless msg __assert__(test, msg) end |
#assert_throws(sym, msg = nil) ⇒ Object
Passes if the block throws expected_symbol
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/microtest/assertions.rb', line 315 def assert_throws(sym, msg=nil) msg = "Expected #{sym} to have been thrown" unless msg test = true catch(sym) do begin yield rescue ArgumentError => e # 1.9 exception default += ", not #{e..split(/ /).last}" rescue NameError => e # 1.8 exception default += ", not #{e.name.inspect}" end test = false end __assert__(test, msg) end |
#flunk(msg = nil) ⇒ Object
Flunk always fails.
339 340 341 |
# File 'lib/microtest/assertions.rb', line 339 def flunk(msg=nil) __assert__(false, msg) end |