Files
crapper/tests/test_languages.py
T
Cursor AgentandRobert C. Martin 10c4e43f7b Add Swift function CRAP scores, coverage, and test discovery.
Swift packages and iOS apps had no per-function complexity or coverage join, so crapper could not write a .metrics/crap.edn entry that uml-viewer and mutator can attach to a function. The Swift reader scores top-level functions, methods, initializers, subscripts, computed properties, and closures bound with let or var. Nested closures stay inside the enclosing function. Decision points match the other languages: if, guard, loops, catch, ternary, each switch case, &&, ||, ??, and optional chaining.

Coverage reads llvm-cov JSON from swift test and xccov JSON from xcodebuild, joined on the file suffix and the function's line span. A package runs swift test --enable-code-coverage; an Xcode project runs xcodebuild and xcrun xccov view --report --json.

Proved by tests/test_swift.py against checked-in llvm-cov and xccov fixtures, the existing suite (174 passed), crapper with every function under CRAP 10, dryer with no duplicate at 0.8 or above, and mutator killing every mutant in the Swift reader.

Co-authored-by: Robert C. Martin <[email protected]>
2026-11-25 23:45:32 +00:00

25 lines
1020 B
Python

import pytest
from crapper.languages import functions_in_file
_SOURCES = [
("clojure", "(defn choose [x] x)\n", "src/demo/core.clj", "choose"),
("java", "class Board { void place() { if (true) return; } }\n", "Board.java", "place"),
("go", "package demo\nfunc Place() int { return 1 }\n", "board.go", "Place"),
("typescript", "export function view() { return 1 }\n", "src/ui.ts", "view"),
("python", "def run():\n return 1\n", "src/app.py", "run"),
("rust", "pub fn open() {}\n", "src/lib.rs", "open"),
("swift", "func open() {}\n", "Sources/Demo/Box.swift", "open"),
]
@pytest.mark.parametrize(("language", "source", "path", "name"), _SOURCES)
def test_each_language_answers_the_same_message(language, source, path, name):
found = functions_in_file(language, source, path, "/proj")
assert found[0].name == name
assert found[0].language == language
def test_unknown_language_has_no_functions():
assert functions_in_file("ruby", "def x\nend\n", "a.rb", "/proj") == []