Flash 63 lines
// Flash-source test suite — the `test "name" { }` block form, exercised
// end to end: flashc transpiles this file and the emitted Zig's test blocks
// run under `zig build test-flash`. A failing expectation fails the build,
// exactly as a host unit test does.
use std
const Answer = 42
fn add(a i32, b i32) i32 {
return a + b
}
fn double(xs []mut i32) void {
for x, i in xs {
xs[i] = x * 2
}
}
// `defer { … }` mutating through an out-pointer: the block runs on scope
// exit, after the body's `*= 10`, so the caller observes both effects.
fn bump(p *mut i32) void {
defer {
p.* += 1
}
p.* *= 10
}
fn mustBePositive(n i32) !i32 {
if n <= 0 {
return error.NotPositive
}
return n
}
test "add sums two integers" {
try std.testing.expectEqual(5, add(2, 3))
}
test "a short declaration and a file-scope const are visible in a test body" {
sum := add(Answer, 0)
try std.testing.expectEqual(42, sum)
}
test "an index-captured for loop mutates through a mut slice" {
var buf [3]i32 = .{ 1, 2, 3 }
double(buf[0..])
try std.testing.expectEqual(6, buf[2])
}
test "a defer block runs on scope exit" {
var n i32 = 1
bump(&n)
try std.testing.expectEqual(11, n)
}
test "an error return propagates to the expectation" {
r := mustBePositive(-1)
try std.testing.expectError(error.NotPositive, r)
ok := try mustBePositive(7)
try std.testing.expectEqual(7, ok)
}