Flesh out test cases for unions

This commit is contained in:
jaina heartles 2022-11-27 01:07:45 -08:00
parent ce40448dc8
commit abf31ea33c
2 changed files with 25 additions and 18 deletions

View File

@ -422,29 +422,36 @@ test "parseQuery" {
try testCase(SubStruct, .{ .sub = .{ .foo = 3, .bar = 3 } }, "sub.foo=3&sub.bar=3");
try testCase(SubStruct, .{ .sub = .{ .foo = 3, .bar = 2 } }, "sub.foo=3");
// TODO: Semantics are ill-defined here
// TODO: Semantics are ill-defined here. What happens if the substruct doesn't have
// default values?
// const SubStruct2 = struct {
// sub: ?struct {
// foo: usize = 1,
// } = null,
// };
// try testCase(SubStruct2, .{ .sub = null }, "");
// try testCase(SubStruct2, .{ .sub = null }, "sub=");
const TestQuery = struct {
int: usize = 3,
boolean: bool = false,
str_enum: ?enum { foo, bar } = null,
// TODO: also here (semantics are well defined it just breaks tests)
// const SubUnion = struct {
// sub: ?union(enum) {
// foo: usize,
// bar: usize,
// } = null,
// };
// try testCase(SubUnion, .{ .sub = null }, "");
// try testCase(SubUnion, .{ .sub = null }, "sub=");
const SubUnion2 = struct {
sub: ?struct {
foo: usize,
val: union(enum) {
bar: []const u8,
baz: []const u8,
},
} = null,
};
try std.testing.expectEqual(TestQuery{
.int = 3,
.boolean = false,
.str_enum = null,
}, try parseQuery(std.testing.allocator, TestQuery, ""));
try std.testing.expectEqual(TestQuery{
.int = 5,
.boolean = true,
.str_enum = .foo,
}, try parseQuery(std.testing.allocator, TestQuery, "?int=5&boolean=yes&str_enum=foo"));
try testCase(SubUnion2, .{ .sub = null }, "");
try testCase(SubUnion2, .{ .sub = .{ .foo = 1, .val = .{ .bar = "abc" } } }, "sub.foo=1&sub.bar=abc");
try testCase(SubUnion2, .{ .sub = .{ .foo = 1, .val = .{ .baz = "abc" } } }, "sub.foo=1&sub.baz=abc");
}

View File

@ -216,7 +216,7 @@ pub const testing = struct {
.Union => {
inline for (comptime std.meta.fieldNames(T)) |f| {
if (std.meta.isTag(expected, f)) {
try std.testing.expect(std.std.meta.isTag(actual, f));
try std.testing.expect(std.meta.isTag(actual, f));
try expectDeepEqual(@field(expected, f), @field(actual, f));
}
}