test(container): render Cell slices test against .zon input
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m0s

This commit is contained in:
2025-02-25 18:41:04 +01:00
parent 4234c9ad0c
commit 9d5a661b4e
6 changed files with 127 additions and 0 deletions

View File

@@ -174,6 +174,90 @@ pub const Border = packed struct {
}
}
}
test "all sides" {
const event = @import("event.zig");
const testing = @import("testing.zig");
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
.border = .{
.color = .green,
.sides = .all,
},
}, .{});
defer container.deinit();
try testing.expectContainerScreen(.{
.rows = 20,
.cols = 30,
}, &container, @import("test/container/border.all.zon"));
}
test "vertical sides" {
const event = @import("event.zig");
const testing = @import("testing.zig");
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
.border = .{
.color = .green,
.sides = .vertical,
},
}, .{});
defer container.deinit();
try testing.expectContainerScreen(.{
.rows = 20,
.cols = 30,
}, &container, @import("test/container/border.vertical.zon"));
}
test "horizontal sides" {
const event = @import("event.zig");
const testing = @import("testing.zig");
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
.border = .{
.color = .blue,
.sides = .horizontal,
},
}, .{});
defer container.deinit();
try testing.expectContainerScreen(.{
.rows = 20,
.cols = 30,
}, &container, @import("test/container/border.horizontal.zon"));
}
// NOTE: this test serves as a template for further test creations. It should not run for testing.
// test "create container zon file" {
// const event = @import("event.zig");
// const testing = @import("testing.zig");
// var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
// .border = .{
// .color = .green,
// .sides = .horizontal,
// },
// }, .{});
// defer container.deinit();
// const size: Size = .{
// .rows = 20,
// .cols = 30,
// };
// const file = try std.fs.cwd().createFile("src/test/container/border.horizontal.zon", .{ .truncate = true });
// defer file.close();
// const allocator = std.testing.allocator;
// var renderer: testing.Renderer = .init(allocator, size);
// defer renderer.deinit();
// try container.handle(.{ .resize = size });
// try renderer.render(Container(event.SystemEvent), &container);
// try renderer.save(file.writer());
// }
};
/// Rectangle configuration struct

View File

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -71,10 +71,50 @@ pub const Renderer = struct {
for (container.elements.items) |*element| try this.render(T, element);
}
pub fn save(this: @This(), writer: anytype) !void {
try std.zon.stringify.serialize(this.screen, .{ .whitespace = false }, writer);
}
};
/// This function is intended to be used only in tests. Test if a `Container`'s
/// rendered contents are equal to the expected `Cell` slice.
///
/// # Test data creation
///
/// Create a .zon file containing the expected `Cell` slice using the `zterm.testing.Renderer.save` method:
///
/// ```zig
/// const file = try std.fs.cwd().createFile("test/container/border/all.zon", .{ .truncate = true });
/// defer file.close();
///
/// const allocator = std.testing.allocator;
/// var renderer: testing.Renderer = .init(allocator, size);
/// defer renderer.deinit();
///
/// try container.handle(.{ .resize = size });
/// try renderer.render(Container(event.SystemEvent), &container);
/// try renderer.save(file.writer());
/// ```
///
/// # Testing against created data
///
/// Then later load that .zon file at compile time and run your test against this `Cell` slice.
///
/// ```zig
/// var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
/// .border = .{
/// .color = .green,
/// .sides = .all,
/// },
/// }, .{});
/// defer container.deinit();
///
/// try testing.expectContainerScreen(.{
/// .rows = 20,
/// .cols = 30,
/// }, &container, @import("test/container/border.all.zon"));
/// ```
pub fn expectContainerScreen(size: Size, container: *Container(event.SystemEvent), expected: []const Cell) !void {
const allocator = std.testing.allocator;
var renderer: Renderer = .init(allocator, size);