add @Type behavior tests from #19985

This commit is contained in:
Travis Staloch 2024-12-24 19:44:43 -08:00 committed by Andrew Kelley
parent b99dbb6fb5
commit 45e4b16caf

View File

@ -377,6 +377,32 @@ test "Type.Enum" {
try testing.expectEqual(@as(u32, 1), @intFromEnum(Bar.a));
try testing.expectEqual(@as(u32, 5), @intFromEnum(Bar.b));
try testing.expectEqual(@as(u32, 6), @intFromEnum(@as(Bar, @enumFromInt(6))));
{ // from https://github.com/ziglang/zig/issues/19985
{ // enum with single field can be initialized.
const E = @Type(.{ .@"enum" = .{
.tag_type = u0,
.is_exhaustive = true,
.fields = &.{.{ .name = "foo", .value = 0 }},
.decls = &.{},
} });
const s: struct { E } = .{.foo};
try testing.expectEqual(.foo, s[0]);
}
{ // meta.FieldEnum() with single field
const S = struct { foo: u8 };
const Fe = std.meta.FieldEnum(S);
var s: S = undefined;
const fe = std.meta.stringToEnum(Fe, "foo") orelse return error.InvalidField;
switch (fe) {
inline else => |tag| {
@field(s, @tagName(tag)) = 42;
},
}
try testing.expectEqual(42, s.foo);
}
}
}
test "Type.Union" {