github.com/k14s/starlark-go@v0.0.0-20200720175618-3a5c849cc368/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  #
    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 = module(
    43      "assert",
    44      fail = error,
    45      eq = _eq,
    46      ne = _ne,
    47      true = _true,
    48      lt = _lt,
    49      contains = _contains,
    50      fails = _fails,
    51  )