forked from google/starlark-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert.star
59 lines (50 loc) · 1.77 KB
/
assert.star
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
# Predeclared built-ins for this module:
#
# error(msg): report an error in Go's test framework without halting execution.
# This is distinct from the built-in fail function, which halts execution.
# catch(f): evaluate f() and returns its evaluation error message, if any
# matches(str, pattern): report whether str matches regular expression pattern.
# module(**kwargs): a constructor for a module.
# _freeze(x): freeze the value x and everything reachable from it.
# _floateq(x, y): reports floating point equality (within 1 ULP).
#
# Clients may use these functions to define their own testing abstractions.
_num = ("float", "int")
def _eq(x, y):
if x != y:
if (type(x) == "float" and type(y) in _num or
type(y) == "float" and type(x) in _num):
if not _floateq(float(x), float(y)):
error("floats: %r != %r (delta > 1 ulp)" % (x, y))
else:
error("%r != %r" % (x, y))
def _ne(x, y):
if x == y:
error("%r == %r" % (x, y))
def _true(cond, msg = "assertion failed"):
if not cond:
error(msg)
def _lt(x, y):
if not (x < y):
error("%s is not less than %s" % (x, y))
def _contains(x, y):
if y not in x:
error("%s does not contain %s" % (x, y))
def _fails(f, pattern):
"assert_fails asserts that evaluation of f() fails with the specified error."
msg = catch(f)
if msg == None:
error("evaluation succeeded unexpectedly (want error matching %r)" % pattern)
elif not matches(pattern, msg):
error("regular expression (%s) did not match error (%s)" % (pattern, msg))
freeze = _freeze # an exported global whose value is the built-in freeze function
assert = module(
"assert",
fail = error,
eq = _eq,
ne = _ne,
true = _true,
lt = _lt,
contains = _contains,
fails = _fails,
)