On this page:
check
8.12

6.55 Unit Testing🔗ℹ

expression

check:

  maybe_eval

  body

  ...

  expected_result

 

expression

check:

  expr expected_result

  ...

 

expression

check expr expected_result

 

maybe_eval

 = 

~eval

 | 

~eval: evaluator_expr

 | 

ϵ

 

expected_result

 = 

~is expected_expr

 | 

~is: expected_body; ...

 | 

~is_now expected_expr

 | 

~is_now: expected_body; ...

 | 

~is_a annot

 | 

~is_a: annot

 | 

~prints_like expected_expr

 | 

~prints_like: expected_body; ...

 | 

~matches expected_bind

 | 

~matches: expected_bind

 | 

~prints expected_expr

 | 

~prints: expected_body; ...

 | 

~throws expected_expr

 | 

~throws: expected_body; ...

 | 

~completes

Evaluates the body or expr form, catching any exception that is thrown, then determines whether the result or exception matches expected_result:

If ~eval is present, then body is quoted to be parsed and evaluated using eval at run time. A typical use of ~eval is to test syntax errors. When evaluator_expr is provided, then its result is installed as the current evaluator for eval; otherwise, an evaluator is created with Evaluator.make_rhombus. The body sequence is evaluated as an interactive form (i.e., eval is called with ~as_interactive: #true).

Providing multiple expr expected_result groups in a single check form is the same as providing each group to a separate check form. The ~eval mode is not supported in the shorthand forms of check.

The evaluation of an expr or body sequence is wrapped with a prompt for the default continuation prompt tag.

When a test fails, a failure message is printed to the current error port, but using the current error display handler when a source location is available. No output is printed for a successful test.

> check 1+1 ~is 2

> check 1+1 ~is 3

check: failed

  got: 2

  expected: 3

> check:

    1+1

    ~is 2

> check:

    1+1 ~is 2

    1+1 ~is 3

check: failed

  got: 2

  expected: 3

> check:

    1+1

    ~is_a Int

> check:

    1+1

    ~is_a String

check: failed

  got: 2

  expected: satisfying String

> check:

    [1+1, 1+2]

    ~matches [_ :: Int, ...]

> check:

    '$(1+1)'

    ~prints_like '2'

> check:

    1+"a"

    ~throws "expected: Number"

> check:

    + // oops: causes syntax error for overall `check` form

    ~throws "infix operator without preceding argument"

+: infix operator without preceding argument

> check:

    ~eval // lets `check` confirm the expected syntax error

    +

    ~throws "infix operator without preceding argument"

> check:

    ~eval

    +

    ~throws values("+", "infix operator without preceding argument")