@@ 210,3 210,31 @@ test "marshal map" {
unreachable;
}
+
+test "marshal union" {
+ const NameAge = union(enum) {
+ name: []const u8,
+ age: isize,
+ };
+
+ var buf: [0x1_000]u8 = undefined;
+ var fbs = io.fixedBufferStream(&buf);
+ const writer = fbs.writer();
+ var e = encoder(writer);
+
+ {
+ const val = NameAge{ .name = "Mary" };
+ try e.encode(val);
+ const reference = &[_]u8{ 0x00, 0x04, 0x4d, 0x61, 0x72, 0x79 };
+ try testing.expectEqualSlices(u8, reference, fbs.getWritten());
+ }
+
+ fbs.reset();
+
+ {
+ const val = NameAge{ .age = 24 };
+ try e.encode(val);
+ const reference = &[_]u8{ 0x01, 0x30 };
+ try testing.expectEqualSlices(u8, reference, fbs.getWritten());
+ }
+}