summaryrefslogtreecommitdiffstats
path: root/theme-compiler/tests
diff options
context:
space:
mode:
Diffstat (limited to 'theme-compiler/tests')
-rw-r--r--theme-compiler/tests/resources/automatic/css/basic_arithmetics.css31
-rw-r--r--theme-compiler/tests/resources/automatic/scss/basic_arithmetics.scss44
-rw-r--r--theme-compiler/tests/src/com/vaadin/sass/internal/expression/ArithmeticExpressionEvaluatorTest.java125
3 files changed, 200 insertions, 0 deletions
diff --git a/theme-compiler/tests/resources/automatic/css/basic_arithmetics.css b/theme-compiler/tests/resources/automatic/css/basic_arithmetics.css
new file mode 100644
index 0000000000..9fd33f2efe
--- /dev/null
+++ b/theme-compiler/tests/resources/automatic/css/basic_arithmetics.css
@@ -0,0 +1,31 @@
+.foo {
+ font: 10px / 8px;
+ font: 5px;
+ margin-left: 9px;
+}
+
+.foo {
+ size: 1;
+}
+
+.foo {
+ bar: 8;
+ bar: 8;
+ bar: 12;
+}
+
+.foo {
+ bar: 2 3;
+ bar: 5;
+ bar: 5;
+}
+
+.foo {
+ bar: 2 -3;
+ bar: -1;
+ bar: -1;
+}
+
+.foo {
+ bar: 14;
+} \ No newline at end of file
diff --git a/theme-compiler/tests/resources/automatic/scss/basic_arithmetics.scss b/theme-compiler/tests/resources/automatic/scss/basic_arithmetics.scss
new file mode 100644
index 0000000000..cc913fe048
--- /dev/null
+++ b/theme-compiler/tests/resources/automatic/scss/basic_arithmetics.scss
@@ -0,0 +1,44 @@
+/*
+*supports:
+* 1. standard arithmetic operations (+, -, *, /, %)
+* 2. / is treated as css operator, unless one of its operands is variable or there is another binary arithmetic operator
+*limits:
+* 1. cannot mix arithmetic and css operations, e.g. "margin: 1px + 3px 2px" will fail
+* 2. space between add and minus operator and their following operand is mandatory. e.g. "1 + 2" is valid, "1+2" is not
+* 3. parenthesis is not supported now.
+*/
+
+$div: 10px;
+.foo {
+ font: 10px/8px; // Plain CSS, no division
+ font: $div/2; // Uses a variable, does division
+ margin-left: 5px + 8px/2px; //Uses +, does division
+}
+
+.foo{
+ size: 5 % 2; // modular
+}
+
+$mul: 2*4; //valid multiply in variable
+$mul1: 2 * 4; //valid multiply in variable
+.foo{
+ bar: $mul;
+ bar: $mul1;
+ bar: 3*4; //valid multiply in declaration
+}
+
+.foo {
+ bar: 2 +3; //'+' is regarded as an unary operator, because no space between '+' and '3'
+ bar: 2+ 3; //valid add expression
+ bar: 2 + 3; //beautiful valid add expression
+}
+
+.foo {
+ bar: 2 -3; //'-' is regarded as an unary operator, because no space between '-' and '3'
+ bar: 2 - 3; //beautiful valid minus expression
+ bar: 2- 3; //valid minus expression
+}
+
+.foo {
+ bar: 2 + 3 * 4; // combinations
+} \ No newline at end of file
diff --git a/theme-compiler/tests/src/com/vaadin/sass/internal/expression/ArithmeticExpressionEvaluatorTest.java b/theme-compiler/tests/src/com/vaadin/sass/internal/expression/ArithmeticExpressionEvaluatorTest.java
new file mode 100644
index 0000000000..8978eb812e
--- /dev/null
+++ b/theme-compiler/tests/src/com/vaadin/sass/internal/expression/ArithmeticExpressionEvaluatorTest.java
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2000-2013 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.sass.internal.expression;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.w3c.css.sac.LexicalUnit;
+
+import com.vaadin.sass.internal.expression.exception.IncompatibleUnitsException;
+import com.vaadin.sass.internal.parser.LexicalUnitImpl;
+
+public class ArithmeticExpressionEvaluatorTest {
+ private ArithmeticExpressionEvaluator evaluator = new ArithmeticExpressionEvaluator();
+
+ @Test
+ public void testPrecedenceSameAsAppearOrder() {
+ // 2 * 3 - 4 = 2
+ LexicalUnitImpl operand2 = LexicalUnitImpl.createInteger(0, 0, null, 2);
+ LexicalUnitImpl operatorMultiply = LexicalUnitImpl.createMultiply(0, 0,
+ operand2);
+ LexicalUnitImpl operand3 = LexicalUnitImpl.createInteger(0, 0,
+ operatorMultiply, 3);
+ LexicalUnitImpl operatorMinus = LexicalUnitImpl.createMinus(0, 0,
+ operand3);
+ LexicalUnitImpl operand4 = LexicalUnitImpl.createInteger(0, 0,
+ operatorMinus, 4);
+ LexicalUnitImpl result = evaluator.evaluate(operand2);
+ Assert.assertEquals(2, result.getIntegerValue());
+ }
+
+ @Test
+ public void testPrecedenceDifferFromAppearOrder() {
+ // 2 - 3 * 4 = -10
+ LexicalUnitImpl operand2 = LexicalUnitImpl.createInteger(0, 0, null, 2);
+ LexicalUnitImpl operatorMinus = LexicalUnitImpl.createMinus(0, 0,
+ operand2);
+ LexicalUnitImpl operand3 = LexicalUnitImpl.createInteger(0, 0,
+ operatorMinus, 3);
+ LexicalUnitImpl operatorMultiply = LexicalUnitImpl.createMultiply(0, 0,
+ operand3);
+ LexicalUnitImpl operand4 = LexicalUnitImpl.createInteger(0, 0,
+ operatorMultiply, 4);
+ LexicalUnitImpl result = evaluator.evaluate(operand2);
+ Assert.assertEquals(-10, result.getIntegerValue());
+ }
+
+ @Test(expected = IncompatibleUnitsException.class)
+ public void testIncompatibleUnit() {
+ // 2cm - 3px
+ LexicalUnitImpl operand2 = LexicalUnitImpl.createCM(0, 0, null, 2);
+ LexicalUnitImpl operatorMinus = LexicalUnitImpl.createMinus(0, 0,
+ operand2);
+ LexicalUnitImpl operand3 = LexicalUnitImpl.createPX(0, 0,
+ operatorMinus, 3);
+ evaluator.evaluate(operand2);
+ }
+
+ @Test
+ public void testMultiplyWithUnitInfirstOperand() {
+ // 2cm * 3 = 6cm
+ LexicalUnitImpl operand2cm = LexicalUnitImpl.createCM(0, 0, null, 2);
+ LexicalUnitImpl operatorMultiply = LexicalUnitImpl.createMultiply(0, 0,
+ operand2cm);
+ LexicalUnitImpl operand3 = LexicalUnitImpl.createInteger(0, 0,
+ operatorMultiply, 3);
+ LexicalUnitImpl result = evaluator.evaluate(operand2cm);
+ Assert.assertEquals(6, result.getIntegerValue());
+ Assert.assertEquals(LexicalUnit.SAC_CENTIMETER,
+ result.getLexicalUnitType());
+ }
+
+ @Test
+ public void testMultiplyWithUnitInSecondOperand() {
+ // 2 * 3cm = 6cm
+ LexicalUnitImpl operand2 = LexicalUnitImpl.createInteger(0, 0, null, 2);
+ LexicalUnitImpl operatorMultiply = LexicalUnitImpl.createMultiply(0, 0,
+ operand2);
+ LexicalUnitImpl operand3cm = LexicalUnitImpl.createCM(0, 0,
+ operatorMultiply, 3);
+ LexicalUnitImpl result = evaluator.evaluate(operand2);
+ Assert.assertEquals(6, result.getIntegerValue());
+ Assert.assertEquals(LexicalUnit.SAC_CENTIMETER,
+ result.getLexicalUnitType());
+ }
+
+ @Test
+ public void testDivideWithSameUnit() {
+ // 4cm / 2cm = 2
+ LexicalUnitImpl operand4cm = LexicalUnitImpl.createCM(0, 0, null, 4);
+ LexicalUnitImpl operatorDivide = LexicalUnitImpl.createSlash(0, 0,
+ operand4cm);
+ LexicalUnitImpl operand2cm = LexicalUnitImpl.createCM(0, 0,
+ operatorDivide, 2);
+ LexicalUnitImpl result = evaluator.evaluate(operand4cm);
+ Assert.assertEquals(2, result.getIntegerValue());
+ Assert.assertEquals(LexicalUnit.SAC_REAL, result.getLexicalUnitType());
+ }
+
+ @Test
+ public void testDivideDenominatorWithoutUnit() {
+ // 4cm / 2 = 2cm
+ LexicalUnitImpl operand4cm = LexicalUnitImpl.createCM(0, 0, null, 4);
+ LexicalUnitImpl operatorDivide = LexicalUnitImpl.createSlash(0, 0,
+ operand4cm);
+ LexicalUnitImpl operand2 = LexicalUnitImpl.createInteger(0, 0,
+ operatorDivide, 2);
+ LexicalUnitImpl result = evaluator.evaluate(operand4cm);
+ Assert.assertEquals(2, result.getIntegerValue());
+ Assert.assertEquals(LexicalUnit.SAC_CENTIMETER,
+ result.getLexicalUnitType());
+ }
+}