Skip to content

Commit

Permalink
Use pyo3-testing macros (#43)
Browse files Browse the repository at this point in the history
Now that v0.3.1 has hit ...
  • Loading branch information
MusicalNinjaDad authored May 26, 2024
1 parent 9c60a55 commit 0cc05a7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 46 deletions.
23 changes: 7 additions & 16 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ Given that you have tested the core code functionality well, you don't need to r
!!! rocket "The `pyo3-testing` crate"
The `pyo3-testing` crate is designed to make this step simple.

It is currently available on a forked version of pyo3, I'll release it separately as a dedicated crate ASAP unless [PR pyo3/#4099](https://github.com/PyO3/pyo3/pull/4099) lands in the meantime.
It is currently available as an independent crate until [PR pyo3/#4099](https://github.com/PyO3/pyo3/pull/4099) lands.

Use it by importing pyo3 from the fork in **rust/fizzbuzzo3/Cargo.toml**:
Use it by adding a dependency in **rust/fizzbuzzo3/Cargo.toml**:
```toml
...
[dependencies]
pyo3 = { git = "https://github.com/MusicalNinjaDad/pyo3.git", branch = "pyo3-testing" }
pyo3-testing = "0.3.1"
...
```

Expand All @@ -84,13 +84,8 @@ Given that you have tested the core code functionality well, you don't need to r
#[pyo3test]
#[pyo3import(py_fizzbuzzo3: from fizzbuzzo3 import fizzbuzz)]
fn test_fizzbuzz() {
let result: PyResult<String> = match fizzbuzz.call1((1i32,)) {
Ok(r) => r.extract(),
Err(e) => Err(e),
};
let result = result.unwrap();
let expected_result = "1";
assert_eq!(result, expected_result);
let result: String = fizzbuzz!(1i32);
assert_eq!(result, "1");
}
```

Expand All @@ -100,14 +95,10 @@ Given that you have tested the core code functionality well, you don't need to r
Example from **`rust/fizzbuzzo3/src.rs`**:
```rust
#[pyo3test]
#[allow(unused_macros)]
#[pyo3import(py_fizzbuzzo3: from fizzbuzzo3 import fizzbuzz)]
fn test_fizzbuzz_string() {
let result: PyResult<bool> = match fizzbuzz.call1(("one",)) {
Ok(_) => Ok(false),
Err(error) if error.is_instance_of::<PyTypeError>(py) => Ok(true),
Err(e) => Err(e),
};
assert!(result.unwrap());
with_py_raises!(PyTypeError, { fizzbuzz.call1(("4",)) })
}
```

Expand Down
4 changes: 2 additions & 2 deletions rust/fizzbuzzo3/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fizzbuzzo3"
version = "1.2.0"
version = "1.3.0"
edition = "2021"

[lib]
Expand All @@ -10,7 +10,7 @@ crate-type = ["cdylib"] # cdylib required for python import, rlib required for

[dependencies]
pyo3 = "0.21.2"
pyo3-testing = "0.2.0"
pyo3-testing = "0.3.1"
fizzbuzz = { path = "../fizzbuzz" }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
37 changes: 9 additions & 28 deletions rust/fizzbuzzo3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,55 +55,36 @@ fn py_fizzbuzzo3(module: &Bound<'_, PyModule>) -> PyResult<()> {
mod tests {
use pyo3::exceptions::PyTypeError;
use pyo3::types::PyDict;
use pyo3_testing::pyo3test;
use pyo3_testing::{pyo3test, with_py_raises};

use super::*;

#[pyo3test]
#[pyo3import(py_fizzbuzzo3: from fizzbuzzo3 import fizzbuzz)]
fn test_fizzbuzz() {
let result: PyResult<String> = match fizzbuzz.call1((1i32,)) {
Ok(r) => r.extract(),
Err(e) => Err(e),
};
let result = result.unwrap();
let expected_result = "1";
assert_eq!(result, expected_result);
let result: String = fizzbuzz!(1i32);
assert_eq!(result, "1");
}

#[pyo3test]
#[pyo3import(py_fizzbuzzo3: from fizzbuzzo3 import fizzbuzz)]
fn test_fizzbuzz_float() {
let result: PyResult<String> = match fizzbuzz.call1((1f32,)) {
Ok(r) => r.extract(),
Err(e) => Err(e),
};
let result = result.unwrap();
let expected_result = "1";
assert_eq!(result, expected_result);
let result: String = fizzbuzz!(1f32);
assert_eq!(result, "1");
}

#[pyo3test]
#[pyo3import(py_fizzbuzzo3: from fizzbuzzo3 import fizzbuzz)]
fn test_fizzbuzz_vec() {
let input = vec![1, 2, 3, 4, 5];
let result: PyResult<String> = match fizzbuzz.call1((input,)) {
Ok(r) => r.extract(),
Err(e) => Err(e),
};
let result = result.unwrap();
let expected_result = "1, 2, fizz, 4, buzz";
assert_eq!(result, expected_result);
let result: String = fizzbuzz!(input);
assert_eq!(result, "1, 2, fizz, 4, buzz");
}

#[pyo3test]
#[allow(unused_macros)]
#[pyo3import(py_fizzbuzzo3: from fizzbuzzo3 import fizzbuzz)]
fn test_fizzbuzz_string() {
let result: PyResult<bool> = match fizzbuzz.call1(("one",)) {
Ok(_) => Ok(false),
Err(error) if error.is_instance_of::<PyTypeError>(py) => Ok(true),
Err(e) => Err(e),
};
assert!(result.unwrap());
with_py_raises!(PyTypeError, { fizzbuzz.call1(("4",)) })
}
}

0 comments on commit 0cc05a7

Please sign in to comment.