2018-09-03 18:50:18 +00:00
|
|
|
//! 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
|
|
|
|
|
2018-06-13 16:02:45 +00:00
|
|
|
/// Removes indentation from multiline strings in pyrun commands
|
2018-06-15 13:59:22 +00:00
|
|
|
#[allow(unused)] // macro scoping is fooling the compiler
|
2018-06-13 16:02:45 +00:00
|
|
|
pub fn indoc(commands: &str) -> String {
|
|
|
|
let indent;
|
|
|
|
if let Some(second) = commands.lines().nth(1) {
|
2018-06-15 19:21:12 +00:00
|
|
|
indent = second
|
|
|
|
.chars()
|
|
|
|
.take_while(char::is_ascii_whitespace)
|
|
|
|
.collect::<String>();
|
2018-06-13 16:02:45 +00:00
|
|
|
} else {
|
|
|
|
indent = "".to_string();
|
|
|
|
}
|
|
|
|
|
2018-06-15 19:21:12 +00:00
|
|
|
commands
|
|
|
|
.trim_right()
|
2018-07-30 21:01:46 +00:00
|
|
|
.replace(&("\n".to_string() + &indent), "\n")
|
|
|
|
+ "\n"
|
2018-06-13 16:02:45 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 18:49:40 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! py_run {
|
2018-06-15 19:21:12 +00:00
|
|
|
($py:expr, $val:expr, $code:expr) => {{
|
2018-09-21 21:32:48 +00:00
|
|
|
let d = pyo3::types::PyDict::new($py);
|
2018-05-02 18:49:40 +00:00
|
|
|
d.set_item(stringify!($val), &$val).unwrap();
|
2018-06-13 16:02:45 +00:00
|
|
|
$py.run(&common::indoc($code), None, Some(d))
|
2018-09-08 22:19:55 +00:00
|
|
|
.map_err(|e| {
|
|
|
|
e.print($py);
|
|
|
|
// So when this c api function the last line called printed the error to stderr,
|
|
|
|
// the output is only written into a buffer which is never flushed because we
|
|
|
|
// panic before flushing. This is where this hack comes into place
|
|
|
|
$py.run("import sys; sys.stderr.flush()", None, None)
|
|
|
|
.unwrap();
|
2018-09-28 21:34:57 +00:00
|
|
|
})
|
|
|
|
.expect(&common::indoc($code))
|
2018-06-15 19:21:12 +00:00
|
|
|
}};
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! py_assert {
|
2018-06-15 19:21:12 +00:00
|
|
|
($py:expr, $val:ident, $assertion:expr) => {
|
|
|
|
py_run!($py, $val, concat!("assert ", $assertion))
|
|
|
|
};
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! py_expect_exception {
|
|
|
|
($py:expr, $val:ident, $code:expr, $err:ident) => {{
|
2018-09-21 21:32:48 +00:00
|
|
|
let d = pyo3::types::PyDict::new($py);
|
2018-05-02 18:49:40 +00:00
|
|
|
d.set_item(stringify!($val), &$val).unwrap();
|
|
|
|
let res = $py.run($code, None, Some(d));
|
|
|
|
let err = res.unwrap_err();
|
2018-09-21 21:32:48 +00:00
|
|
|
if !err.matches($py, $py.get_type::<pyo3::exceptions::$err>()) {
|
2018-05-02 18:49:40 +00:00
|
|
|
panic!(format!("Expected {} but got {:?}", stringify!($err), err))
|
|
|
|
}
|
2018-06-15 19:21:12 +00:00
|
|
|
}};
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|