From d7c7320443b4fc67f540d9e434d43f5190c4c2ca Mon Sep 17 00:00:00 2001 From: Marto Date: Wed, 29 Apr 2026 11:33:50 +0200 Subject: [PATCH] Basic test implementation --- build.zig | 15 +++++++++++++++ src/infrastructure/road.zig | 17 ++++++++++------- src/main.zig | 3 --- src/test.zig | 3 +++ 4 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 src/test.zig diff --git a/build.zig b/build.zig index 20be055..0356b98 100644 --- a/build.zig +++ b/build.zig @@ -130,6 +130,20 @@ pub fn build(b: *std.Build) void { run_cmd.addArgs(args); } + const tests = b.addTest(.{ + .root_module = b.createModule(.{ + .root_source_file = b.path("src/test.zig"), + .target = target, + .optimize = optimize, + .imports = &.{ + .{ .name = "raylib", .module = raylib }, + }, + }), + }); + + tests.root_module.linkLibrary(raylib_artifact); + const run_tests = b.addRunArtifact(tests); + // Creates an executable that will run `test` blocks from the provided module. // Here `mod` needs to define a target, which is why earlier we made sure to // set the releative field. @@ -156,6 +170,7 @@ pub fn build(b: *std.Build) void { const test_step = b.step("test", "Run tests"); // test_step.dependOn(&run_mod_tests.step); test_step.dependOn(&run_exe_tests.step); + test_step.dependOn(&run_tests.step); // Just like flags, top level steps are also listed in the `--help` menu. // diff --git a/src/infrastructure/road.zig b/src/infrastructure/road.zig index 065820b..8a9ee6e 100644 --- a/src/infrastructure/road.zig +++ b/src/infrastructure/road.zig @@ -46,20 +46,23 @@ test "valid road nodes" { defer _ = gpa.deinit(); const allocator = gpa.allocator(); - const start: Node = .init(34, .{500, 500}); + const start: Node = .init(34, .{.x = 500, .y = 500}); const start_ptr = try allocator.create(Node); + defer { + start_ptr.*.deinit(allocator); + allocator.destroy(start_ptr); + } start_ptr.* = start; - const end: Node = .init(227, .{600, 500}); + const end: Node = .init(227, .{.x = 600, .y = 500}); const end_ptr = try allocator.create(Node); + defer { + end_ptr.*.deinit(allocator); + allocator.destroy(end_ptr); + } end_ptr.* = end; const road: Road = .init(11, start_ptr, end_ptr); try expect(road.nodes[0].id == 34 and road.nodes[1].id == 227); -} - -test "false" { - const truth = false; - try expect(truth); } \ No newline at end of file diff --git a/src/main.zig b/src/main.zig index 49cd6aa..27991ee 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4,9 +4,6 @@ const rl = @import("raylib"); const c = @import("constants.zig"); const Simulator = @import("simulator.zig").Simulator; -// TODO PLANS -// Additionally add more robust error and test handling - pub fn main(init: std.process.Init) !void { const allocator = init.gpa; diff --git a/src/test.zig b/src/test.zig new file mode 100644 index 0000000..3e97cd6 --- /dev/null +++ b/src/test.zig @@ -0,0 +1,3 @@ +test { + _ = @import("infrastructure/road.zig"); +} \ No newline at end of file