119 lines
3.6 KiB
Python
119 lines
3.6 KiB
Python
"""Verify generated .pyi stubs are consistent with pycapnp runtime.
|
|
|
|
Test file lives alongside the ``.capnp`` schemas + generated ``.pyi``
|
|
stubs. pycapnp's import hook handles runtime loading.
|
|
basedpyright type-checks this file together with the project.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Ensure this directory is importable
|
|
_HERE = Path(__file__).parent
|
|
if str(_HERE) not in sys.path:
|
|
sys.path.insert(0, str(_HERE))
|
|
|
|
import capnp # pyright: ignore[reportMissingTypeStubs] # activates import hook
|
|
import test_generics_capnp
|
|
import test_interface_capnp
|
|
import test_nested_capnp
|
|
import test_simple_capnp
|
|
|
|
|
|
class TestSimpleStub:
|
|
"""test_simple.capnp — basic struct."""
|
|
|
|
def test_new_message(self) -> None:
|
|
p = test_simple_capnp.Person.new_message(
|
|
name="Alice", age=30, email="alice@example.com"
|
|
)
|
|
assert p.name == "Alice"
|
|
assert p.age == 30
|
|
|
|
def test_field_assignment(self) -> None:
|
|
p = test_simple_capnp.Person.new_message()
|
|
p.name = "Bob"
|
|
p.age = 25
|
|
assert p.name == "Bob"
|
|
|
|
def test_to_dict(self) -> None:
|
|
p = test_simple_capnp.Person.new_message(name="Carol", age=28)
|
|
d = p.to_dict()
|
|
assert d["name"] == "Carol"
|
|
|
|
def test_from_dict(self) -> None:
|
|
p = test_simple_capnp.Person.new_message()
|
|
_ = p.from_dict({"name": "Dave", "age": 35})
|
|
assert p.name == "Dave"
|
|
|
|
def test_serialize_roundtrip(self) -> None:
|
|
p = test_simple_capnp.Person.new_message(name="Eve", age=22)
|
|
data = p.to_bytes()
|
|
p2 = test_simple_capnp.Person.from_bytes(data)
|
|
assert p2 is not None
|
|
|
|
|
|
class TestNestedStub:
|
|
"""test_nested.capnp — nested types + lists."""
|
|
|
|
def test_nested_message(self) -> None:
|
|
ab = test_nested_capnp.AddressBook.new_message()
|
|
assert ab.people is not None
|
|
|
|
|
|
class TestGenericsStub:
|
|
"""test_generics.capnp — generic structs."""
|
|
|
|
def test_holder_loads(self) -> None:
|
|
c = test_generics_capnp.Container.new_message()
|
|
assert c is not None
|
|
|
|
|
|
class TestInterfaceStub:
|
|
"""test_interface.capnp — interfaces + method params."""
|
|
|
|
def test_calcserver_loads(self) -> None:
|
|
cs = test_interface_capnp.CalcServer.new_message()
|
|
assert cs is not None
|
|
|
|
|
|
class TestAddressBookStub:
|
|
"""addressbook.capnp — classic example."""
|
|
|
|
def test_person_new_message(self) -> None:
|
|
import addressbook_capnp
|
|
p = addressbook_capnp.Person.new_message(
|
|
name="Alice", email="alice@example.com"
|
|
)
|
|
assert p.name == "Alice"
|
|
|
|
def test_addressbook_new_message(self) -> None:
|
|
import addressbook_capnp
|
|
ab = addressbook_capnp.AddressBook.new_message()
|
|
assert ab.people is not None
|
|
|
|
|
|
class TestDummyStub:
|
|
"""dummy.capnp — comprehensive test schema."""
|
|
|
|
def test_all_types_loads(self) -> None:
|
|
from pathlib import Path
|
|
parser = capnp.SchemaParser() # pyright: ignore[reportAttributeAccessIssue]
|
|
m = parser.load(str(Path(__file__).parent / "dummy.capnp"))
|
|
msg = m.TestAllTypes.new_message(textField="hello", int32Field=42)
|
|
assert msg.textField == "hello"
|
|
|
|
def test_unions(self) -> None:
|
|
from pathlib import Path
|
|
parser = capnp.SchemaParser() # pyright: ignore[reportAttributeAccessIssue]
|
|
m = parser.load(str(Path(__file__).parent / "dummy.capnp"))
|
|
u = m.TestUnion.new_message()
|
|
assert u is not None
|
|
|
|
|
|
# Note: multi/consumer.capnp cross-file import test skipped —
|
|
# pycapnp's SchemaParser crashes when resolving annotation imports
|
|
# across schema files. The .pyi stubs are still generated and kept.
|