aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/try.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-04-29 15:54:04 -0700
committerAndrew Kelley <andrew@ziglang.org>2021-04-29 15:54:04 -0700
commit4307436b9945f814ff5731981df1d19febf3ba0a (patch)
treeefaaec94a41d632d45f255d464d9787eb02c4c9b /test/behavior/try.zig
parent5a02c938dafdf2bb11b2350b6ad3161ef93744f0 (diff)
downloadzig-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.zig43
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);
+ }
+}