Covers both repaired chains directly: a module and a class pair
each keep both of their own members, nothing from the second
dropped. A control confirms a lone module or class with no chain
still resolves to its own name and line.
(cherry picked from commit ddf32c80e4)
291 lines
9.2 KiB
Python
291 lines
9.2 KiB
Python
"""Tests for the OCaml extractor (graphify/extractors/ocaml.py)."""
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
pytest.importorskip("tree_sitter_ocaml")
|
|
|
|
from graphify.extract import extract_ocaml
|
|
|
|
|
|
def _write(tmp_path: Path, name: str, body: str) -> Path:
|
|
p = tmp_path / name
|
|
p.write_text(body, encoding="utf-8")
|
|
return p
|
|
|
|
|
|
def _labels(r) -> set[str]:
|
|
return {n["label"] for n in r["nodes"]}
|
|
|
|
|
|
def _rel_pairs(r, relation: str) -> set[tuple[str, str]]:
|
|
lab = {n["id"]: n["label"] for n in r["nodes"]}
|
|
return {
|
|
(lab.get(e["source"], e["source"]), lab.get(e["target"], e["target"]))
|
|
for e in r["edges"]
|
|
if e["relation"] == relation
|
|
}
|
|
|
|
|
|
IMPL = """\
|
|
open Stdlib
|
|
|
|
module Shapes = struct
|
|
type shape = Circle | Square | Triangle
|
|
|
|
let pi = 3.14159
|
|
|
|
let area_of radius =
|
|
let squared = radius *. radius in
|
|
pi *. squared
|
|
|
|
let describe r =
|
|
let a = area_of r in
|
|
print_float a
|
|
end
|
|
|
|
let main () =
|
|
let a = Shapes.area_of 2.0 in
|
|
print_float a
|
|
"""
|
|
|
|
|
|
def test_impl_defines_module_values_and_types(tmp_path):
|
|
r = extract_ocaml(_write(tmp_path, "shapes.ml", IMPL))
|
|
assert "error" not in r
|
|
labels = _labels(r)
|
|
# module, values/functions, type, variant constructors
|
|
assert {"Shapes", "pi", "area_of", "describe", "main", "shape"} <= labels
|
|
assert {"Circle", "Square", "Triangle"} <= labels
|
|
|
|
|
|
def test_impl_containment_and_defines(tmp_path):
|
|
r = extract_ocaml(_write(tmp_path, "shapes.ml", IMPL))
|
|
# file defines the top-level module and top-level `main`
|
|
defines = _rel_pairs(r, "defines")
|
|
assert ("shapes.ml", "Shapes") in defines
|
|
assert ("shapes.ml", "main") in defines
|
|
# module contains its members
|
|
contains = _rel_pairs(r, "contains")
|
|
assert ("Shapes", "area_of") in contains
|
|
assert ("Shapes", "shape") in contains
|
|
# variant constructors are contained by their type
|
|
assert ("shape", "Circle") in contains
|
|
|
|
|
|
def test_impl_calls_resolve_same_file(tmp_path):
|
|
r = extract_ocaml(_write(tmp_path, "shapes.ml", IMPL))
|
|
calls = _rel_pairs(r, "calls")
|
|
# describe -> area_of is a same-file, unambiguous resolution
|
|
assert ("describe", "area_of") in calls
|
|
# a qualified call `Shapes.area_of` resolves to the value `area_of`, NOT the
|
|
# module qualifier `Shapes`.
|
|
assert ("main", "area_of") in calls
|
|
assert ("main", "Shapes") not in calls
|
|
|
|
|
|
def test_qualified_external_call_does_not_bind_to_local_same_name(tmp_path):
|
|
"""A qualified call `M.f` to an EXTERNAL module (not defined in this file)
|
|
must not bind to a same-named local `f` — that would be a false edge and,
|
|
when the caller is that local `f`, a `f -> f` self-loop. It is kept as a
|
|
distinct external target labelled by the full path (e.g. Hardcaml's
|
|
`Reg_spec.create` next to a local `let create`)."""
|
|
src = (
|
|
"let create x =\n"
|
|
" let spec = Reg_spec.create x in\n" # external, same bare name as local
|
|
" spec\n"
|
|
"let run () = Scope.create ()\n" # external, same bare name
|
|
)
|
|
r = extract_ocaml(_write(tmp_path, "counter.ml", src))
|
|
calls = _rel_pairs(r, "calls")
|
|
assert ("create", "create") not in calls # no self-loop
|
|
assert ("create", "Reg_spec.create") in calls # kept distinct
|
|
assert ("run", "Scope.create") in calls
|
|
assert ("run", "create") not in calls # not the local create
|
|
# no dangling edges introduced by the qualified external stubs
|
|
ids = {n["id"] for n in r["nodes"]}
|
|
assert all(e["source"] in ids and e["target"] in ids for e in r["edges"])
|
|
|
|
|
|
def test_qualified_call_into_local_module_resolves(tmp_path):
|
|
"""A qualified call whose qualifier IS a module defined in this file still
|
|
resolves to the local definition."""
|
|
src = (
|
|
"module M = struct\n"
|
|
" let helper x = x\n"
|
|
"end\n"
|
|
"let run () = M.helper 1\n"
|
|
)
|
|
r = extract_ocaml(_write(tmp_path, "m.ml", src))
|
|
calls = _rel_pairs(r, "calls")
|
|
assert ("run", "helper") in calls
|
|
assert not any(t == "M.helper" for _, t in calls) # not left as an external stub
|
|
|
|
|
|
def test_impl_open_emits_import(tmp_path):
|
|
r = extract_ocaml(_write(tmp_path, "shapes.ml", IMPL))
|
|
imports = _rel_pairs(r, "imports_from")
|
|
assert ("shapes.ml", "Stdlib") in imports
|
|
|
|
|
|
def test_open_stub_is_sourceless(tmp_path):
|
|
# An `open`ed external module must be a SOURCELESS stub so the corpus rewire
|
|
# can collapse/prune it without baking this file's path into the id (#1402).
|
|
r = extract_ocaml(_write(tmp_path, "shapes.ml", IMPL))
|
|
stubs = [n for n in r["nodes"] if n["label"] == "Stdlib"]
|
|
assert stubs and all(n["source_file"] == "" for n in stubs)
|
|
# origin_file is an internal rewire hint, never a real source path.
|
|
assert all(n.get("source_location") == "" for n in stubs)
|
|
|
|
|
|
INTERFACE = """\
|
|
open Base
|
|
|
|
module type Store = sig
|
|
type t
|
|
val make : int -> t
|
|
val size : t -> int
|
|
end
|
|
|
|
type color = Red | Green | Blue
|
|
|
|
val hello : string -> unit
|
|
"""
|
|
|
|
|
|
def test_interface_defines_signatures(tmp_path):
|
|
r = extract_ocaml(_write(tmp_path, "store.mli", INTERFACE))
|
|
assert "error" not in r
|
|
labels = _labels(r)
|
|
assert {"Store", "make", "size", "color", "hello"} <= labels
|
|
assert {"Red", "Green", "Blue"} <= labels
|
|
# interfaces have no expression bodies -> no calls
|
|
assert not [e for e in r["edges"] if e["relation"] == "calls"]
|
|
|
|
|
|
def test_no_dangling_edges(tmp_path):
|
|
r = extract_ocaml(_write(tmp_path, "shapes.ml", IMPL))
|
|
ids = {n["id"] for n in r["nodes"]}
|
|
for e in r["edges"]:
|
|
assert e["source"] in ids, e
|
|
assert e["target"] in ids, e
|
|
|
|
|
|
def test_missing_file_returns_error(tmp_path):
|
|
r = extract_ocaml(tmp_path / "nope.ml")
|
|
assert r["nodes"] == [] and r["edges"] == []
|
|
assert "error" in r
|
|
|
|
|
|
CLASSES = """\
|
|
let log s = print_string s
|
|
|
|
class counter = object
|
|
val mutable count = 0
|
|
method get = count
|
|
method incr = count <- count + 1
|
|
end
|
|
|
|
class widget = object
|
|
method render = log "rendered"
|
|
end
|
|
"""
|
|
|
|
|
|
def test_impl_extracts_classes_methods_and_instance_vars(tmp_path):
|
|
r = extract_ocaml(_write(tmp_path, "ui.ml", CLASSES))
|
|
assert "error" not in r
|
|
labels = _labels(r)
|
|
# the classes, their methods, and their instance variables are all present
|
|
assert {"counter", "get", "incr", "count", "widget", "render"} <= labels
|
|
# methods hang off their class via the "method" relation
|
|
method = _rel_pairs(r, "method")
|
|
assert {("counter", "get"), ("counter", "incr"),
|
|
("widget", "render")} <= method
|
|
# an instance `val` is a field, so it uses "contains"
|
|
contains = _rel_pairs(r, "contains")
|
|
assert ("counter", "count") in contains
|
|
|
|
|
|
def test_class_method_body_calls_are_attributed_to_the_method(tmp_path):
|
|
r = extract_ocaml(_write(tmp_path, "ui.ml", CLASSES))
|
|
# `method render = log ...` calls the top-level `log`, credited to render
|
|
assert ("render", "log") in _rel_pairs(r, "calls")
|
|
|
|
|
|
CLASS_INTERFACE = """\
|
|
class counter : object
|
|
val count : int
|
|
method get : int
|
|
method incr : unit
|
|
end
|
|
"""
|
|
|
|
|
|
def test_interface_extracts_class_signatures(tmp_path):
|
|
r = extract_ocaml(_write(tmp_path, "counter.mli", CLASS_INTERFACE))
|
|
assert "error" not in r
|
|
assert {"counter", "get", "incr", "count"} <= _labels(r)
|
|
method = _rel_pairs(r, "method")
|
|
assert {("counter", "get"), ("counter", "incr")} <= method
|
|
contains = _rel_pairs(r, "contains")
|
|
assert ("counter", "count") in contains
|
|
# a signature has no expression body -> no calls
|
|
assert not [e for e in r["edges"] if e["relation"] == "calls"]
|
|
|
|
|
|
AND_CHAINED_MODULES = """\
|
|
module Alpha = struct
|
|
let f x = x
|
|
end
|
|
and Beta = struct
|
|
let g x = x
|
|
end
|
|
"""
|
|
|
|
|
|
def test_and_chained_modules_are_not_dropped(tmp_path):
|
|
"""`module A = ... and B = ...` nests every binding under ONE
|
|
module_definition node; taking only the first silently dropped every
|
|
module after it, along with everything defined inside it."""
|
|
r = extract_ocaml(_write(tmp_path, "pair.ml", AND_CHAINED_MODULES))
|
|
assert "error" not in r
|
|
assert {"Alpha", "f", "Beta", "g"} <= _labels(r)
|
|
defines = _rel_pairs(r, "defines")
|
|
assert {("pair.ml", "Alpha"), ("pair.ml", "Beta")} <= defines
|
|
contains = _rel_pairs(r, "contains")
|
|
assert {("Alpha", "f"), ("Beta", "g")} <= contains
|
|
|
|
|
|
AND_CHAINED_CLASSES = """\
|
|
class alpha = object
|
|
method m = 1
|
|
end
|
|
and beta = object
|
|
method n = 2
|
|
end
|
|
"""
|
|
|
|
|
|
def test_and_chained_classes_are_not_dropped(tmp_path):
|
|
"""`class a = ... and b = ...` (mutually recursive classes) nests every
|
|
binding under ONE class_definition node the same way modules do; taking
|
|
only the first silently dropped every class after it."""
|
|
r = extract_ocaml(_write(tmp_path, "pair.ml", AND_CHAINED_CLASSES))
|
|
assert "error" not in r
|
|
assert {"alpha", "m", "beta", "n"} <= _labels(r)
|
|
method = _rel_pairs(r, "method")
|
|
assert {("alpha", "m"), ("beta", "n")} <= method
|
|
|
|
|
|
def test_single_module_and_class_are_unaffected(tmp_path):
|
|
"""Control: a lone module/class (no `and` chain) must still resolve to
|
|
its own name and line, not the file's or each other's."""
|
|
r = extract_ocaml(_write(tmp_path, "solo.ml", "module Solo = struct\n let v = 1\nend\n"))
|
|
assert "error" not in r
|
|
assert {"Solo", "v"} <= _labels(r)
|
|
node = next(n for n in r["nodes"] if n["label"] == "Solo")
|
|
assert node["source_location"] == "L1"
|