From 313913734adaf48de1088f4ce6346b74a282a284 Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Mon, 27 Jul 2020 00:07:29 -0400 Subject: [PATCH] tests for `math.asin` and `math.atan` --- src/builtin/modules/math.rs | 71 +++++++++++++++++++++++++++++++++++-- src/value/number/mod.rs | 5 +-- tests/math-module.rs | 71 ++++++++++++++++++++++++++++++++++++- 3 files changed, 141 insertions(+), 6 deletions(-) diff --git a/src/builtin/modules/math.rs b/src/builtin/modules/math.rs index 814f83b..fef1d91 100644 --- a/src/builtin/modules/math.rs +++ b/src/builtin/modules/math.rs @@ -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 { args.max_args(1)?; let number = args.get_err(0, "number")?; @@ -221,6 +218,74 @@ fn acos(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { }) } +fn asin(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { + 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 { + 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 { args.max_args(2)?; todo!() diff --git a/src/value/number/mod.rs b/src/value/number/mod.rs index 18547cf..e6b87c1 100644 --- a/src/value/number/mod.rs +++ b/src/value/number/mod.rs @@ -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 { diff --git a/tests/math-module.rs b/tests/math-module.rs index 2bff4b4..1b51562 100644 --- a/tests/math-module.rs +++ b/tests/math-module.rs @@ -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" +);