fix(cpp): recover doctest/Catch2 string-named TEST_CASE nodes (#2594)

doctest/Catch2 register tests with a string-literal name (TEST_CASE("...")),
which tree-sitter-cpp parses as an ERROR node, so every test function was
silently dropped. Adds a line-anchored regex fallback that appends one code
node per top-level TEST_CASE/TEST_CASE_TEMPLATE/SCENARIO plus a contains edge
from the file node, mirroring the Groovy Spock fallback.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ousama Ben Younes
2026-08-15 17:17:29 +01:00
committed by safishamsi
co-authored by Claude Opus 4.8
parent 6f1cbe5fda
commit 54b3b78955
3 changed files with 130 additions and 2 deletions
+85 -2
View File
@@ -1864,9 +1864,92 @@ def extract_c(path: Path) -> dict:
return _extract_generic(path, _C_CONFIG)
# doctest / Catch2 name each test case with a string literal
# (``TEST_CASE("name")``), which tree-sitter-cpp cannot parse: the construct
# becomes an ERROR node and the whole test function is dropped from the graph
# (issue #2594). Recover the test cases with a regex fallback, mirroring the
# Spock handling of Groovy ``def "feature"()`` above. Scoped to doctest +
# Catch2, which share this string-named-macro surface.
#
# Only the top-level test-declaration macros are recovered as callable nodes.
# ``SUBCASE`` / ``SECTION`` are *nested* scopes inside a test body and
# ``TEST_SUITE`` is a grouping wrapper, not a test function — emitting them as
# file-contained nodes would fabricate wrong-granularity nodes and edges.
_CPP_STRING_TEST_MACROS = (
"TEST_CASE", "TEST_CASE_TEMPLATE", "SCENARIO",
)
# The name group consumes C-string escapes (``\"``, ``\\``) so a test whose
# name embeds an escaped quote is captured whole, not truncated at the escape.
_CPP_STRING_TEST_RE = re.compile(
r'^[ \t]*(?:' + "|".join(_CPP_STRING_TEST_MACROS) + r')\s*\(\s*"((?:[^"\\]|\\.)+)"',
re.MULTILINE,
)
def _augment_cpp_string_tests(path: Path, result: dict) -> dict:
"""Append callable nodes for doctest/Catch2 string-named test cases that
tree-sitter-cpp drops as ERROR nodes (issue #2594).
The generic C++ pass still recovers the surrounding functions and include
edges reliably, so this only adds the missing ``TEST_CASE("...")`` nodes and
their ``contains`` edge from the file node — it does not rebuild the result.
Matching is line-anchored raw text, mirroring the Spock fallback above; it is
deliberately not comment/preprocessor aware (a ``TEST_CASE`` disabled behind
a block comment or ``#if 0`` may still surface as a node, exactly as a
commented Spock ``def "feature"()`` would).
"""
try:
source = path.read_text(errors="replace")
except OSError:
return result
matches = list(_CPP_STRING_TEST_RE.finditer(source))
if not matches:
return result
str_path = str(path)
stem = _file_stem(path)
file_nid = _make_id(str_path)
nodes = result.setdefault("nodes", [])
edges = result.setdefault("edges", [])
seen_ids = {n.get("id") for n in nodes}
for m in matches:
test_name = m.group(1)
line = source.count("\n", 0, m.start()) + 1
test_nid = _make_id(stem, test_name)
if test_nid in seen_ids:
continue
seen_ids.add(test_nid)
# Keep the raw test name as the label (mirroring the Spock fallback,
# which labels feature methods with their quoted string name).
nodes.append({
"id": test_nid,
"label": f'"{test_name}"',
"file_type": "code",
"source_file": str_path,
"source_location": f"L{line}",
})
edges.append({
"source": file_nid,
"target": test_nid,
"relation": "contains",
"confidence": "EXTRACTED",
"source_file": str_path,
"source_location": f"L{line}",
"weight": 1.0,
})
return result
def extract_cpp(path: Path) -> dict:
"""Extract functions, classes, and includes from a .cpp/.cc/.cxx/.hpp file."""
return _extract_generic(path, _CPP_CONFIG)
"""Extract functions, classes, and includes from a .cpp/.cc/.cxx/.hpp file.
Recovers doctest/Catch2 ``TEST_CASE("name")`` test cases that tree-sitter-cpp
drops as ERROR nodes (issue #2594), mirroring the Spock fallback for Groovy.
"""
result = _extract_generic(path, _CPP_CONFIG)
return _augment_cpp_string_tests(path, result)
def extract_ruby(path: Path) -> dict:
+17
View File
@@ -0,0 +1,17 @@
#include <doctest/doctest.h>
static int add(int a, int b) { return a + b; }
TEST_CASE("addition works") {
SUBCASE("small operands") {
CHECK(add(1, 2) == 3);
}
}
TEST_CASE("subtraction works") {
CHECK(add(5, -1) == 4);
}
TEST_CASE("handles \"quoted\" names") {
CHECK(add(0, 0) == 0);
}
+28
View File
@@ -187,6 +187,34 @@ def test_cpp_finds_methods():
# C++ extractor captures the constructor and public-visible methods
assert any("HttpClient" in l for l in labels)
def test_cpp_recovers_doctest_string_named_test_cases():
"""doctest/Catch2 ``TEST_CASE("name")`` uses a string literal where the
grammar expects an identifier, so tree-sitter-cpp drops the whole test
function as an ERROR node (#2594). The regex fallback must recover one
callable node per test case, keeping the raw name as the label, without
losing the surrounding functions the tree-sitter pass still parses.
"""
r = extract_cpp(FIXTURES / "sample_doctest.cpp")
labels = _labels(r)
assert '"addition works"' in labels
assert '"subtraction works"' in labels
# The plain function next to the test cases must still be present.
assert any(l.startswith("add") for l in labels)
# A nested SUBCASE is a scope inside a test body, not a file-level test
# function, so it must not be emitted as its own callable node.
assert '"small operands"' not in labels
# An escaped quote inside the test name is captured whole, not truncated
# at the first ``\"``.
assert r'"handles \"quoted\" names"' in labels
# Each recovered test case is contained by its file node.
file_id = next(n["id"] for n in r["nodes"] if n["label"] == "sample_doctest.cpp")
test_ids = {n["id"] for n in r["nodes"] if n["label"].startswith('"')}
contained = {
e["target"] for e in r["edges"]
if e["relation"] == "contains" and e["source"] == file_id
}
assert test_ids and test_ids <= contained
def test_cpp_finds_includes():
r = extract_cpp(FIXTURES / "sample.cpp")
assert "imports" in _relations(r)