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.
pull/2/head
Manlio Perillo 1 year ago
parent a2b7fbe3e5
commit 5c488a1402

@ -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. // Test that `zig build -Dhealed -Dn=n test` selects the nth exercise.
const case_step = createCase(b, "case-1"); const case_step = createCase(b, "case-1");
var i: usize = 0;
for (exercises[0 .. exercises.len - 1]) |ex| { for (exercises[0 .. exercises.len - 1]) |ex| {
i += 1; const n = ex.number();
if (ex.skip) continue; if (ex.skip) continue;
const cmd = b.addSystemCommand( 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.expectExitCode(0);
if (ex.check_stdout) if (ex.check_stdout) {
expectStdOutMatch(cmd, ex.output) expectStdOutMatch(cmd, ex.output);
else cmd.expectStdErrEqual("");
} else {
expectStdErrMatch(cmd, ex.output); expectStdErrMatch(cmd, ex.output);
cmd.expectStdOutEqual("");
}
cmd.step.dependOn(&heal_step.step); 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. // Test that `zig build -Dhealed -Dn=n test` skips disabled esercises.
const case_step = createCase(b, "case-2"); const case_step = createCase(b, "case-2");
var i: usize = 0;
for (exercises[0 .. exercises.len - 1]) |ex| { for (exercises[0 .. exercises.len - 1]) |ex| {
i += 1; const n = ex.number();
if (!ex.skip) continue; if (!ex.skip) continue;
const cmd = b.addSystemCommand( 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.expectExitCode(0);
cmd.expectStdOutEqual(""); cmd.expectStdOutEqual("");
expectStdErrMatch(cmd, b.fmt("{s} skipped", .{ex.main_file})); expectStdErrMatch(cmd, b.fmt("{s} skipped", .{ex.main_file}));

Loading…
Cancel
Save