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