github.com/lab47/exprcore@v0.0.0-20210525052339-fb7d6bd9331e/exprcoretest/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  }
    17  
    18  def _ne(x, y) {
    19      if x == y {
    20          error("%r == %r" % (x, y))
    21      }
    22  }
    23  
    24  def _true(cond, msg = "assertion failed") {
    25      if not cond {
    26          error(msg)
    27      }
    28  }
    29  
    30  def _lt(x, y) {
    31      if not (x < y) {
    32          error("%s is not less than %s" % (x, y))
    33      }
    34  }
    35  
    36  def _contains(x, y) {
    37      if y not in x {
    38          error("%s does not contain %s" % (x, y))
    39      }
    40  }
    41  
    42  def _fails(f, pattern) {
    43      "assert_fails asserts that evaluation of f() fails with the specified error."
    44      msg = catch(f)
    45      if msg == None {
    46          error("evaluation succeeded unexpectedly (want error matching %r)" % pattern)
    47      } elif not matches(pattern, msg) {
    48          error("regular expression (%s) did not match error (%s)" % (pattern, msg))
    49      }
    50  }
    51  
    52  freeze = _freeze  # an exported global whose value is the built-in freeze function
    53  
    54  assert = module(
    55      "assert",
    56      fail: error,
    57      eq: _eq,
    58      ne: _ne,
    59      true: _true,
    60      lt: _lt,
    61      contains: _contains,
    62      fails: _fails,
    63  )