Standard library / std::test

std::test

This module provides test-only helpers that record test failures without aborting the process.

These helpers are intended to be used under silk test and integrate with the language-level assert behavior in test builds (testing).

Each helper also carries a Formal Silk contract requiring BUILD_MODE == "test" via std::test::requires_test_mode() so downstream verification can model them as test-only APIs.

Exported API#

expect#

expect(ok: bool, message: string? = None);

Semantics:

  • When ok is true, expect does nothing.
  • When ok is false, expect records a test failure.
  • When message is Some(...), it is used as the failure message.
  • When message is None, the default message is "expect failed".

expect_equal#

fn (X, Y) expect_equal (expected: X, actual: Y) -> bool;

Semantics:

  • Returns true when expected == (actual as X) and records no failure.
  • Returns false when expected != (actual as X) and records a failure.

Note: in Silk currently, equality must be supported for the concrete instantiated types used at the call site.

expect_error#

fn (E) expect_error (err: E?) -> bool;

Semantics:

  • Returns true when err is Some(...).
  • Returns false when err is None and records a failure.

Example#

import std::test;

test "parsing smoke test" {
  let value = Some(42);

  std::test::expect(value != None, "value should be present");
  std::test::expect_equal(42, match (value) {
    Some(v) => v,
    None => 0,
  });
  std::test::expect_error(Some("boom"));
}

Source repository · Edit this page · View Markdown