Skip to content

Commit

Permalink
Refactor py_expect_exception to also verify error string representation
Browse files Browse the repository at this point in the history
  • Loading branch information
Askaholic committed Oct 13, 2020
1 parent 95cebd8 commit 007bfb7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,22 @@ macro_rules! py_expect_exception {
let d = [(stringify!($val), &$val)].into_py_dict($py);

let res = $py.run($code, None, Some(d));
let err = res.unwrap_err();
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
);
err
}};
}
2 changes: 1 addition & 1 deletion tests/test_arithmetics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ mod return_not_implemented {
c2,
&format!("class Other: pass\nc2 {} Other()", operator),
PyTypeError
)
);
}

fn _test_inplace_binary_operator(operator: &str, dunder: &str) {
Expand Down

0 comments on commit 007bfb7

Please sign in to comment.