alandonovan | a1b28d8 | 2018-03-13 10:59:24 -0400 | [diff] [blame] | 1 | # Predeclared built-ins for this module: |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 2 | # |
| 3 | # error(msg): report an error in Go's test framework without halting execution. |
alandonovan | 30ae18b | 2019-05-28 16:29:25 -0400 | [diff] [blame] | 4 | # This is distinct from the built-in fail function, which halts execution. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 5 | # catch(f): evaluate f() and returns its evaluation error message, if any |
| 6 | # matches(str, pattern): report whether str matches regular expression pattern. |
alandonovan | 9d97771 | 2019-01-04 13:04:59 -0500 | [diff] [blame] | 7 | # module(**kwargs): a constructor for a module. |
alandonovan | a1b28d8 | 2018-03-13 10:59:24 -0400 | [diff] [blame] | 8 | # _freeze(x): freeze the value x and everything reachable from it. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 9 | # |
| 10 | # Clients may use these functions to define their own testing abstractions. |
| 11 | |
| 12 | def _eq(x, y): |
alandonovan | 9d97771 | 2019-01-04 13:04:59 -0500 | [diff] [blame] | 13 | if x != y: |
| 14 | error("%r != %r" % (x, y)) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 15 | |
| 16 | def _ne(x, y): |
alandonovan | 9d97771 | 2019-01-04 13:04:59 -0500 | [diff] [blame] | 17 | if x == y: |
| 18 | error("%r == %r" % (x, y)) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 19 | |
alandonovan | 9d97771 | 2019-01-04 13:04:59 -0500 | [diff] [blame] | 20 | def _true(cond, msg = "assertion failed"): |
| 21 | if not cond: |
| 22 | error(msg) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 23 | |
| 24 | def _lt(x, y): |
alandonovan | 9d97771 | 2019-01-04 13:04:59 -0500 | [diff] [blame] | 25 | if not (x < y): |
| 26 | error("%s is not less than %s" % (x, y)) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 27 | |
| 28 | def _contains(x, y): |
alandonovan | 9d97771 | 2019-01-04 13:04:59 -0500 | [diff] [blame] | 29 | if y not in x: |
| 30 | error("%s does not contain %s" % (x, y)) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 31 | |
| 32 | def _fails(f, pattern): |
alandonovan | 9d97771 | 2019-01-04 13:04:59 -0500 | [diff] [blame] | 33 | "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 Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 39 | |
alandonovan | 9d97771 | 2019-01-04 13:04:59 -0500 | [diff] [blame] | 40 | freeze = _freeze # an exported global whose value is the built-in freeze function |
alandonovan | ffb61b8 | 2017-11-10 13:23:11 -0500 | [diff] [blame] | 41 | |
alandonovan | 9d97771 | 2019-01-04 13:04:59 -0500 | [diff] [blame] | 42 | assert = module( |
| 43 | "assert", |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 44 | fail = error, |
| 45 | eq = _eq, |
| 46 | ne = _ne, |
| 47 | true = _true, |
| 48 | lt = _lt, |
| 49 | contains = _contains, |
| 50 | fails = _fails, |
| 51 | ) |