Standard library · System and tools

testing

Writing tests for your code: equality assertions, a pass and fail count, and a summary at the end.

import testing
On this page
  1. Functions

A test is a small program that checks your code does what it should. The testing package has assertions that print a clear message when they fail but do not stop the program, so every test runs and Summary says at the end how many passed and how many failed.

The value Summary returns is the number of failures. Return it from main so that the test program ends with a non-zero code in CI.

Functions

Assert

func Assert(cond: bool)
Records a failure when the condition is false.
testing-Assert.salam
import testing

func add(a: int, b: int): int:
    ret a + b
end

func main:
    testing.Reset()
    testing.Assert(add(2, 2) == 4)
    testing.AssertMsg(add(1, 1) == 2, "one and one")
    testing.AssertTrue(true)
    testing.AssertFalse(add(0, 0) == 1)
    testing.AssertEqInt(add(3, 4), 7)
    testing.AssertEqInt64(10000000000 as i64, 10000000000 as i64)
    testing.AssertEqFloat(0.1 + 0.2, 0.3, 0.0001)
    testing.AssertContains("the Salam language", "Salam")
    testing.AssertEqStr("hello", "hello")
    testing.AssertEqBool(1 < 2, true)
    testing.AssertNeInt(1, 2)
    testing.AssertNeStr("a", "b")
    testing.AssertNil(null)
    testing.AssertEqInt(add(2, 2), 5)
    println testing.Passed(), testing.Failed()
    println testing.Summary()
end
OutputFAIL: ints differ: got 4 want 5 13 1 -- tests: 13 passed, 1 failed 1

AssertMsg

func AssertMsg(cond: bool, msg: str)
Like Assert, with a message of your own.

The example for this item is shown with the ones above: testing-Assert.salam

AssertTrue

func AssertTrue(cond: bool)
The condition has to be true.

The example for this item is shown with the ones above: testing-Assert.salam

AssertFalse

func AssertFalse(cond: bool)
The condition has to be false.

The example for this item is shown with the ones above: testing-Assert.salam

AssertEqInt

func AssertEqInt(got: int, want: int)
Two whole numbers have to be equal; on failure both values are printed.

The example for this item is shown with the ones above: testing-Assert.salam

AssertEqInt64

func AssertEqInt64(got: i64, want: i64)
Two 64-bit whole numbers have to be equal.

The example for this item is shown with the ones above: testing-Assert.salam

AssertEqFloat

func AssertEqFloat(got: f64, want: f64, eps: f64)
Two decimal numbers have to differ by less than eps. Never compare decimals with ==.

The example for this item is shown with the ones above: testing-Assert.salam

AssertContains

func AssertContains(hay: str, needle: str)
The first string has to contain the second.

The example for this item is shown with the ones above: testing-Assert.salam

AssertEqStr

func AssertEqStr(got: str, want: str)
Two strings have to be equal.

The example for this item is shown with the ones above: testing-Assert.salam

AssertEqBool

func AssertEqBool(got: bool, want: bool)
Two booleans have to be equal.

The example for this item is shown with the ones above: testing-Assert.salam

AssertNeInt

func AssertNeInt(a: int, b: int)
Two whole numbers have to differ.

The example for this item is shown with the ones above: testing-Assert.salam

AssertNeStr

func AssertNeStr(a: str, b: str)
Two strings have to differ.

The example for this item is shown with the ones above: testing-Assert.salam

AssertNil

func AssertNil(p: void*)
The pointer has to be null.

The example for this item is shown with the ones above: testing-Assert.salam

AssertNotNil

func AssertNotNil(p: void*)
The pointer must not be null.

The example for this item is shown with the ones above: testing-Assert.salam

Reset

func Reset()
Sets the counters back to zero.

The example for this item is shown with the ones above: testing-Assert.salam

Passed

func Passed(): int
The number of assertions that passed.

The example for this item is shown with the ones above: testing-Assert.salam

Failed

func Failed(): int
The number of assertions that failed.

The example for this item is shown with the ones above: testing-Assert.salam

Summary

func Summary(): int
Prints the summary and returns the number of failures.

The example for this item is shown with the ones above: testing-Assert.salam