From 54b3b789556ac42d74b0a9158ebb623f57c38109 Mon Sep 17 00:00:00 2001 From: Ousama Ben Younes Date: Sat, 15 Aug 2026 17:17:29 +0100 Subject: [PATCH] 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) --- graphify/extract.py | 87 ++++++++++++++++++++++++++++++- tests/fixtures/sample_doctest.cpp | 17 ++++++ tests/test_languages.py | 28 ++++++++++ 3 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/sample_doctest.cpp diff --git a/graphify/extract.py b/graphify/extract.py index 1822c55..459fa87 100644 --- a/graphify/extract.py +++ b/graphify/extract.py @@ -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: diff --git a/tests/fixtures/sample_doctest.cpp b/tests/fixtures/sample_doctest.cpp new file mode 100644 index 0000000..4f8f7cc --- /dev/null +++ b/tests/fixtures/sample_doctest.cpp @@ -0,0 +1,17 @@ +#include + +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); +} diff --git a/tests/test_languages.py b/tests/test_languages.py index aff689d..a65b650 100644 --- a/tests/test_languages.py +++ b/tests/test_languages.py @@ -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)