Merge pull request #241 from pganssle/othermod_example

Add tests for othermod
This commit is contained in:
konstin 2018-10-01 15:01:20 +02:00 committed by GitHub
commit 0ec24aa8db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 1 deletions

View file

@ -46,7 +46,9 @@ setup(
'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,

View file

@ -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
}
@ -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(())
}

View file

@ -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