grass/tests/imports.rs

90 lines
2.3 KiB
Rust
Raw Normal View History

2020-02-01 23:09:22 -05:00
use std::io::Write;
use tempfile::Builder;
use grass::StyleSheet;
2020-04-24 19:42:04 -04:00
/// Create a temporary file with the given name
/// and contents.
///
/// This must be a macro rather than a function
/// because the tempfile will be deleted when it
/// exits scope
macro_rules! tempfile {
($name:literal, $content:literal) => {
let mut f = Builder::new()
.rand_bytes(0)
.prefix("")
.suffix($name)
.tempfile_in("")
.unwrap();
write!(f, "{}", $content).unwrap();
};
2020-02-01 23:09:22 -05:00
}
2020-04-24 19:42:04 -04:00
#[test]
fn imports_variable() {
let input = "@import \"imports_variable\";\na {\n color: $a;\n}";
tempfile!("imports_variable", "$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 import_no_quotes() {
let input = "@import import_no_quotes";
tempfile!("import_no_quotes", "$a: red;");
match grass::StyleSheet::new(input.to_string()) {
Ok(..) => panic!("did not fail"),
Err(e) => assert_eq!(
"Error: Expected string.",
e.to_string()
.chars()
.take_while(|c| *c != '\n')
.collect::<String>()
.as_str()
),
}
}
2020-04-24 19:42:04 -04:00
#[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)
);
}