Refactor py_expect_exception to also verify error string representation

This commit is contained in:
Askaholic 2020-10-13 14:02:14 -08:00
parent 95cebd8fee
commit 007bfb7ab0
No known key found for this signature in database
GPG Key ID: 9EA6CC37362892A1
2 changed files with 15 additions and 2 deletions

View File

@ -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
}};
}

View File

@ -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) {