From 3d7e3f82e216a973bc8fe0da20b178b9ea19daf9 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Thu, 12 Mar 2026 21:04:35 +0100 Subject: [PATCH] refactor: add test cases for writeJsonValue of objects and arrays --- test/tests.zig | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/tests.zig b/test/tests.zig index 8519b92..e455e0a 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -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;