2017-02-02 14:09:27 -08:00
|
|
|
const assert = @import("std").debug.assert;
|
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "tryOnErrorUnion" {
|
2017-02-28 00:07:11 -08:00
|
|
|
tryOnErrorUnionImpl();
|
|
|
|
comptime tryOnErrorUnionImpl();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tryOnErrorUnionImpl() {
|
2017-04-21 13:46:33 -07:00
|
|
|
const x = try (returnsTen()) |val| {
|
2017-02-02 14:09:27 -08:00
|
|
|
val + 1
|
|
|
|
} else |err| switch (err) {
|
|
|
|
error.ItBroke, error.NoMem => 1,
|
|
|
|
error.CrappedOut => i32(2),
|
|
|
|
};
|
|
|
|
assert(x == 11);
|
|
|
|
}
|
|
|
|
|
|
|
|
error ItBroke;
|
|
|
|
error NoMem;
|
|
|
|
error CrappedOut;
|
|
|
|
fn returnsTen() -> %i32 {
|
|
|
|
10
|
|
|
|
}
|
2017-02-05 15:58:58 -08:00
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "tryWithoutVars" {
|
2017-02-05 15:58:58 -08:00
|
|
|
const result1 = try (failIfTrue(true)) {
|
|
|
|
1
|
|
|
|
} else {
|
|
|
|
i32(2)
|
|
|
|
};
|
|
|
|
assert(result1 == 2);
|
|
|
|
|
|
|
|
const result2 = try (failIfTrue(false)) {
|
|
|
|
1
|
|
|
|
} else {
|
|
|
|
i32(2)
|
|
|
|
};
|
|
|
|
assert(result2 == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn failIfTrue(ok: bool) -> %void {
|
|
|
|
if (ok) {
|
|
|
|
return error.ItBroke;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-02-28 00:07:11 -08:00
|
|
|
|
2017-05-01 19:37:34 -07:00
|
|
|
test "try then not executed with assignment" {
|
|
|
|
try (failIfTrue(true)) {
|
|
|
|
unreachable;
|
|
|
|
} else |err| {
|
|
|
|
assert(err == error.ItBroke);
|
|
|
|
}
|
|
|
|
}
|