|
1 | 1 | const std = @import("std");
|
2 | 2 | const msgpack = @import("msgpack.zig");
|
3 | 3 |
|
4 |
| -test "readBool: null" { |
5 |
| - const buffer = [_]u8{0xc0}; |
6 |
| - var stream = std.io.fixedBufferStream(&buffer); |
7 |
| - var unpacker = msgpack.unpackerNoAlloc(stream.reader()); |
8 |
| - try std.testing.expectEqual(null, try unpacker.read(?bool)); |
9 |
| -} |
| 4 | +const packed_null = [_]u8{0xc0}; |
| 5 | +const packed_true = [_]u8{0xc3}; |
| 6 | +const packed_false = [_]u8{0xc2}; |
| 7 | +const packed_zero = [_]u8{0x00}; |
10 | 8 |
|
11 | 9 | test "readBool: false" {
|
12 |
| - const buffer = [_]u8{0xc2}; |
13 |
| - var stream = std.io.fixedBufferStream(&buffer); |
14 |
| - var unpacker = msgpack.unpackerNoAlloc(stream.reader()); |
15 |
| - try std.testing.expectEqual(false, try unpacker.read(bool)); |
| 10 | + try std.testing.expectEqual(false, try msgpack.unpackFromBytes(bool, &packed_false, .{})); |
16 | 11 | }
|
17 | 12 |
|
18 | 13 | test "readBool: true" {
|
19 |
| - const buffer = [_]u8{0xc3}; |
20 |
| - var stream = std.io.fixedBufferStream(&buffer); |
21 |
| - var unpacker = msgpack.unpackerNoAlloc(stream.reader()); |
22 |
| - try std.testing.expectEqual(true, try unpacker.read(bool)); |
| 14 | + try std.testing.expectEqual(true, try msgpack.unpackFromBytes(bool, &packed_true, .{})); |
23 | 15 | }
|
24 | 16 |
|
25 |
| -test "readBool: wrong data" { |
26 |
| - const buffer = [_]u8{0x00}; |
27 |
| - var stream = std.io.fixedBufferStream(&buffer); |
28 |
| - var unpacker = msgpack.unpackerNoAlloc(stream.reader()); |
29 |
| - try std.testing.expectError(error.InvalidFormat, unpacker.read(bool)); |
| 17 | +test "readBool: null" { |
| 18 | + try std.testing.expectEqual(null, try msgpack.unpackFromBytes(?bool, &packed_null, .{})); |
| 19 | +} |
| 20 | + |
| 21 | +test "readBool: wrong type" { |
| 22 | + try std.testing.expectError(error.InvalidFormat, msgpack.unpackFromBytes(bool, &packed_zero, .{})); |
30 | 23 | }
|
31 | 24 |
|
32 | 25 | test "writeBool: false" {
|
33 |
| - var buffer: [100]u8 = undefined; |
34 |
| - var stream = std.io.fixedBufferStream(&buffer); |
35 |
| - var packer = msgpack.packer(stream.writer()); |
36 |
| - try packer.writeBool(false); |
37 |
| - try std.testing.expectEqualSlices(u8, &.{0xc2}, stream.getWritten()); |
| 26 | + var output = std.ArrayList(u8).init(std.testing.allocator); |
| 27 | + defer output.deinit(); |
| 28 | + try msgpack.pack(bool, output.writer(), false); |
| 29 | + try std.testing.expectEqualSlices(u8, &packed_false, output.items); |
38 | 30 | }
|
39 | 31 |
|
40 | 32 | test "writeBool: true" {
|
41 |
| - var buffer: [100]u8 = undefined; |
42 |
| - var stream = std.io.fixedBufferStream(&buffer); |
43 |
| - var packer = msgpack.packer(stream.writer()); |
44 |
| - try packer.writeBool(true); |
45 |
| - try std.testing.expectEqualSlices(u8, &.{0xc3}, stream.getWritten()); |
| 33 | + var output = std.ArrayList(u8).init(std.testing.allocator); |
| 34 | + defer output.deinit(); |
| 35 | + try msgpack.pack(bool, output.writer(), true); |
| 36 | + try std.testing.expectEqualSlices(u8, &packed_true, output.items); |
| 37 | +} |
| 38 | + |
| 39 | +test "writeBool: null" { |
| 40 | + var output = std.ArrayList(u8).init(std.testing.allocator); |
| 41 | + defer output.deinit(); |
| 42 | + try msgpack.pack(?bool, output.writer(), null); |
| 43 | + try std.testing.expectEqualSlices(u8, &packed_null, output.items); |
46 | 44 | }
|
0 commit comments