PHP and Javascript implementations of a simple markdown parser
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ExpressionSetTests.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. class ExpressionSetTests extends BaseTest {
  2. test_simpleMath() {
  3. const grid = new SpreadsheetGrid(1, 1);
  4. grid.cells[0][0].originalValue = CellValue.fromCellString('=7*3');
  5. const expressionSet = new CellExpressionSet(grid);
  6. expressionSet.calculateCells();
  7. const expected = CellValue.fromValue(21);
  8. this.assertEqual(grid.cells[0][0].outputValue, expected);
  9. }
  10. test_reference() {
  11. const grid = new SpreadsheetGrid(3, 1);
  12. grid.cells[0][0].originalValue = CellValue.fromValue(123);
  13. grid.cells[1][0].originalValue = CellValue.fromValue(3);
  14. grid.cells[2][0].originalValue = CellValue.fromCellString('=A1*B1');
  15. const expressionSet = new CellExpressionSet(grid);
  16. expressionSet.calculateCells();
  17. const expected = CellValue.fromValue(369);
  18. this.assertEqual(grid.cells[2][0].outputValue, expected);
  19. }
  20. test_infixPriority() {
  21. const grid = new SpreadsheetGrid(1, 1);
  22. grid.cells[0][0].originalValue = CellValue.fromCellString('=4*9/3+7-4');
  23. const expressionSet = new CellExpressionSet(grid);
  24. expressionSet.calculateCells();
  25. const expected = CellValue.fromValue(15);
  26. this.assertEqual(grid.cells[0][0].outputValue, expected);
  27. }
  28. test_filledFormula() {
  29. const grid = new SpreadsheetGrid(3, 3);
  30. grid.cells[0][0].originalValue = CellValue.fromValue(4);
  31. grid.cells[1][0].originalValue = CellValue.fromValue(5);
  32. grid.cells[2][0].originalValue = CellValue.fromCellString('=A1*B1 FILL');
  33. grid.cells[0][1].originalValue = CellValue.fromValue(6);
  34. grid.cells[1][1].originalValue = CellValue.fromValue(7);
  35. grid.cells[0][2].originalValue = CellValue.fromValue(8);
  36. grid.cells[1][2].originalValue = CellValue.fromValue(9);
  37. const expressionSet = new CellExpressionSet(grid);
  38. expressionSet.calculateCells();
  39. this.assertEqual(grid.cells[2][0].outputValue, CellValue.fromValue(20));
  40. this.assertEqual(grid.cells[2][1].outputValue, CellValue.fromValue(42));
  41. this.assertEqual(grid.cells[2][2].outputValue, CellValue.fromValue(72));
  42. }
  43. test_dependencies() {
  44. const grid = new SpreadsheetGrid(2, 4);
  45. grid.cells[0][0].originalValue = CellValue.fromValue(1);
  46. grid.cells[1][0].originalValue = CellValue.fromCellString('=A1+B2');
  47. grid.cells[0][1].originalValue = CellValue.fromValue(2);
  48. grid.cells[1][1].originalValue = CellValue.fromCellString('=A2+B3');
  49. grid.cells[0][2].originalValue = CellValue.fromValue(3);
  50. grid.cells[1][2].originalValue = CellValue.fromCellString('=A3+B4');
  51. grid.cells[0][3].originalValue = CellValue.fromValue(4);
  52. grid.cells[1][3].originalValue = CellValue.fromCellString('=A4');
  53. const expressionSet = new CellExpressionSet(grid);
  54. expressionSet.calculateCells();
  55. this.assertEqual(grid.cells[1][0].outputValue, CellValue.fromValue(10));
  56. this.assertEqual(grid.cells[1][1].outputValue, CellValue.fromValue(9));
  57. this.assertEqual(grid.cells[1][2].outputValue, CellValue.fromValue(7));
  58. this.assertEqual(grid.cells[1][3].outputValue, CellValue.fromValue(4));
  59. }
  60. _test_simple_formula(formula, expected) {
  61. const grid = new SpreadsheetGrid(1, 1);
  62. grid.cells[0][0].originalValue = CellValue.fromCellString(formula);
  63. const expressionSet = new CellExpressionSet(grid);
  64. expressionSet.calculateCells();
  65. const result = grid.cells[0][0].outputValue;
  66. const exp = (expected instanceof CellValue) ? expected : CellValue.fromValue(expected);
  67. this.assertEqual(result.type, exp.type);
  68. this.assertEqual(result.decimals, exp.decimals);
  69. this.assertEqual(result.formattedValue, exp.formattedValue);
  70. this.assertEqual(result.value, exp.value);
  71. }
  72. test_func_abs() {
  73. this._test_simple_formula('=ABS(-3)', 3);
  74. this._test_simple_formula('=ABS(4)', 4);
  75. }
  76. test_func_and() {
  77. this._test_simple_formula('=AND(FALSE, FALSE)', false);
  78. this._test_simple_formula('=AND(FALSE, TRUE)', false);
  79. this._test_simple_formula('=AND(TRUE, FALSE)', false);
  80. this._test_simple_formula('=AND(TRUE, TRUE)', true);
  81. this._test_simple_formula('=AND(TRUE, TRUE, TRUE, TRUE)', true);
  82. this._test_simple_formula('=AND(TRUE, TRUE, TRUE, FALSE)', false);
  83. }
  84. test_func_average() {
  85. this._test_simple_formula('=AVERAGE(4, 6, 2, 4)', 4);
  86. this._test_simple_formula('=AVERAGE(4, 6, 2, "foo", 4)', 4);
  87. }
  88. test_func_ceiling() {
  89. this._test_simple_formula('=CEILING(3.1)', 4);
  90. this._test_simple_formula('=CEILING(3)', 3);
  91. this._test_simple_formula('=CEILING(-3.1)', -3);
  92. }
  93. test_func_exp() {
  94. this._test_simple_formula('=EXP(1)', 2.718281828459045);
  95. }
  96. test_func_floor() {
  97. this._test_simple_formula('=FLOOR(3.1)', 3);
  98. this._test_simple_formula('=FLOOR(3)', 3);
  99. this._test_simple_formula('=FLOOR(-3.1)', -4);
  100. }
  101. test_func_if() {
  102. this._test_simple_formula('=IF(FALSE, 4, 6)', 6);
  103. this._test_simple_formula('=IF(TRUE, 4, 6)', 4);
  104. }
  105. test_func_ifs() {
  106. this._test_simple_formula('=IFS(TRUE, 1, FALSE, 2, FALSE, 3, FALSE, 4, 5)', 1);
  107. this._test_simple_formula('=IFS(FALSE, 1, TRUE, 2, FALSE, 3, FALSE, 4, 5)', 2);
  108. this._test_simple_formula('=IFS(FALSE, 1, FALSE, 2, TRUE, 3, FALSE, 4, 5)', 3);
  109. this._test_simple_formula('=IFS(FALSE, 1, FALSE, 2, FALSE, 3, TRUE, 4, 5)', 4);
  110. this._test_simple_formula('=IFS(FALSE, 1, FALSE, 2, FALSE, 3, FALSE, 4, 5)', 5);
  111. }
  112. test_func_ln() {
  113. this._test_simple_formula('=LN(2.718281828459045)', 1);
  114. }
  115. test_func_log() {
  116. this._test_simple_formula('=LOG(1000, 10)', 3);
  117. this._test_simple_formula('=LOG(64, 2)', 6);
  118. }
  119. test_func_lower() {
  120. this._test_simple_formula('=LOWER("MiXeD")', 'mixed');
  121. }
  122. test_func_max() {
  123. this._test_simple_formula('=MAX(4, 8, 5, 2)', 8);
  124. }
  125. test_func_min() {
  126. this._test_simple_formula('=MIN(4, 8, 5, 2)', 2);
  127. }
  128. test_func_mod() {
  129. this._test_simple_formula('=MOD(37, 4)', 1);
  130. }
  131. test_func_not() {
  132. this._test_simple_formula('=NOT(TRUE)', false);
  133. this._test_simple_formula('=NOT(FALSE)', true);
  134. }
  135. test_func_or() {
  136. this._test_simple_formula('=OR(FALSE, FALSE)', false);
  137. this._test_simple_formula('=OR(FALSE, TRUE)', true);
  138. this._test_simple_formula('=OR(TRUE, FALSE)', true);
  139. this._test_simple_formula('=OR(TRUE, TRUE)', true);
  140. this._test_simple_formula('=OR(FALSE, FALSE, FALSE, TRUE)', true);
  141. }
  142. test_func_power() {
  143. this._test_simple_formula('=POWER(2, 3)', 8);
  144. }
  145. test_func_round() {
  146. this._test_simple_formula('=ROUND(3.1)', 3);
  147. this._test_simple_formula('=ROUND(3.5)', 4);
  148. this._test_simple_formula('=ROUND(4)', 4);
  149. this._test_simple_formula('=ROUND(-3.1)', -3);
  150. this._test_simple_formula('=ROUND(-3.5)', -3);
  151. this._test_simple_formula('=ROUND(-3.9)', -4);
  152. this._test_simple_formula('=ROUND(3.1415926535, 1)', 3.1);
  153. this._test_simple_formula('=ROUND(3.1415926535, 2)', 3.14);
  154. this._test_simple_formula('=ROUND(31.415926535, -1)', 30);
  155. }
  156. test_func_sqrt() {
  157. this._test_simple_formula('=SQRT(16)', 4);
  158. }
  159. test_func_substitute() {
  160. this._test_simple_formula('=SUBSTITUTE("cat sat on the mat", "at", "ot")', 'cot sot on the mot');
  161. this._test_simple_formula('=SUBSTITUTE("cAt saT on the mat", "at", "ot")', 'cot sot on the mot');
  162. this._test_simple_formula('=SUBSTITUTE("c.*t s.*t on the m.*t", ".*t", "ot")', 'cot sot on the mot');
  163. }
  164. test_func_sum() {
  165. this._test_simple_formula('=SUM(1, 2, 3, 4, 5)', 15);
  166. }
  167. test_func_upper() {
  168. this._test_simple_formula('=UPPER("mIxEd")', 'MIXED');
  169. }
  170. test_func_xor() {
  171. this._test_simple_formula('=XOR(FALSE, FALSE)', false);
  172. this._test_simple_formula('=XOR(FALSE, TRUE)', true);
  173. this._test_simple_formula('=XOR(TRUE, FALSE)', true);
  174. this._test_simple_formula('=XOR(TRUE, TRUE)', false);
  175. this._test_simple_formula('=XOR(FALSE, FALSE, TRUE)', true);
  176. this._test_simple_formula('=XOR(TRUE, FALSE, TRUE)', false);
  177. }
  178. test_format() {
  179. this._test_simple_formula('=2.718281828459045 ; number 3', new CellValue('2.718', 2.718281828459045, 'number', 3));
  180. this._test_simple_formula('=2.718281828459045 ; percent 2', new CellValue('271.83%', 2.718281828459045, 'percent', 2));
  181. this._test_simple_formula('=2.718281828459045 ; currency 2', new CellValue('$2.72', 2.718281828459045, 'currency', 2));
  182. }
  183. }