diff --git a/tests/tests.rs b/tests/tests.rs new file mode 100644 index 0000000..0dfd0c7 --- /dev/null +++ b/tests/tests.rs @@ -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") +} diff --git a/tests/wrapper_tests.rs b/tests/wrapper_tests/mod.rs similarity index 69% rename from tests/wrapper_tests.rs rename to tests/wrapper_tests/mod.rs index 1bec7fe..ea25bc8 100644 --- a/tests/wrapper_tests.rs +++ b/tests/wrapper_tests/mod.rs @@ -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") }