blob: c6e480fc45888284f683e0c380404f070ade9d70 [file] [log] [blame]
alandonovana1b28d82018-03-13 10:59:24 -04001# Predeclared built-ins for this module:
Alan Donovan312d1a52017-10-02 10:10:28 -04002#
3# error(msg): report an error in Go's test framework without halting execution.
alandonovan30ae18b2019-05-28 16:29:25 -04004# This is distinct from the built-in fail function, which halts execution.
Alan Donovan312d1a52017-10-02 10:10:28 -04005# catch(f): evaluate f() and returns its evaluation error message, if any
6# matches(str, pattern): report whether str matches regular expression pattern.
alandonovan9d977712019-01-04 13:04:59 -05007# module(**kwargs): a constructor for a module.
alandonovana1b28d82018-03-13 10:59:24 -04008# _freeze(x): freeze the value x and everything reachable from it.
Alan Donovan312d1a52017-10-02 10:10:28 -04009#
10# Clients may use these functions to define their own testing abstractions.
11
12def _eq(x, y):
alandonovan9d977712019-01-04 13:04:59 -050013 if x != y:
14 error("%r != %r" % (x, y))
Alan Donovan312d1a52017-10-02 10:10:28 -040015
16def _ne(x, y):
alandonovan9d977712019-01-04 13:04:59 -050017 if x == y:
18 error("%r == %r" % (x, y))
Alan Donovan312d1a52017-10-02 10:10:28 -040019
alandonovan9d977712019-01-04 13:04:59 -050020def _true(cond, msg = "assertion failed"):
21 if not cond:
22 error(msg)
Alan Donovan312d1a52017-10-02 10:10:28 -040023
24def _lt(x, y):
alandonovan9d977712019-01-04 13:04:59 -050025 if not (x < y):
26 error("%s is not less than %s" % (x, y))
Alan Donovan312d1a52017-10-02 10:10:28 -040027
28def _contains(x, y):
alandonovan9d977712019-01-04 13:04:59 -050029 if y not in x:
30 error("%s does not contain %s" % (x, y))
Alan Donovan312d1a52017-10-02 10:10:28 -040031
32def _fails(f, pattern):
alandonovan9d977712019-01-04 13:04:59 -050033 "assert_fails asserts that evaluation of f() fails with the specified error."
34 msg = catch(f)
35 if msg == None:
36 error("evaluation succeeded unexpectedly (want error matching %r)" % pattern)
37 elif not matches(pattern, msg):
38 error("regular expression (%s) did not match error (%s)" % (pattern, msg))
Alan Donovan312d1a52017-10-02 10:10:28 -040039
alandonovan9d977712019-01-04 13:04:59 -050040freeze = _freeze # an exported global whose value is the built-in freeze function
alandonovanffb61b82017-11-10 13:23:11 -050041
alandonovan9d977712019-01-04 13:04:59 -050042assert = module(
43 "assert",
Alan Donovan312d1a52017-10-02 10:10:28 -040044 fail = error,
45 eq = _eq,
46 ne = _ne,
47 true = _true,
48 lt = _lt,
49 contains = _contains,
50 fails = _fails,
51)