Add tests for othermod
This adds othermod to the rustapi_module extension and tests the contents of the module.
This commit is contained in:
parent
da906fb715
commit
7abd436a0d
|
@ -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,
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
Loading…
Reference in New Issue