lawrence-laz / neotest-zig

Test runner for Zig in Neovim using Neotest backend.
MIT License
27 stars 7 forks source link

Support for decl tests and unnamed tests #2

Open freakmangd opened 1 year ago

freakmangd commented 1 year ago

test "add" works, but the other two tests don't.

fn add(a: anytype, b: anytype) @TypeOf(a, b) {
    return a + b;
}

test "add" {
    try std.testing.expectEqual(3, add(1, 2));
}

test add {
    try std.testing.expectEqual(3, add(1, 2));
}

test {
    try std.testing.expectEqual(3, add(1, 2));
}

We can get test add working just by changing this line:

@@ -61,7 +61,7 @@ function M.discover_positions(path)
        local query = [[
                ;;query
                (TestDecl
-                       (STRINGLITERALSINGLE) @test.name
+                       [(IDENTIFIER) (STRINGLITERALSINGLE)] @test.name
                ) @test.definition
        ]]
        log.debug("Running query", query)

I couldn't figure out a way to capture unnamed tests, but that's mainly because I'm unfamiliar with the system, I'm sure its doable.

lawrence-laz commented 1 year ago

Hmm, the unnamed one might be a bit of an issue, as it seems like it cannot be filtered (from zig docs):

By convention, non-named tests should only be used to make other tests run. Non-named tests cannot be filtered.

It says that unnamed tests should only be used to run other tests, so maybe they should only run when the whole file is running.

I'll try to look into this

lawrence-laz commented 1 year ago

Ok, so skipping unnamed tests is impossible (at least with standard test runner) right now in zig, so they will always run (from zig docs):

[...] Note that non-named tests are run even when using the --test-filter [text] command line parameter.

The problem is of two parts now:

  1. Controlling when unnamed tests run - impossible with default test runner, but this package uses a custom test runner, so maybe something can be done with that.
  2. Discovering and displaying test results correctly with unnamed tests - this might require a custom test position generator on neotest side, since unnamed tests don't have a name/identifier and neotest requires one. With a custom generator hooked in unnamed tests would probably show up as test0 test1, etc., same way they currently appear in zig test command.