Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit 928e4f0

Browse files
committed
Add back missing tests
1 parent 2710192 commit 928e4f0

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

tests/test_module_validation.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
from typing import (
2+
List,
3+
Optional,
4+
Set,
5+
)
6+
7+
import pytest
8+
from betterproto2.plugin.module_validation import ModuleValidator
9+
10+
11+
@pytest.mark.parametrize(
12+
["text", "expected_collisions"],
13+
[
14+
pytest.param(
15+
["import os"],
16+
None,
17+
id="single import",
18+
),
19+
pytest.param(
20+
["import os", "import sys"],
21+
None,
22+
id="multiple imports",
23+
),
24+
pytest.param(
25+
["import os", "import os"],
26+
{"os"},
27+
id="duplicate imports",
28+
),
29+
pytest.param(
30+
["from os import path", "import os"],
31+
None,
32+
id="duplicate imports with alias",
33+
),
34+
pytest.param(
35+
["from os import path", "import os as os_alias"],
36+
None,
37+
id="duplicate imports with alias",
38+
),
39+
pytest.param(
40+
["from os import path", "import os as path"],
41+
{"path"},
42+
id="duplicate imports with alias",
43+
),
44+
pytest.param(
45+
["import os", "class os:"],
46+
{"os"},
47+
id="duplicate import with class",
48+
),
49+
pytest.param(
50+
["import os", "class os:", " pass", "import sys"],
51+
{"os"},
52+
id="duplicate import with class and another",
53+
),
54+
pytest.param(
55+
["def test(): pass", "class test:"],
56+
{"test"},
57+
id="duplicate class and function",
58+
),
59+
pytest.param(
60+
["def test(): pass", "def test(): pass"],
61+
{"test"},
62+
id="duplicate functions",
63+
),
64+
pytest.param(
65+
["def test(): pass", "test = 100"],
66+
{"test"},
67+
id="function and variable",
68+
),
69+
pytest.param(
70+
["def test():", " test = 3"],
71+
None,
72+
id="function and variable in function",
73+
),
74+
pytest.param(
75+
[
76+
"def test(): pass",
77+
"'''",
78+
"def test(): pass",
79+
"'''",
80+
"def test_2(): pass",
81+
],
82+
None,
83+
id="duplicate functions with multiline string",
84+
),
85+
pytest.param(
86+
["def test(): pass", "# def test(): pass"],
87+
None,
88+
id="duplicate functions with comments",
89+
),
90+
pytest.param(
91+
["from test import (", " A", " B", " C", ")"],
92+
None,
93+
id="multiline import",
94+
),
95+
pytest.param(
96+
["from test import (", " A", " B", " C", ")", "from test import A"],
97+
{"A"},
98+
id="multiline import with duplicate",
99+
),
100+
],
101+
)
102+
def test_module_validator(text: List[str], expected_collisions: Optional[Set[str]]):
103+
line_iterator = iter(text)
104+
validator = ModuleValidator(line_iterator)
105+
valid = validator.validate()
106+
if expected_collisions is None:
107+
assert valid
108+
else:
109+
assert set(validator.collisions.keys()) == expected_collisions
110+
assert not valid

tests/test_typing_compiler.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from betterproto2.plugin.typing_compiler import (
2+
DirectImportTypingCompiler,
3+
NoTyping310TypingCompiler,
4+
TypingImportTypingCompiler,
5+
)
6+
7+
8+
def test_direct_import_typing_compiler():
9+
compiler = DirectImportTypingCompiler()
10+
assert compiler.imports() == {}
11+
assert compiler.optional("str") == "Optional[str]"
12+
assert compiler.imports() == {"typing": {"Optional"}}
13+
assert compiler.list("str") == "List[str]"
14+
assert compiler.imports() == {"typing": {"Optional", "List"}}
15+
assert compiler.dict("str", "int") == "Dict[str, int]"
16+
assert compiler.imports() == {"typing": {"Optional", "List", "Dict"}}
17+
assert compiler.union("str", "int") == "Union[str, int]"
18+
assert compiler.imports() == {"typing": {"Optional", "List", "Dict", "Union"}}
19+
assert compiler.iterable("str") == "Iterable[str]"
20+
assert compiler.imports() == {"typing": {"Optional", "List", "Dict", "Union", "Iterable"}}
21+
assert compiler.async_iterable("str") == "AsyncIterable[str]"
22+
assert compiler.imports() == {"typing": {"Optional", "List", "Dict", "Union", "Iterable", "AsyncIterable"}}
23+
assert compiler.async_iterator("str") == "AsyncIterator[str]"
24+
assert compiler.imports() == {
25+
"typing": {
26+
"Optional",
27+
"List",
28+
"Dict",
29+
"Union",
30+
"Iterable",
31+
"AsyncIterable",
32+
"AsyncIterator",
33+
}
34+
}
35+
36+
37+
def test_typing_import_typing_compiler():
38+
compiler = TypingImportTypingCompiler()
39+
assert compiler.imports() == {}
40+
assert compiler.optional("str") == "typing.Optional[str]"
41+
assert compiler.imports() == {"typing": None}
42+
assert compiler.list("str") == "typing.List[str]"
43+
assert compiler.imports() == {"typing": None}
44+
assert compiler.dict("str", "int") == "typing.Dict[str, int]"
45+
assert compiler.imports() == {"typing": None}
46+
assert compiler.union("str", "int") == "typing.Union[str, int]"
47+
assert compiler.imports() == {"typing": None}
48+
assert compiler.iterable("str") == "typing.Iterable[str]"
49+
assert compiler.imports() == {"typing": None}
50+
assert compiler.async_iterable("str") == "typing.AsyncIterable[str]"
51+
assert compiler.imports() == {"typing": None}
52+
assert compiler.async_iterator("str") == "typing.AsyncIterator[str]"
53+
assert compiler.imports() == {"typing": None}
54+
55+
56+
def test_no_typing_311_typing_compiler():
57+
compiler = NoTyping310TypingCompiler()
58+
assert compiler.imports() == {}
59+
assert compiler.optional("str") == "str | None"
60+
assert compiler.imports() == {}
61+
assert compiler.list("str") == "list[str]"
62+
assert compiler.imports() == {}
63+
assert compiler.dict("str", "int") == "dict[str, int]"
64+
assert compiler.imports() == {}
65+
assert compiler.union("str", "int") == "str | int"
66+
assert compiler.imports() == {}
67+
assert compiler.iterable("str") == "Iterable[str]"
68+
assert compiler.async_iterable("str") == "AsyncIterable[str]"
69+
assert compiler.async_iterator("str") == "AsyncIterator[str]"
70+
assert compiler.imports() == {"collections.abc": {"Iterable", "AsyncIterable", "AsyncIterator"}}

0 commit comments

Comments
 (0)