std.testing

Assertions for moon tests.

Importuse std.testing

Functions

fn assert(condition: bool, message: string)#

Requires condition; otherwise stops with assertion failed: <message>.

  • condition: Requirement that must hold.
  • message: Text reported when it does not.
use std.testing

fn test_math_helpers() {
  testing.assert(2 + 2 == 4, "addition")
  testing.assert_not(false, "must stay false")
  testing.assert_true(true)
  testing.assert_false(false)
  testing.pass("math helpers")
}
fn assert_not(condition: bool, message: string)#

Requires condition to be false.

  • condition: Requirement that must not hold.
  • message: Text reported when it does.
fn assert_true(value: bool)#

Requires value == true.

  • value: Value that must be true.
fn assert_false(value: bool)#

Requires value == false.

  • value: Value that must be false.
fn assert_eq<A, B>(actual: A, expected: B)#

Generic equality assertion using the language's == semantics.

  • actual: Value produced by the code under test.
  • expected: Value it must equal.
use std.testing

fn test_comparison_assertions() {
  testing.assert_eq([1, 2], [1, 2])
  testing.assert_ne("moon", "Sun")
  testing.assert_str_eq("moon".to_upper(), "MOON")
  testing.assert_str_ne("a", "b")
  testing.assert_gt(4, 3)
  testing.assert_gte(4, 4)
  testing.assert_lt(3, 4)
  testing.assert_lte(4, 4)
}
fn fail(message: string)#

Prints a failure marker, then stops execution.

  • message: Text printed with the failure marker.
fn pass(message: string)#

Prints a passing-test marker.

  • message: Text printed with the passing marker.
fn assert_eq<A, B>(actual: A, expected: B)#

Generic equality assertion using the language's == semantics.

  • actual: Value produced by the code under test.
  • expected: Value it must equal.
use std.testing

fn test_comparison_assertions() {
  testing.assert_eq([1, 2], [1, 2])
  testing.assert_ne("moon", "Sun")
  testing.assert_str_eq("moon".to_upper(), "MOON")
  testing.assert_str_ne("a", "b")
  testing.assert_gt(4, 3)
  testing.assert_gte(4, 4)
  testing.assert_lt(3, 4)
  testing.assert_lte(4, 4)
}