Skip to content

Commit bf523c4

Browse files
committed
Merge remote-tracking branch 'origin/feat-pyo3-tests' into feat-pyo3
2 parents e40f9de + 6efae3a commit bf523c4

9 files changed

+214
-250
lines changed

justfile

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Install the environment to develop the extension.
22
prelude:
3-
pip3 install pyo3-pack
4-
cargo install pyo3-pack
53
pip3 install virtualenv
64
virtualenv -p $(which python3) .env
5+
source .env/bin/activate
6+
7+
pip3 install pyo3-pack pytest pytest-benchmark
78

89
# Setup the environment to develop the extension.
910
wakeup:
@@ -14,22 +15,22 @@ sleep:
1415
deactivate
1516

1617
# Compile and install the Rust library.
17-
rust:
18+
rust: wakeup
1819
export PYTHON_SYS_EXECUTABLE=$(which python3)
1920
cargo check
2021
pyo3-pack develop --binding_crate pyo3 --release --strip
2122

2223
# Run Python.
23-
python-run file='':
24-
.env/bin/python {{file}}
24+
python-run file='': wakeup
25+
python {{file}}
2526

2627
# Run the tests.
27-
test:
28-
@.env/bin/python tests/init.py
28+
test: wakeup
29+
py.test tests
2930

3031
# Inspect the `python-ext-wasm` extension.
31-
inspect:
32-
.env/bin/python -c "help('wasmer')"
32+
inspect: wakeup
33+
python -c "help('wasmer')"
3334

3435
# Local Variables:
3536
# mode: makefile

tests/init.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/instance.py

Lines changed: 0 additions & 98 deletions
This file was deleted.

tests/memory_view.py

Lines changed: 0 additions & 96 deletions
This file was deleted.

tests/test_benchmarks.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from wasmer import Instance, Uint8MemoryView, Value, validate
2+
import inspect
3+
import os
4+
import pytest
5+
6+
here = os.path.dirname(os.path.realpath(__file__))
7+
TEST_BYTES = open(here + '/tests.wasm', 'rb').read()
8+
9+
def test_sum_benchmark(benchmark):
10+
instance = Instance(TEST_BYTES)
11+
sum_func = instance.exports['sum']
12+
13+
def bench():
14+
return sum_func(1, 2)
15+
16+
assert benchmark(bench) == 3

tests/test_instance.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from wasmer import Instance, Uint8MemoryView, Value, validate
2+
import inspect
3+
import os
4+
import pytest
5+
6+
here = os.path.dirname(os.path.realpath(__file__))
7+
TEST_BYTES = open(here + '/tests.wasm', 'rb').read()
8+
INVALID_TEST_BYTES = open(here + '/invalid.wasm', 'rb').read()
9+
10+
def test_is_a_class():
11+
assert inspect.isclass(Instance)
12+
13+
def test_can_construct():
14+
assert isinstance(Instance(TEST_BYTES), Instance)
15+
16+
def test_failed_to_instantiate():
17+
with pytest.raises(RuntimeError) as context_manager:
18+
Instance(INVALID_TEST_BYTES)
19+
20+
exception = context_manager.value
21+
assert str(exception) == (
22+
'Failed to instantiate the module:\n compile error: Validation error "Invalid type"'
23+
)
24+
25+
def test_function_does_not_exist():
26+
with pytest.raises(KeyError) as context_manager:
27+
Instance(TEST_BYTES).exports['foo']
28+
29+
exception = context_manager.value
30+
assert str(exception) == "'foo'"
31+
32+
def test_basic_sum():
33+
assert Instance(TEST_BYTES).exports['sum'](1, 2) == 3
34+
35+
def test_call_arity_0():
36+
assert Instance(TEST_BYTES).exports['arity_0']() == 42
37+
38+
def test_call_i32_i32():
39+
assert Instance(TEST_BYTES).exports['i32_i32'](7) == 7
40+
41+
def test_call_i64_i64():
42+
assert Instance(TEST_BYTES).exports['i64_i64'](7) == 7
43+
44+
def test_call_f32_f32():
45+
assert Instance(TEST_BYTES).exports['f32_f32'](7.) == 7.
46+
47+
def test_call_f64_f64():
48+
assert Instance(TEST_BYTES).exports['f64_f64'](7.) == 7.
49+
50+
def test_call_i32_i64_f32_f64_f64():
51+
assert round(Instance(TEST_BYTES).exports['i32_i64_f32_f64_f64'](1, 2, 3.4, 5.6), 6) == (
52+
1 + 2 + 3.4 + 5.6
53+
)
54+
55+
def test_call_bool_casted_to_i32():
56+
assert Instance(TEST_BYTES).exports['bool_casted_to_i32']() == 1
57+
58+
def test_call_string():
59+
assert Instance(TEST_BYTES).exports['string']() == 1048576
60+
61+
def test_validate():
62+
assert validate(TEST_BYTES)
63+
64+
def test_validate_invalid():
65+
assert not validate(INVALID_TEST_BYTES)
66+
67+
def test_memory_view():
68+
assert isinstance(Instance(TEST_BYTES).uint8_memory_view(), Uint8MemoryView)

0 commit comments

Comments
 (0)