From 007bfb7ab061cfd3285006a9b059bf967e128c9f Mon Sep 17 00:00:00 2001 From: Askaholic Date: Tue, 13 Oct 2020 14:02:14 -0800 Subject: [PATCH] Refactor py_expect_exception to also verify error string representation --- tests/common.rs | 15 ++++++++++++++- tests/test_arithmetics.rs | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/common.rs b/tests/common.rs index 4cd3c084..c110f5d4 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -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::()) { 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 }}; } diff --git a/tests/test_arithmetics.rs b/tests/test_arithmetics.rs index f0c38724..5fd81952 100644 --- a/tests/test_arithmetics.rs +++ b/tests/test_arithmetics.rs @@ -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) {