From 2a79fa2a4053c0d8bd960652da3d5524d9f5445a Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Fri, 22 May 2020 23:30:38 -0400 Subject: [PATCH] implement some of the more esoteric binary ops addition, subtraction, and multiplication of functions and maps, along with correct error messages and tests --- src/value/ops.rs | 64 ++++++++++++++++++++++------------------- tests/addition.rs | 18 ++++++++++++ tests/error.rs | 8 ------ tests/multiplication.rs | 23 +++++++++++++++ tests/subtraction.rs | 33 +++++++++++++++++++++ 5 files changed, 108 insertions(+), 38 deletions(-) create mode 100644 tests/multiplication.rs diff --git a/src/value/ops.rs b/src/value/ops.rs index 998bcf7..a738145 100644 --- a/src/value/ops.rs +++ b/src/value/ops.rs @@ -186,9 +186,9 @@ impl Value { return Err(( format!( "Undefined operation \"{} {} {}\".", - v.to_css_string(span)?, + v.inspect(span)?, op, - other.to_css_string(span)? + other.inspect(span)? ), span, ) @@ -217,9 +217,9 @@ impl Value { return Err(( format!( "Undefined operation \"{} {} {}\".", - self.to_css_string(span)?, + self.inspect(span)?, op, - other.to_css_string(span)? + other.inspect(span)? ), span, ) @@ -256,14 +256,14 @@ impl Value { } let precedence = Op::Plus.precedence(); Ok(match self { - Self::Map(..) => { + Self::Map(..) | Self::Function(..) => { return Err(( format!("{} isn't a valid CSS value.", self.inspect(span)?), span, ) .into()) } - Self::Function(..) | Self::ArgList(..) => todo!(), + Self::ArgList(..) => todo!(), Self::Important | Self::True | Self::False => match other { Self::String(s, QuoteKind::Quoted) => Value::String( format!("{}{}", self.to_css_string(span)?, s), @@ -316,13 +316,20 @@ impl Value { format!("{}{}{}", num, unit, other.to_css_string(span)?), QuoteKind::None, ), + Self::Map(..) | Self::Function(..) => { + return Err(( + format!("{} isn't a valid CSS value.", other.inspect(span)?), + span, + ) + .into()) + } _ => { return Err(( format!( "Undefined operation \"{}{} + {}\".", num, unit, - other.to_css_string(span)? + other.inspect(span)? ), span, ) @@ -338,11 +345,7 @@ impl Value { ), _ => { return Err(( - format!( - "Undefined operation \"{} + {}\".", - c, - other.to_css_string(span)? - ), + format!("Undefined operation \"{} + {}\".", c, other.inspect(span)?), span, ) .into()) @@ -396,7 +399,9 @@ impl Value { } let precedence = Op::Mul.precedence(); Ok(match self { - Self::Null => todo!(), + Self::Null => { + Value::String(format!("-{}", other.to_css_string(span)?), QuoteKind::None) + } Self::Dimension(num, unit) => match other { Self::Dimension(num2, unit2) => { if !unit.comparable(&unit2) { @@ -428,6 +433,13 @@ impl Value { format!("{}{}-{}", num, unit, other.to_css_string(span)?), QuoteKind::None, ), + Self::Map(..) | Self::Function(..) => { + return Err(( + format!("{} isn't a valid CSS value.", other.inspect(span)?), + span, + ) + .into()) + } _ => todo!(), }, Self::Color(c) => match other { @@ -437,11 +449,7 @@ impl Value { Self::Null => Value::String(format!("{}-", c), QuoteKind::None), Self::Dimension(..) | Self::Color(..) => { return Err(( - format!( - "Undefined operation \"{} - {}\".", - c, - other.to_css_string(span)? - ), + format!("Undefined operation \"{} - {}\".", c, other.inspect(span)?), span, ) .into()) @@ -544,7 +552,7 @@ impl Value { "Undefined operation \"{}{} * {}\".", num, unit, - other.to_css_string(span)? + other.inspect(span)? ), span, ) @@ -576,8 +584,8 @@ impl Value { return Err(( format!( "Undefined operation \"{} * {}\".", - self.to_css_string(span)?, - other.to_css_string(span)? + self.inspect(span)?, + other.inspect(span)? ), span, ) @@ -628,11 +636,7 @@ impl Value { Self::Null => Value::String(format!("{}/", c), QuoteKind::None), Self::Dimension(..) | Self::Color(..) => { return Err(( - format!( - "Undefined operation \"{} / {}\".", - c, - other.to_css_string(span)? - ), + format!("Undefined operation \"{} / {}\".", c, other.inspect(span)?), span, ) .into()) @@ -720,8 +724,8 @@ impl Value { return Err(( format!( "Undefined operation \"{} % {}\".", - Value::Dimension(n, u).to_css_string(span)?, - other.to_css_string(span)? + Value::Dimension(n, u).inspect(span)?, + other.inspect(span)? ), span, ) @@ -732,8 +736,8 @@ impl Value { return Err(( format!( "Undefined operation \"{} % {}\".", - self.to_css_string(span)?, - other.to_css_string(span)? + self.inspect(span)?, + other.inspect(span)? ), span, ) diff --git a/tests/addition.rs b/tests/addition.rs index 95d13db..795506c 100644 --- a/tests/addition.rs +++ b/tests/addition.rs @@ -278,3 +278,21 @@ test!( "a {\n color: 1+/2;\n}\n", "a {\n color: 1/2;\n}\n" ); +error!( + map_lhs_add, + "a {color: (a: b) + 1;}", "Error: (a: b) isn't a valid CSS value." +); +error!( + map_rhs_add, + "a {color: 1 + (a: b);}", "Error: (a: b) isn't a valid CSS value." +); +error!( + function_lhs_add, + "a {color: get-function(lighten) + 1;}", + "Error: get-function(\"lighten\") isn't a valid CSS value." +); +error!( + function_rhs_add, + "a {color: 1 + get-function(lighten);}", + "Error: get-function(\"lighten\") isn't a valid CSS value." +); diff --git a/tests/error.rs b/tests/error.rs index 3f0df3c..ab14a79 100644 --- a/tests/error.rs +++ b/tests/error.rs @@ -167,11 +167,3 @@ error!( operator_mul, "a {color: 5 - *;}", "Error: Expected expression." ); -error!( - map_lhs_add, - "a {color: (a: b) + 1;}", "Error: (a: b) isn't a valid CSS value." -); -error!( - map_rhs_add, - "a {color: 1 + (a: b);}", "Error: (a: b) isn't a valid CSS value." -); diff --git a/tests/multiplication.rs b/tests/multiplication.rs new file mode 100644 index 0000000..e61d6d2 --- /dev/null +++ b/tests/multiplication.rs @@ -0,0 +1,23 @@ +#![cfg(test)] + +#[macro_use] +mod macros; + +error!( + map_lhs_mul, + "a {color: (a: b) * 1;}", "Error: Undefined operation \"(a: b) * 1\"." +); +error!( + map_rhs_mul, + "a {color: 1 * (a: b);}", "Error: Undefined operation \"1 * (a: b)\"." +); +error!( + function_lhs_mul, + "a {color: get-function(lighten) * 1;}", + "Error: Undefined operation \"get-function(\"lighten\") * 1\"." +); +error!( + function_rhs_mul, + "a {color: 1 * get-function(lighten);}", + "Error: Undefined operation \"1 * get-function(\"lighten\")\"." +); diff --git a/tests/subtraction.rs b/tests/subtraction.rs index 8ad6fb4..e15d8f7 100644 --- a/tests/subtraction.rs +++ b/tests/subtraction.rs @@ -214,3 +214,36 @@ test!( "a {\n color: 1em- 0.0em;\n}\n", "a {\n color: 1em- 0em;\n}\n" ); +test!( + null_minus_number, + "a {\n color: null - 1;\n}\n", + "a {\n color: -1;\n}\n" +); +test!( + null_minus_unquoted_string, + "a {\n color: null - foo;\n}\n", + "a {\n color: -foo;\n}\n" +); +error!( + null_minus_function, + "a {\n color: null - get-function(lighten);\n}\n", + "Error: get-function(\"lighten\") isn't a valid CSS value." +); +error!( + map_lhs_sub, + "a {color: (a: b) - 1;}", "Error: (a: b) isn't a valid CSS value." +); +error!( + map_rhs_sub, + "a {color: 1 - (a: b);}", "Error: (a: b) isn't a valid CSS value." +); +error!( + function_lhs_sub, + "a {color: get-function(lighten) - 1;}", + "Error: get-function(\"lighten\") isn't a valid CSS value." +); +error!( + function_rhs_sub, + "a {color: 1 - get-function(lighten);}", + "Error: get-function(\"lighten\") isn't a valid CSS value." +);