-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1494 from PyO3/enhance-py-run
Extend py_run! to take locals dict and refactor tests using it
- Loading branch information
Showing
10 changed files
with
279 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,43 @@ | ||
//! Useful tips for writing tests: | ||
//! - Tests are run in parallel; There's still a race condition in test_owned with some other test | ||
//! - You need to use flush=True to get any output from print | ||
//! Some common macros for tests | ||
#[macro_export] | ||
macro_rules! py_assert { | ||
($py:expr, $val:ident, $assertion:expr) => { | ||
pyo3::py_run!($py, $val, concat!("assert ", $assertion)) | ||
($py:expr, $($val:ident)+, $assertion:literal) => { | ||
pyo3::py_run!($py, $($val)+, concat!("assert ", $assertion)) | ||
}; | ||
($py:expr, *$dict:expr, $assertion:literal) => { | ||
pyo3::py_run!($py, *$dict, concat!("assert ", $assertion)) | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! py_expect_exception { | ||
($py:expr, $val:ident, $code:expr, $err:ident) => {{ | ||
// Case1: idents & no err_msg | ||
($py:expr, $($val:ident)+, $code:expr, $err:ident) => {{ | ||
use pyo3::types::IntoPyDict; | ||
let d = [(stringify!($val), &$val)].into_py_dict($py); | ||
|
||
let res = $py.run($code, None, Some(d)); | ||
let d = [$((stringify!($val), $val.to_object($py)),)+].into_py_dict($py); | ||
py_expect_exception!($py, *d, $code, $err) | ||
}}; | ||
// Case2: dict & no err_msg | ||
($py:expr, *$dict:expr, $code:expr, $err:ident) => {{ | ||
let res = $py.run($code, None, Some($dict)); | ||
let err = res.expect_err(&format!("Did not raise {}", stringify!($err))); | ||
if !err.matches($py, $py.get_type::<pyo3::exceptions::$err>()) { | ||
panic!("Expected {} but got {:?}", stringify!($err), err) | ||
} | ||
err | ||
}}; | ||
($py:expr, $val:ident, $code:expr, $err:ident, $err_msg:expr) => {{ | ||
let err = py_expect_exception!($py, $val, $code, $err); | ||
assert_eq!( | ||
err.instance($py) | ||
.str() | ||
.expect("error str() failed") | ||
.to_str() | ||
.expect("message was not valid utf8"), | ||
$err_msg | ||
); | ||
// Case3: idents & err_msg | ||
($py:expr, $($val:ident)+, $code:expr, $err:ident, $err_msg:literal) => {{ | ||
let err = py_expect_exception!($py, $($val)+, $code, $err); | ||
// Suppose that the error message looks like 'TypeError: ~' | ||
assert_eq!(format!("Py{}", err), concat!(stringify!($err), ": ", $err_msg)); | ||
err | ||
}}; | ||
// Case4: dict & err_msg | ||
($py:expr, *$dict:expr, $code:expr, $err:ident, $err_msg:literal) => {{ | ||
let err = py_expect_exception!($py, *$dict, $code, $err); | ||
assert_eq!(format!("Py{}", err), concat!(stringify!($err), ": ", $err_msg)); | ||
err | ||
}}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.