add tests for unitful NaN passed to builtin functions

This commit is contained in:
Connor Skees 2020-08-15 20:07:27 -04:00
parent 2b647ffd49
commit 679bb94b53
5 changed files with 108 additions and 13 deletions

View File

@ -23,8 +23,8 @@ pub(crate) fn length(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<
pub(crate) fn nth(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(2)?;
let mut list = args.get_err(0, "list")?.as_list();
let n = match args.get_err(1, "n")? {
Value::Dimension(Some(num), ..) => num,
let (n, unit) = match args.get_err(1, "n")? {
Value::Dimension(Some(num), unit, ..) => (num, unit),
Value::Dimension(None, u, ..) => {
return Err((format!("$n: NaN{} is not an int.", u), args.span()).into())
}
@ -44,8 +44,9 @@ pub(crate) fn nth(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Val
if n.abs() > Number::from(list.len()) {
return Err((
format!(
"$n: Invalid index {} for a list with {} elements.",
"$n: Invalid index {}{} for a list with {} elements.",
n,
unit,
list.len()
),
args.span(),
@ -83,8 +84,8 @@ pub(crate) fn set_nth(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult
Value::Map(m) => (m.as_list(), ListSeparator::Comma, Brackets::None),
v => (vec![v], ListSeparator::Space, Brackets::None),
};
let n = match args.get_err(1, "n")? {
Value::Dimension(Some(num), ..) => num,
let (n, unit) = match args.get_err(1, "n")? {
Value::Dimension(Some(num), unit, ..) => (num, unit),
Value::Dimension(None, u, ..) => {
return Err((format!("$n: NaN{} is not an int.", u), args.span()).into())
}
@ -105,7 +106,10 @@ pub(crate) fn set_nth(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult
if n.abs() > Number::from(len) {
return Err((
format!("$n: Invalid index {} for a list with {} elements.", n, len),
format!(
"$n: Invalid index {}{} for a list with {} elements.",
n, unit, len
),
args.span(),
)
.into());

View File

@ -17,7 +17,7 @@ pub(crate) fn percentage(mut args: CallArgs, parser: &mut Parser<'_>) -> SassRes
args.max_args(1)?;
let num = match args.get_err(0, "number")? {
Value::Dimension(Some(n), Unit::None, _) => Some(n * Number::from(100)),
Value::Dimension(None, ..) => None,
Value::Dimension(None, Unit::None, _) => None,
v @ Value::Dimension(..) => {
return Err((
format!(

View File

@ -110,8 +110,8 @@ pub(crate) fn str_slice(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResu
Value::Dimension(Some(n), Unit::None, _) => (n.to_integer() + BigInt::from(str_len + 1))
.to_usize()
.unwrap(),
Value::Dimension(None, u, ..) => {
return Err((format!("NaN{} is not an int.", u), args.span()).into())
Value::Dimension(None, Unit::None, ..) => {
return Err(("NaN is not an int.", args.span()).into())
}
v @ Value::Dimension(..) => {
return Err((
@ -143,8 +143,8 @@ pub(crate) fn str_slice(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResu
Value::Dimension(Some(n), Unit::None, _) => (n.to_integer() + BigInt::from(str_len + 1))
.to_usize()
.unwrap_or(str_len + 1),
Value::Dimension(None, u, ..) => {
return Err((format!("NaN{} is not an int.", u), args.span()).into())
Value::Dimension(None, Unit::None, ..) => {
return Err(("NaN is not an int.", args.span()).into())
}
v @ Value::Dimension(..) => {
return Err((
@ -243,8 +243,8 @@ pub(crate) fn str_insert(mut args: CallArgs, parser: &mut Parser<'_>) -> SassRes
return Err((format!("$index: {} is not an int.", n), args.span()).into())
}
Value::Dimension(Some(n), Unit::None, _) => n,
Value::Dimension(None, u, ..) => {
return Err((format!("$index: NaN{} is not an int.", u), args.span()).into())
Value::Dimension(None, Unit::None, ..) => {
return Err(("$index: NaN is not an int.", args.span()).into())
}
v @ Value::Dimension(..) => {
return Err((

View File

@ -338,3 +338,12 @@ error!(
invalid_item_in_comma_separated_list_inside_interpolation,
"a {\n color: #{red, color * #abc};\n}\n", "Error: Undefined operation \"color * #abc\"."
);
error!(
nth_invalid_index_message_contains_unit,
"a {\n color: nth([], 1px);\n}\n", "Error: $n: Invalid index 1px for a list with 0 elements."
);
error!(
set_nth_invalid_index_message_contains_unit,
"a {\n color: set-nth([], 1px, a);\n}\n",
"Error: $n: Invalid index 1px for a list with 0 elements."
);

View File

@ -77,3 +77,85 @@ test!(
"$n: (0/0);\na {\n color: max(1px, $n, 0);\n}\n",
"a {\n color: 1px;\n}\n"
);
error!(
unitful_nan_str_slice_start,
"@use \"sass:math\";\na {\n color: str-slice(\"\", math.acos(2));\n}\n",
"Error: $start: Expected NaNdeg to have no units."
);
error!(
unitful_nan_str_slice_end,
"@use \"sass:math\";\na {\n color: str-slice(\"\", 0, math.acos(2));\n}\n",
"Error: $end: Expected NaNdeg to have no units."
);
error!(
unitful_nan_str_insert_index,
"@use \"sass:math\";\na {\n color: str-insert(\"\", \"\", math.acos(2));\n}\n",
"Error: $index: Expected NaNdeg to have no units."
);
error!(
unitful_nan_percentage,
"@use \"sass:math\";\na {\n color: percentage(math.acos(2));\n}\n",
"Error: $number: Expected NaNdeg to have no units."
);
error!(
unitful_nan_round,
"@use \"sass:math\";\na {\n color: round(math.acos(2));\n}\n", "Error: Infinity or NaN toInt"
);
error!(
unitful_nan_ceil,
"@use \"sass:math\";\na {\n color: ceil(math.acos(2));\n}\n", "Error: Infinity or NaN toInt"
);
error!(
unitful_nan_floor,
"@use \"sass:math\";\na {\n color: floor(math.acos(2));\n}\n", "Error: Infinity or NaN toInt"
);
test!(
unitful_nan_abs,
"@use \"sass:math\";\na {\n color: abs(math.acos(2));\n}\n",
"a {\n color: NaNdeg;\n}\n"
);
error!(
unitful_nan_random,
"@use \"sass:math\";\na {\n color: random(math.acos(2));\n}\n",
"Error: $limit: NaNdeg is not an int."
);
test!(
unitful_nan_min_first_arg,
"@use \"sass:math\";\na {\n color: min(math.acos(2), 1px);\n}\n",
"a {\n color: NaNdeg;\n}\n"
);
test!(
unitful_nan_min_last_arg,
"@use \"sass:math\";\na {\n color: min(1px, math.acos(2));\n}\n",
"a {\n color: 1px;\n}\n"
);
test!(
unitful_nan_min_middle_arg,
"@use \"sass:math\";\na {\n color: min(1px, math.acos(2), 0);\n}\n",
"a {\n color: 0;\n}\n"
);
test!(
unitful_nan_max_first_arg,
"@use \"sass:math\";\na {\n color: max(math.acos(2), 1px);\n}\n",
"a {\n color: NaNdeg;\n}\n"
);
test!(
unitful_nan_max_last_arg,
"@use \"sass:math\";\na {\n color: max(1px, math.acos(2));\n}\n",
"a {\n color: 1px;\n}\n"
);
test!(
unitful_nan_max_middle_arg,
"@use \"sass:math\";\na {\n color: max(1px, math.acos(2), 0);\n}\n",
"a {\n color: 1px;\n}\n"
);
error!(
unitful_nan_nth_n,
"@use \"sass:math\";\na {\n color: nth([a], math.acos(2));\n}\n",
"Error: $n: NaNdeg is not an int."
);
error!(
unitful_nan_set_nth_n,
"@use \"sass:math\";\na {\n color: set-nth([a], math.acos(2), b);\n}\n",
"Error: $n: NaNdeg is not an int."
);