Skip to content

Commit 45e4b16

Browse files
travisstalochandrewrk
authored andcommitted
add @Type behavior tests from #19985
1 parent b99dbb6 commit 45e4b16

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

test/behavior/type.zig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,32 @@ test "Type.Enum" {
377377
try testing.expectEqual(@as(u32, 1), @intFromEnum(Bar.a));
378378
try testing.expectEqual(@as(u32, 5), @intFromEnum(Bar.b));
379379
try testing.expectEqual(@as(u32, 6), @intFromEnum(@as(Bar, @enumFromInt(6))));
380+
381+
{ // from https://github.com/ziglang/zig/issues/19985
382+
{ // enum with single field can be initialized.
383+
const E = @Type(.{ .@"enum" = .{
384+
.tag_type = u0,
385+
.is_exhaustive = true,
386+
.fields = &.{.{ .name = "foo", .value = 0 }},
387+
.decls = &.{},
388+
} });
389+
const s: struct { E } = .{.foo};
390+
try testing.expectEqual(.foo, s[0]);
391+
}
392+
393+
{ // meta.FieldEnum() with single field
394+
const S = struct { foo: u8 };
395+
const Fe = std.meta.FieldEnum(S);
396+
var s: S = undefined;
397+
const fe = std.meta.stringToEnum(Fe, "foo") orelse return error.InvalidField;
398+
switch (fe) {
399+
inline else => |tag| {
400+
@field(s, @tagName(tag)) = 42;
401+
},
402+
}
403+
try testing.expectEqual(42, s.foo);
404+
}
405+
}
380406
}
381407

382408
test "Type.Union" {

0 commit comments

Comments
 (0)