Skip to content

Commit

Permalink
Add a simple test for "embed" function in Writer
Browse files Browse the repository at this point in the history
Add a 'testbed' facility for writing more test.
  • Loading branch information
MFAshby committed Oct 26, 2024
1 parent f54c606 commit 1a0355a
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Writer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,47 @@ fn newLineAndIndent(writer: *Writer) anyerror!void {
fn raw(writer: *Writer, s: []const u8) anyerror!void {
try writer.sink.write(s);
}

test {
_ = T;
}
const T = struct {
const Testbed = struct {
buf: std.ArrayList(u8),
fn init(a: std.mem.Allocator) Testbed {
return .{
.buf = std.ArrayList(u8).init(a),
};
}
fn writer(self: *Testbed, indent: []const u8) Writer {
return Writer.init(.{
.context = self,
.writeFn = write,
}, .{ .indent = indent });
}
fn write(context: *const anyopaque, data: []const u8) anyerror!void {
// TODO not sure why context is const.
var self: *Testbed = @constCast(@alignCast(@ptrCast(context)));
try self.buf.appendSlice(data);
}
fn output(self: *Testbed) []const u8 {
return self.buf.items;
}
fn deinit(self: *Testbed) void {
self.buf.deinit();
}
};
test "embed" {
var tb = Testbed.init(std.testing.allocator);
defer tb.deinit();
var wtr = tb.writer(" ");
try wtr.xmlDeclaration("UTF-8", null);
try wtr.elementStart("foo");
try wtr.embed("<bar>Baz!</bar>");
try wtr.elementEnd("foo");
try std.testing.expectEqualStrings(
\\<?xml version="1.0" encoding="UTF-8"?>
\\<foo><bar>Baz!</bar></foo>
, tb.output());
}
};

0 comments on commit 1a0355a

Please sign in to comment.