diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-04-29 15:54:04 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-04-29 15:54:04 -0700 |
| commit | 4307436b9945f814ff5731981df1d19febf3ba0a (patch) | |
| tree | efaaec94a41d632d45f255d464d9787eb02c4c9b /test/behavior/try.zig | |
| parent | 5a02c938dafdf2bb11b2350b6ad3161ef93744f0 (diff) | |
| download | zig-4307436b9945f814ff5731981df1d19febf3ba0a.tar.gz zig-4307436b9945f814ff5731981df1d19febf3ba0a.zip | |
move behavior tests from test/stage1/ to test/
And fix test cases to make them pass. This is in preparation for
starting to pass behavior tests with self-hosted.
Diffstat (limited to 'test/behavior/try.zig')
| -rw-r--r-- | test/behavior/try.zig | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/test/behavior/try.zig b/test/behavior/try.zig new file mode 100644 index 0000000000..9e93183c3b --- /dev/null +++ b/test/behavior/try.zig @@ -0,0 +1,43 @@ +const expect = @import("std").testing.expect; + +test "try on error union" { + tryOnErrorUnionImpl(); + comptime tryOnErrorUnionImpl(); +} + +fn tryOnErrorUnionImpl() void { + const x = if (returnsTen()) |val| val + 1 else |err| switch (err) { + error.ItBroke, error.NoMem => 1, + error.CrappedOut => @as(i32, 2), + else => unreachable, + }; + expect(x == 11); +} + +fn returnsTen() anyerror!i32 { + return 10; +} + +test "try without vars" { + const result1 = if (failIfTrue(true)) 1 else |_| @as(i32, 2); + expect(result1 == 2); + + const result2 = if (failIfTrue(false)) 1 else |_| @as(i32, 2); + expect(result2 == 1); +} + +fn failIfTrue(ok: bool) anyerror!void { + if (ok) { + return error.ItBroke; + } else { + return; + } +} + +test "try then not executed with assignment" { + if (failIfTrue(true)) { + unreachable; + } else |err| { + expect(err == error.ItBroke); + } +} |
