Skip to content

Commit

Permalink
test: add snapshot test of macro expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
antalsz committed Jul 2, 2024
1 parent a2fa204 commit 372edf9
Show file tree
Hide file tree
Showing 4 changed files with 2,475 additions and 26 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ indexmap = { version = "2.0.0", optional = true }

[dev-dependencies]
thiserror = "1.0"
macrotest = { git = "https://github.com/antalsz/macrotest.git", branch = "optional-dependencies" }
45 changes: 45 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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")
}

#[test]
fn test_macro_expansion() {
// To regenerate the snapshot, delete the generated
// `tests/wrapper_tests/mod.expanded.rs` file and retun the test.
macrotest::expand("tests/wrapper_tests/mod.rs")
}
Loading

0 comments on commit 372edf9

Please sign in to comment.