6 $test_start = Time.now if Object.const_defined?(:Time)
8 unless RUBY_ENGINE == "mruby"
18 # Create the assertion in a readable way
19 def assertion_string(err, str, iso=nil, e=nil, bt=nil)
21 msg += " [#{iso}]" if iso && !iso.empty?
22 msg += " => #{e}" if e && !e.to_s.empty?
23 msg += " (#{GEMNAME == 'mruby-test' ? 'core' : "mrbgems: #{GEMNAME}"})"
24 if $mrbtest_assert && $mrbtest_assert.size > 0
25 $mrbtest_assert.each do |idx, assert_msg, diff|
26 msg += "\n - Assertion[#{idx}]"
27 msg += " #{assert_msg}." if assert_msg && !assert_msg.empty?
28 msg += "\n#{diff}" if diff && !diff.empty?
31 msg += "\nbacktrace:\n\t#{bt.join("\n\t")}" if bt
36 # Verify a code block.
38 # str : A remark which will be printed in case
39 # this assertion fails
40 # iso : The ISO reference code of the feature
41 # which will be tested by this
43 def assert(str = 'Assertion failed', iso = '')
44 t_print(str, (iso != '' ? " [#{iso}]" : ''), ' : ') if $mrbtest_verbose
47 $mrbtest_assert_idx = 0
49 if($mrbtest_assert.size > 0)
50 $asserts.push(assertion_string('Fail: ', str, iso))
57 rescue MRubyTestSkip => e
58 $asserts.push(assertion_string('Skip: ', str, iso, e))
62 bt = e.backtrace if $mrbtest_verbose
63 $asserts.push(assertion_string("#{e.class}: ", str, iso, e, bt))
69 t_print("\n") if $mrbtest_verbose
72 def assertion_diff(exp, act)
73 " Expected: #{exp.inspect}\n" \
74 " Actual: #{act.inspect}"
77 def assert_true(obj, msg = nil, diff = nil)
79 $mrbtest_assert_idx += 1
81 diff ||= " Expected #{obj.inspect} to be true."
82 $mrbtest_assert.push([$mrbtest_assert_idx, msg, diff])
88 def assert_false(obj, msg = nil, diff = nil)
90 diff ||= " Expected #{obj.inspect} to be false."
92 assert_true(!obj, msg, diff)
95 def assert_equal(exp, act_or_msg = nil, msg = nil, &block)
96 ret, exp, act, msg = _eval_assertion(:==, exp, act_or_msg, msg, block)
98 diff = assertion_diff(exp, act)
100 assert_true(ret, msg, diff)
103 def assert_not_equal(exp, act_or_msg = nil, msg = nil, &block)
104 ret, exp, act, msg = _eval_assertion(:==, exp, act_or_msg, msg, block)
106 diff = " Expected #{act.inspect} to not be equal to #{exp.inspect}."
108 assert_true(!ret, msg, diff)
111 def assert_same(*args); _assert_same(true, *args) end
112 def assert_not_same(*args); _assert_same(false, *args) end
113 def _assert_same(affirmed, exp, act, msg = nil)
114 unless ret = exp.equal?(act) == affirmed
115 exp_str, act_str = [exp, act].map do |o|
116 "#{o.inspect} (class=#{o.class}, oid=#{o.__id__})"
118 diff = " Expected #{act_str} to #{'not ' unless affirmed}be the same as #{exp_str}."
120 assert_true(ret, msg, diff)
123 def assert_nil(obj, msg = nil)
124 unless ret = obj.nil?
125 diff = " Expected #{obj.inspect} to be nil."
127 assert_true(ret, msg, diff)
130 def assert_include(*args); _assert_include(true, *args) end
131 def assert_not_include(*args); _assert_include(false, *args) end
132 def _assert_include(affirmed, collection, obj, msg = nil)
133 unless ret = collection.include?(obj) == affirmed
134 diff = " Expected #{collection.inspect} to #{'not ' unless affirmed}include #{obj.inspect}."
136 assert_true(ret, msg, diff)
140 # Fails unless +obj+ is a kind of +cls+.
141 def assert_kind_of(cls, obj, msg = nil)
142 unless ret = obj.kind_of?(cls)
143 diff = " Expected #{obj.inspect} to be a kind of #{cls}, not #{obj.class}."
145 assert_true(ret, msg, diff)
149 # Fails unless +exp+ is equal to +act+ in terms of a Float
150 def assert_float(exp, act, msg = nil)
151 e, a = exp.to_f, act.to_f
152 if (e.infinite? || a.infinite?) && e != a ||
153 e.nan? && !a.nan? || !e.nan? && a.nan?
154 flunk(msg, " Expected #{act} to be #{exp}.")
155 elsif (n = (e - a).abs) > Mrbtest::FLOAT_TOLERANCE
156 flunk(msg, " Expected |#{exp} - #{act}| (#{n}) to be <= #{Mrbtest::FLOAT_TOLERANCE}.")
162 def assert_raise(*exc)
163 msg = (exc.last.is_a? String) ? exc.pop : nil
164 exc = exc.empty? ? StandardError : exc.size == 1 ? exc[0] : exc
169 rescue Exception => e
170 diff = " #{exc} exception expected, not\n" \
171 " Class: <#{e.class}>\n" \
175 diff = " #{exc} expected but nothing was raised."
180 def assert_nothing_raised(msg = nil)
183 rescue Exception => e
184 diff = " Exception raised:\n" \
185 " Class: <#{e.class}>\n" \
197 def flunk(msg = nil, diff = "Epic Fail!")
198 assert_true(false, msg, diff)
202 # Report the test result and print all assertions
203 # which were reported broken.
207 $asserts.each do |msg|
211 $total_test = $ok_test + $ko_test + $kill_test + $skip_test
212 t_print("Total: #{$total_test}\n")
214 t_print(" OK: #{$ok_test}\n")
215 t_print(" KO: #{$ko_test}\n")
216 t_print("Crash: #{$kill_test}\n")
217 t_print(" Skip: #{$skip_test}\n")
219 if Object.const_defined?(:Time)
220 t_time = Time.now - $test_start
221 t_print(" Time: #{t_time.round(2)} seconds\n")
224 $ko_test == 0 && $kill_test == 0
227 def _eval_assertion(meth, exp, act_or_msg, msg, block)
229 exp, act, msg = exp, block.call, act_or_msg
231 exp, act, msg = exp, act_or_msg, msg
233 return exp.__send__(meth, act), exp, act, msg
238 class MRubyTestSkip < NotImplementedError; end
241 raise MRubyTestSkip.new(cause)