On this page:
check
0.46+9.2.0.5

5.6 Unit Testing🔗ℹ

expression

check:

  maybe_eval

  body

  ...

  expected_result

  ... ~nonempty

 

expression

check:

  expr expected_result ... ~nonempty

  ...

 

expression

check expr expected_result ... ~nonempty

 

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_values_bind

 | 

~matches: expected_values_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 each expected_result:

  • In ~is mode, evaluates the expected_expr or expected_body, then checks that the original result is not an exception and is equal by ==.

  • In ~is_now mode, check like ~is mode, but using is_now.

  • In ~is_a mode, checks that the original result is not an exception and satisfies annot.

  • In ~prints_like mode, evaluates the expected_expr or expected_body, then checks that the original result is not an exception and has the same printed form as the expected result.

  • In ~matches mode, checks that the original result is not an exception, that the number of result values matches the number of supplied binds in expected_values_bind, and that each value matches the corresponding bind.

  • In ~prints mode, evaluates the expected_expr or expected_body to obtain any number (as multiple values) of strings or regexps, then checks that there is no exception and the output string contains each expected string and matches each expected regexp. Use rx'' to check that nothing is printed, since empty output includes the string "".

  • In ~throws mode, evaluates the expected_expr or expected_body to obtain any number (as multiple values) of strings and regexps, then checks that original body or expr threw an exception whose message constains each expected string and matches each expected regexp.

  • In ~completes mode, checks merely that the original result is not an exception. A #void is implicitly added to the end of body or expr, so it could end with a definition, as long as no other kind of expected_result is present for the same expr or body sequence.

When expected_results are present for one expr or body sequence, then they must all match for the check to be considered passing. Printed output is captured for an expr or body sequence only when at least one expected_result is a ~prints_like or ~prints form.

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_a Int ~prints rx''

> 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

    ~is_a Int

    ~prints rx''

> check:

    [1+1, 1+2]

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

> check:

    '$(1+1)'

    ~prints_like '2'

> check:

    println("hi", "there")

    ~prints values("hi", "ere", rx'["a" - "z" space]*')

> check:

    1+"a"

    ~throws "expected: Number"

check: failed

  got: exception +: value does not satisfy annotation

                   annotation: Number

                   value: "a"

  expected: exception "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")