1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
module AE
module Legacy #:nodoc:
# 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+.
#
module Assertions
# Private method upon which all of the legacy assertions are based
# (except for #assert itself).
#
# @raise [Assertion] If test fails.
#
# @return nothing
def __assert__(test, msg=nil)
msg = "failed assertion (no message given)" unless msg
raise Assertion.new(msg, :backtrace=>caller[1..-1]) unless test
end
private :__assert__
# The assertion upon which all other assertions are based.
#
# @example
# assert [1, 2].include?(5)
#
# @return [Assertor] if `test` not given
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
# Passes if the block yields true.
#
# @example
# assert_block "Couldn't do the thing" do
# do_the_thing
# end
#
# @raise [Assertion] if test fails
#
# @return nothing
def assert_block(msg=nil) # :yields:
test = ! yield
msg = "assertion failed" unless msg
__assert__(test, msg)
end
# 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.
#
# @example
# assert_equal 'MY STRING', 'my string'.upcase
#
# @raise [Assertion] if test fails
#
# @return nothing
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
# Passes if expected_float and actual_float are equal within delta tolerance.
#
# @example
# assert_in_delta 0.05, (50000.0 / 10**6), 0.00001
#
# @raise [Assertion] if test fails
#
# @return nothing
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
# Passes if object .instance_of? klass
#
# @example
# assert_instance_of String, 'foo'
#
# @raise [Assertion] if test fails
#
# @return nothing
def assert_instance_of(cls, obj, msg=nil)
test = (cls === obj)
msg = "Expected #{obj} to be a #{cls}" unless msg
__assert__(test, msg)
end
# Passes if object .kind_of? klass
#
# @example
# assert_kind_of Object, 'foo'
#
# @raise [Assertion] if test fails
#
# @return nothing
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
# Passes if string =~ pattern.
#
# @example
# assert_match(/\d+/, 'five, 6, seven')
#
# @raise [Assertion] if test fails
#
# @return nothing
def assert_match(exp, act, msg=nil)
test = (act =~ exp)
msg = "Expected #{act.inspect} to match #{exp.inspect}" unless msg
__assert__(test, msg)
end
# Passes if object is nil.
#
# @example
# assert_nil [1, 2].uniq!
#
# @raise [Assertion] if test fails
#
# @return nothing
def assert_nil(obj, msg=nil)
test = obj.nil?
msg = "Expected #{obj.inspect} to be nil" unless msg
__assert__(test, msg)
end
# Passes if regexp !~ string
#
# @example
# assert_no_match(/two/, 'one 2 three')
#
# @raise [Assertion] if test fails
#
# @return nothing
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
# Passes if expected != actual
#
# @example
# assert_not_equal 'some string', 5
#
# @raise [Assertion] if test fails
#
# @return nothing
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
# Passes if ! object .nil?
#
# @example
# assert_not_nil '1 two 3'.sub!(/two/, '2')
#
# @raise [Assertion] if test fails
#
# @return nothing
def assert_not_nil(obj, msg=nil)
test = ! obj.nil?
msg = "Expected #{obj.inspect} to not be nil" unless msg
__assert__(test, msg)
end
# Passes if ! actual .equal? expected
#
# @example
# assert_not_same Object.new, Object.new
#
# @raise [Assertion] if test fails
#
# @return nothing
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
# Compares the +object1+ with +object2+ using operator.
#
# Passes if object1.send(operator, object2) is true.
#
# @example
# assert_operator 5, :>=, 4
#
# @raise [Assertion] if test fails
#
# @return nothing
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
# Passes if the block raises one of the given exceptions.
#
# @example
# assert_raise RuntimeError, LoadError do
# raise 'Boom!!!'
# end
#
# @raise [Assertion] if test fails
#
# @return nothing
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
alias_method :assert_raise, :assert_raises
# Provides a way to assert that a procedure
# <i>does not</i> raise an exception.
#
# @example
# refute_raises(StandardError){ raise }
#
#def assert_raises!(exception, &block)
# begin
# block.call(*a)
# rescue exception
# raise Assertion
# end
#end
#alias_method :refute_raises, :assert_raises!
# Passes if +object+ respond_to? +method+.
#
# @example
# assert_respond_to 'bugbear', :slice
#
# @raise [Assertion] if test fails
#
# @return nothing
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
# Passes if +actual+ .equal? +expected+ (i.e. they are the same instance).
#
# @example
# o = Object.new
# assert_same(o, o)
#
# @raise [Assertion] if test fails
#
# @return nothing
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
# Passes if the method send returns a true value.
# The parameter +send_array+ is composed of:
#
# * A receiver
# * A method
# * Arguments to the method
#
# @example
# assert_send [[1, 2], :include?, 4]
#
# @raise [Assertion] if test fails
#
# @return nothing
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
# Passes if the block throws expected_symbol
#
# @example
# assert_throws :done do
# throw :done
# end
#
# @raise [Assertion] if test fails
#
# @return nothing
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.message.split(/ /).last}"
rescue NameError => e # 1.8 exception
default += ", not #{e.name.inspect}"
end
test = false
end
__assert__(test, msg)
end
# Assert that an Array, or any other object the responds to #include?
# thus contains the given element.
#
# @raise [Assertion] if test fails
#
# @return nothing
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
# Flunk always fails.
#
# @example
# flunk 'Not done testing yet.'
#
# @raise [Assertion] always
#
# @return nothing
def flunk(msg=nil)
__assert__(false, msg)
end
end #module Assertions
end #module Legacy
module World
include AE::Legacy::Assertions
end
end
|