authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-24 17:42:55-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-24 17:47:39-07:00
logf5f5b9373deae53a544497147cfd1380df34c000
tree072d937b2b90ca2127afbd4fe463e7b8a7a6891b
parent20cc560c2dba4834e683692dabd43162a5ce8d22

std.meta: fix unit tests depending on unstable behavior

The unit tests of std.meta depended on `@typeInfo` for the same type returning a slice of declarations and fields with the same pointer address. This is not something guaranteed by the language specification.

1 files changed, 35 insertions(+), 5 deletions(-)

lib/std/meta.zig+35-5
...@@ -592,11 +592,41 @@ pub fn FieldEnum(comptime T: type) type {...@@ -592,11 +592,41 @@ pub fn FieldEnum(comptime T: type) type {
592fn expectEqualEnum(expected: anytype, actual: @TypeOf(expected)) !void {592fn expectEqualEnum(expected: anytype, actual: @TypeOf(expected)) !void {
593 // TODO: https://github.com/ziglang/zig/issues/7419593 // TODO: https://github.com/ziglang/zig/issues/7419
594 // testing.expectEqual(@typeInfo(expected).Enum, @typeInfo(actual).Enum);594 // testing.expectEqual(@typeInfo(expected).Enum, @typeInfo(actual).Enum);
595 try testing.expectEqual(@typeInfo(expected).Enum.layout, @typeInfo(actual).Enum.layout);595 try testing.expectEqual(
596 try testing.expectEqual(@typeInfo(expected).Enum.tag_type, @typeInfo(actual).Enum.tag_type);596 @typeInfo(expected).Enum.layout,
597 comptime try testing.expectEqualSlices(std.builtin.Type.EnumField, @typeInfo(expected).Enum.fields, @typeInfo(actual).Enum.fields);597 @typeInfo(actual).Enum.layout,
598 comptime try testing.expectEqualSlices(std.builtin.Type.Declaration, @typeInfo(expected).Enum.decls, @typeInfo(actual).Enum.decls);598 );
599 try testing.expectEqual(@typeInfo(expected).Enum.is_exhaustive, @typeInfo(actual).Enum.is_exhaustive);599 try testing.expectEqual(
600 @typeInfo(expected).Enum.tag_type,
601 @typeInfo(actual).Enum.tag_type,
602 );
603 // For comparing decls and fields, we cannot use the meta eql function here
604 // because the language does not guarantee that the slice pointers for field names
605 // and decl names will be the same.
606 comptime {
607 const expected_fields = @typeInfo(expected).Enum.fields;
608 const actual_fields = @typeInfo(actual).Enum.fields;
609 if (expected_fields.len != actual_fields.len) return error.FailedTest;
610 for (expected_fields) |expected_field, i| {
611 const actual_field = actual_fields[i];
612 try testing.expectEqual(expected_field.value, actual_field.value);
613 try testing.expectEqualStrings(expected_field.name, actual_field.name);
614 }
615 }
616 comptime {
617 const expected_decls = @typeInfo(expected).Enum.decls;
618 const actual_decls = @typeInfo(actual).Enum.decls;
619 if (expected_decls.len != actual_decls.len) return error.FailedTest;
620 for (expected_decls) |expected_decl, i| {
621 const actual_decl = actual_decls[i];
622 try testing.expectEqual(expected_decl.is_pub, actual_decl.is_pub);
623 try testing.expectEqualStrings(expected_decl.name, actual_decl.name);
624 }
625 }
626 try testing.expectEqual(
627 @typeInfo(expected).Enum.is_exhaustive,
628 @typeInfo(actual).Enum.is_exhaustive,
629 );
600}630}
601631
602test "std.meta.FieldEnum" {632test "std.meta.FieldEnum" {