testing
Writing tests for your code: equality assertions, a pass and fail count, and a summary at the end.
import testing
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)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()
endAssertMsg
func AssertMsg(cond: bool, msg: str)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 example for this item is shown with the ones above: testing-Assert.salam
AssertFalse
func AssertFalse(cond: bool)The example for this item is shown with the ones above: testing-Assert.salam
AssertEqInt
func AssertEqInt(got: int, want: int)The example for this item is shown with the ones above: testing-Assert.salam
AssertEqInt64
func AssertEqInt64(got: i64, want: i64)The example for this item is shown with the ones above: testing-Assert.salam
AssertEqFloat
func AssertEqFloat(got: f64, want: f64, eps: f64)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 example for this item is shown with the ones above: testing-Assert.salam
AssertEqStr
func AssertEqStr(got: str, want: str)The example for this item is shown with the ones above: testing-Assert.salam
AssertEqBool
func AssertEqBool(got: bool, want: bool)The example for this item is shown with the ones above: testing-Assert.salam
AssertNeInt
func AssertNeInt(a: int, b: int)The example for this item is shown with the ones above: testing-Assert.salam
AssertNeStr
func AssertNeStr(a: str, b: str)The example for this item is shown with the ones above: testing-Assert.salam
AssertNil
func AssertNil(p: void*)The example for this item is shown with the ones above: testing-Assert.salam
AssertNotNil
func AssertNotNil(p: void*)The example for this item is shown with the ones above: testing-Assert.salam
Reset
func Reset()The example for this item is shown with the ones above: testing-Assert.salam
Passed
func Passed(): intThe example for this item is shown with the ones above: testing-Assert.salam
Failed
func Failed(): intThe example for this item is shown with the ones above: testing-Assert.salam
Summary
func Summary(): intThe example for this item is shown with the ones above: testing-Assert.salam