Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

16.8 KB
Raw Blame
"""Table-driven checks that a language node map says what the grammar means.

A ``LanguageNodeMap`` is a set of node-type strings, so nothing about it fails
loudly when it is wrong: naming a node type the grammar never emits is silently
inert, and naming one that means something else silently produces confident
wrong numbers. Importing cleanly proves neither. These tests run the real
walker over small snippets and assert the function names and cyclomatic
complexity that come back, which is the only thing that does.

Each case is (label, source, expected rows), so adding a language is a table
entry rather than a new test.

Grammars are not all installed everywhere, so every case guards with
``_require_language`` the way ``test_complexity_walker`` does.
"""

from __future__ import annotations

import pytest

from repowise.core.analysis.health.complexity import walk_file
from repowise.core.analysis.health.complexity.languages import get_language_map


def _require_language(language: str) -> None:
    try:
        from repowise.core.ingestion.parser import _get_language
    except Exception:
        pytest.skip(f"tree-sitter language pack missing for {language}")
    if _get_language(language) is None:
        pytest.skip(f"tree-sitter language pack missing for {language}")


def _rows(language: str, source: str, suffix: str) -> dict[str, int]:
    """Return ``{function name: ccn}`` from a real walk of *source*."""
    fc = walk_file(f"sample.{suffix}", language, source.encode("utf-8"))
    return {f.name: f.ccn for f in fc.functions}


# --------------------------------------------------------------------------- #
# Elixir: deliberately absent
# --------------------------------------------------------------------------- #


def test_elixir_has_no_complexity_map() -> None:
    """The absence is load-bearing, so it is asserted rather than assumed.

    tree-sitter-elixir emits ``call`` for ``defmodule``, ``def``, ``defp`` and
    an ordinary call alike, told apart only by the target's identifier text,
    which a set of node types cannot express. A map keyed on ``call`` reports
    the module as the function and hides every real one.
    """
    assert get_language_map("elixir") is None


def test_elixir_reports_nothing_rather_than_a_wrong_function() -> None:
    """No rows is the honest degrade. One row named ``defmodule`` is not.

    No grammar guard here on purpose: ``walk_file`` returns on the missing-map
    check before it ever asks for a parser, so this holds either way.
    """
    source = "defmodule A do\n  def one(x), do: x\nend\n\ndefmodule B do\n  def two(y), do: y\nend\n"
    fc = walk_file("sample.ex", "elixir", source.encode("utf-8"))
    assert fc.functions == []


# --------------------------------------------------------------------------- #
# Objective-C
# --------------------------------------------------------------------------- #

_OBJC_CASES: list[tuple[str, str, dict[str, int]]] = [
    (
        "file-scope globals and prototypes are not functions",
        """
static int gCounter = 0;
static NSString *gName = nil;
int helper(int x);

@implementation Ledger
- (int)post:(int)amount { if (amount > 0) { return 1; } return 0; }
@end
""",
        {"post": 2},
    ),
    (
        "a catch clause adds a branch",
        """
@implementation L
- (void)run {
  @try { [self go]; }
  @catch (NSException *e) { NSLog(@"x"); }
}
@end
""",
        {"run": 2},
    ),
    (
        "fast enumeration counts as a loop",
        """
@implementation L
- (int)count:(NSArray *)keys {
  int n = 0;
  for (NSString *k in keys) { n++; }
  return n;
}
@end
""",
        {"count": 2},
    ),
    (
        "a plain C function definition is still a function",
        """
int helper(int x) {
  for (int i = 0; i < x; i++) { x += i; }
  return x;
}
""",
        {"helper": 2},
    ),
    (
        "a ternary inside a declaration is reached",
        """
@implementation L
- (int)pick:(int)n { int b = n > 0 ? 1 : 2; return b; }
@end
""",
        {"pick": 2},
    ),
    (
        "a block nested in a method rolls into that method",
        """
@implementation L
- (void)run { void (^b)(void) = ^{ if (1) { NSLog(@"x"); } }; b(); }
@end
""",
        {"run": 2},
    ),
    (
        "a flat switch counts once for the dispatch",
        """
@implementation L
- (int)pick:(int)n {
  switch (n) { case 1: return 1; case 2: return 2; default: return 0; }
}
@end
""",
        {"pick": 2},
    ),
    (
        "a switch whose arms carry control flow counts them",
        """
@implementation L
- (int)pick:(int)n {
  switch (n) {
    case 1: { if (n > 0) { return 1; } return 5; }
    case 2: { for (int i = 0; i < n; i++) { n += i; } return 2; }
    default: return 0;
  }
}
@end
""",
        {"pick": 6},
    ),
]


@pytest.mark.parametrize(
    ("source", "expected"),
    [(src, exp) for _, src, exp in _OBJC_CASES],
    ids=[label for label, _, _ in _OBJC_CASES],
)
def test_objective_c_complexity(source: str, expected: dict[str, int]) -> None:
    _require_language("objectivec")
    assert _rows("objectivec", source, "m") == expected


# --------------------------------------------------------------------------- #
# F#
# --------------------------------------------------------------------------- #

_FSHARP_CASES: list[tuple[str, str, dict[str, int]]] = [
    (
        "every function is named rather than anonymous",
        "let one x = x\nlet two y = y\n",
        {"one": 1, "two": 1},
    ),
    (
        "a branch and a loop each add one",
        'let f x =\n  let y = 1\n  if x > 0 then\n    for i in 1..x do\n      printfn "%d" i\n  0\n',
        {"f": 3},
    ),
    (
        "a flat match counts once for the dispatch, at any width",
        'let f x =\n  match x with\n  | 1 -> "a"\n  | 2 -> "b"\n  | _ -> "c"\n',
        {"f": 2},
    ),
    (
        "control flow inside a match arm counts on top of the dispatch",
        'let f x =\n  match x with\n  | 1 -> (if x > 5 then "a" else "b")\n'
        '  | _ -> (for i in 1..x do printfn "%d" i\n          "c")\n',
        {"f": 4},
    ),
    (
        "a with handler is a branch",
        "let f x =\n  try\n    x\n  with\n  | _ -> 0\n",
        {"f": 2},
    ),
]


@pytest.mark.parametrize(
    ("source", "expected"),
    [(src, exp) for _, src, exp in _FSHARP_CASES],
    ids=[label for label, _, _ in _FSHARP_CASES],
)
def test_fsharp_complexity(source: str, expected: dict[str, int]) -> None:
    _require_language("fsharp")
    assert _rows("fsharp", source, "fs") == expected


def test_fsharp_member_is_named_by_its_method_not_its_self_identifier() -> None:
    """``member this.M(x)`` is ``M``, not ``this`` and not ``<anonymous>``."""
    _require_language("fsharp")
    source = "type T() =\n  member this.M(x) =\n    let y = 1\n    if x > 0 then 1 else 2\n"
    assert set(_rows("fsharp", source, "fs")) == {"M"}


def test_fsharp_flat_match_agrees_with_rust() -> None:
    """The dispatch is worth one branch in both, so the languages agree.

    ``case_kinds`` names the ``rules`` wrapper rather than the ``rule`` arm. On
    the arm, a three-arm match would score 4 against Rust's 2.
    """
    _require_language("fsharp")
    _require_language("rust")
    fsharp = 'let f x =\n  match x with\n  | 1 -> "a"\n  | 2 -> "b"\n  | _ -> "c"\n'
    rust = 'fn f(x: i32) -> &str {\n    match x {\n        1 => "a",\n        2 => "b",\n        _ => "c",\n    }\n}\n'
    assert _rows("fsharp", fsharp, "fs")["f"] == _rows("rust", rust, "rs")["f"] == 2


def test_fsharp_multi_handler_with_counts_once_not_per_clause() -> None:
    """A documented departure, pinned so it is a decision rather than a surprise.

    CATCH is meant to add one per clause. ``rules`` is a single node however
    many handlers hang off it, and the alternative inflates every match.
    """
    _require_language("fsharp")
    source = (
        "let f x =\n  try\n    x\n  with\n"
        "  | :? System.IO.IOException -> 1\n"
        "  | :? System.OverflowException -> 2\n"
        "  | _ -> 0\n"
    )
    assert _rows("fsharp", source, "fs") == {"f": 2}


def test_fsharp_single_expression_body_is_a_known_undercount() -> None:
    """Pinning current behaviour so a traversal fix is a deliberate change.

    ``walk_file`` takes the ``body`` field and walks that node's children, so a
    body that IS the branch is never visited. Correct for block-bodied
    languages, wrong here. When the traversal root changes this should become
    2, and this test is the place that says so.
    """
    _require_language("fsharp")
    assert _rows("fsharp", "let f x =\n  if x > 0 then 1 else 2\n", "fs") == {"f": 1}


# --------------------------------------------------------------------------- #
# Go
# --------------------------------------------------------------------------- #

_GO_CASES: list[tuple[str, str, dict[str, int]]] = [
    (
        "a flat select counts once for the dispatch, at any width",
        "package main\n\nfunc drain(ch chan int, done chan bool) int {\n"
        "\ttotal := 0\n"
        "\tselect {\n"
        "\tcase v := <-ch:\n\t\ttotal += v\n"
        "\tcase <-done:\n\t\ttotal += 100\n"
        "\tcase ch <- 1:\n\t\ttotal += 1\n"
        "\t}\n"
        "\treturn total\n}\n",
        {"drain": 2},
    ),
    (
        "a select with a default arm stays flat",
        "package main\n\nfunc drain(ch chan int, done chan bool) int {\n"
        "\ttotal := 0\n"
        "\tselect {\n"
        "\tcase v := <-ch:\n\t\ttotal += v\n"
        "\tcase <-done:\n\t\ttotal += 100\n"
        "\tdefault:\n\t\ttotal -= 1\n"
        "\t}\n"
        "\treturn total\n}\n",
        {"drain": 2},
    ),
    (
        "control flow inside a select arm counts on top of the dispatch",
        "package main\n\nfunc pick(ch chan int) int {\n"
        "\tselect {\n"
        "\tcase v := <-ch:\n"
        "\t\tif v > 0 {\n\t\t\treturn v\n\t\t}\n"
        "\t\treturn 0\n"
        "\tdefault:\n\t\treturn -1\n"
        "\t}\n}\n",
        {"pick": 4},
    ),
]


@pytest.mark.parametrize(
    ("source", "expected"),
    [(src, exp) for _, src, exp in _GO_CASES],
    ids=[label for label, _, _ in _GO_CASES],
)
def test_go_complexity(source: str, expected: dict[str, int]) -> None:
    _require_language("go")
    assert _rows("go", source, "go") == expected


def test_go_select_agrees_with_an_equivalent_switch() -> None:
    """``select`` is a dispatch, so an arm is worth what a ``switch`` case is.

    tree-sitter-go spells the construct ``select_statement`` with
    ``communication_case`` arms; neither type appeared in the map, so every arm
    was invisible and a three-arm select scored CCN 1 (a straight line) while
    the equivalent three-case switch scored 2. Only ``default`` registered,
    because ``default_case`` is shared with ``switch``.
    """
    _require_language("go")
    select = (
        "package main\n\nfunc pick(ch chan int) int {\n"
        "\tselect {\n"
        "\tcase v := <-ch:\n"
        "\t\tif v > 0 {\n\t\t\treturn v\n\t\t}\n"
        "\t\treturn 0\n"
        "\tdefault:\n\t\treturn -1\n"
        "\t}\n}\n"
    )
    switch = (
        "package main\n\nfunc pick(ch chan int) int {\n"
        "\tswitch {\n"
        "\tcase ch != nil:\n"
        "\t\tif ch != nil {\n\t\t\treturn 1\n\t\t}\n"
        "\t\treturn 0\n"
        "\tdefault:\n\t\treturn -1\n"
        "\t}\n}\n"
    )
    assert _rows("go", select, "go")["pick"] == _rows("go", switch, "go")["pick"] == 4


# --------------------------------------------------------------------------- #
# TypeScript / JavaScript
# --------------------------------------------------------------------------- #

_TS_CASES: list[tuple[str, str, dict[str, int]]] = [
    (
        "a flat switch counts once for the dispatch, default included",
        """
function pick(x: number): number {
  switch (x) { case 1: return 1; case 2: return 2; default: return 0; }
}
""",
        {"pick": 2},
    ),
    (
        "control flow inside a default arm counts on top of the dispatch",
        """
function pick(x: number): number {
  switch (x) {
    case 1: return 1;
    default: { if (x > 0) { return 2; } return 3; }
  }
}
""",
        {"pick": 4},
    ),
]


@pytest.mark.parametrize(
    ("source", "expected"),
    [(src, exp) for _, src, exp in _TS_CASES],
    ids=[label for label, _, _ in _TS_CASES],
)
def test_typescript_complexity(source: str, expected: dict[str, int]) -> None:
    _require_language("typescript")
    assert _rows("typescript", source, "ts") == expected


def test_typescript_default_arm_agrees_with_java() -> None:
    """``switch_default`` is a separate node in the JS/TS grammar.

    ``case_kinds`` named only ``switch_case``, so a complex ``default:`` arm
    never counted while Java / C# / Objective-C / C counted theirs. The same
    dispatch must score the same in both languages.
    """
    _require_language("typescript")
    _require_language("java")
    typescript = """
function pick(x: number): number {
  switch (x) {
    case 1: return 1;
    default: { if (x > 0) { return 2; } return 3; }
  }
}
"""
    java = """
public class A {
  int pick(int x) {
    switch (x) {
      case 1: return 1;
      default: { if (x > 0) { return 2; } return 3; }
    }
  }
}
"""
    assert _rows("typescript", typescript, "ts")["pick"] == _rows("java", java, "java")["pick"] == 4


# --------------------------------------------------------------------------- #
# PHP
# --------------------------------------------------------------------------- #

_PHP_CASES: list[tuple[str, str, dict[str, int]]] = [
    (
        "functions and methods are walked with CCN entry 1",
        """<?php
function standalone($x) {
    return $x;
}
class Service {
    public function handle($x) {
        return $x;
    }
}
""",
        {"standalone": 1, "handle": 1},
    ),
    (
        "if, elseif, else if and ternaries add branch points",
        """<?php
function branches($a, $b) {
    if ($a > 0) {
        return 1;
    } elseif ($a < 0) {
        return 2;
    } else if ($b > 0) {
        return 3;
    }
    return $b ? 4 : 5;
}
""",
        {"branches": 5},
    ),
    (
        "boolean operators &&, ||, and, or add to CCN",
        """<?php
function booleans($a, $b, $c, $d) {
    if ($a && $b) {
        return 1;
    }
    if ($c || $d) {
        return 2;
    }
    return ($a and $b) or ($c and $d);
}
""",
        {"booleans": 8},
    ),
    (
        "xor does not short-circuit, so it adds no path; and / or still do",
        """<?php
function wordy($a, $b, $c) {
    if ($a xor $b) {
        return 1;
    }
    return $a and $b or $c;
}
""",
        {"wordy": 4},
    ),
    (
        "loops and try-catch add complexity",
        """<?php
function control_flow($items) {
    try {
        foreach ($items as $item) {
            while ($item > 0) {
                $item--;
            }
        }
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}
""",
        {"control_flow": 4},
    ),
    (
        "match and switch expressions count dispatch",
        """<?php
function dispatch($x) {
    switch ($x) {
        case 1:
            return 10;
        case 2:
            return 20;
        default:
            return 0;
    }
}
function matching($x) {
    return match ($x) {
        1 => 10,
        2, 3 => 20,
        default => 0,
    };
}
""",
        {"dispatch": 2, "matching": 2},
    ),
]


@pytest.mark.parametrize(
    ("source", "expected"),
    [(src, exp) for _, src, exp in _PHP_CASES],
    ids=[label for label, _, _ in _PHP_CASES],
)
def test_php_complexity(source: str, expected: dict[str, int]) -> None:
    _require_language("php")
    assert _rows("php", source, "php") == expected


def test_php_name_nodes_count_only_in_php_calls() -> None:
    """The ``name`` identifier match is gated to PHP's call kinds.

    No other mapped grammar has a ``name`` node or PHP's call node kinds, so
    their assertion counting cannot change through that gate.
    """
    from repowise.core.analysis.health.complexity.assertions import _PHP_CALL_KINDS
    from repowise.core.analysis.health.complexity.languages import LANGUAGE_MAPS
    from repowise.core.ingestion.parser import _get_language, grammar_tag_for

    php_only = _PHP_CALL_KINDS | {"name"}
    for language in LANGUAGE_MAPS:
        if language == "php":
            continue
        try:
            grammar = _get_language(grammar_tag_for(language, "x"))
        except Exception:
            continue
        if grammar is None:
            continue
        kinds = {
            grammar.node_kind_for_id(i)
            for i in range(grammar.node_kind_count)
            if grammar.node_kind_is_named(i)
        }
        assert not kinds & php_only, language


def test_php_argument_named_assert_is_not_an_assertion() -> None:
    _require_language("php")
    source = b"""<?php
class FooTest {
    public function testIt() {
        $this->assertSame(1, $x);
        $this->assertSame(2, $y);
        $this->assertSame(3, $z);
        helper($assertion);
        helper($assertion);
    }
}
"""
    fc = walk_file("FooTest.php", "php", source)
    (fn,) = fc.functions
    assert fn.assertion_count == 3