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