From 5c488a1402be83f198cc29f23f448313001a9a1c Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Thu, 4 May 2023 16:34:17 +0200 Subject: [PATCH] test: improve test case 1 and 2 In test case 1 and 2, remove the `i` variable and use `ex.number()` instead. In test case 2, when checking the exercise output from stderr, also check that stdout is empty and vice versa. --- test/tests.zig | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/test/tests.zig b/test/tests.zig index 752ca50..060b553 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -32,21 +32,23 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step { // Test that `zig build -Dhealed -Dn=n test` selects the nth exercise. const case_step = createCase(b, "case-1"); - var i: usize = 0; for (exercises[0 .. exercises.len - 1]) |ex| { - i += 1; + const n = ex.number(); if (ex.skip) continue; const cmd = b.addSystemCommand( - &.{ b.zig_exe, "build", "-Dhealed", b.fmt("-Dn={}", .{i}), "test" }, + &.{ b.zig_exe, "build", "-Dhealed", b.fmt("-Dn={}", .{n}), "test" }, ); - cmd.setName(b.fmt("zig build -Dhealed -Dn={} test", .{i})); + cmd.setName(b.fmt("zig build -Dhealed -Dn={} test", .{n})); cmd.expectExitCode(0); - if (ex.check_stdout) - expectStdOutMatch(cmd, ex.output) - else + if (ex.check_stdout) { + expectStdOutMatch(cmd, ex.output); + cmd.expectStdErrEqual(""); + } else { expectStdErrMatch(cmd, ex.output); + cmd.expectStdOutEqual(""); + } cmd.step.dependOn(&heal_step.step); @@ -60,15 +62,14 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step { // Test that `zig build -Dhealed -Dn=n test` skips disabled esercises. const case_step = createCase(b, "case-2"); - var i: usize = 0; for (exercises[0 .. exercises.len - 1]) |ex| { - i += 1; + const n = ex.number(); if (!ex.skip) continue; const cmd = b.addSystemCommand( - &.{ b.zig_exe, "build", "-Dhealed", b.fmt("-Dn={}", .{i}), "test" }, + &.{ b.zig_exe, "build", "-Dhealed", b.fmt("-Dn={}", .{n}), "test" }, ); - cmd.setName(b.fmt("zig build -Dhealed -Dn={} test", .{i})); + cmd.setName(b.fmt("zig build -Dhealed -Dn={} test", .{n})); cmd.expectExitCode(0); cmd.expectStdOutEqual(""); expectStdErrMatch(cmd, b.fmt("{s} skipped", .{ex.main_file}));