1 | $ok_test = 0
|
---|
2 | $ko_test = 0
|
---|
3 | $kill_test = 0
|
---|
4 | $asserts = []
|
---|
5 | $test_start = Time.now if Object.const_defined?(:Time)
|
---|
6 |
|
---|
7 | # Implementation of print due to the reason that there might be no print
|
---|
8 | def t_print(*args)
|
---|
9 | i = 0
|
---|
10 | len = args.size
|
---|
11 | while i < len
|
---|
12 | str = args[i].to_s
|
---|
13 | __t_printstr__ str rescue print str
|
---|
14 | i += 1
|
---|
15 | end
|
---|
16 | end
|
---|
17 |
|
---|
18 | ##
|
---|
19 | # Create the assertion in a readable way
|
---|
20 | def assertion_string(err, str, iso=nil, e=nil, bt=nil)
|
---|
21 | msg = "#{err}#{str}"
|
---|
22 | msg += " [#{iso}]" if iso && iso != ''
|
---|
23 | msg += " => #{e.message}" if e
|
---|
24 | msg += " (mrbgems: #{GEMNAME})" if Object.const_defined?(:GEMNAME)
|
---|
25 | if $mrbtest_assert && $mrbtest_assert.size > 0
|
---|
26 | $mrbtest_assert.each do |idx, str, diff|
|
---|
27 | msg += "\n - Assertion[#{idx}] Failed: #{str}\n#{diff}"
|
---|
28 | end
|
---|
29 | end
|
---|
30 | msg += "\nbacktrace:\n\t#{bt.join("\n\t")}" if bt
|
---|
31 | msg
|
---|
32 | end
|
---|
33 |
|
---|
34 | ##
|
---|
35 | # Verify a code block.
|
---|
36 | #
|
---|
37 | # str : A remark which will be printed in case
|
---|
38 | # this assertion fails
|
---|
39 | # iso : The ISO reference code of the feature
|
---|
40 | # which will be tested by this
|
---|
41 | # assertion
|
---|
42 | def assert(str = 'Assertion failed', iso = '')
|
---|
43 | t_print(str, (iso != '' ? " [#{iso}]" : ''), ' : ') if $mrbtest_verbose
|
---|
44 | begin
|
---|
45 | $mrbtest_assert = []
|
---|
46 | $mrbtest_assert_idx = 0
|
---|
47 | yield
|
---|
48 | if($mrbtest_assert.size > 0)
|
---|
49 | $asserts.push(assertion_string('Fail: ', str, iso, nil))
|
---|
50 | $ko_test += 1
|
---|
51 | t_print('F')
|
---|
52 | else
|
---|
53 | $ok_test += 1
|
---|
54 | t_print('.')
|
---|
55 | end
|
---|
56 | rescue Exception => e
|
---|
57 | bt = e.backtrace if $mrbtest_verbose
|
---|
58 | if e.class.to_s == 'MRubyTestSkip'
|
---|
59 | $asserts.push "Skip: #{str} #{iso} #{e.cause}"
|
---|
60 | t_print('?')
|
---|
61 | else
|
---|
62 | $asserts.push(assertion_string("#{e.class}: ", str, iso, e, bt))
|
---|
63 | $kill_test += 1
|
---|
64 | t_print('X')
|
---|
65 | end
|
---|
66 | ensure
|
---|
67 | $mrbtest_assert = nil
|
---|
68 | end
|
---|
69 | t_print("\n") if $mrbtest_verbose
|
---|
70 | end
|
---|
71 |
|
---|
72 | def assertion_diff(exp, act)
|
---|
73 | " Expected: #{exp.inspect}\n" +
|
---|
74 | " Actual: #{act.inspect}"
|
---|
75 | end
|
---|
76 |
|
---|
77 | def assert_true(ret, msg = nil, diff = nil)
|
---|
78 | if $mrbtest_assert
|
---|
79 | $mrbtest_assert_idx += 1
|
---|
80 | unless ret
|
---|
81 | msg = "Expected #{ret.inspect} to be true" unless msg
|
---|
82 | diff = assertion_diff(true, ret) unless diff
|
---|
83 | $mrbtest_assert.push([$mrbtest_assert_idx, msg, diff])
|
---|
84 | end
|
---|
85 | end
|
---|
86 | ret
|
---|
87 | end
|
---|
88 |
|
---|
89 | def assert_false(ret, msg = nil, diff = nil)
|
---|
90 | if $mrbtest_assert
|
---|
91 | $mrbtest_assert_idx += 1
|
---|
92 | if ret
|
---|
93 | msg = "Expected #{ret.inspect} to be false" unless msg
|
---|
94 | diff = assertion_diff(false, ret) unless diff
|
---|
95 |
|
---|
96 | $mrbtest_assert.push([$mrbtest_assert_idx, msg, diff])
|
---|
97 | end
|
---|
98 | end
|
---|
99 | !ret
|
---|
100 | end
|
---|
101 |
|
---|
102 | def assert_equal(arg1, arg2 = nil, arg3 = nil)
|
---|
103 | if block_given?
|
---|
104 | exp, act, msg = arg1, yield, arg2
|
---|
105 | else
|
---|
106 | exp, act, msg = arg1, arg2, arg3
|
---|
107 | end
|
---|
108 |
|
---|
109 | msg = "Expected to be equal" unless msg
|
---|
110 | diff = assertion_diff(exp, act)
|
---|
111 | assert_true(exp == act, msg, diff)
|
---|
112 | end
|
---|
113 |
|
---|
114 | def assert_not_equal(arg1, arg2 = nil, arg3 = nil)
|
---|
115 | if block_given?
|
---|
116 | exp, act, msg = arg1, yield, arg2
|
---|
117 | else
|
---|
118 | exp, act, msg = arg1, arg2, arg3
|
---|
119 | end
|
---|
120 |
|
---|
121 | msg = "Expected to be not equal" unless msg
|
---|
122 | diff = assertion_diff(exp, act)
|
---|
123 | assert_false(exp == act, msg, diff)
|
---|
124 | end
|
---|
125 |
|
---|
126 | def assert_nil(obj, msg = nil)
|
---|
127 | msg = "Expected #{obj.inspect} to be nil" unless msg
|
---|
128 | diff = assertion_diff(nil, obj)
|
---|
129 | assert_true(obj.nil?, msg, diff)
|
---|
130 | end
|
---|
131 |
|
---|
132 | def assert_include(collection, obj, msg = nil)
|
---|
133 | msg = "Expected #{collection.inspect} to include #{obj.inspect}" unless msg
|
---|
134 | diff = " Collection: #{collection.inspect}\n" +
|
---|
135 | " Object: #{obj.inspect}"
|
---|
136 | assert_true(collection.include?(obj), msg, diff)
|
---|
137 | end
|
---|
138 |
|
---|
139 | def assert_not_include(collection, obj, msg = nil)
|
---|
140 | msg = "Expected #{collection.inspect} to not include #{obj.inspect}" unless msg
|
---|
141 | diff = " Collection: #{collection.inspect}\n" +
|
---|
142 | " Object: #{obj.inspect}"
|
---|
143 | assert_false(collection.include?(obj), msg, diff)
|
---|
144 | end
|
---|
145 |
|
---|
146 | def assert_raise(*exp)
|
---|
147 | ret = true
|
---|
148 | if $mrbtest_assert
|
---|
149 | $mrbtest_assert_idx += 1
|
---|
150 | msg = exp.last.class == String ? exp.pop : nil
|
---|
151 | msg = msg.to_s + " : " if msg
|
---|
152 | should_raise = false
|
---|
153 | begin
|
---|
154 | yield
|
---|
155 | should_raise = true
|
---|
156 | rescue Exception => e
|
---|
157 | msg = "#{msg}#{exp.inspect} exception expected, not"
|
---|
158 | diff = " Class: <#{e.class}>\n" +
|
---|
159 | " Message: #{e.message}"
|
---|
160 | if not exp.any?{|ex| ex.instance_of?(Module) ? e.kind_of?(ex) : ex == e.class }
|
---|
161 | $mrbtest_assert.push([$mrbtest_assert_idx, msg, diff])
|
---|
162 | ret = false
|
---|
163 | end
|
---|
164 | end
|
---|
165 |
|
---|
166 | exp = exp.first if exp.first
|
---|
167 | if should_raise
|
---|
168 | msg = "#{msg}#{exp.inspect} expected but nothing was raised."
|
---|
169 | $mrbtest_assert.push([$mrbtest_assert_idx, msg, nil])
|
---|
170 | ret = false
|
---|
171 | end
|
---|
172 | end
|
---|
173 | ret
|
---|
174 | end
|
---|
175 |
|
---|
176 | def assert_nothing_raised(*exp)
|
---|
177 | ret = true
|
---|
178 | if $mrbtest_assert
|
---|
179 | $mrbtest_assert_idx += 1
|
---|
180 | msg = exp.last.class == String ? exp.pop : ""
|
---|
181 | begin
|
---|
182 | yield
|
---|
183 | rescue Exception => e
|
---|
184 | msg = "#{msg} exception raised."
|
---|
185 | diff = " Class: <#{e.class}>\n" +
|
---|
186 | " Message: #{e.message}"
|
---|
187 | $mrbtest_assert.push([$mrbtest_assert_idx, msg, diff])
|
---|
188 | ret = false
|
---|
189 | end
|
---|
190 | end
|
---|
191 | ret
|
---|
192 | end
|
---|
193 |
|
---|
194 | ##
|
---|
195 | # Fails unless +obj+ is a kind of +cls+.
|
---|
196 | def assert_kind_of(cls, obj, msg = nil)
|
---|
197 | msg = "Expected #{obj.inspect} to be a kind of #{cls}, not #{obj.class}" unless msg
|
---|
198 | diff = assertion_diff(cls, obj.class)
|
---|
199 | assert_true(obj.kind_of?(cls), msg, diff)
|
---|
200 | end
|
---|
201 |
|
---|
202 | ##
|
---|
203 | # Fails unless +exp+ is equal to +act+ in terms of a Float
|
---|
204 | def assert_float(exp, act, msg = nil)
|
---|
205 | msg = "Float #{exp} expected to be equal to float #{act}" unless msg
|
---|
206 | diff = assertion_diff(exp, act)
|
---|
207 | assert_true check_float(exp, act), msg, diff
|
---|
208 | end
|
---|
209 |
|
---|
210 | ##
|
---|
211 | # Report the test result and print all assertions
|
---|
212 | # which were reported broken.
|
---|
213 | def report()
|
---|
214 | t_print("\n")
|
---|
215 |
|
---|
216 | $asserts.each do |msg|
|
---|
217 | t_print "#{msg}\n"
|
---|
218 | end
|
---|
219 |
|
---|
220 | $total_test = $ok_test+$ko_test+$kill_test
|
---|
221 | t_print("Total: #{$total_test}\n")
|
---|
222 |
|
---|
223 | t_print(" OK: #{$ok_test}\n")
|
---|
224 | t_print(" KO: #{$ko_test}\n")
|
---|
225 | t_print("Crash: #{$kill_test}\n")
|
---|
226 |
|
---|
227 | if Object.const_defined?(:Time)
|
---|
228 | t_time = Time.now - $test_start
|
---|
229 | t_print(" Time: #{t_time.round(2)} seconds\n")
|
---|
230 | end
|
---|
231 | end
|
---|
232 |
|
---|
233 | ##
|
---|
234 | # Performs fuzzy check for equality on methods returning floats
|
---|
235 | def check_float(a, b)
|
---|
236 | tolerance = Mrbtest::FLOAT_TOLERANCE
|
---|
237 | a = a.to_f
|
---|
238 | b = b.to_f
|
---|
239 | if a.finite? and b.finite?
|
---|
240 | (a-b).abs < tolerance
|
---|
241 | else
|
---|
242 | true
|
---|
243 | end
|
---|
244 | end
|
---|
245 |
|
---|
246 | ##
|
---|
247 | # Skip the test
|
---|
248 | class MRubyTestSkip < NotImplementedError
|
---|
249 | attr_accessor :cause
|
---|
250 | def initialize(cause)
|
---|
251 | @cause = cause
|
---|
252 | end
|
---|
253 | end
|
---|
254 |
|
---|
255 | def skip(cause = "")
|
---|
256 | raise MRubyTestSkip.new(cause)
|
---|
257 | end
|
---|