Skip to content

Commit

Permalink
Merge pull request #241 from pganssle/othermod_example
Browse files Browse the repository at this point in the history
Add tests for othermod
  • Loading branch information
konstin authored Oct 1, 2018
2 parents da906fb + 7abd436 commit 0ec24aa
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
4 changes: 3 additions & 1 deletion examples/rustapi_module/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def get_py_version_cfgs():
'Operating System :: MacOS :: MacOS X',
],
packages=['rustapi_module'],
rust_extensions=[RustExtension('rustapi_module.datetime', 'Cargo.toml',
rust_extensions=[RustExtension('rustapi_module.othermod', 'Cargo.toml',
rustc_flags=get_py_version_cfgs()),
RustExtension('rustapi_module.datetime', 'Cargo.toml',
rustc_flags=get_py_version_cfgs())],
install_requires=install_requires,
tests_require=tests_require,
Expand Down
11 changes: 11 additions & 0 deletions examples/rustapi_module/src/othermod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ pub struct ModClass {

#[pymethods]
impl ModClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|_| ModClass {
_somefield: String::from("contents"),
})
}

fn noop(&self, x: usize) -> usize {
x
}
Expand All @@ -25,5 +32,9 @@ fn double(x: i32) -> i32 {
fn othermod(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_function!(double))?;
m.add_class::<ModClass>()?;

m.add("USIZE_MIN", usize::min_value())?;
m.add("USIZE_MAX", usize::max_value())?;

Ok(())
}
36 changes: 36 additions & 0 deletions examples/rustapi_module/tests/test_othermod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from rustapi_module import othermod

from hypothesis import given, assume
from hypothesis import strategies as st

INTEGER32_ST = st.integers(min_value=(-(2**31)), max_value=(2**31 - 1))
USIZE_ST = st.integers(min_value=othermod.USIZE_MIN,
max_value=othermod.USIZE_MAX)

@given(x=INTEGER32_ST)
def test_double(x):
expected = x*2
assume(-(2**31) <= expected <= (2**31 - 1))
assert othermod.double(x) == expected

def test_modclass():
# Test that the repr of the class itself doesn't crash anything
repr(othermod.ModClass)

assert isinstance(othermod.ModClass, type)

def test_modclass_instance():
mi = othermod.ModClass()

repr(mi)
repr(mi.__class__)

assert isinstance(mi, othermod.ModClass)
assert isinstance(mi, object)

@given(x=USIZE_ST)
def test_modclas_noop(x):
mi = othermod.ModClass()

assert mi.noop(x) == x

0 comments on commit 0ec24aa

Please sign in to comment.