/
fancy_spec.fy
241 lines (202 loc) · 6.19 KB
/
fancy_spec.fy
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
class FancySpec {
"""
The FancySpec class is used for defining FancySpec testsuites.
Have a look at the tests/ directory to see some examples.
"""
def initialize: @description test_obj: @test_obj (@description) {
@spec_tests = []
}
def FancySpec describe: test_obj with: block {
spec = FancySpec new: test_obj
block call_with_receiver: spec
spec run
}
def FancySpec describe: description for: test_obj with: block {
spec = FancySpec new: description test_obj: test_obj
block call_with_receiver: spec
spec run
}
def it: spec_info_string when: spec_block {
test = SpecTest new: spec_info_string block: spec_block
@spec_tests << test
}
def it: spec_info_string for: method_name when: spec_block {
test = SpecTest new: spec_info_string block: spec_block
# try {
# @test_obj method: method_name . if_true: |method| {
# method tests << test
# }
# } catch MethodNotFoundError => e {
# # ignore errors
# }
@spec_tests << test
}
def run {
"Running tests for: " ++ @description ++ ": " print
@spec_tests each: |test| {
test run: @test_obj
}
# Console newline Console newline
# untested_methods = @test_obj methods select: |m| {
# m tests size == 0
# }
# untested_methods empty? if_false: {
# ["WARNING: These methods need tests:",
# untested_methods map: 'name . select: |m| { m whitespace? not } . join: ", "] println
# }
Console newline
}
class SpecTest {
@@failed_positive = []
@@failed_negative = []
def SpecTest failed_test: actual_and_expected {
@@failed_positive << [actual_and_expected, caller(6) at: 0]
}
def SpecTest failed_negative_test: value {
@@failed_negative << [value, caller(6) at: 0]
}
def initialize: @info_str block: @block {
{ @@failed_positive = [] } unless: @@failed_positive
{ @@failed_negative = [] } unless: @@failed_negative
}
def run: test_obj {
@@failed_positive = []
@@failed_negative = []
try {
@block call
} catch IOError => e {
SpecTest failed_test: [e, "UNKNOWN"]
}
any_failure = nil
if: (@@failed_positive size > 0) then: {
any_failure = true
Console newline
"> FAILED: " ++ test_obj ++ " " ++ @info_str print
print_failed_positive
}
if: (@@failed_negative size > 0) then: {
any_failure = true
Console newline
"> FAILED: " ++ test_obj ++ " " ++ @info_str print
print_failed_negative
}
{ "." print } unless: any_failure
}
def print_failed_positive {
" [" ++ (@@failed_positive size) ++ " unexpected values]" println
print_failed_common: @@failed_positive
}
def print_failed_negative {
" [" ++ (@@failed_negative size) ++ " unexpected values]" println
"Should not have gotten the following values: " println
print_failed_common: @@failed_negative
}
def print_failed_common: failures {
failures each: |f| {
actual, expected = f first
location = f second gsub(/:(\d+):in `[^']+'/, " +\1")
location = location split: "/" . from: -2 to: -1 . join: "/"
location println
" Expected: #{expected inspect}" println
" Received: #{actual inspect}" println
}
}
}
class PositiveMatcher {
"""PositiveMatcher expects its actual value to be equal to an
expected value.
If the values are not equal, a SpecTest failure is generated."""
def initialize: @actual_value {
}
def == expected_value {
unless: (@actual_value == expected_value) do: {
SpecTest failed_test: [@actual_value, expected_value]
}
}
def != expected_value {
unless: (@actual_value != expected_value) do: {
SpecTest failed_negative_test: [@actual_value, expected_value]
}
}
def raise: exception_class {
try {
@actual_value call
} catch exception_class {
# ok
} catch Exception => e {
SpecTest failed_test: [e class, exception_class]
}
}
def raise: exception_class with: block {
try {
@actual_value call
} catch exception_class => e {
block call: [e]
# ok
} catch Exception => e {
SpecTest failed_test: [e class, exception_class]
}
}
def unknown_message: msg with_params: params {
"""Forwardy any other message and parameters to the object itself
and checks the return value."""
unless: (@actual_value send_message: msg with_params: params) do: {
SpecTest failed_test: [@actual_value, params first]
}
}
def be: block {
unless: (block call: [@actual_value]) do: {
SpecTest failed_test: [@actual_value, nil]
}
}
}
class NegativeMatcher {
"""NegativeMatcher expects its actual value to be unequal to an
expected value.
If the values are equal, a SpecTest failure is generated."""
def initialize: @actual_value {
}
def == expected_value {
if: (@actual_value == expected_value) then: {
SpecTest failed_negative_test: @actual_value
}
}
def != expected_value {
if: (@actual_value != expected_value) then: {
SpecTest failed_test: [@actual_value, expected_value]
}
}
def raise: exception_class {
try {
@actual_value call
} catch exception_class {
SpecTest failed_negative_test: [exception_class, nil]
} catch Exception => e {
true
# ok
}
}
def unknown_message: msg with_params: params {
"""Forwardy any other message and parameters to the object itself
and checks the return value."""
if: (@actual_value send_message: msg with_params: params) then: {
SpecTest failed_negative_test: @actual_value
}
}
def be: block {
if: (block call: [@actual_value]) then: {
SpecTest failed_negative_test: @actual_value
}
}
}
}
class Object {
def should {
"Returns a PositiveMatcher for self."
FancySpec PositiveMatcher new: self
}
def should_not {
"Returns a NegativeMatcher for self."
FancySpec NegativeMatcher new: self
}
}