github.com/google/skylark@v0.0.0-20181101142754-a5f7082aabed/skylarktest/assert.sky (about) 1 2 # Predeclared built-ins for this module: 3 # 4 # error(msg): report an error in Go's test framework without halting execution. 5 # catch(f): evaluate f() and returns its evaluation error message, if any 6 # matches(str, pattern): report whether str matches regular expression pattern. 7 # struct: a constructor for a simple HasFields implementation. 8 # _freeze(x): freeze the value x and everything reachable from it. 9 # 10 # Clients may use these functions to define their own testing abstractions. 11 12 def _eq(x, y): 13 if x != y: 14 error("%r != %r" % (x, y)) 15 16 def _ne(x, y): 17 if x == y: 18 error("%r == %r" % (x, y)) 19 20 def _true(cond, msg="assertion failed"): 21 if not cond: 22 error(msg) 23 24 def _lt(x, y): 25 if not (x < y): 26 error("%s is not less than %s" % (x, y)) 27 28 def _contains(x, y): 29 if y not in x: 30 error("%s does not contain %s" % (x, y)) 31 32 def _fails(f, pattern): 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)) 39 40 freeze = _freeze # an exported global whose value is the built-in freeze function 41 42 assert = struct( 43 fail = error, 44 eq = _eq, 45 ne = _ne, 46 true = _true, 47 lt = _lt, 48 contains = _contains, 49 fails = _fails, 50 )