Examples · learn/14-tests · 200 of 339
assert.test
tin run examples/learn/14-tests/assert.test.tinfn discount(total: int, percent: int): Result<int, string> {
if percent < 0 || percent > 100 { return Err("percent must be between 0 and 100") }
Ok(total - total * percent / 100)
}
test "a quarter off" {
assertEq(discount(1000, 25), Ok(750)) // assertEq works on any type, even Result
}
test "silly percentages are refused" {
assert(discount(1000, 150).isErr(), "150% should be refused")
}
test "nothing off" {
let r = discount(1000, 0)
assert(r == Ok(1000)) // without a message, the condition is the message
}