You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArithmeticExpressionEvaluatorTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2000-2013 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.sass.internal.expression;
  17. import org.junit.Assert;
  18. import org.junit.Test;
  19. import org.w3c.css.sac.LexicalUnit;
  20. import com.vaadin.sass.internal.expression.exception.IncompatibleUnitsException;
  21. import com.vaadin.sass.internal.parser.LexicalUnitImpl;
  22. public class ArithmeticExpressionEvaluatorTest {
  23. private ArithmeticExpressionEvaluator evaluator = new ArithmeticExpressionEvaluator();
  24. @Test
  25. public void testPrecedenceSameAsAppearOrder() {
  26. // 2 * 3 - 4 = 2
  27. LexicalUnitImpl operand2 = LexicalUnitImpl.createInteger(0, 0, null, 2);
  28. LexicalUnitImpl operatorMultiply = LexicalUnitImpl.createMultiply(0, 0,
  29. operand2);
  30. LexicalUnitImpl operand3 = LexicalUnitImpl.createInteger(0, 0,
  31. operatorMultiply, 3);
  32. LexicalUnitImpl operatorMinus = LexicalUnitImpl.createMinus(0, 0,
  33. operand3);
  34. LexicalUnitImpl operand4 = LexicalUnitImpl.createInteger(0, 0,
  35. operatorMinus, 4);
  36. LexicalUnitImpl result = evaluator.evaluate(operand2);
  37. Assert.assertEquals(2, result.getIntegerValue());
  38. }
  39. @Test
  40. public void testPrecedenceDifferFromAppearOrder() {
  41. // 2 - 3 * 4 = -10
  42. LexicalUnitImpl operand2 = LexicalUnitImpl.createInteger(0, 0, null, 2);
  43. LexicalUnitImpl operatorMinus = LexicalUnitImpl.createMinus(0, 0,
  44. operand2);
  45. LexicalUnitImpl operand3 = LexicalUnitImpl.createInteger(0, 0,
  46. operatorMinus, 3);
  47. LexicalUnitImpl operatorMultiply = LexicalUnitImpl.createMultiply(0, 0,
  48. operand3);
  49. LexicalUnitImpl operand4 = LexicalUnitImpl.createInteger(0, 0,
  50. operatorMultiply, 4);
  51. LexicalUnitImpl result = evaluator.evaluate(operand2);
  52. Assert.assertEquals(-10, result.getIntegerValue());
  53. }
  54. @Test(expected = IncompatibleUnitsException.class)
  55. public void testIncompatibleUnit() {
  56. // 2cm - 3px
  57. LexicalUnitImpl operand2 = LexicalUnitImpl.createCM(0, 0, null, 2);
  58. LexicalUnitImpl operatorMinus = LexicalUnitImpl.createMinus(0, 0,
  59. operand2);
  60. LexicalUnitImpl operand3 = LexicalUnitImpl.createPX(0, 0,
  61. operatorMinus, 3);
  62. evaluator.evaluate(operand2);
  63. }
  64. @Test
  65. public void testMultiplyWithUnitInfirstOperand() {
  66. // 2cm * 3 = 6cm
  67. LexicalUnitImpl operand2cm = LexicalUnitImpl.createCM(0, 0, null, 2);
  68. LexicalUnitImpl operatorMultiply = LexicalUnitImpl.createMultiply(0, 0,
  69. operand2cm);
  70. LexicalUnitImpl operand3 = LexicalUnitImpl.createInteger(0, 0,
  71. operatorMultiply, 3);
  72. LexicalUnitImpl result = evaluator.evaluate(operand2cm);
  73. Assert.assertEquals(6, result.getIntegerValue());
  74. Assert.assertEquals(LexicalUnit.SAC_CENTIMETER,
  75. result.getLexicalUnitType());
  76. }
  77. @Test
  78. public void testMultiplyWithUnitInSecondOperand() {
  79. // 2 * 3cm = 6cm
  80. LexicalUnitImpl operand2 = LexicalUnitImpl.createInteger(0, 0, null, 2);
  81. LexicalUnitImpl operatorMultiply = LexicalUnitImpl.createMultiply(0, 0,
  82. operand2);
  83. LexicalUnitImpl operand3cm = LexicalUnitImpl.createCM(0, 0,
  84. operatorMultiply, 3);
  85. LexicalUnitImpl result = evaluator.evaluate(operand2);
  86. Assert.assertEquals(6, result.getIntegerValue());
  87. Assert.assertEquals(LexicalUnit.SAC_CENTIMETER,
  88. result.getLexicalUnitType());
  89. }
  90. @Test
  91. public void testDivideWithSameUnit() {
  92. // 4cm / 2cm = 2
  93. LexicalUnitImpl operand4cm = LexicalUnitImpl.createCM(0, 0, null, 4);
  94. LexicalUnitImpl operatorDivide = LexicalUnitImpl.createSlash(0, 0,
  95. operand4cm);
  96. LexicalUnitImpl operand2cm = LexicalUnitImpl.createCM(0, 0,
  97. operatorDivide, 2);
  98. LexicalUnitImpl result = evaluator.evaluate(operand4cm);
  99. Assert.assertEquals(2, result.getIntegerValue());
  100. Assert.assertEquals(LexicalUnit.SAC_REAL, result.getLexicalUnitType());
  101. }
  102. @Test
  103. public void testDivideDenominatorWithoutUnit() {
  104. // 4cm / 2 = 2cm
  105. LexicalUnitImpl operand4cm = LexicalUnitImpl.createCM(0, 0, null, 4);
  106. LexicalUnitImpl operatorDivide = LexicalUnitImpl.createSlash(0, 0,
  107. operand4cm);
  108. LexicalUnitImpl operand2 = LexicalUnitImpl.createInteger(0, 0,
  109. operatorDivide, 2);
  110. LexicalUnitImpl result = evaluator.evaluate(operand4cm);
  111. Assert.assertEquals(2, result.getIntegerValue());
  112. Assert.assertEquals(LexicalUnit.SAC_CENTIMETER,
  113. result.getLexicalUnitType());
  114. }
  115. }