Skip to content

Commit b1ad308

Browse files
authored
move python test source to .py files (#57)
Signed-off-by: karthik2804 <karthik.ganeshram@fermyon.com>
1 parent 8989679 commit b1ad308

9 files changed

Lines changed: 243 additions & 265 deletions

src/test/python_source/app.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import tests
2+
import resource_borrow_export
3+
import resource_aggregates
4+
import resource_alias1
5+
import resource_borrow_in_record
6+
from tests import exports, imports
7+
from tests.imports import resource_borrow_import
8+
from tests.imports import simple_import_and_export
9+
from tests.exports import resource_alias2
10+
from tests.types import Result, Ok
11+
from typing import Tuple, List, Optional
12+
13+
class SimpleExport(exports.SimpleExport):
14+
def foo(self, v: int) -> int:
15+
return v + 3
16+
17+
class SimpleImportAndExport(exports.SimpleImportAndExport):
18+
def foo(self, v: int) -> int:
19+
return simple_import_and_export.foo(v) + 3
20+
21+
class ResourceImportAndExport(exports.ResourceImportAndExport):
22+
pass
23+
24+
class ResourceBorrowExport(exports.ResourceBorrowExport):
25+
def foo(self, v: resource_borrow_export.Thing) -> int:
26+
return v.value + 2
27+
28+
class ResourceWithLists(exports.ResourceWithLists):
29+
pass
30+
31+
class ResourceAggregates(exports.ResourceAggregates):
32+
def foo(
33+
self,
34+
r1: exports.resource_aggregates.R1,
35+
r2: exports.resource_aggregates.R2,
36+
r3: exports.resource_aggregates.R3,
37+
t1: Tuple[resource_aggregates.Thing, exports.resource_aggregates.R1],
38+
t2: Tuple[resource_aggregates.Thing],
39+
v1: exports.resource_aggregates.V1,
40+
v2: exports.resource_aggregates.V2,
41+
l1: List[resource_aggregates.Thing],
42+
l2: List[resource_aggregates.Thing],
43+
o1: Optional[resource_aggregates.Thing],
44+
o2: Optional[resource_aggregates.Thing],
45+
result1: Result[resource_aggregates.Thing, None],
46+
result2: Result[resource_aggregates.Thing, None]
47+
) -> int:
48+
if o1 is None:
49+
host_o1 = None
50+
else:
51+
host_o1 = o1.value
52+
53+
if o2 is None:
54+
host_o2 = None
55+
else:
56+
host_o2 = o2.value
57+
58+
if isinstance(result1, Ok):
59+
host_result1 = Ok(result1.value.value)
60+
else:
61+
host_result1 = result1
62+
63+
if isinstance(result2, Ok):
64+
host_result2 = Ok(result2.value.value)
65+
else:
66+
host_result2 = result2
67+
68+
return imports.resource_aggregates.foo(
69+
imports.resource_aggregates.R1(r1.thing.value),
70+
imports.resource_aggregates.R2(r2.thing.value),
71+
imports.resource_aggregates.R3(r3.thing1.value, r3.thing2.value),
72+
(t1[0].value, imports.resource_aggregates.R1(t1[1].thing.value)),
73+
(t2[0].value,),
74+
imports.resource_aggregates.V1Thing(v1.value.value),
75+
imports.resource_aggregates.V2Thing(v2.value.value),
76+
list(map(lambda x: x.value, l1)),
77+
list(map(lambda x: x.value, l2)),
78+
host_o1,
79+
host_o2,
80+
host_result1,
81+
host_result2
82+
) + 4
83+
84+
class ResourceAlias1(exports.ResourceAlias1):
85+
def a(self, f: exports.resource_alias1.Foo) -> List[resource_alias1.Thing]:
86+
return list(
87+
map(
88+
resource_alias1.wrap_thing,
89+
imports.resource_alias1.a(imports.resource_alias1.Foo(f.thing.value))
90+
)
91+
)
92+
93+
class ResourceAlias2(exports.ResourceAlias2):
94+
def b(self, f: exports.resource_alias2.Foo, g: exports.resource_alias1.Foo) -> List[resource_alias1.Thing]:
95+
return list(
96+
map(
97+
resource_alias1.wrap_thing,
98+
imports.resource_alias2.b(
99+
imports.resource_alias2.Foo(f.thing.value),
100+
exports.resource_alias1.Foo(g.thing.value)
101+
)
102+
)
103+
)
104+
105+
class ResourceBorrowInRecord(exports.ResourceBorrowInRecord):
106+
def test(self, a: List[exports.resource_borrow_in_record.Foo]) -> List[resource_borrow_in_record.Thing]:
107+
return list(
108+
map(
109+
resource_borrow_in_record.wrap_thing,
110+
imports.resource_borrow_in_record.test(
111+
list(map(lambda x: imports.resource_borrow_in_record.Foo(x.thing.value), a))
112+
)
113+
)
114+
)
115+
116+
class Tests(tests.Tests):
117+
def test_resource_borrow_import(self, v: int) -> int:
118+
return resource_borrow_import.foo(resource_borrow_import.Thing(v + 1)) + 4
119+
120+
def test_resource_alias(self, things: List[imports.resource_alias1.Thing]) -> List[imports.resource_alias1.Thing]:
121+
return things
122+
123+
def add(self, a: imports.resource_floats.Float, b: imports.resource_floats.Float) -> imports.resource_floats.Float:
124+
return imports.resource_floats.Float(a.get() + b.get() + 5)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from tests.exports import resource_aggregates
2+
from tests.imports.resource_aggregates import Thing as HostThing
3+
4+
class Thing(resource_aggregates.Thing):
5+
def __init__(self, v: int):
6+
self.value = HostThing(v + 1)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from tests.exports import resource_alias1
2+
from tests.imports.resource_alias1 import Thing as HostThing
3+
4+
class Thing(resource_alias1.Thing):
5+
def __init__(self, v: str):
6+
self.value = HostThing(v + " Thing.__init__")
7+
8+
def get(self) -> str:
9+
return self.value.get() + " Thing.get"
10+
11+
def wrap_thing(thing: HostThing) -> Thing:
12+
mine = Thing.__new__(Thing)
13+
mine.value = thing
14+
return mine
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from tests.exports import resource_borrow_export
2+
3+
class Thing(resource_borrow_export.Thing):
4+
def __init__(self, v: int):
5+
self.value = v + 1
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from tests.exports import resource_borrow_in_record
2+
from tests.imports.resource_borrow_in_record import Thing as HostThing
3+
4+
class Thing(resource_borrow_in_record.Thing):
5+
def __init__(self, v: str):
6+
self.value = HostThing(v + " Thing.__init__")
7+
8+
def get(self) -> str:
9+
return self.value.get() + " Thing.get"
10+
11+
def wrap_thing(thing: HostThing) -> Thing:
12+
mine = Thing.__new__(Thing)
13+
mine.value = thing
14+
return mine
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from tests.exports import resource_floats_exports
2+
from tests.imports.resource_floats_imports import Float as HostFloat
3+
from typing import Self
4+
5+
class Float(resource_floats_exports.Float):
6+
def __init__(self, v: float):
7+
self.value = HostFloat(v + 1)
8+
9+
def get(self) -> str:
10+
return self.value.get() + 3
11+
12+
@staticmethod
13+
def add(a: Self, b: float) -> Self:
14+
return Float(HostFloat.add(a.value, b).get() + 5)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from tests.exports import resource_import_and_export
2+
from tests.imports.resource_import_and_export import Thing as HostThing
3+
from typing import Self
4+
5+
class Thing(resource_import_and_export.Thing):
6+
def __init__(self, v: int):
7+
self.value = HostThing(v + 7)
8+
9+
def foo(self) -> int:
10+
return self.value.foo() + 3
11+
12+
def bar(self, v: int):
13+
self.value.bar(v + 4)
14+
15+
@staticmethod
16+
def baz(a: Self, b: Self) -> Self:
17+
with HostThing.baz(a.value, b.value) as bar:
18+
value = bar.foo()
19+
return Thing(value + 9)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from tests.exports import resource_with_lists
2+
from tests.imports.resource_with_lists import Thing as HostThing
3+
from typing import List
4+
5+
class Thing(resource_with_lists.Thing):
6+
def __init__(self, v: bytes):
7+
x = bytearray(v)
8+
x.extend(b" Thing.__init__")
9+
self.value = HostThing(bytes(x))
10+
11+
def foo(self) -> bytes:
12+
x = bytearray(self.value.foo())
13+
x.extend(b" Thing.foo")
14+
return bytes(x)
15+
16+
def bar(self, v: bytes):
17+
x = bytearray(v)
18+
x.extend(b" Thing.bar")
19+
self.value.bar(bytes(x))
20+
21+
@staticmethod
22+
def baz(v: bytes) -> bytes:
23+
x = bytearray(v)
24+
x.extend(b" Thing.baz")
25+
y = bytearray(HostThing.baz(bytes(x)))
26+
y.extend(b" Thing.baz again")
27+
return bytes(y)

0 commit comments

Comments
 (0)