From 9ab9ebf33f7637236cea568a3565888b78fd08e5 Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Mon, 15 May 2023 15:23:10 +0200 Subject: Refactor testing support Following the implementation in `std.Build.Step.Compile, add the Kind type to differentiate between a normal executable and a test executable running zig tests. Replace `Exercise.run_test` field with `kind`. Compile the exercise in both the exe and test cases, reducing code duplication. Add the `check_output` and `check_test` methods in ZiglingStep, in order to differentiate the code checking a normal executable and a test executable. Update the tests to correctly check both the exe and test cases. Remove the temporary code added in commit 832772c. --- test/tests.zig | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) (limited to 'test') diff --git a/test/tests.zig b/test/tests.zig index b25b29c..45c075c 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -93,7 +93,7 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step { const case_step = createCase(b, "case-3"); for (exercises[0 .. exercises.len - 1]) |ex| { - if (ex.skip or ex.run_test) continue; + if (ex.skip) continue; if (ex.hint) |hint| { const n = ex.number(); @@ -249,21 +249,6 @@ fn check_output(step: *Step, exercise: Exercise, reader: Reader) !void { return; } - if (exercise.run_test) { - { - const actual = try readLine(reader, &buf) orelse "EOF"; - const expect = b.fmt("Testing {s}...", .{exercise.main_file}); - try check(step, exercise, expect, actual); - } - - { - const actual = try readLine(reader, &buf) orelse "EOF"; - try check(step, exercise, "", actual); - } - - return; - } - { const actual = try readLine(reader, &buf) orelse "EOF"; const expect = b.fmt("Compiling {s}...", .{exercise.main_file}); @@ -278,12 +263,19 @@ fn check_output(step: *Step, exercise: Exercise, reader: Reader) !void { { const actual = try readLine(reader, &buf) orelse "EOF"; - const expect = "PASSED:"; + const expect = switch (exercise.kind) { + .exe => "PASSED:", + .@"test" => "PASSED", + }; try check(step, exercise, expect, actual); } // Skip the exercise output. - const nlines = 1 + mem.count(u8, exercise.output, "\n") + 1; + const nlines = switch (exercise.kind) { + .exe => 1 + mem.count(u8, exercise.output, "\n") + 1, + .@"test" => 1, + }; + var lineno: usize = 0; while (lineno < nlines) : (lineno += 1) { _ = try readLine(reader, &buf) orelse @panic("EOF"); -- cgit v1.2.3