Fix deserializer tests

This commit is contained in:
jaina heartles 2022-12-18 05:43:36 -08:00
parent 67855b57c9
commit 4ec3a61bb6

View file

@ -407,7 +407,8 @@ test "Deserializer" {
{
const T = struct { foo: []const u8, bar: bool };
var ds = Deserializer(T){};
var ds = Deserializer(T){ .arena = std.heap.ArenaAllocator.init(std.testing.allocator) };
defer ds.deinit();
try ds.setSerializedField("foo", "123");
try ds.setSerializedField("bar", "true");
@ -420,7 +421,8 @@ test "Deserializer" {
{
const T = struct { foo: []const u8, bar: bool };
var ds = Deserializer(T){};
var ds = Deserializer(T){ .arena = std.heap.ArenaAllocator.init(std.testing.allocator) };
defer ds.deinit();
try std.testing.expectError(error.UnknownField, ds.setSerializedField("baz", "123"));
}
@ -430,7 +432,8 @@ test "Deserializer" {
foo: struct { bar: bool, baz: bool },
};
var ds = Deserializer(T){};
var ds = Deserializer(T){ .arena = std.heap.ArenaAllocator.init(std.testing.allocator) };
defer ds.deinit();
try ds.setSerializedField("foo.bar", "true");
try ds.setSerializedField("foo.baz", "true");
@ -439,29 +442,45 @@ test "Deserializer" {
try util.testing.expectDeepEqual(T{ .foo = .{ .bar = true, .baz = true } }, val);
}
// Union embedding
// Union behavior
{
const T = struct {
foo: union(enum) { bar: bool, baz: bool },
foo: union(enum) {
bar: struct {
bar: bool,
},
baz: struct {
baz: bool,
},
},
};
var ds = Deserializer(T){};
try ds.setSerializedField("bar", "true");
var ds = Deserializer(T){ .arena = std.heap.ArenaAllocator.init(std.testing.allocator) };
defer ds.deinit();
try ds.setSerializedField("foo.bar", "true");
const val = try ds.finish(std.testing.allocator);
defer ds.finishFree(std.testing.allocator, val);
try util.testing.expectDeepEqual(T{ .foo = .{ .bar = true } }, val);
try util.testing.expectDeepEqual(T{ .foo = .{ .bar = .{ .bar = true } } }, val);
}
// Returns error if multiple union fields specified
{
const T = struct {
foo: union(enum) { bar: bool, baz: bool },
foo: union(enum) {
bar: struct {
bar: bool,
},
baz: struct {
baz: bool,
},
},
};
var ds = Deserializer(T){};
try ds.setSerializedField("bar", "true");
try ds.setSerializedField("baz", "true");
var ds = Deserializer(T){ .arena = std.heap.ArenaAllocator.init(std.testing.allocator) };
defer ds.deinit();
try ds.setSerializedField("foo.bar", "true");
try ds.setSerializedField("foo.baz", "true");
try std.testing.expectError(error.DuplicateUnionMember, ds.finish(std.testing.allocator));
}
@ -470,7 +489,8 @@ test "Deserializer" {
{
const T = struct { foo: []const u8 = "123", bar: bool = true };
var ds = Deserializer(T){};
var ds = Deserializer(T){ .arena = std.heap.ArenaAllocator.init(std.testing.allocator) };
defer ds.deinit();
const val = try ds.finish(std.testing.allocator);
defer ds.finishFree(std.testing.allocator, val);
@ -481,7 +501,8 @@ test "Deserializer" {
{
const T = struct { foo: []const u8, bar: bool };
var ds = Deserializer(T){};
var ds = Deserializer(T){ .arena = std.heap.ArenaAllocator.init(std.testing.allocator) };
defer ds.deinit();
try ds.setSerializedField("foo", "123");
try std.testing.expectError(error.MissingField, ds.finish(std.testing.allocator));
@ -494,7 +515,8 @@ test "Deserializer" {
qux: ?union(enum) { quux: usize } = null,
};
var ds = Deserializer(T){};
var ds = Deserializer(T){ .arena = std.heap.ArenaAllocator.init(std.testing.allocator) };
defer ds.deinit();
const val = try ds.finish(std.testing.allocator);
defer ds.finishFree(std.testing.allocator, val);
@ -507,9 +529,10 @@ test "Deserializer" {
qux: ?union(enum) { quux: usize } = null,
};
var ds = Deserializer(T){};
var ds = Deserializer(T){ .arena = std.heap.ArenaAllocator.init(std.testing.allocator) };
defer ds.deinit();
try ds.setSerializedField("foo.baz", "3");
try ds.setSerializedField("quux", "3");
try ds.setSerializedField("qux", "3");
const val = try ds.finish(std.testing.allocator);
defer ds.finishFree(std.testing.allocator, val);
@ -522,9 +545,10 @@ test "Deserializer" {
qux: ?union(enum) { quux: usize } = null,
};
var ds = Deserializer(T){};
var ds = Deserializer(T){ .arena = std.heap.ArenaAllocator.init(std.testing.allocator) };
defer ds.deinit();
try ds.setSerializedField("foo.bar", "3");
try ds.setSerializedField("quux", "3");
try ds.setSerializedField("qux", "3");
try std.testing.expectError(error.MissingField, ds.finish(std.testing.allocator));
}