| 1 |
"""Test cases for capsule dependency analysis.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import os.path |
| 6 |
|
| 7 |
from mypy.errors import CompileError |
| 8 |
from mypy.test.config import test_temp_dir |
| 9 |
from mypy.test.data import DataDrivenTestCase |
| 10 |
from mypyc.analysis.capsule_deps import find_class_dependencies, find_implicit_op_dependencies |
| 11 |
from mypyc.options import CompilerOptions |
| 12 |
from mypyc.test.testutil import ( |
| 13 |
ICODE_GEN_BUILTINS, |
| 14 |
MypycDataSuite, |
| 15 |
assert_test_output, |
| 16 |
build_ir_for_single_file2, |
| 17 |
infer_ir_build_options_from_test_name, |
| 18 |
use_custom_builtins, |
| 19 |
) |
| 20 |
from mypyc.transform.lower import lower_ir |
| 21 |
|
| 22 |
files = ["capsule-deps.test"] |
| 23 |
|
| 24 |
|
| 25 |
class TestCapsuleDeps(MypycDataSuite): |
| 26 |
files = files |
| 27 |
base_path = test_temp_dir |
| 28 |
|
| 29 |
def run_case(self, testcase: DataDrivenTestCase) -> None: |
| 30 |
options = infer_ir_build_options_from_test_name(testcase.name) |
| 31 |
if options is None: |
| 32 |
# Skipped test case |
| 33 |
return |
| 34 |
with use_custom_builtins(os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase): |
| 35 |
try: |
| 36 |
module_ir, _, _, _ = build_ir_for_single_file2(testcase.input, options) |
| 37 |
except CompileError as e: |
| 38 |
actual = e.messages |
| 39 |
else: |
| 40 |
all_deps: set[str] = set() |
| 41 |
for fn in module_ir.functions: |
| 42 |
compiler_options = CompilerOptions() |
| 43 |
lower_ir(fn, compiler_options) |
| 44 |
deps = find_implicit_op_dependencies(fn) |
| 45 |
if deps: |
| 46 |
for dep in deps: |
| 47 |
all_deps.add(repr(dep)) |
| 48 |
for cl in module_ir.classes: |
| 49 |
deps = find_class_dependencies(cl) |
| 50 |
if deps: |
| 51 |
for dep in deps: |
| 52 |
all_deps.add(repr(dep)) |
| 53 |
actual = sorted(all_deps) if all_deps else ["No deps"] |
| 54 |
|
| 55 |
assert_test_output(testcase, actual, "Invalid test output", testcase.output) |