-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbase.gd
384 lines (287 loc) · 8.46 KB
/
base.gd
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# base test class
#extends Object
var _tests = []
var filename = "<unknown>"
var _results = null
func _init():
var t
for t in get_method_list():
# print(t.to_json())
if t["name"].begins_with("test_") && t["args"].size() <= 0:
add(t["name"])
func class_setup():
pass
func class_teardown():
pass
func setup():
pass
func teardown():
pass
# ------------------------------------------------------------------------
func add(test_name):
if test_name == null:
return
if typeof(test_name) == TYPE_STRING:
if not(test_name in _tests):
_tests.append(test_name)
else:
var t
for t in test_name:
if t != null && not(t in _tests):
_tests.append(t)
func add_all(all_tests):
add(all_tests)
func set_tests(test_names):
_tests = []
add(test_names)
func skip(test_name):
if test_name == null:
return
if typeof(test_name) == TYPE_STRING:
_tests.erase(test_name)
else:
var t
for t in test_name:
_tests.erase(test_name)
# ------------------------------------------------------------------------
func check_true(text, bool_val):
if ! bool_val:
if _results != null:
_results.add_error(text)
return false
return true
func check_that(text, actual, matcher):
return check_true(text + ": " + matcher.describe(actual), matcher.matches(actual))
func check(text = ""):
return Checker.new(_results, text)
# ------------------------------------------------------------------------
# result_collector must implement these methods:
# func start_suite(suite_name)
# func end_suite()
# func start_test(name)
# func end_test()
# func add_error(message)
# func has_errors() (does the current suite have any errors?)
func run(result_collector):
self._results = result_collector
result_collector.start_suite(filename)
class_setup()
if result_collector.has_error():
return
var t
for t in _tests:
if has_method(t):
run_test(t)
else:
result_collector.add_error("Setup Error: requested function does not exist: " + t)
class_teardown()
result_collector.end_suite()
func run_test(name):
if _results != null:
_results.start_test(name)
setup()
call(name)
teardown()
if _results != null:
_results.end_test()
# -------------------------------------------------------------------------
class Matcher:
func matches(value):
return false
func describe(value):
return ""
func _as_str(value):
if value == null:
return "<null>"
if typeof(value) == TYPE_DICTIONARY:
return value.to_json()
return "[" + str(value) + "]"
func _is_list(value):
# return typeof(value) == TYPE_ARRAY || typeof(value) == TYPE_INT_ARRAY || typeof(value) == TYPE_REAL_ARRAY || typeof(value) == TYPE_STRING_ARRAY
# All the arrays are in this specific range.
# This needs to be checked against future versions of Godot.
var v = typeof(value)
return v >= TYPE_ARRAY && v <= TYPE_COLOR_ARRAY
class IsMatcher:
extends Matcher
var val
var _epsilon
func _init(v, epsilon = 0.00001):
val = v
_epsilon = epsilon
func matches(value):
return _inner_match(val, value, [])
func _inner_match(v1, v2, seen):
seen.append(v2)
# Check lists the same way
if _is_list(v1) && _is_list(v2):
if v1.size() != v2.size():
return false
var i
for i in range(0, v1.size()):
if v2[i] in seen:
# This isn't a correct check; instead,
# we need to ensure that the seen version of v2[i]
# matches up with the corresponding seen version of v1[i]
if v1[i] != v2[i]:
return false
# Else prevent infinite loop by just
# saying it's right.
elif ! _inner_match(v1[i], v2[i], seen):
return false
return true
# Cannot perform a "==" if the types are different
if typeof(v1) != typeof(v2):
return false
if v1 == v2:
return true
if typeof(v1) == TYPE_DICTIONARY:
if v1.size() != v2.size():
return false
var k
for k in v1:
if not (k in v2):
return false
if v2[k] in seen:
# This isn't a correct check; instead,
# we need to ensure that the seen version of v2[i]
# matches up with the corresponding seen version of v1[i]
if v1[k] != v2[k]:
return false
elif ! _inner_match(v1[k], v2[k], seen):
return false
return true
if typeof(v1) == TYPE_REAL:
# Closeness
return abs(float(v1) - v2) <= _epsilon
# Any other type should have == match right.
return false
func describe(value):
return "expected " + _as_str(val) + ", found " + _as_str(value)
static func is(value, epsilon = 0.00001):
# "is" can be used to make a clear English-like sentence.
# So, if the value is a matcher, then just use that matcher instead of
# adding another layer around "is".
if typeof(value) == TYPE_OBJECT && value.has_method("matches") && value.has_method("describe"):
return value
# Need to wrap the value in an is check.
return IsMatcher.new(value, epsilon)
static func equals(value):
return IsMatcher.new(value)
class NotMatcher:
var val
func _init(v):
if typeof(v) == TYPE_OBJECT && v extends Matcher:
val = v
else:
val = IsMatcher.new(v)
func matches(value):
return ! val.matches(value)
func describe(value):
return "expected not: " + val.describe(value)
static func is_not(value):
return NotMatcher.new(value)
class BetweenMatcher:
var lo
var hi
func _init(l, h):
lo = float(l)
hi = float(h)
func matches(value):
return value != null && float(value) >= lo && float(value) <= hi
func describe(value):
return "expected [" + str(lo) + ", " + str(hi) + "], found " + str(value)
static func between(lo, hi):
return BetweenMatcher.new(lo, hi)
class NearMatcher:
var _epsilon
var _val
func _init(val, epsilon = 0.00001):
_epsilon = float(epsilon)
_val = float(val)
func matches(value):
return abs(float(value) - _val) <= _epsilon
func describe(value):
return "expected " + str(_val) + " within " + str(_epsilon) + ", found " + str(value)
static func near(val, epsilon = 0.00001):
return NearMatcher.new(val, epsilon)
class ContainsMatcher:
extends Matcher
var _val
func _init(val):
_val = val
func matches(actual):
if actual == null:
return false
if typeof(actual) == TYPE_STRING:
# Expect a string to contain a sub-string
return actual.find(_val) >= 0
if _is_list(actual):
if _is_list(_val):
# each "val" must be in the actual list
var v
for v in _val:
if ! (v in actual):
return false
return true
return _val in actual
if typeof(actual) == TYPE_RECT2:
if typeof(_val) == TYPE_RECT2:
return actual.encloses(_val)
if typeof(_val) == TYPE_VECTOR2:
return actual.has_point(_val)
return false
if typeof(actual) == TYPE_PLANE:
if typeof(_val) == TYPE_VECTOR3:
return actual.has_point(_val)
return false
if typeof(actual) == TYPE_DICTIONARY:
if _is_list(_val):
return actual.has_all(_val)
return actual.has(_val)
# These don't really make sense.
# Object values should just be checked for equality.
#if typeof(actual) == TYPE_OBJECT:
# return actual.get(_val) != null
return false
func describe(actual):
return "expected " + _as_str(actual) + " to contain " + _as_str(_val)
static func contains(val):
return ContainsMatcher.new(val)
class EmptyMatcher:
extends Matcher
func matches(actual):
if _is_list(actual):
return actual.size() <= 0
if typeof(actual) == TYPE_DICTIONARY:
return actual.keys().size() <= 0
return false
func describe(actual):
if _is_list(actual):
return "expected " + _as_str(actual) + " to be an empty list"
if typeof(actual) == TYPE_DICTIONARY:
return "expected " + _as_str(actual) + " to be an empty dictionary"
return "expected " + _as_str(actual) + " to be an empty list or dictionary"
static func empty():
return EmptyMatcher.new()
# ---------------------------------------------------------------------------
class Checker:
var _text
var _results
func _init(results, text):
_results = results
_text = text
func that(actual, matcher):
var res
var msg
if typeof(matcher) == TYPE_BOOL:
res = matcher
msg = _text
else:
res = matcher.matches(actual)
msg = _text + ": " + matcher.describe(actual)
if ! res:
if _results != null:
_results.add_error(msg)
return false
return true