aego/testing
Test functions are package functions named TestXxx in *_test.aes files, and aego test runs them headless. The documentation sandbox does not run aego test, so the example calls its test function from Ready; in a project, move TestDamage into a _test.aes file and drop the script.
Example
package demo
import (
"aego/log"
"aego/math"
"aego/testing"
)
func damage(hp int, hit int) int {
return math.MaxInt(hp - hit, 0)
}
func label(hp int) string {
if hp == 0 {
return "dead"
}
return "alive"
}
func TestDamage() {
testing.Equal(damage(10, 3), 7)
testing.Equal(damage(2, 5), 0)
testing.EqualString(label(damage(2, 5)), "dead")
testing.EqualFloat(math.Sqrt(2), 1.4142, 0.001)
testing.True(damage(10, 0) == 10, "a zero hit changes nothing")
testing.False(damage(1, 1) > 0, "the last hit kills")
}
script Runner {
func Ready() {
TestDamage()
log.Info("TestDamage passed")
}
}
Functions
| Equal | (got int, want int) | Fails the test when two ints differ. |
| EqualFloat | (got float, want float, eps float) | Fails the test when got is further than eps from want, or is NaN. |
| EqualString | (got string, want string) | Fails the test when two strings differ. |
| Fail | (msg string) | Stops the test and reports msg. |
| False | (v bool, msg string) | Fails the test with msg when v is true. |
| True | (v bool, msg string) | Fails the test with msg when v is false. |
Equal function
func Equal(got int, want int)
Fails the test when two ints differ.
Example
testing.Equal(score(), 100)
EqualFloat function
func EqualFloat(got float, want float, eps float)
Fails the test when got is further than eps from want, or is NaN.
Example
testing.EqualFloat(math.Sqrt(2), 1.4142, 0.001)
EqualString function
func EqualString(got string, want string)
Fails the test when two strings differ.
Example
testing.EqualString(name(), "Player")
Fail function
func Fail(msg string)
Stops the test and reports msg.
Example
testing.Fail("unreachable")
False function
func False(v bool, msg string)
Fails the test with msg when v is true.
Example
testing.False(dead, "player should be alive")
True function
func True(v bool, msg string)
Fails the test with msg when v is false.
Example
testing.True(hp > 0, "player should be alive")