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 ``co
(path: Path, result: dict)
| 3529 | |
| 3530 | |
| 3531 | def _augment_cpp_string_tests(path: Path, result: dict) -> dict: |
| 3532 | """Append callable nodes for doctest/Catch2 string-named test cases that |
| 3533 | tree-sitter-cpp drops as ERROR nodes (issue #2594). |
| 3534 | |
| 3535 | The generic C++ pass still recovers the surrounding functions and include |
| 3536 | edges reliably, so this only adds the missing ``TEST_CASE("...")`` nodes and |
| 3537 | their ``contains`` edge from the file node — it does not rebuild the result. |
| 3538 | |
| 3539 | Matching is line-anchored raw text, mirroring the Spock fallback above; it is |
| 3540 | deliberately not comment/preprocessor aware (a ``TEST_CASE`` disabled behind |
| 3541 | a block comment or ``#if 0`` may still surface as a node, exactly as a |
| 3542 | commented Spock ``def "feature"()`` would). |
| 3543 | """ |
| 3544 | try: |
| 3545 | source = _read_source_text(path, warn=False) |
| 3546 | except OSError: |
| 3547 | return result |
| 3548 | matches = list(_CPP_STRING_TEST_RE.finditer(source)) |
| 3549 | if not matches: |
| 3550 | return result |
| 3551 | |
| 3552 | str_path = str(path) |
| 3553 | stem = _file_stem(path) |
| 3554 | file_nid = _make_id(str_path) |
| 3555 | # A test name that is all punctuation (TEST_CASE("***")) normalizes to empty, |
| 3556 | # so _make_id(stem, name) collapses onto this bare-stem id — colliding with |
| 3557 | # the file's namespace and silently swallowing every later such test under |
| 3558 | # one id (#1899). Detect that collapse and fall back to a line-positional id. |
| 3559 | stem_collapse_id = _make_id(stem) |
| 3560 | nodes = result.setdefault("nodes", []) |
| 3561 | edges = result.setdefault("edges", []) |
| 3562 | seen_ids = {n.get("id") for n in nodes} |
| 3563 | |
| 3564 | for m in matches: |
| 3565 | test_name = m.group(1) |
| 3566 | line = source.count("\n", 0, m.start()) + 1 |
| 3567 | test_nid = _make_id(stem, test_name) |
| 3568 | if test_nid == stem_collapse_id: |
| 3569 | test_nid = _make_id(stem, "test", f"L{line}") |
| 3570 | if test_nid in seen_ids: |
| 3571 | continue |
| 3572 | seen_ids.add(test_nid) |
| 3573 | # Keep the raw test name as the label (mirroring the Spock fallback, |
| 3574 | # which labels feature methods with their quoted string name). |
| 3575 | nodes.append({ |
| 3576 | "id": test_nid, |
| 3577 | "label": f'"{test_name}"', |
| 3578 | "file_type": "code", |
| 3579 | "source_file": str_path, |
| 3580 | "source_location": f"L{line}", |
| 3581 | }) |
| 3582 | edges.append({ |
| 3583 | "source": file_nid, |
| 3584 | "target": test_nid, |
| 3585 | "relation": "contains", |
| 3586 | "confidence": "EXTRACTED", |
| 3587 | "source_file": str_path, |
| 3588 | "source_location": f"L{line}", |
no test coverage detected