127 lines
3.9 KiB
Python
127 lines
3.9 KiB
Python
"""Tests for capnp_stubgen.models."""
|
|
|
|
from capnp_stubgen.models import PythonType, TypeInfo, TypeRegistry
|
|
|
|
|
|
class TestPythonType:
|
|
def test_simple_render(self):
|
|
assert PythonType(name="int").render() == "int"
|
|
|
|
def test_none_render(self):
|
|
assert PythonType(name="None").render() == "None"
|
|
|
|
def test_qualified_render(self):
|
|
pt = PythonType(name="Person", qualifiers=["AddressBook"])
|
|
assert pt.render() == "AddressBook.Person"
|
|
|
|
def test_params_render(self):
|
|
inner = PythonType(name="Person")
|
|
pt = PythonType(name="Sequence", params=[inner])
|
|
assert pt.render() == "Sequence[Person]"
|
|
|
|
def test_nested_params(self):
|
|
inner = PythonType(name="int")
|
|
mid = PythonType(name="Sequence", params=[inner])
|
|
outer = PythonType(name="Sequence", params=[mid])
|
|
assert outer.render() == "Sequence[Sequence[int]]"
|
|
|
|
def test_with_reader_suffix(self):
|
|
pt = PythonType(name="Person")
|
|
r = pt.with_reader_suffix()
|
|
assert r.name == "PersonReader"
|
|
|
|
def test_with_builder_suffix(self):
|
|
pt = PythonType(name="Person")
|
|
b = pt.with_builder_suffix()
|
|
assert b.name == "PersonBuilder"
|
|
|
|
|
|
class TestTypeInfo:
|
|
def test_basic(self):
|
|
info = TypeInfo(
|
|
type_id=42,
|
|
name="Foo",
|
|
scoped_name="MyFile.Foo",
|
|
node={"struct": {}},
|
|
schema=None,
|
|
kind="struct",
|
|
)
|
|
assert info.type_id == 42
|
|
assert info.name == "Foo"
|
|
assert info.kind == "struct"
|
|
assert info.generic_params == []
|
|
assert info.parent_type_id is None
|
|
|
|
def test_with_generics(self):
|
|
info = TypeInfo(
|
|
type_id=1,
|
|
name="Map",
|
|
scoped_name="Map",
|
|
node={},
|
|
schema=None,
|
|
kind="struct",
|
|
generic_params=["Key", "Value"],
|
|
parent_type_id=5,
|
|
)
|
|
assert info.generic_params == ["Key", "Value"]
|
|
assert info.parent_type_id == 5
|
|
|
|
|
|
class TestTypeRegistry:
|
|
def test_register_and_get(self):
|
|
reg = TypeRegistry()
|
|
info = TypeInfo(1, "Foo", "Foo", {}, None, "struct")
|
|
reg.register(info)
|
|
assert reg.get(1) is info
|
|
|
|
def test_get_missing(self):
|
|
reg = TypeRegistry()
|
|
assert reg.get(999) is None
|
|
|
|
def test_get_or_raise(self):
|
|
reg = TypeRegistry()
|
|
info = TypeInfo(1, "Foo", "Foo", {}, None, "struct")
|
|
reg.register(info)
|
|
assert reg.get_or_raise(1) is info
|
|
import pytest
|
|
with pytest.raises(KeyError):
|
|
_ = reg.get_or_raise(999)
|
|
|
|
def test_contains(self):
|
|
reg = TypeRegistry()
|
|
info: TypeInfo = TypeInfo(1, "Foo", "Foo", {}, None, "struct")
|
|
reg.register(info)
|
|
assert 1 in reg
|
|
assert 2 not in reg
|
|
|
|
def test_get_children(self):
|
|
reg = TypeRegistry()
|
|
parent = TypeInfo(1, "Parent", "Parent", {}, None, "struct")
|
|
child = TypeInfo(2, "Child", "Parent.Child", {}, None, "struct",
|
|
parent_type_id=1)
|
|
reg.register(parent)
|
|
reg.register(child)
|
|
children = reg.get_children(1)
|
|
assert len(children) == 1
|
|
assert children[0] is child
|
|
|
|
def test_get_top_level_types(self):
|
|
reg = TypeRegistry()
|
|
file_id = 100
|
|
t1 = TypeInfo(1, "A", "A", {}, None, "struct", parent_type_id=file_id)
|
|
t2 = TypeInfo(2, "B", "B", {}, None, "enum", parent_type_id=file_id)
|
|
t3 = TypeInfo(3, "C", "A.C", {}, None, "struct", parent_type_id=1)
|
|
reg.register(t1)
|
|
reg.register(t2)
|
|
reg.register(t3)
|
|
top = reg.get_top_level_types(file_id)
|
|
assert len(top) == 2
|
|
assert t1 in top
|
|
assert t2 in top
|
|
|
|
def test_len(self):
|
|
reg = TypeRegistry()
|
|
assert len(reg) == 0
|
|
reg.register(TypeInfo(1, "A", "A", {}, None, "struct"))
|
|
assert len(reg) == 1
|