From abf31ea33c4bdf69d0134c3e12a919d38a5a67ff Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Sun, 27 Nov 2022 01:07:45 -0800 Subject: [PATCH] Flesh out test cases for unions --- src/http/query.zig | 41 ++++++++++++++++++++++++----------------- src/util/lib.zig | 2 +- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/http/query.zig b/src/http/query.zig index 1396696..7bbdcf1 100644 --- a/src/http/query.zig +++ b/src/http/query.zig @@ -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"); } diff --git a/src/util/lib.zig b/src/util/lib.zig index 84b2122..9829ae3 100644 --- a/src/util/lib.zig +++ b/src/util/lib.zig @@ -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)); } }