grass/tests/macros.rs

55 lines
1.7 KiB
Rust
Raw Normal View History

2020-02-01 23:09:22 -05:00
#![cfg(test)]
2020-03-01 14:53:52 -05:00
#[macro_export]
2020-02-01 23:09:22 -05:00
macro_rules! test {
2020-03-22 18:13:19 -04:00
($( #[$attr:meta] ),*$func:ident, $input:expr) => {
$(#[$attr])*
2020-02-01 23:09:22 -05:00
#[test]
#[allow(non_snake_case)]
2020-02-01 23:09:22 -05:00
fn $func() {
let mut buf = Vec::new();
grass::StyleSheet::new($input.to_string())
2020-02-02 10:27:08 -05:00
.expect(concat!("failed to parse on ", $input))
.print_as_css(&mut buf)
.expect(concat!("failed to pretty print on ", $input));
2020-02-01 23:09:22 -05:00
assert_eq!(
String::from($input),
String::from_utf8(buf).expect("produced invalid utf8")
);
}
};
2020-03-22 18:13:19 -04:00
($( #[$attr:meta] ),*$func:ident, $input:expr, $output:expr) => {
$(#[$attr])*
2020-02-01 23:09:22 -05:00
#[test]
#[allow(non_snake_case)]
2020-02-01 23:09:22 -05:00
fn $func() {
let mut buf = Vec::new();
grass::StyleSheet::new($input.to_string())
2020-02-01 23:09:22 -05:00
.expect(concat!("failed to parse on ", $input))
.print_as_css(&mut buf)
.expect(concat!("failed to pretty print on ", $input));
assert_eq!(
String::from($output),
String::from_utf8(buf).expect("produced invalid utf8")
);
}
};
2020-02-02 10:27:08 -05:00
}
2020-03-21 14:39:16 -04:00
/// Verify the error *message*
/// Span and scope information are not yet tested
2020-03-21 14:39:16 -04:00
#[macro_export]
macro_rules! error {
2020-03-22 18:13:19 -04:00
($( #[$attr:meta] ),*$func:ident, $input:expr, $err:expr) => {
$(#[$attr])*
2020-03-21 14:39:16 -04:00
#[test]
#[allow(non_snake_case)]
fn $func() {
match grass::StyleSheet::new($input.to_string()) {
2020-03-21 14:39:16 -04:00
Ok(..) => panic!("did not fail"),
Err(e) => assert_eq!($err, e.to_string().chars().take_while(|c| *c != '\n').collect::<String>().as_str()),
2020-03-21 14:39:16 -04:00
}
}
};
}