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.
This commit is contained in:
Manlio Perillo 2023-05-04 16:34:17 +02:00
parent a2b7fbe3e5
commit 5c488a1402

View file

@ -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}));