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()
|
|
|
|
.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-05-02 18:49:40 +00:00
|
|
|
let d = PyDict::new($py);
|
|
|
|
d.set_item(stringify!($val), &$val).unwrap();
|
2018-06-13 16:02:45 +00:00
|
|
|
$py.run(&common::indoc($code), None, Some(d))
|
|
|
|
.map_err(|e| e.print($py))
|
|
|
|
.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) => {{
|
|
|
|
let d = PyDict::new($py);
|
|
|
|
d.set_item(stringify!($val), &$val).unwrap();
|
|
|
|
let res = $py.run($code, None, Some(d));
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
if !err.matches($py, $py.get_type::<exc::$err>()) {
|
|
|
|
panic!(format!("Expected {} but got {:?}", stringify!($err), err))
|
|
|
|
}
|
2018-06-15 19:21:12 +00:00
|
|
|
}};
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|