Skip to content

Commit

Permalink
test: add behavior tests for catch |e| switch (e)
Browse files Browse the repository at this point in the history
  • Loading branch information
dweiller committed Dec 1, 2023
1 parent 5578eaf commit 0f08626
Showing 1 changed file with 165 additions and 0 deletions.
165 changes: 165 additions & 0 deletions test/behavior/switch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 0f08626

Please sign in to comment.