test/stubgen: add tests for schema transpiling
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
"""Tests for capnp_stubgen.emitter."""
|
||||
|
||||
from capnp_stubgen.emitter import Emitter
|
||||
|
||||
|
||||
class TestEmitterBasic:
|
||||
def test_header(self):
|
||||
e = Emitter("test.capnp")
|
||||
content = e.render()
|
||||
assert '"""Auto-generated type stub for test.capnp."""' in content
|
||||
assert "from __future__ import annotations" in content
|
||||
|
||||
def test_class_definition(self):
|
||||
e = Emitter("test.capnp")
|
||||
e.begin_class("Foo")
|
||||
e.add_field("bar", "int")
|
||||
e.end_class()
|
||||
content = e.render()
|
||||
assert "class Foo:" in content
|
||||
assert " bar: int" in content
|
||||
|
||||
def test_type_alias(self):
|
||||
e = Emitter("test.capnp")
|
||||
e.add_typing_import("Literal")
|
||||
e.add_type_alias("Gender", 'Literal["male", "female"]')
|
||||
content = e.render()
|
||||
assert 'Gender = Literal["male", "female"]' in content
|
||||
assert "from typing import Literal" in content
|
||||
|
||||
def test_method(self):
|
||||
e = Emitter("test.capnp")
|
||||
e.add_method("foo", params=["self", "x: int"], return_type="str")
|
||||
content = e.render()
|
||||
assert "def foo(self, x: int) -> str: ..." in content
|
||||
|
||||
def test_static_method(self):
|
||||
e = Emitter("test.capnp")
|
||||
e.add_static_method("create", params=["x: int"], return_type="Foo")
|
||||
content = e.render()
|
||||
assert "@staticmethod" in content
|
||||
assert "def create(x: int) -> Foo: ..." in content
|
||||
|
||||
def test_imports_grouped(self):
|
||||
e = Emitter("test.capnp")
|
||||
e.add_typing_import("Literal")
|
||||
e.add_typing_import("Sequence", "collections.abc")
|
||||
e.add_typing_import("Iterator", "collections.abc")
|
||||
content = e.render()
|
||||
# typing imports grouped
|
||||
assert "from typing import Literal" in content
|
||||
# collections.abc imports grouped
|
||||
assert "from collections.abc import Iterator, Sequence" in content
|
||||
|
||||
def test_nested_classes(self):
|
||||
e = Emitter("test.capnp")
|
||||
e.begin_class("Outer")
|
||||
e.add_field("x", "int")
|
||||
e.begin_class("Inner")
|
||||
e.add_field("y", "str")
|
||||
e.end_class()
|
||||
e.end_class()
|
||||
content = e.render()
|
||||
assert "class Outer:" in content
|
||||
assert " x: int" in content
|
||||
assert " class Inner:" in content
|
||||
assert " y: str" in content
|
||||
|
||||
def test_decorators(self):
|
||||
e = Emitter("test.capnp")
|
||||
e.add_decorator("overload")
|
||||
e.add_method("foo", params=["x: int"], return_type="str")
|
||||
content = e.render()
|
||||
assert "@overload" in content
|
||||
assert "def foo(x: int) -> str: ..." in content
|
||||
Reference in New Issue
Block a user