source: EcnlProtoTool/trunk/mruby-1.2.0/test/assert.rb@ 270

Last change on this file since 270 was 270, checked in by coas-nagasima, 7 years ago

mruby版ECNLプロトタイピング・ツールを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby
File size: 6.4 KB
Line 
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
8def 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
20end
21
22##
23# Create the assertion in a readable way
24def 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
36end
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
46def 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
73end
74
75def assertion_diff(exp, act)
76 " Expected: #{exp.inspect}\n" +
77 " Actual: #{act.inspect}"
78end
79
80def 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
90end
91
92def 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
103end
104
105def 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)
115end
116
117def 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)
127end
128
129def 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)
133end
134
135def 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)
140end
141
142def 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)
147end
148
149def 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
177end
178
179def 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
195end
196
197##
198# Fails unless +obj+ is a kind of +cls+.
199def 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)
203end
204
205##
206# Fails unless +exp+ is equal to +act+ in terms of a Float
207def 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
211end
212
213##
214# Report the test result and print all assertions
215# which were reported broken.
216def 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
234end
235
236##
237# Performs fuzzy check for equality on methods returning floats
238def 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
247end
248
249##
250# Skip the test
251class MRubyTestSkip < NotImplementedError
252 attr_accessor :cause
253 def initialize(cause)
254 @cause = cause
255 end
256end
257
258def skip(cause = "")
259 raise MRubyTestSkip.new(cause)
260end
Note: See TracBrowser for help on using the repository browser.