Skip to content

Commit

Permalink
test: move test code into a separate module
Browse files Browse the repository at this point in the history
This enables easier manual testing with `cargo expand`, and will enable easier
snapshot testing later.
  • Loading branch information
antalsz committed Jul 9, 2024
1 parent a2fa204 commit 627e224
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
38 changes: 38 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use pyo3::{types::PyModule, PyResult, Python};

// The code being tested is in a separate module so it can be expanded (see
// [`test_macro_expansion`]) without expanding the contents of the tests
// themselves. This file must be in a subdirectory (`wrapper_tests/mod.rs`
// instead of `wrapper_tests.rs`) because the generated `.expanded.rs` file
// cannot be in the root `tests/` directory or `cargo test` will attempt to
// build it as a test case as well.
mod wrapper_tests;

#[test]
fn test_enum_as_data_struct_member() {
wrapper_tests::append_to_inittab();
pyo3::prepare_freethreaded_python();
let result: PyResult<()> = Python::with_gil(|py| {
let code = r#"
from wrapper_tests import TestEnumUnaliased, TestEnumAliased, TestStruct, TestUnionEnum
struct = TestStruct()
assert struct.test_enum_unaliased == TestEnumUnaliased.One
assert struct.test_enum_aliased == TestEnumAliased.NONE
struct.test_enum_unaliased = TestEnumUnaliased.Two
struct.test_enum_aliased = TestEnumAliased.Two
assert struct.test_enum_unaliased == TestEnumUnaliased.Two
assert struct.test_enum_aliased == TestEnumAliased.Two
assert TestUnionEnum.new_unit().is_unit()
"#;
PyModule::from_code(py, code, "example.py", "example")?;

Ok(())
});

result.expect("python code should execute without issue")
}
27 changes: 1 addition & 26 deletions tests/wrapper_tests.rs → tests/wrapper_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,31 +77,6 @@ fn wrapper_tests(py: Python<'_>, m: &PyModule) -> PyResult<()> {
python::init_submodule("wrapper_tests", py, m)
}

#[test]
fn test_enum_as_data_struct_member() {
pub fn append_to_inittab() {
pyo3::append_to_inittab!(wrapper_tests);
pyo3::prepare_freethreaded_python();
let result: PyResult<()> = Python::with_gil(|py| {
let code = r#"
from wrapper_tests import TestEnumUnaliased, TestEnumAliased, TestStruct, TestUnionEnum
struct = TestStruct()
assert struct.test_enum_unaliased == TestEnumUnaliased.One
assert struct.test_enum_aliased == TestEnumAliased.NONE
struct.test_enum_unaliased = TestEnumUnaliased.Two
struct.test_enum_aliased = TestEnumAliased.Two
assert struct.test_enum_unaliased == TestEnumUnaliased.Two
assert struct.test_enum_aliased == TestEnumAliased.Two
assert TestUnionEnum.new_unit().is_unit()
"#;
PyModule::from_code(py, code, "example.py", "example")?;

Ok(())
});

result.expect("python code should execute without issue")
}

0 comments on commit 627e224

Please sign in to comment.