mrcjkb / neotest-haskell

Neotest adapter for Haskell (cabal or stack) with support for Sydtest, Hspec and Tasty
GNU General Public License v2.0
55 stars 4 forks source link

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

Open mrcjkb opened 1 year ago

mrcjkb commented 1 year ago

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 https://github.com/mrcjkb/neotest-haskell/issues/102#issuecomment-1596276072

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

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 1 year ago

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
)