tests for math.asin and math.atan

This commit is contained in:
Connor Skees 2020-07-27 00:07:29 -04:00
parent a3e6607394
commit 313913734a
3 changed files with 141 additions and 6 deletions

View File

@ -183,9 +183,6 @@ trig_fn!(cos, cos_deg);
trig_fn!(sin, sin_deg);
trig_fn!(tan, tan_deg);
trig_fn!(asin, asin_deg);
trig_fn!(atan, atan_deg);
fn acos(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
let number = args.get_err(0, "number")?;
@ -221,6 +218,74 @@ fn acos(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
})
}
fn asin(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
let number = args.get_err(0, "number")?;
Ok(match number {
Value::Dimension(Some(n), Unit::None, ..) => {
if n > Number::from(1) || n < Number::from(-1) {
return Ok(Value::Dimension(None, Unit::Deg, true));
} else if n.is_zero() {
return Ok(Value::Dimension(Some(Number::zero()), Unit::Deg, true));
}
Value::Dimension(n.asin(), Unit::Deg, true)
}
v @ Value::Dimension(Some(..), ..) => {
return Err((
format!(
"$number: Expected {} to be unitless.",
v.inspect(args.span())?
),
args.span(),
)
.into())
}
Value::Dimension(None, ..) => Value::Dimension(None, Unit::Deg, true),
v => {
return Err((
format!("$number: {} is not a number.", v.inspect(args.span())?),
args.span(),
)
.into())
}
})
}
fn atan(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
let number = args.get_err(0, "number")?;
Ok(match number {
Value::Dimension(Some(n), Unit::None, ..) => {
if n.is_zero() {
return Ok(Value::Dimension(Some(Number::zero()), Unit::Deg, true));
}
Value::Dimension(n.atan(), Unit::Deg, true)
}
v @ Value::Dimension(Some(..), ..) => {
return Err((
format!(
"$number: Expected {} to be unitless.",
v.inspect(args.span())?
),
args.span(),
)
.into())
}
Value::Dimension(None, ..) => Value::Dimension(None, Unit::Deg, true),
v => {
return Err((
format!("$number: {} is not a number.", v.inspect(args.span())?),
args.span(),
)
.into())
}
})
}
fn atan2(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(2)?;
todo!()

View File

@ -155,9 +155,10 @@ impl Number {
trig_fn!(cos, cos_deg);
trig_fn!(sin, sin_deg);
trig_fn!(tan, tan_deg);
inverse_trig_fn!(acos);
trig_fn!(asin, asin_deg);
trig_fn!(atan, atan_deg);
inverse_trig_fn!(asin);
inverse_trig_fn!(atan);
}
impl Default for Number {

View File

@ -213,7 +213,6 @@ test!(
"@use 'sass:math';\na {\n color: math.tan(2 * math.$pi);\n}\n",
"a {\n color: 0;\n}\n"
);
test!(
acos_above_one,
"@use 'sass:math';\na {\n color: math.acos(2);\n}\n",
@ -249,3 +248,73 @@ test!(
"@use 'sass:math';\na {\n color: math.acos((0 / 0));\n}\n",
"a {\n color: NaNdeg;\n}\n"
);
test!(
asin_above_one,
"@use 'sass:math';\na {\n color: math.asin(2);\n}\n",
"a {\n color: NaNdeg;\n}\n"
);
test!(
asin_below_negative_one,
"@use 'sass:math';\na {\n color: math.asin(-2);\n}\n",
"a {\n color: NaNdeg;\n}\n"
);
test!(
asin_one,
"@use 'sass:math';\na {\n color: math.asin(1);\n}\n",
"a {\n color: 90deg;\n}\n"
);
test!(
asin_negative_one,
"@use 'sass:math';\na {\n color: math.asin(-1);\n}\n",
"a {\n color: -90deg;\n}\n"
);
test!(
asin_zero,
"@use 'sass:math';\na {\n color: math.asin(0);\n}\n",
"a {\n color: 0deg;\n}\n"
);
test!(
asin_point_five,
"@use 'sass:math';\na {\n color: math.asin(.5);\n}\n",
"a {\n color: 30deg;\n}\n"
);
test!(
asin_nan,
"@use 'sass:math';\na {\n color: math.asin((0 / 0));\n}\n",
"a {\n color: NaNdeg;\n}\n"
);
test!(
atan_above_one,
"@use 'sass:math';\na {\n color: math.atan(2);\n}\n",
"a {\n color: 63.4349488229deg;\n}\n"
);
test!(
atan_below_negative_one,
"@use 'sass:math';\na {\n color: math.atan(-2);\n}\n",
"a {\n color: -63.4349488229deg;\n}\n"
);
test!(
atan_one,
"@use 'sass:math';\na {\n color: math.atan(1);\n}\n",
"a {\n color: 45deg;\n}\n"
);
test!(
atan_negative_one,
"@use 'sass:math';\na {\n color: math.atan(-1);\n}\n",
"a {\n color: -45deg;\n}\n"
);
test!(
atan_zero,
"@use 'sass:math';\na {\n color: math.atan(0);\n}\n",
"a {\n color: 0deg;\n}\n"
);
test!(
atan_point_five,
"@use 'sass:math';\na {\n color: math.atan(.5);\n}\n",
"a {\n color: 26.5650511771deg;\n}\n"
);
test!(
atan_nan,
"@use 'sass:math';\na {\n color: math.atan((0 / 0));\n}\n",
"a {\n color: NaNdeg;\n}\n"
);