source: EcnlProtoTool/trunk/mruby-1.3.0/test/assert.rb@ 331

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

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby;charset=UTF-8
File size: 6.3 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 __t_printstr__ str rescue print str
14 i += 1
15 end
16end
17
18##
19# Create the assertion in a readable way
20def 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
32end
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
42def 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
70end
71
72def assertion_diff(exp, act)
73 " Expected: #{exp.inspect}\n" +
74 " Actual: #{act.inspect}"
75end
76
77def 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
87end
88
89def 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
100end
101
102def 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)
112end
113
114def 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)
124end
125
126def 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)
130end
131
132def 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)
137end
138
139def 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)
144end
145
146def 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
174end
175
176def 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
192end
193
194##
195# Fails unless +obj+ is a kind of +cls+.
196def 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)
200end
201
202##
203# Fails unless +exp+ is equal to +act+ in terms of a Float
204def 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
208end
209
210##
211# Report the test result and print all assertions
212# which were reported broken.
213def 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
231end
232
233##
234# Performs fuzzy check for equality on methods returning floats
235def 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
244end
245
246##
247# Skip the test
248class MRubyTestSkip < NotImplementedError
249 attr_accessor :cause
250 def initialize(cause)
251 @cause = cause
252 end
253end
254
255def skip(cause = "")
256 raise MRubyTestSkip.new(cause)
257end
Note: See TracBrowser for help on using the repository browser.