nvim-neotest / neotest

An extensible framework for interacting with tests within NeoVim.
MIT License
2.22k stars 107 forks source link

Is it possible to transform names shown in test summary? #42

Closed sidlatau closed 2 years ago

sidlatau commented 2 years ago

I am writing adapter to dart tests and have an issue with test names - there could be valid names with variation of single/double/triple quates:

image

Is it possible to transform namespace/test names that are shown in the test summary? I would want to remove surrounding quates.

Here is a tree-sitter query:

  local query = [[
  ;; group blocks
  (expression_statement 
    (identifier) @group (#eq? @group "group")
    (selector (argument_part (arguments (argument (string_literal) @namespace.name )))))
    @namespace.definition

  ;; tests blocks
  (expression_statement 
    (identifier) @testFunc (#any-of? @testFunc "test" "testWidgets")
    (selector (argument_part (arguments (argument (string_literal) @test.name))))) 
    @test.definition
  ]]
rcarriga commented 2 years ago

Great to hear you're writing an adapter!

You can change the names before returning them

  local tree = lib.treesitter.parse_positions(path, query)

  for position in tree:iter() do
    if position.type == "test" then
      position.name = ...
    end
  end
  return tree
end

Though if you want to edit the IDs you have to do that before the tree is constructed by providing a position_id function to the third opts argument.

If you haven't already, enable type checking for the lua LSP to check neotest you should see everything nicely typed (such as position.type will show valid values).

sidlatau commented 2 years ago

Thanks, this worked:

    for _, position in tree:iter() do
        if position.type == "test" or position.type == "namespace" then
            position.name = remove_surrounding_quates(position.name)
        end
    end

And thanks for the hint to enable types, that helps a lot!