2021-09-27 14:02:44 -04:00
|
|
|
@test
|
|
|
|
def test_basic():
|
|
|
|
from python import mymodule
|
|
|
|
assert str(mymodule.multiply(3, 4)) == '12'
|
|
|
|
test_basic()
|
|
|
|
|
|
|
|
@test
|
|
|
|
def test_pybridge():
|
|
|
|
@python
|
|
|
|
def test_pydef(n: int) -> str:
|
|
|
|
return ''.join(map(str, range(n)))
|
|
|
|
assert test_pydef(5) == '01234'
|
|
|
|
test_pybridge()
|
|
|
|
|
|
|
|
from python import mymodule
|
|
|
|
@test
|
|
|
|
def test_conversions():
|
|
|
|
T = tuple[dict[str,float],tuple[int,float]]
|
|
|
|
t = mymodule.print_args(
|
|
|
|
(4,5),
|
|
|
|
{'a': 3.14, 'b': 2.123},
|
|
|
|
True,
|
2021-09-30 15:04:26 -04:00
|
|
|
{'ACGT'},
|
2021-09-27 14:02:44 -04:00
|
|
|
[['abc'], ['1.1', '2.2'], list[str]()]
|
|
|
|
)
|
2022-04-07 00:33:49 -04:00
|
|
|
assert T.__from_py__(t.p) == ({'a': 3.14, 'b': 2.123}, (222, 3.14))
|
2021-09-27 14:02:44 -04:00
|
|
|
test_conversions()
|
|
|
|
|
|
|
|
@test
|
|
|
|
def test_pythrow():
|
2022-07-26 13:06:00 -07:00
|
|
|
from python import mymodule.throw_exc() -> None as te
|
2021-09-27 14:02:44 -04:00
|
|
|
try:
|
|
|
|
te()
|
|
|
|
except PyError as e:
|
|
|
|
assert e.pytype + ":" + e.message == "ValueError:foo"
|
|
|
|
return
|
|
|
|
assert False
|
|
|
|
test_pythrow()
|
|
|
|
|
|
|
|
@test
|
|
|
|
def test_pyargs():
|
|
|
|
from python import mymodule
|
|
|
|
assert str(mymodule.print_args_var(1, 2, 3)) == "a=1, b=2, c=3, args=(), kwargs={}"
|
|
|
|
assert str(mymodule.print_args_var(1, z=5, b=2)) == "a=1, b=2, c=1, args=(), kwargs={'z': 5}"
|
|
|
|
assert str(mymodule.print_args_var(1, *(1,2,3,4,5), z=5)) == "a=1, b=1, c=2, args=(3, 4, 5), kwargs={'z': 5}"
|
|
|
|
test_pyargs()
|
2022-04-07 00:33:49 -04:00
|
|
|
|
|
|
|
@test
|
|
|
|
def test_roundtrip(x: T, T: type):
|
|
|
|
assert T.__from_py__(x.__to_py__()) == x
|
|
|
|
|
|
|
|
test_roundtrip(42)
|
|
|
|
test_roundtrip(3.14)
|
|
|
|
test_roundtrip(True)
|
|
|
|
test_roundtrip(False)
|
|
|
|
test_roundtrip(byte(99))
|
|
|
|
test_roundtrip('hello world')
|
|
|
|
test_roundtrip('')
|
|
|
|
test_roundtrip(List[int]())
|
|
|
|
test_roundtrip([11, 22, 33])
|
|
|
|
test_roundtrip(Set[int]())
|
|
|
|
test_roundtrip({11, 22, 33})
|
|
|
|
test_roundtrip(Dict[str,int]())
|
|
|
|
test_roundtrip({'aa': 11, 'bb': 22, 'cc': 33})
|
|
|
|
test_roundtrip((11, 1.1, '11', [1, 1], {1}, {1: 1}))
|
|
|
|
test_roundtrip(())
|