refactor: add test cases for writeJsonValue of objects and arrays

This commit is contained in:
CJ van den Berg 2026-03-12 21:04:35 +01:00
parent 9461fea5a8
commit 3d7e3f82e2
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -24,6 +24,7 @@ const isNull = cbor_mod.isNull;
const writeArrayHeader = cbor_mod.writeArrayHeader;
const writeMapHeader = cbor_mod.writeMapHeader;
const writeValue = cbor_mod.writeValue;
const writeJsonValue = cbor_mod.writeJsonValue;
const extract = cbor_mod.extract;
const extractAlloc = cbor_mod.extractAlloc;
const extract_cbor = cbor_mod.extract_cbor;
@ -687,6 +688,33 @@ test "cbor.writeValue nested union json" {
);
}
test "cbor.writeJsonValue array" {
var buf: [128]u8 = undefined;
var writer: Io.Writer = .fixed(&buf);
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
var arr = std.json.Array.init(arena.allocator());
try arr.append(.{ .integer = 1 });
try arr.append(.{ .string = "hello" });
try arr.append(.{ .bool = true });
try writeJsonValue(&writer, .{ .array = arr });
var json_buf: [128]u8 = undefined;
try expectEqualStrings("[1,\"hello\",true]", try toJson(writer.buffered(), &json_buf));
}
test "cbor.writeJsonValue object" {
var buf: [128]u8 = undefined;
var writer: Io.Writer = .fixed(&buf);
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
var obj = std.json.ObjectMap.init(arena.allocator());
try obj.put("x", .{ .integer = 42 });
try obj.put("y", .{ .string = "hello" });
try writeJsonValue(&writer, .{ .object = obj });
var json_buf: [128]u8 = undefined;
try expectEqualStrings("{\"x\":42,\"y\":\"hello\"}", try toJson(writer.buffered(), &json_buf));
}
test "cbor.extract tagged union" {
const TestUnion = union(enum) { a: f32, b: []const u8 };
var buf: [128]u8 = undefined;