2017-01-05 16:57:48 +08:00
|
|
|
const assert = @import("std").debug.assert;
|
2017-02-08 06:19:51 +08:00
|
|
|
const mem = @import("std").mem;
|
2017-01-05 16:57:48 +08:00
|
|
|
|
2016-12-19 13:41:37 +08:00
|
|
|
pub fn foo() -> %i32 {
|
|
|
|
const x = %return bar();
|
2017-12-22 13:50:30 +08:00
|
|
|
return x + 1;
|
2016-12-19 13:41:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bar() -> %i32 {
|
|
|
|
return 13;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn baz() -> %i32 {
|
|
|
|
const y = foo() %% 1234;
|
|
|
|
return y + 1;
|
|
|
|
}
|
|
|
|
|
2017-05-24 09:38:31 +08:00
|
|
|
test "error wrapping" {
|
2016-12-19 13:41:37 +08:00
|
|
|
assert(%%baz() == 15);
|
|
|
|
}
|
|
|
|
|
2016-12-22 13:12:27 +08:00
|
|
|
error ItBroke;
|
|
|
|
fn gimmeItBroke() -> []const u8 {
|
2017-12-22 13:50:30 +08:00
|
|
|
return @errorName(error.ItBroke);
|
2016-12-22 13:12:27 +08:00
|
|
|
}
|
|
|
|
|
2017-05-24 09:38:31 +08:00
|
|
|
test "@errorName" {
|
2017-02-13 06:22:35 +08:00
|
|
|
assert(mem.eql(u8, @errorName(error.AnError), "AnError"));
|
|
|
|
assert(mem.eql(u8, @errorName(error.ALongerErrorName), "ALongerErrorName"));
|
2016-12-22 13:12:27 +08:00
|
|
|
}
|
2016-12-22 14:42:30 +08:00
|
|
|
error AnError;
|
|
|
|
error ALongerErrorName;
|
2016-12-22 13:12:27 +08:00
|
|
|
|
|
|
|
|
2017-05-24 09:38:31 +08:00
|
|
|
test "error values" {
|
2016-12-22 14:20:08 +08:00
|
|
|
const a = i32(error.err1);
|
|
|
|
const b = i32(error.err2);
|
|
|
|
assert(a != b);
|
|
|
|
}
|
|
|
|
error err1;
|
|
|
|
error err2;
|
|
|
|
|
|
|
|
|
2017-05-24 09:38:31 +08:00
|
|
|
test "redefinition of error values allowed" {
|
2016-12-22 14:20:08 +08:00
|
|
|
shouldBeNotEqual(error.AnError, error.SecondError);
|
|
|
|
}
|
|
|
|
error AnError;
|
|
|
|
error AnError;
|
|
|
|
error SecondError;
|
|
|
|
fn shouldBeNotEqual(a: error, b: error) {
|
2017-12-22 13:50:30 +08:00
|
|
|
if (a == b) unreachable;
|
2016-12-22 14:20:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-24 09:38:31 +08:00
|
|
|
test "error binary operator" {
|
2016-12-22 14:42:30 +08:00
|
|
|
const a = errBinaryOperatorG(true) %% 3;
|
|
|
|
const b = errBinaryOperatorG(false) %% 3;
|
|
|
|
assert(a == 3);
|
|
|
|
assert(b == 10);
|
|
|
|
}
|
|
|
|
error ItBroke;
|
|
|
|
fn errBinaryOperatorG(x: bool) -> %isize {
|
2017-12-22 13:50:30 +08:00
|
|
|
return if (x) error.ItBroke else isize(10);
|
2016-12-22 14:42:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-24 09:38:31 +08:00
|
|
|
test "unwrap simple value from error" {
|
2016-12-22 14:42:30 +08:00
|
|
|
const i = %%unwrapSimpleValueFromErrorDo();
|
|
|
|
assert(i == 13);
|
|
|
|
}
|
2017-12-22 13:50:30 +08:00
|
|
|
fn unwrapSimpleValueFromErrorDo() -> %isize { return 13; }
|
2016-12-22 14:42:30 +08:00
|
|
|
|
2016-12-22 14:20:08 +08:00
|
|
|
|
2017-05-24 09:38:31 +08:00
|
|
|
test "error return in assignment" {
|
2016-12-22 21:48:08 +08:00
|
|
|
%%doErrReturnInAssignment();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn doErrReturnInAssignment() -> %void {
|
|
|
|
var x : i32 = undefined;
|
|
|
|
x = %return makeANonErr();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn makeANonErr() -> %i32 {
|
|
|
|
return 1;
|
|
|
|
}
|