2017-02-02 22:09:27 +00:00
|
|
|
const assert = @import("std").debug.assert;
|
|
|
|
|
2017-03-16 20:02:35 +00:00
|
|
|
test "tryOnErrorUnion" {
|
2017-02-28 08:07:11 +00:00
|
|
|
tryOnErrorUnionImpl();
|
|
|
|
comptime tryOnErrorUnionImpl();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tryOnErrorUnionImpl() {
|
2017-04-21 21:46:33 +01:00
|
|
|
const x = try (returnsTen()) |val| {
|
2017-02-02 22:09:27 +00: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 23:58:58 +00:00
|
|
|
|
2017-03-16 20:02:35 +00:00
|
|
|
test "tryWithoutVars" {
|
2017-02-05 23:58:58 +00: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 08:07:11 +00:00
|
|
|
|
|
|
|
// TODO
|
|
|
|
//fn tryThenNotExecutedWithAssignment() {
|
|
|
|
// @setFnTest(this);
|
|
|
|
//
|
2017-04-21 21:46:33 +01:00
|
|
|
// try (failIfTrue(true)) {
|
2017-03-26 09:58:48 +01:00
|
|
|
// unreachable;
|
2017-02-28 08:07:11 +00:00
|
|
|
// } else |err| {
|
|
|
|
// assert(err == error.ItBroke);
|
|
|
|
// }
|
|
|
|
//}
|