Module: AE::Legacy::Assertions
- Included in:
- World
- Defined in:
- lib/ae/legacy.rb
Overview
Test::Unit Legacy Assertions
This module provides a compatibility layer for Test::Unit. This is an optional module and is intended for providing an easier transition from Test::Unit to AE assertions.
Note that two methods are not provided, #assert_nothing_raised
, and #assert_nothing_thrown
.
Instance Method Summary collapse
-
#__assert__(test, msg = nil) ⇒ Object
private
Private method upon which all of the legacy assertions are based (except for #assert itself).
-
#assert(test = nil, 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_includes(elem, array, msg = nil) ⇒ Object
Assert that an Array, or any other object the responds to #include? thus contains the given element.
-
#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) ⇒ Object (private)
Private method upon which all of the legacy assertions are based (except for #assert itself).
22 23 24 25 |
# File 'lib/ae/legacy.rb', line 22 def __assert__(test, msg=nil) msg = "failed assertion (no message given)" unless msg raise Assertion.new(msg, :backtrace=>caller[1..-1]) unless test end |
#assert(test = nil, msg = nil) ⇒ Assertor
The assertion upon which all other assertions are based.
35 36 37 38 39 40 41 42 |
# File 'lib/ae/legacy.rb', line 35 def assert(test=nil, msg=nil) if test msg = "failed assertion (no message given)" unless msg raise Assertion.new(msg, :backtrace=>caller) unless test else Assertor.new(self, :backtrace=>caller) # TODO: Probably remove this! end end |
#assert_block(msg = nil) ⇒ Object
Passes if the block yields true.
54 55 56 57 58 |
# File 'lib/ae/legacy.rb', line 54 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.
72 73 74 75 76 |
# File 'lib/ae/legacy.rb', line 72 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.
86 87 88 89 90 |
# File 'lib/ae/legacy.rb', line 86 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_includes(elem, array, msg = nil) ⇒ Object
Assert that an Array, or any other object the responds to #include? thus contains the given element.
344 345 346 347 348 |
# File 'lib/ae/legacy.rb', line 344 def assert_includes(elem, array, msg=nil) test = array.include?(elem) msg = "Expected #{elem.inspect} is not found in #{array.inspect}" unless msg __assert__(test, msg) end |
#assert_instance_of(cls, obj, msg = nil) ⇒ Object
Passes if object .instance_of? klass
100 101 102 103 104 |
# File 'lib/ae/legacy.rb', line 100 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
114 115 116 117 118 |
# File 'lib/ae/legacy.rb', line 114 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.
128 129 130 131 132 |
# File 'lib/ae/legacy.rb', line 128 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.
142 143 144 145 146 |
# File 'lib/ae/legacy.rb', line 142 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
156 157 158 159 160 |
# File 'lib/ae/legacy.rb', line 156 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
170 171 172 173 174 |
# File 'lib/ae/legacy.rb', line 170 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?
184 185 186 187 188 |
# File 'lib/ae/legacy.rb', line 184 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
198 199 200 201 202 |
# File 'lib/ae/legacy.rb', line 198 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.
214 215 216 217 218 |
# File 'lib/ae/legacy.rb', line 214 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.
230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/ae/legacy.rb', line 230 def assert_raises(*args) msg = (Module === args.last ? nil : args.pop) begin yield msg = "Expected #{exp} to be raised" unless msg __assert__(false, msg) rescue Exception => e test = (exp === e) msg = "Expected #{exp} to be raised, but got #{e.class}" unless msg __assert__(test, msg) return e end end |
#assert_respond_to(obj, meth, msg = nil) ⇒ Object
Passes if object
respond_to? method
.
269 270 271 272 273 274 275 |
# File 'lib/ae/legacy.rb', line 269 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).
286 287 288 289 290 |
# File 'lib/ae/legacy.rb', line 286 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
305 306 307 308 309 310 |
# File 'lib/ae/legacy.rb', line 305 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
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/ae/legacy.rb', line 322 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.
358 359 360 |
# File 'lib/ae/legacy.rb', line 358 def flunk(msg=nil) __assert__(false, msg) end |