From 0f086267d231246dc7ce91bf7f6587902bccc253 Mon Sep 17 00:00:00 2001 From: dweiller <4678790+dweiller@users.noreply.github.com> Date: Thu, 30 Nov 2023 19:12:47 +1100 Subject: [PATCH] test: add behavior tests for catch |e| switch (e) --- test/behavior/switch.zig | 165 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) diff --git a/test/behavior/switch.zig b/test/behavior/switch.zig index ae4923609fb4..36fde40f7546 100644 --- a/test/behavior/switch.zig +++ b/test/behavior/switch.zig @@ -831,3 +831,168 @@ test "peer type resolution on switch captures ignores unused payload bits" { try expect(x == 123); } + +test "switch on error union catch" { + const S = struct { + fn doTheTest() !void { + const Error = error{ A, B, C }; + { + var a: Error!u64 = 3; + _ = &a; + const b: u64 = a catch |err| switch (err) { + error.A => 0, + error.B => @intFromError(err), + error.C => @intFromError(err), + }; + try std.testing.expectEqual(@as(u64, 3), b); + } + { + var a: Error!u64 = 3; + _ = &a; + const b: u64 = a catch |err| switch (err) { + error.A, error.B => 0, + error.C => @intFromError(err), + }; + try std.testing.expectEqual(@as(u64, 3), b); + } + { + var a: Error!u64 = 3; + _ = &a; + const b: u64 = a catch |err| switch (err) { + error.A => 0, + error.C, error.B => @intFromError(err), + }; + try std.testing.expectEqual(@as(u64, 3), b); + } + { + var a: Error!u64 = error.A; + _ = &a; + const b: u64 = a catch |err| switch (err) { + error.A => 0, + error.B => @intFromError(err), + error.C => @intFromError(err), + }; + try std.testing.expectEqual(@as(u64, 0), b); + } + { + var a: Error!u64 = error.A; + _ = &a; + const b: u64 = a catch |err| switch (err) { + error.A, error.B => 0, + error.C => @intFromError(err), + }; + try std.testing.expectEqual(@as(u64, 0), b); + } + { + var a: Error!u64 = error.A; + _ = &a; + const b: u64 = a catch |err| switch (err) { + error.A => 0, + error.C, error.B => @intFromError(err), + }; + try std.testing.expectEqual(@as(u64, 0), b); + } + { + var a: Error!u64 = 3; + _ = &a; + const b: u64 = a catch |err| switch (err) { + error.A => 0, + else => @intFromError(err), + }; + try std.testing.expectEqual(@as(u64, 3), b); + } + { + var a: Error!u64 = error.A; + _ = &a; + const b: u64 = a catch |err| switch (err) { + error.A => 1, + else => @intFromError(err), + }; + try std.testing.expectEqual(@as(u64, 1), b); + } + { + var a: Error!u64 = error.A; + _ = &a; + const b: u64 = a catch |err| switch (err) { + error.A => |e| @intFromError(e), + else => @intFromError(err), + }; + try std.testing.expectEqual(@as(u64, @intFromError(error.A)), b); + } + { + var a: Error!u64 = error.B; + _ = &a; + const b: u64 = a catch |err| switch (err) { + error.A => 0, + else => @intFromError(err), + }; + try std.testing.expectEqual(@as(u64, @intFromError(error.B)), b); + } + { + var a: Error!u64 = error.B; + _ = &a; + const b: u64 = a catch |err| switch (err) { + error.A => 0, + inline else => @intFromError(err), + }; + try std.testing.expectEqual(@as(u64, @intFromError(error.B)), b); + } + { + var a: Error!u64 = error.B; + _ = &a; + const b: u64 = a catch |err| switch (err) { + error.A => |e| @intFromError(e), + inline else => @intFromError(err), + }; + try std.testing.expectEqual(@as(u64, @intFromError(error.B)), b); + } + { + var a: Error!u64 = error.B; + _ = &a; + const b: u64 = a catch |err| switch (err) { + error.A => 0, + error.B, error.C => @intFromError(err), + }; + try std.testing.expectEqual(@as(u64, @intFromError(error.B)), b); + } + { + var a: Error!u64 = error.B; + _ = &a; + const b: u64 = a catch |err| switch (err) { + error.A => 0, + error.B, error.C => |e| @intFromError(e), + }; + try std.testing.expectEqual(@as(u64, @intFromError(error.B)), b); + } + { + var a: Error!u64 = error.B; + _ = &a; + const b: u64 = a catch |err| switch (err) { + error.A => 0, + inline error.B, error.C => |e| @intFromError(e), + }; + try std.testing.expectEqual(@as(u64, @intFromError(error.B)), b); + } + { + var a: error{}!u64 = 0; + _ = &a; + const b: u64 = a catch |err| switch (err) { + else => |e| return e, + }; + try std.testing.expectEqual(@as(u64, 0), b); + } + { + var a: error{}!u64 = 0; + _ = &a; + const b: u64 = a catch |err| switch (err) { + error.UnknownError => return error.Fail, + else => |e| return e, + }; + try std.testing.expectEqual(@as(u64, 0), b); + } + } + }; + + try comptime S.doTheTest(); + try S.doTheTest(); +}