introspection
This commit is contained in:
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
0
tests/introspection/__init__.py
Normal file
0
tests/introspection/__init__.py
Normal file
18
tests/introspection/conftest.py
Normal file
18
tests/introspection/conftest.py
Normal file
@@ -0,0 +1,18 @@
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp_package(tmp_path: Path):
|
||||
"""
|
||||
Creates a temporary Python package and adds it to sys.path.
|
||||
"""
|
||||
pkg = tmp_path / "testpkg"
|
||||
pkg.mkdir()
|
||||
(pkg / "__init__.py").write_text('"""Test package."""\n')
|
||||
|
||||
sys.path.insert(0, str(tmp_path))
|
||||
yield pkg
|
||||
sys.path.remove(str(tmp_path))
|
||||
15
tests/introspection/test_alias_safety.py
Normal file
15
tests/introspection/test_alias_safety.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from docforge import GriffeLoader
|
||||
|
||||
|
||||
def test_alias_does_not_crash(temp_package):
|
||||
(temp_package / "alias.py").write_text(
|
||||
'''from typing import List
|
||||
Alias = List[int]
|
||||
'''
|
||||
)
|
||||
|
||||
loader = GriffeLoader()
|
||||
project = loader.load_project(["testpkg.alias"])
|
||||
|
||||
module = project.get_module("testpkg.alias")
|
||||
assert "Alias" in module.members
|
||||
26
tests/introspection/test_classes_and_methods.py
Normal file
26
tests/introspection/test_classes_and_methods.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from docforge import GriffeLoader
|
||||
|
||||
|
||||
def test_class_and_methods(temp_package):
|
||||
(temp_package / "cls.py").write_text(
|
||||
'''class MyClass:
|
||||
"""Class doc."""
|
||||
|
||||
def method(self, x: int) -> int:
|
||||
"""Method doc."""
|
||||
return x
|
||||
'''
|
||||
)
|
||||
|
||||
loader = GriffeLoader()
|
||||
project = loader.load_project(["testpkg.cls"])
|
||||
module = project.get_module("testpkg.cls")
|
||||
|
||||
cls = module.get_object("MyClass")
|
||||
assert cls.kind == "class"
|
||||
assert cls.docstring == "Class doc."
|
||||
assert "method" in cls.members
|
||||
|
||||
method = cls.get_member("method")
|
||||
assert method.kind in {"method", "function"}
|
||||
assert method.signature is not None
|
||||
20
tests/introspection/test_functions_and_signatures.py
Normal file
20
tests/introspection/test_functions_and_signatures.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from docforge import GriffeLoader
|
||||
|
||||
|
||||
def test_function_signature(temp_package):
|
||||
(temp_package / "fn.py").write_text(
|
||||
'''def add(a: int, b: int = 1) -> int:
|
||||
"""Adds numbers."""
|
||||
return a + b
|
||||
'''
|
||||
)
|
||||
|
||||
loader = GriffeLoader()
|
||||
project = loader.load_project(["testpkg.fn"])
|
||||
module = project.get_module("testpkg.fn")
|
||||
|
||||
fn = module.get_object("add")
|
||||
assert fn.kind == "function"
|
||||
assert fn.signature is not None
|
||||
assert "a" in fn.signature
|
||||
assert "b" in fn.signature
|
||||
12
tests/introspection/test_import_failures.py
Normal file
12
tests/introspection/test_import_failures.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from docforge import GriffeLoader
|
||||
|
||||
|
||||
def test_import_failure_does_not_crash():
|
||||
loader = GriffeLoader()
|
||||
|
||||
project = loader.load_project(
|
||||
["nonexistent.module", "sys"]
|
||||
)
|
||||
|
||||
# sys should still load
|
||||
assert "sys" in project.modules
|
||||
14
tests/introspection/test_missing_docstrings.py
Normal file
14
tests/introspection/test_missing_docstrings.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from docforge import GriffeLoader
|
||||
|
||||
|
||||
def test_missing_docstrings(temp_package):
|
||||
(temp_package / "nodoc.py").write_text(
|
||||
'''def f(): pass'''
|
||||
)
|
||||
|
||||
loader = GriffeLoader()
|
||||
project = loader.load_project(["testpkg.nodoc"])
|
||||
module = project.get_module("testpkg.nodoc")
|
||||
|
||||
fn = module.get_object("f")
|
||||
assert fn.docstring is None
|
||||
12
tests/introspection/test_multiple_modules.py
Normal file
12
tests/introspection/test_multiple_modules.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from docforge import GriffeLoader
|
||||
|
||||
|
||||
def test_load_multiple_modules(temp_package):
|
||||
(temp_package / "a.py").write_text('"""A."""\n')
|
||||
(temp_package / "b.py").write_text('"""B."""\n')
|
||||
|
||||
loader = GriffeLoader()
|
||||
project = loader.load_project(["testpkg.a", "testpkg.b"])
|
||||
|
||||
assert len(project.modules) == 2
|
||||
assert set(project.get_module_list()) == {"testpkg.a", "testpkg.b"}
|
||||
16
tests/introspection/test_private_members.py
Normal file
16
tests/introspection/test_private_members.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from docforge import GriffeLoader
|
||||
|
||||
|
||||
def test_private_members_excluded(temp_package):
|
||||
(temp_package / "priv.py").write_text(
|
||||
'''def _hidden(): pass
|
||||
def visible(): pass
|
||||
'''
|
||||
)
|
||||
|
||||
loader = GriffeLoader()
|
||||
project = loader.load_project(["testpkg.priv"])
|
||||
module = project.get_module("testpkg.priv")
|
||||
|
||||
assert "visible" in module.members
|
||||
assert "_hidden" not in module.members
|
||||
21
tests/introspection/test_single_module.py
Normal file
21
tests/introspection/test_single_module.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from docforge import GriffeLoader
|
||||
|
||||
|
||||
def test_load_single_module(temp_package):
|
||||
(temp_package / "mod.py").write_text(
|
||||
'''"""Module docstring."""\n
|
||||
def foo():
|
||||
"""Foo docstring."""
|
||||
pass
|
||||
'''
|
||||
)
|
||||
|
||||
loader = GriffeLoader()
|
||||
project = loader.load_project(["testpkg.mod"])
|
||||
|
||||
assert project.name == "testpkg"
|
||||
assert "testpkg.mod" in project.modules
|
||||
|
||||
module = project.get_module("testpkg.mod")
|
||||
assert module.docstring == "Module docstring."
|
||||
assert "foo" in module.members
|
||||
Reference in New Issue
Block a user