investigate feature complete inspect()

This commit is contained in:
ConnorSkees 2020-04-26 18:27:08 -04:00
parent fb7fac5a53
commit c4de587f4e
3 changed files with 124 additions and 109 deletions

View File

@ -109,15 +109,16 @@ fn visit_quoted_string(buf: &mut String, force_double_quote: bool, string: &str)
impl Value {
pub fn is_null(&self, span: Span) -> SassResult<bool> {
match self {
&Value::Null => Ok(true),
Value::Ident(i, QuoteKind::None) if i.is_empty() => Ok(true),
Ok(match self {
&Value::Null => true,
Value::Ident(i, QuoteKind::None) if i.is_empty() => true,
Self::BinaryOp(..) | Self::Paren(..) | Self::UnaryOp(..) => {
self.clone().eval(span)?.is_null(span)
}
Self::List(v, _, Brackets::None) => Ok(v.iter().all(|f| f.is_null(span).unwrap())),
_ => Ok(false),
self.clone().eval(span)?.is_null(span)?
}
Self::List(v, _, Brackets::Bracketed) if v.is_empty() => false,
Self::List(v, ..) => v.iter().all(|f| f.is_null(span).unwrap()),
_ => false,
})
}
pub fn to_css_string(&self, span: Span) -> SassResult<String> {
@ -136,6 +137,7 @@ impl Value {
)
.into())
}
// TODO: should to_css_string on function fail?
Self::Function(func) => format!("get-function(\"{}\")", func.name()),
Self::List(vals, sep, brackets) => match brackets {
Brackets::None => vals
@ -253,6 +255,8 @@ impl Value {
}
}
// TODO:
// https://github.com/sass/dart-sass/blob/d4adea7569832f10e3a26d0e420ae51640740cfb/lib/src/ast/sass/expression/list.dart#L39
pub fn inspect(&self, span: Span) -> SassResult<String> {
Ok(match self {
Value::List(v, _, brackets) if v.is_empty() => match brackets {
@ -296,7 +300,7 @@ impl Value {
.collect::<SassResult<Vec<String>>>()?
.join(", ")
),
Value::Paren(v) => format!("({})", v.inspect(span)?),
Value::Paren(v) => v.inspect(span)?,
v => v.to_css_string(span)?,
})
}

112
tests/inspect.rs Normal file
View File

@ -0,0 +1,112 @@
#![cfg(test)]
#[macro_use]
mod macros;
test!(
inspect_unquoted_string,
"a {\n color: inspect(foo)\n}\n",
"a {\n color: foo;\n}\n"
);
test!(
inspect_dbl_quoted_string,
"a {\n color: inspect(\"foo\")\n}\n",
"a {\n color: \"foo\";\n}\n"
);
test!(
inspect_sgl_quoted_string,
"a {\n color: inspect(\"foo\")\n}\n",
"a {\n color: \"foo\";\n}\n"
);
test!(
inspect_unitless_number,
"a {\n color: inspect(1)\n}\n",
"a {\n color: 1;\n}\n"
);
test!(
inspect_px_number,
"a {\n color: inspect(1px)\n}\n",
"a {\n color: 1px;\n}\n"
);
test!(
inspect_color_3_hex,
"a {\n color: inspect(#fff)\n}\n",
"a {\n color: #fff;\n}\n"
);
test!(
inspect_color_6_hex,
"a {\n color: inspect(#ffffff)\n}\n",
"a {\n color: #ffffff;\n}\n"
);
test!(
inspect_color_name,
"a {\n color: inspect(red)\n}\n",
"a {\n color: red;\n}\n"
);
test!(
inspect_true,
"a {\n color: inspect(true)\n}\n",
"a {\n color: true;\n}\n"
);
test!(
inspect_false,
"a {\n color: inspect(false)\n}\n",
"a {\n color: false;\n}\n"
);
test!(
inspect_null,
"a {\n color: inspect(null)\n}\n",
"a {\n color: null;\n}\n"
);
test!(
inspect_empty_brackets,
"a {\n color: inspect([]);\n}\n",
"a {\n color: [];\n}\n"
);
test!(
inspect_comma_separated_one_val,
"a {\n color: inspect((1, ));\n}\n",
"a {\n color: (1,);\n}\n"
);
test!(
inspect_comma_separated_one_val_bracketed,
"a {\n color: inspect([1, ]);\n}\n",
"a {\n color: [1,];\n}\n"
);
test!(
inspect_space_separated_one_val_bracketed,
"a {\n color: inspect(append((), 1, space));\n}\n",
"a {\n color: 1;\n}\n"
);
test!(
inspect_list_of_empty_list,
"a {\n color: inspect(((), ()));\n}\n",
"a {\n color: (), ();\n}\n"
);
test!(
#[ignore]
inspect_comma_separated_list_of_comma_separated_lists,
"a {\n color: inspect([(1, 2), (3, 4)]);\n}\n",
"a {\n color: [(1, 2), (3, 4)];\n}\n"
);
test!(
inspect_empty_list,
"a {\n color: inspect(())\n}\n",
"a {\n color: ();\n}\n"
);
test!(
inspect_spaced_list,
"a {\n color: inspect(1 2 3)\n}\n",
"a {\n color: 1 2 3;\n}\n"
);
test!(
#[ignore]
inspect_comma_list,
"a {\n color: inspect(1, 2, 3)\n}\n",
"a {\n color: 1, 2, 3;\n}\n"
);
test!(
inspect_parens,
"a {\n color: inspect((((a))));\n}\n",
"a {\n color: a;\n}\n"
);

View File

@ -187,91 +187,6 @@ test!(
"a {\n color: unitless(foo)\n}\n",
"a {\n color: true;\n}\n"
);
test!(
inspect_unquoted_string,
"a {\n color: inspect(foo)\n}\n",
"a {\n color: foo;\n}\n"
);
test!(
inspect_dbl_quoted_string,
"a {\n color: inspect(\"foo\")\n}\n",
"a {\n color: \"foo\";\n}\n"
);
test!(
inspect_sgl_quoted_string,
"a {\n color: inspect(\"foo\")\n}\n",
"a {\n color: \"foo\";\n}\n"
);
test!(
inspect_unitless_number,
"a {\n color: inspect(1)\n}\n",
"a {\n color: 1;\n}\n"
);
test!(
inspect_px_number,
"a {\n color: inspect(1px)\n}\n",
"a {\n color: 1px;\n}\n"
);
test!(
inspect_color_3_hex,
"a {\n color: inspect(#fff)\n}\n",
"a {\n color: #fff;\n}\n"
);
test!(
inspect_color_6_hex,
"a {\n color: inspect(#ffffff)\n}\n",
"a {\n color: #ffffff;\n}\n"
);
test!(
inspect_color_name,
"a {\n color: inspect(red)\n}\n",
"a {\n color: red;\n}\n"
);
test!(
inspect_true,
"a {\n color: inspect(true)\n}\n",
"a {\n color: true;\n}\n"
);
test!(
inspect_false,
"a {\n color: inspect(false)\n}\n",
"a {\n color: false;\n}\n"
);
test!(
inspect_null,
"a {\n color: inspect(null)\n}\n",
"a {\n color: null;\n}\n"
);
test!(
inspect_empty_brackets,
"a {\n color: inspect([]);\n}\n",
"a {\n color: [];\n}\n"
);
test!(
inspect_comma_separated_one_val,
"a {\n color: inspect((1, ));\n}\n",
"a {\n color: (1,);\n}\n"
);
test!(
inspect_comma_separated_one_val_bracketed,
"a {\n color: inspect([1, ]);\n}\n",
"a {\n color: [1,];\n}\n"
);
test!(
inspect_space_separated_one_val_bracketed,
"a {\n color: inspect(append((), 1, space));\n}\n",
"a {\n color: 1;\n}\n"
);
test!(
inspect_list_of_empty_list,
"a {\n color: inspect(((), ()));\n}\n",
"a {\n color: (), ();\n}\n"
);
test!(
inspect_comma_separated_list_of_comma_separated_lists,
"a {\n color: inspect([(1, 2), (3, 4)]);\n}\n",
"a {\n color: [(1, 2), (3, 4)];\n}\n"
);
test!(
variable_does_exist,
"$a: red; a {\n color: variable-exists(a)\n}\n",
@ -339,19 +254,3 @@ error!(
mixin_exists_non_string,
"a {color: mixin-exists(12px)}", "Error: $name: 12px is not a string."
);
test!(
inspect_empty_list,
"a {\n color: inspect(())\n}\n",
"a {\n color: ();\n}\n"
);
test!(
inspect_spaced_list,
"a {\n color: inspect(1 2 3)\n}\n",
"a {\n color: 1 2 3;\n}\n"
);
test!(
#[ignore]
inspect_comma_list,
"a {\n color: inspect(1, 2, 3)\n}\n",
"a {\n color: 1, 2, 3;\n}\n"
);