Flesh out test cases for unions
This commit is contained in:
parent
ce40448dc8
commit
abf31ea33c
2 changed files with 25 additions and 18 deletions
|
@ -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 = 3 } }, "sub.foo=3&sub.bar=3");
|
||||||
try testCase(SubStruct, .{ .sub = .{ .foo = 3, .bar = 2 } }, "sub.foo=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 {
|
// const SubStruct2 = struct {
|
||||||
// sub: ?struct {
|
// sub: ?struct {
|
||||||
// foo: usize = 1,
|
// foo: usize = 1,
|
||||||
// } = null,
|
// } = null,
|
||||||
// };
|
// };
|
||||||
// try testCase(SubStruct2, .{ .sub = null }, "");
|
// try testCase(SubStruct2, .{ .sub = null }, "");
|
||||||
|
// try testCase(SubStruct2, .{ .sub = null }, "sub=");
|
||||||
|
|
||||||
const TestQuery = struct {
|
// TODO: also here (semantics are well defined it just breaks tests)
|
||||||
int: usize = 3,
|
// const SubUnion = struct {
|
||||||
boolean: bool = false,
|
// sub: ?union(enum) {
|
||||||
str_enum: ?enum { foo, bar } = null,
|
// 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 testCase(SubUnion2, .{ .sub = null }, "");
|
||||||
try std.testing.expectEqual(TestQuery{
|
try testCase(SubUnion2, .{ .sub = .{ .foo = 1, .val = .{ .bar = "abc" } } }, "sub.foo=1&sub.bar=abc");
|
||||||
.int = 3,
|
try testCase(SubUnion2, .{ .sub = .{ .foo = 1, .val = .{ .baz = "abc" } } }, "sub.foo=1&sub.baz=abc");
|
||||||
.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"));
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -216,7 +216,7 @@ pub const testing = struct {
|
||||||
.Union => {
|
.Union => {
|
||||||
inline for (comptime std.meta.fieldNames(T)) |f| {
|
inline for (comptime std.meta.fieldNames(T)) |f| {
|
||||||
if (std.meta.isTag(expected, 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));
|
try expectDeepEqual(@field(expected, f), @field(actual, f));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue