go.starlark.net@v0.0.0-20231101134539-556fd59b42f6/starlarktest/assert.star (about)

     1  # Predeclared built-ins for this module:
     2  #
     3  # error(msg): report an error in Go's test framework without halting execution.
     4  #  This is distinct from the built-in fail function, which halts 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  # module(**kwargs): a constructor for a module.
     8  # _freeze(x): freeze the value x and everything reachable from it.
     9  # _floateq(x, y): reports floating point equality (within 1 ULP).
    10  #
    11  # Clients may use these functions to define their own testing abstractions.
    12  
    13  _num = ("float", "int")
    14  
    15  def _eq(x, y):
    16      if x != y:
    17  	if (type(x) == "float" and type(y) in _num or
    18  	    type(y) == "float" and type(x) in _num):
    19  	    if not _floateq(float(x), float(y)):
    20  		error("floats: %r != %r (delta > 1 ulp)" % (x, y))
    21  	else:
    22              error("%r != %r" % (x, y))
    23  
    24  def _ne(x, y):
    25      if x == y:
    26          error("%r == %r" % (x, y))
    27  
    28  def _true(cond, msg = "assertion failed"):
    29      if not cond:
    30          error(msg)
    31  
    32  def _lt(x, y):
    33      if not (x < y):
    34          error("%s is not less than %s" % (x, y))
    35  
    36  def _contains(x, y):
    37      if y not in x:
    38          error("%s does not contain %s" % (x, y))
    39  
    40  def _fails(f, pattern):
    41      "assert_fails asserts that evaluation of f() fails with the specified error."
    42      msg = catch(f)
    43      if msg == None:
    44          error("evaluation succeeded unexpectedly (want error matching %r)" % pattern)
    45      elif not matches(pattern, msg):
    46          error("regular expression (%s) did not match error (%s)" % (pattern, msg))
    47  
    48  freeze = _freeze  # an exported global whose value is the built-in freeze function
    49  
    50  assert = module(
    51      "assert",
    52      fail = error,
    53      eq = _eq,
    54      ne = _ne,
    55      true = _true,
    56      lt = _lt,
    57      contains = _contains,
    58      fails = _fails,
    59  )