1 $undefined = Object.new
8 $test_start = Time.now if Object.const_defined?(:Time)
11 unless RUBY_ENGINE == "mruby"
18 def _str_match?(pattern, str)
19 File.fnmatch?(pattern, str, File::FNM_EXTGLOB|File::FNM_DOTMATCH)
30 def _assertion_indent(indent)
34 while nl = index("\n", off)
36 nl += 1 while slice(nl) == "\n"
38 str = indent.dup if off == 0
39 str += slice(off, nl - off) + indent
54 # Create the assertion in a readable way
55 def assertion_string(err, str, iso=nil, e=nil, bt=nil)
57 msg += " [#{iso}]" if iso && !iso.empty?
58 msg += " => #{e}" if e && !e.to_s.empty?
59 if Object.const_defined?(:GEMNAME)
60 msg += " (#{GEMNAME == 'mruby-test' ? 'core' : "mrbgems: #{GEMNAME}"})"
63 $mrbtest_assert.each do |idx, assert_msg, diff|
64 msg += "\n - Assertion[#{idx}]"
65 msg += " #{assert_msg}." if assert_msg && !assert_msg.empty?
66 msg += "\n#{diff}" if diff && !diff.empty?
69 msg += "\nbacktrace:\n #{bt.join("\n ")}" if bt && !bt.empty?
74 # Verify a code block.
76 # str : A remark which will be printed in case
77 # this assertion fails
78 # iso : The ISO reference code of the feature
79 # which will be tested by this
81 def assert(str = 'assert', iso = '')
82 t_print(str, (iso != '' ? " [#{iso}]" : ''), ' : ') if $mrbtest_verbose
84 $mrbtest_child_noassert ||= [0]
85 $mrbtest_child_noassert << 0
86 parent_asserts = $asserts
88 parent_mrbtest_assert = $mrbtest_assert
91 if $mrbtest_assert_idx && !$mrbtest_assert_idx.empty?
92 $mrbtest_assert_idx[-1] += 1
93 $mrbtest_assert_idx << 0
95 $mrbtest_assert_idx = [0]
96 class << $mrbtest_assert_idx
97 alias to_s _assertion_join
102 if $mrbtest_assert.size > 0
103 if $mrbtest_assert.size == $mrbtest_child_noassert[-1]
104 $asserts.push(assertion_string('Skip: ', str, iso))
105 $mrbtest_child_noassert[-2] += 1
109 $asserts.push(assertion_string('Fail: ', str, iso))
113 elsif $mrbtest_assert_idx[-1] == 0
114 $asserts.push(assertion_string('Warn: ', str, iso, 'no assertion'))
121 rescue MRubyTestSkip => e
122 $asserts.push(assertion_string('Skip: ', str, iso, e))
124 $mrbtest_child_noassert[-2] += 1
126 rescue Exception => e
127 $asserts.push(assertion_string("#{e.class}: ", str, iso, e, e.backtrace))
131 if $mrbtest_assert_idx.size > 1
132 $asserts.each do |mesg|
133 idx = $mrbtest_assert_idx[0..-2]._assertion_join
134 mesg = mesg._assertion_indent(" ")
136 # Give `mesg` as a `diff` argument to avoid adding extra periods.
137 parent_mrbtest_assert << [idx, nil, mesg]
140 parent_asserts.concat $asserts
142 $asserts = parent_asserts
144 $mrbtest_assert = parent_mrbtest_assert
145 $mrbtest_assert_idx.pop
146 $mrbtest_assert_idx = nil if $mrbtest_assert_idx.empty?
147 $mrbtest_child_noassert.pop
151 t_print("\n") if $mrbtest_verbose
154 def assertion_diff(exp, act)
155 " Expected: #{exp.inspect}\n" \
156 " Actual: #{act.inspect}"
159 def assert_true(obj, msg = nil, diff = nil)
160 if $mrbtest_assert_idx && $mrbtest_assert_idx.size > 0
161 $mrbtest_assert_idx[-1] += 1
163 diff ||= " Expected #{obj.inspect} to be true."
164 $mrbtest_assert.push([$mrbtest_assert_idx.to_s, msg, diff])
170 def assert_false(obj, msg = nil, diff = nil)
172 diff ||= " Expected #{obj.inspect} to be false."
174 assert_true(!obj, msg, diff)
177 def assert_equal(exp, act_or_msg = nil, msg = nil, &block)
178 ret, exp, act, msg = _eval_assertion(:==, exp, act_or_msg, msg, block)
180 diff = assertion_diff(exp, act)
182 assert_true(ret, msg, diff)
185 def assert_not_equal(exp, act_or_msg = nil, msg = nil, &block)
186 ret, exp, act, msg = _eval_assertion(:==, exp, act_or_msg, msg, block)
188 diff = " Expected #{act.inspect} to not be equal to #{exp.inspect}."
190 assert_true(!ret, msg, diff)
193 def assert_same(*args); _assert_same(true, *args) end
194 def assert_not_same(*args); _assert_same(false, *args) end
195 def _assert_same(affirmed, exp, act, msg = nil)
196 unless ret = exp.equal?(act) == affirmed
197 exp_str, act_str = [exp, act].map do |o|
198 "#{o.inspect} (class=#{o.class}, oid=#{o.__id__})"
200 diff = " Expected #{act_str} to #{'not ' unless affirmed}be the same as #{exp_str}."
202 assert_true(ret, msg, diff)
205 def assert_nil(obj, msg = nil)
206 unless ret = obj.nil?
207 diff = " Expected #{obj.inspect} to be nil."
209 assert_true(ret, msg, diff)
212 def assert_not_nil(obj, msg = nil)
214 diff = " Expected #{obj.inspect} to not be nil."
216 assert_false(ret, msg, diff)
219 def assert_include(*args); _assert_include(true, *args) end
220 def assert_not_include(*args); _assert_include(false, *args) end
221 def _assert_include(affirmed, collection, obj, msg = nil)
222 unless ret = collection.include?(obj) == affirmed
223 diff = " Expected #{collection.inspect} to #{'not ' unless affirmed}include #{obj.inspect}."
225 assert_true(ret, msg, diff)
228 def assert_predicate(*args); _assert_predicate(true, *args) end
229 def assert_not_predicate(*args); _assert_predicate(false, *args) end
230 def _assert_predicate(affirmed, obj, op, msg = nil)
231 unless ret = obj.__send__(op) == affirmed
232 diff = " Expected #{obj.inspect} to #{'not ' unless affirmed}be #{op}."
234 assert_true(ret, msg, diff)
237 def assert_operator(*args); _assert_operator(true, *args) end
238 def assert_not_operator(*args); _assert_operator(false, *args) end
239 def _assert_operator(affirmed, obj1, op, obj2 = $undefined, msg = nil)
240 return _assert_predicate(affirmed, obj1, op, msg) if $undefined.equal?(obj2)
241 unless ret = obj1.__send__(op, obj2) == affirmed
242 diff = " Expected #{obj1.inspect} to #{'not ' unless affirmed}be #{op} #{obj2.inspect}."
244 assert_true(ret, msg, diff)
248 # Fail unless +str+ matches against +pattern+.
250 # +pattern+ is interpreted as pattern for File.fnmatch?. It may contain the
251 # following metacharacters:
254 # Matches any string.
257 # Matches any one character.
259 # <code>[_SET_]</code>, <code>[^_SET_]</code> (<code>[!_SET_]</code>) ::
260 # Matches any one character in _SET_. Behaves like character sets in
261 # Regexp, including set negation (<code>[^a-z]</code>).
263 # <code>{_A_,_B_}</code> ::
264 # Matches pattern _A_ or pattern _B_.
266 # <code> \ </code> ::
267 # Escapes the next character.
268 def assert_match(*args); _assert_match(true, *args) end
269 def assert_not_match(*args); _assert_match(false, *args) end
270 def _assert_match(affirmed, pattern, str, msg = nil)
271 unless ret = _str_match?(pattern, str) == affirmed
272 diff = " Expected #{pattern.inspect} to #{'not ' unless affirmed}match #{str.inspect}."
274 assert_true(ret, msg, diff)
278 # Fails unless +obj+ is a kind of +cls+.
279 def assert_kind_of(cls, obj, msg = nil)
280 unless ret = obj.kind_of?(cls)
281 diff = " Expected #{obj.inspect} to be a kind of #{cls}, not #{obj.class}."
283 assert_true(ret, msg, diff)
287 # Fails unless +exp+ is equal to +act+ in terms of a Float
288 def assert_float(exp, act, msg = nil)
289 e, a = exp.to_f, act.to_f
290 if e.finite? && a.finite? && (n = (e - a).abs) > Mrbtest::FLOAT_TOLERANCE
291 flunk(msg, " Expected |#{exp} - #{act}| (#{n}) to be <= #{Mrbtest::FLOAT_TOLERANCE}.")
292 elsif (e.infinite? || a.infinite?) && e != a ||
293 e.nan? && !a.nan? || !e.nan? && a.nan?
294 flunk(msg, " Expected #{act} to be #{exp}.")
300 def assert_raise(*exc)
301 msg = (exc.last.is_a? String) ? exc.pop : nil
302 exc = exc.empty? ? StandardError : exc.size == 1 ? exc[0] : exc
308 rescue Exception => e
309 diff = " #{exc} exception expected, not\n" \
310 " Class: <#{e.class}>\n" \
314 diff = " #{exc} expected but nothing was raised."
319 def assert_nothing_raised(msg = nil)
322 rescue Exception => e
323 diff = " Exception raised:\n" \
324 " Class: <#{e.class}>\n" \
332 def assert_raise_with_message(*args, &block)
333 _assert_raise_with_message(:plain, *args, &block)
335 def assert_raise_with_message_pattern(*args, &block)
336 _assert_raise_with_message(:pattern, *args, &block)
338 def _assert_raise_with_message(type, exc, exp_msg, msg = nil, &block)
339 e = msg ? assert_raise(exc, msg, &block) : assert_raise(exc, &block)
340 e ? ($mrbtest_assert_idx[-1]-=1) : (return e)
343 unless ret = type == :pattern ? _str_match?(exp_msg, err_msg) : exp_msg == err_msg
344 diff = " Expected Exception(#{exc}) was raised, but the message doesn't match.\n"
346 diff += " Expected #{exp_msg.inspect} to match #{err_msg.inspect}."
348 diff += assertion_diff(exp_msg, err_msg)
351 assert_true(ret, msg, diff)
358 def flunk(msg = "Epic Fail!", diff = "")
359 assert_true(false, msg, diff)
363 # Report the test result and print all assertions
364 # which were reported broken.
368 $asserts.each do |msg|
372 $total_test = $ok_test + $ko_test + $kill_test + $warning_test + $skip_test
373 t_print(" Total: #{$total_test}\n")
375 t_print(" OK: #{$ok_test}\n")
376 t_print(" KO: #{$ko_test}\n")
377 t_print(" Crash: #{$kill_test}\n")
378 t_print("Warning: #{$warning_test}\n")
379 t_print(" Skip: #{$skip_test}\n")
381 if Object.const_defined?(:Time)
382 t_time = Time.now - $test_start
383 t_print(" Time: #{t_time.round(2)} seconds\n")
386 $ko_test == 0 && $kill_test == 0
389 def _eval_assertion(meth, exp, act_or_msg, msg, block)
391 exp, act, msg = exp, block.call, act_or_msg
393 exp, act, msg = exp, act_or_msg, msg
395 return exp.__send__(meth, act), exp, act, msg
400 class MRubyTestSkip < NotImplementedError; end
403 raise MRubyTestSkip.new(cause)