1
0
mirror of https://github.com/exaloop/codon.git synced 2025-06-03 15:03:52 +08:00
codon/test/python/pybridge.codon
A. R. Shajii 753151157d
Py interop fix (#23)
* Fix incref/decref

* Fixes

* Fix set __to_py__

* Add more Python conversion tests

* clang-format

* Make from_py/to_py use cobj instead of pyobj

* Remove unneeded increfs

* Fix cython

* Ignore __init_test__ in doc generation

* Add exception check

Co-authored-by: Ishak Numanagić <ishak.numanagic@gmail.com>
Co-authored-by: Ibrahim Numanagić <ibrahimpasa@gmail.com>
2022-04-07 00:33:49 -04:00

67 lines
1.7 KiB
Python

@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,
{'ACGT'},
[['abc'], ['1.1', '2.2'], list[str]()]
)
assert T.__from_py__(t.p) == ({'a': 3.14, 'b': 2.123}, (222, 3.14))
test_conversions()
@test
def test_pythrow():
from python import mymodule.throw_exc() -> void as te
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()
@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(())