Browse the docs

silk.testing.testing.

Assertions for tests, and the filter rules that the test runner uses to select tests.

When to use#

Use Testing.expect in a test function to check a condition. Use Testing.matches or Testing.matchesQualified in a custom test runner that must select tests with the same rules as the built-in runner.

Details#

An assertion failure is an ordinary typed failure, AssertionError. It needs no service and holds the message of the caller. A failed expect stops the test with that failure.

The filters compare the file path exactly. They compare the test name as an ASCII substring and ignore ASCII case. They do not fold non-ASCII letters.

Examples#

Check a condition in a test function#

silk
import silk.testing.testing { AssertionError, Testing }

fn double(value: i32) -> i32 {
  return value * 2
}

test effect fn doubleAddsTheValueToItself() -> ()
  ! AssertionError<'static> {
  run Testing.expect(double(21) == 42, "double(21) is 42")
}

pub fn main() -> i32 {
  return 0
}

Import as Testing with import silk.testing.testing { Testing }.

Public declarations: 2.

AssertionError#

silk
pub struct AssertionError<'message>

The failure of a Testing.expect check. It holds the message of the caller.

Details#

Read the message with Testing.assertionMessage.

Implementation Debug for AssertionError<'message>#

silk
impl<'message> Debug for AssertionError<'message>

Operation debug#

silk
effect fn debug(self: &Self, formatter: &mut Formatter) -> () ! WriterError ? &mut Writer

Writes the message, as AssertionError { message: "..." }.

Testing#

silk
pub struct Testing

The owner type of the test helpers. It has no fields and is never constructed.

Implementation Testing#

silk
impl Testing

Associated function expect#

silk
pub effect fn expect<'message>(
    condition: bool,
    message: string<'message>
  ) -> () ! AssertionError<'message>

Fails with an AssertionError that holds message if condition is false.

Details#

If condition is true, this function does nothing. Write message as the expected fact, such as "list is empty", so that a failure report reads as the broken expectation.

Errors#

Associated function assertionMessage#

silk
pub fn assertionMessage<'message>(error: &'message AssertionError<'message>) -> string<'message>

Returns the message of an AssertionError.

Associated function matches#

silk
pub fn matches(
    path: string,
    name: string,
    fileFilter: string,
    nameFilter: string,
  ) -> bool

Returns true if a test with file path and name name passes both filters.

Details#

  • fileFilter must be equal to path, byte for byte. An empty fileFilter passes each file.

  • nameFilter must occur in name. The comparison ignores ASCII case. An empty nameFilter passes each name.

Gotchas#

The name comparison folds only ASCII letters. Other letters must match exactly.

Associated function matchesQualified#

silk
pub fn matchesQualified(
    path: string,
    module: string,
    name: string,
    fileFilter: string,
    nameFilter: string,
  ) -> bool

Returns true if a test passes both filters, where the name filter applies to the qualified name module::name.

When to use#

Use this function in a custom runner that reports tests by qualified name, as the built-in runner does. Use Testing.matches to match the plain name only.

Details#

fileFilter must be equal to path, byte for byte. An empty fileFilter passes each file. nameFilter can occur anywhere in module::name, also across the :: separator. The comparison ignores ASCII case.

Category: Testing.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.

View the source of this page