mirror of
https://github.com/unclebob/dryer.git
synced 2026-11-26 18:58:51 +00:00
Compare Swift functions so renamed locals still match.
A .swift file was not a source file, so a scan reported no candidates. .swift now selects the swift grammar, and a function with a body is a candidate form. Proved: test_renamed_locals_in_swift_functions_match failed with an empty report, then passed with one swift pair at score 1.0. The full suite passed, 81 tests. Co-authored-by: Robert C. Martin <[email protected]>
This commit is contained in:
co-authored by
Robert C. Martin
parent
66ff6d21a4
commit
a37f7d1827
@@ -21,6 +21,7 @@ EXTENSIONS = {
|
||||
".cts": "typescript",
|
||||
".rs": "rust",
|
||||
".py": "python",
|
||||
".swift": "swift",
|
||||
}
|
||||
|
||||
SKIP_DIRS = {
|
||||
|
||||
@@ -130,6 +130,14 @@ def _typescript_nodes(data: bytes, root) -> list:
|
||||
return found
|
||||
|
||||
|
||||
def _swift_nodes(data: bytes, root) -> list:
|
||||
found = []
|
||||
for node in descendants(root):
|
||||
if node.type == "function_declaration" and _has(node, "function_body"):
|
||||
found.append(node)
|
||||
return found
|
||||
|
||||
|
||||
def _rust_nodes(data: bytes, root) -> list:
|
||||
found = []
|
||||
for node in descendants(root):
|
||||
@@ -149,6 +157,7 @@ _PICKERS = {
|
||||
"python": ("python", _python_nodes),
|
||||
"typescript": (_grammar, _typescript_nodes),
|
||||
"rust": ("rust", _rust_nodes),
|
||||
"swift": ("swift", _swift_nodes),
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
from pathlib import Path
|
||||
|
||||
from dryer.scan import find_duplicates, scan_files
|
||||
|
||||
|
||||
def write_source(root: Path, name: str, text: str) -> None:
|
||||
path = root / name
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(text, encoding="utf-8")
|
||||
|
||||
|
||||
def pairs(root: Path):
|
||||
files = sorted(path for path in root.rglob("*") if path.is_file())
|
||||
entries, warnings = scan_files(files, root, min_lines=1, min_nodes=1)
|
||||
assert warnings == []
|
||||
return find_duplicates(entries, threshold=0.99)
|
||||
|
||||
|
||||
SWIFT_LEFT = """\
|
||||
func alpha(xs: [Int]) -> [Int] {
|
||||
let ys = filter(xs, odd)
|
||||
return map(ys, inc)
|
||||
}
|
||||
"""
|
||||
|
||||
SWIFT_RIGHT = """\
|
||||
func beta(items: [Int]) -> [Int] {
|
||||
let kept = filter(items, even)
|
||||
return map(kept, dec)
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
def test_renamed_locals_in_swift_functions_match(tmp_path):
|
||||
write_source(tmp_path, "left.swift", SWIFT_LEFT)
|
||||
write_source(tmp_path, "right.swift", SWIFT_RIGHT)
|
||||
found = pairs(tmp_path)
|
||||
assert [(item.language, item.left.file, item.right.file, item.score) for item in found] == [
|
||||
("swift", "left.swift", "right.swift", 1.0)
|
||||
]
|
||||
Reference in New Issue
Block a user