refactor how @ import is tested

This commit is contained in:
ConnorSkees 2020-04-24 19:42:04 -04:00
parent 0969df8fe5
commit ecf36cd6d3

View File

@ -3,26 +3,70 @@ use tempfile::Builder;
use grass::StyleSheet; use grass::StyleSheet;
macro_rules! test_import { /// Create a temporary file with the given name
($func:ident, $input:literal => $output:literal | $( $name:literal($content:literal) ),*) => { /// and contents.
#[test] ///
fn $func() { /// This must be a macro rather than a function
$( /// because the tempfile will be deleted when it
let mut f = Builder::new().rand_bytes(0).prefix("").suffix($name).tempfile_in("").unwrap(); /// exits scope
write!(f, $content).unwrap(); macro_rules! tempfile {
)* ($name:literal, $content:literal) => {
let sass = StyleSheet::new($input.to_string()) let mut f = Builder::new()
.expect(concat!("failed to parse on ", $input)); .rand_bytes(0)
assert_eq!( .prefix("")
String::from($output), .suffix($name)
sass .tempfile_in("")
); .unwrap();
} write!(f, "{}", $content).unwrap();
} };
} }
// we have to use test name as filename because tests are run multithreaded in the same directory, so some names may conflict #[test]
test_import!(imports_variable, "@import \"imports_variable\";\na {\n color: $a;\n}" => "a {\n color: red;\n}\n" | "imports_variable"("$a: red;")); fn imports_variable() {
test_import!(single_quotes_import, "@import 'single_quotes_import';\na {\n color: $a;\n}" => "a {\n color: red;\n}\n" | "single_quotes_import"("$a: red;")); let input = "@import \"imports_variable\";\na {\n color: $a;\n}";
test_import!(finds_name_scss, "@import \"finds_name_scss\";\na {\n color: $a;\n}" => "a {\n color: red;\n}\n" | "finds_name_scss.scss"("$a: red;")); tempfile!("imports_variable", "$a: red;");
test_import!(finds_underscore_name_scss, "@import \"finds_underscore_name_scss\";\na {\n color: $a;\n}" => "a {\n color: red;\n}\n" | "_finds_underscore_name_scss.scss"("$a: red;")); assert_eq!(
"a {\n color: red;\n}\n",
&StyleSheet::new(input.to_string()).expect(input)
);
}
#[test]
fn import_no_semicolon() {
let input = "@import \"import_no_semicolon\"\na {\n color: $a;\n}";
tempfile!("import_no_semicolon", "$a: red;");
assert_eq!(
"a {\n color: red;\n}\n",
&StyleSheet::new(input.to_string()).expect(input)
);
}
#[test]
fn single_quotes_import() {
let input = "@import 'single_quotes_import';\na {\n color: $a;\n}";
tempfile!("single_quotes_import", "$a: red;");
assert_eq!(
"a {\n color: red;\n}\n",
&StyleSheet::new(input.to_string()).expect(input)
);
}
#[test]
fn finds_name_scss() {
let input = "@import \"finds_name_scss\";\na {\n color: $a;\n}";
tempfile!("finds_name_scss.scss", "$a: red;");
assert_eq!(
"a {\n color: red;\n}\n",
&StyleSheet::new(input.to_string()).expect(input)
);
}
#[test]
fn finds_underscore_name_scss() {
let input = "@import \"finds_underscore_name_scss\";\na {\n color: $a;\n}";
tempfile!("_finds_underscore_name_scss.scss", "$a: red;");
assert_eq!(
"a {\n color: red;\n}\n",
&StyleSheet::new(input.to_string()).expect(input)
);
}