feat: add slightly more robust fmtBuf API

This commit is contained in:
CJ van den Berg 2026-03-12 21:42:13 +01:00
parent 0b1c94eae2
commit 46e7709232
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 20 additions and 0 deletions

View file

@ -9,6 +9,7 @@ const expectEqualStrings = std.testing.expectEqualStrings;
const expectError = std.testing.expectError;
const fmt = cbor_mod.fmt;
const fmtBuf = cbor_mod.fmtBuf;
const toJson = cbor_mod.toJson;
const toJsonPretty = cbor_mod.toJsonPretty;
const fromJson = cbor_mod.fromJson;
@ -44,6 +45,19 @@ test "cbor simple" {
);
}
test "cbor.fmtBuf success" {
var buf: [128]u8 = undefined;
try expectEqualDeep(
try fmtBuf(&buf, .{ "five", 5, "four", 4, .{ "three", 3 } }),
&[_]u8{ 0x85, 0x64, 0x66, 0x69, 0x76, 0x65, 0x05, 0x64, 0x66, 0x6f, 0x75, 0x72, 0x04, 0x82, 0x65, 0x74, 0x68, 0x72, 0x65, 0x65, 0x03 },
);
}
test "cbor.fmtBuf overflow" {
var buf: [4]u8 = undefined;
try expectError(error.NoSpaceLeft, fmtBuf(&buf, .{ "five", 5, "four", 4, .{ "three", 3 } }));
}
test "cbor exit message" {
var buf: [128]u8 = undefined;
try expectEqualDeep(