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