mrcjkb/neotest-haskell

tasty: properly detect top-level `testCase`s that are referenced by a `testGroup` elsewhere

Opened this issue · 1 comments

mrcjkb commented

Do you think tree-sitter could be able to walk up the tree of nodes and detect the testGroup that's just up ahead of the function that defines the list of tests?

Originally posted by @Kleidukos in #102 (comment)

Example:

specs :: TestTree
specs =
  testGroup
    "Parser Tests"
    [ testGroup "Stage 1" stage1Tests
    , testGroup "Stage 2" stage2Tests
    , testGroup "Stage 3" stage3Tests
    , testGroup "Stage 4" stage4Tests
    ]

stage1Tests :: [TestTree]
stage1Tests =
  [ testCase "Multi-digit return" testMultiDigitReturn
  , testCase "Bunch of newlines" testBunchOfNewlines
  , testCase "No newlines" testNoNewlines
  , testCase "Missing closing paren" testMissingClosingParen
  , testCase "Missing return value" testMissingReturnValue
  , testCase "Missing closing brace" testMissingClosingBrace
  ]

should be detected as

  • namespace: "Parser Tests:
    • namespace: "Stage 1
      • [test] -> test cases

Currently, the test groups and test cases are not associated with each other, because there are no tree-sitter queries that link tests with test groups in other trees.

I'm not sure if this is possible with scheme queries, but it is worth looking into.

mrcjkb commented

Note to self:

This query properly links the stage1Tests function with testGroup "Stage 1" stage1Tests, but there does not seem to be a way to capture both as a @namespace.definition. It looks like only the second @namespace.definition capture is applied.
I don't think scheme is powerful enough to define a capture that encompasses both the testGroup exp_apply and the function.

;; testGroups that call functions with testCases
(
(_
(_
(_
(exp_apply
  (exp_name) @ns_func_name
  (#lua-match? @ns_func_name "^.*testGroup")
  (exp_literal (string) @namespace.name)
  (_ (variable) @ref_name)
) @namespace.definition
)
)
)
(function
  name: (variable) @ref_name
(_
  (exp_apply
    (exp_name) @test_func_name
    (#lua-match? @test_func_name "^.*testCase")
    (exp_literal) @test.name
  ) @test.definition
)
) @namespace.definition
)