PHP and Javascript implementations of a simple markdown parser
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. const actual = grid.cells[0][0].outputValue;
  9. this.assertEqual(expected, actual);
  10. }
  11. test_reference() {
  12. const grid = new SpreadsheetGrid(3, 1);
  13. grid.cells[0][0].originalValue = CellValue.fromValue(123);
  14. grid.cells[1][0].originalValue = CellValue.fromValue(3);
  15. grid.cells[2][0].originalValue = CellValue.fromCellString('=A1*B1');
  16. const expressionSet = new CellExpressionSet(grid);
  17. expressionSet.calculateCells();
  18. const expected = CellValue.fromValue(369);
  19. const actual = grid.cells[2][0].outputValue;
  20. this.assertEqual(expected, actual);
  21. }
  22. test_infixPriority() {
  23. const grid = new SpreadsheetGrid(1, 1);
  24. grid.cells[0][0].originalValue = CellValue.fromCellString('=4*9/3+7-4');
  25. const expressionSet = new CellExpressionSet(grid);
  26. expressionSet.calculateCells();
  27. const expected = CellValue.fromValue(15);
  28. const actual = grid.cells[0][0].outputValue;
  29. this.assertEqual(expected, actual);
  30. }
  31. test_filledFormula() {
  32. const grid = new SpreadsheetGrid(3, 3);
  33. grid.cells[0][0].originalValue = CellValue.fromValue(4);
  34. grid.cells[1][0].originalValue = CellValue.fromValue(5);
  35. grid.cells[2][0].originalValue = CellValue.fromCellString('=A1*B1 FILL');
  36. grid.cells[0][1].originalValue = CellValue.fromValue(6);
  37. grid.cells[1][1].originalValue = CellValue.fromValue(7);
  38. grid.cells[0][2].originalValue = CellValue.fromValue(8);
  39. grid.cells[1][2].originalValue = CellValue.fromValue(9);
  40. const expressionSet = new CellExpressionSet(grid);
  41. expressionSet.calculateCells();
  42. this.assertEqual(CellValue.fromValue(20), grid.cells[2][0].outputValue);
  43. this.assertEqual(CellValue.fromValue(42), grid.cells[2][1].outputValue);
  44. this.assertEqual(CellValue.fromValue(72), grid.cells[2][2].outputValue);
  45. }
  46. test_dependencies() {
  47. const grid = new SpreadsheetGrid(2, 4);
  48. grid.cells[0][0].originalValue = CellValue.fromValue(1);
  49. grid.cells[1][0].originalValue = CellValue.fromCellString('=A1+B2');
  50. grid.cells[0][1].originalValue = CellValue.fromValue(2);
  51. grid.cells[1][1].originalValue = CellValue.fromCellString('=A2+B3');
  52. grid.cells[0][2].originalValue = CellValue.fromValue(3);
  53. grid.cells[1][2].originalValue = CellValue.fromCellString('=A3+B4');
  54. grid.cells[0][3].originalValue = CellValue.fromValue(4);
  55. grid.cells[1][3].originalValue = CellValue.fromCellString('=A4');
  56. const expressionSet = new CellExpressionSet(grid);
  57. expressionSet.calculateCells();
  58. this.assertEqual(CellValue.fromValue(10), grid.cells[1][0].outputValue);
  59. this.assertEqual(CellValue.fromValue(9), grid.cells[1][1].outputValue);
  60. this.assertEqual(CellValue.fromValue(7), grid.cells[1][2].outputValue);
  61. this.assertEqual(CellValue.fromValue(4), grid.cells[1][3].outputValue);
  62. }
  63. _test_simple_formula(expected, formula) {
  64. const grid = new SpreadsheetGrid(1, 1);
  65. grid.cells[0][0].originalValue = CellValue.fromCellString(formula);
  66. const expressionSet = new CellExpressionSet(grid);
  67. expressionSet.calculateCells();
  68. const actual = grid.cells[0][0].outputValue;
  69. const exp = (expected instanceof CellValue) ? expected : CellValue.fromValue(expected);
  70. this.assertEqual(exp.type, actual.type);
  71. this.assertEqual(exp.decimals, actual.decimals);
  72. this.assertEqual(exp.formattedValue, actual.formattedValue);
  73. this.assertEqual(exp.value, actual.value);
  74. }
  75. test_func_abs() {
  76. this._test_simple_formula(3, '=ABS(-3)');
  77. this._test_simple_formula(4, '=ABS(4)');
  78. }
  79. test_func_and() {
  80. this._test_simple_formula(false, '=AND(FALSE, FALSE)');
  81. this._test_simple_formula(false, '=AND(FALSE, TRUE)');
  82. this._test_simple_formula(false, '=AND(TRUE, FALSE)');
  83. this._test_simple_formula(true, '=AND(TRUE, TRUE)');
  84. this._test_simple_formula(true, '=AND(TRUE, TRUE, TRUE, TRUE)');
  85. this._test_simple_formula(false, '=AND(TRUE, TRUE, TRUE, FALSE)');
  86. }
  87. test_func_average() {
  88. this._test_simple_formula(4, '=AVERAGE(4, 6, 2, 4)');
  89. this._test_simple_formula(4, '=AVERAGE(4, 6, 2, "foo", 4)');
  90. }
  91. test_func_ceiling() {
  92. this._test_simple_formula(4, '=CEILING(3.1)');
  93. this._test_simple_formula(3, '=CEILING(3)');
  94. this._test_simple_formula(-3, '=CEILING(-3.1)');
  95. }
  96. test_func_exp() {
  97. this._test_simple_formula(2.718281828459045, '=EXP(1)');
  98. }
  99. test_func_floor() {
  100. this._test_simple_formula(3, '=FLOOR(3.1)');
  101. this._test_simple_formula(3, '=FLOOR(3)');
  102. this._test_simple_formula(-4, '=FLOOR(-3.1)');
  103. }
  104. test_func_if() {
  105. this._test_simple_formula(6, '=IF(FALSE, 4, 6)');
  106. this._test_simple_formula(4, '=IF(TRUE, 4, 6)');
  107. }
  108. test_func_ifs() {
  109. this._test_simple_formula(1, '=IFS(TRUE, 1, FALSE, 2, FALSE, 3, FALSE, 4, 5)');
  110. this._test_simple_formula(2, '=IFS(FALSE, 1, TRUE, 2, FALSE, 3, FALSE, 4, 5)');
  111. this._test_simple_formula(3, '=IFS(FALSE, 1, FALSE, 2, TRUE, 3, FALSE, 4, 5)');
  112. this._test_simple_formula(4, '=IFS(FALSE, 1, FALSE, 2, FALSE, 3, TRUE, 4, 5)');
  113. this._test_simple_formula(5, '=IFS(FALSE, 1, FALSE, 2, FALSE, 3, FALSE, 4, 5)');
  114. }
  115. test_func_ln() {
  116. this._test_simple_formula(1, '=LN(2.718281828459045)');
  117. }
  118. test_func_log() {
  119. this._test_simple_formula(3, '=LOG(1000, 10)');
  120. this._test_simple_formula(6, '=LOG(64, 2)');
  121. }
  122. test_func_lower() {
  123. this._test_simple_formula('mixed', '=LOWER("MiXeD")');
  124. }
  125. test_func_max() {
  126. this._test_simple_formula(8, '=MAX(4, 8, 5, 2)');
  127. }
  128. test_func_min() {
  129. this._test_simple_formula(2, '=MIN(4, 8, 5, 2)');
  130. }
  131. test_func_mod() {
  132. this._test_simple_formula(1, '=MOD(37, 4)');
  133. }
  134. test_func_not() {
  135. this._test_simple_formula(false, '=NOT(TRUE)');
  136. this._test_simple_formula(true, '=NOT(FALSE)');
  137. }
  138. test_func_or() {
  139. this._test_simple_formula(false, '=OR(FALSE, FALSE)');
  140. this._test_simple_formula(true, '=OR(FALSE, TRUE)');
  141. this._test_simple_formula(true, '=OR(TRUE, FALSE)');
  142. this._test_simple_formula(true, '=OR(TRUE, TRUE)');
  143. this._test_simple_formula(true, '=OR(FALSE, FALSE, FALSE, TRUE)');
  144. }
  145. test_func_power() {
  146. this._test_simple_formula(8, '=POWER(2, 3)');
  147. }
  148. test_func_round() {
  149. this._test_simple_formula(3, '=ROUND(3.1)');
  150. this._test_simple_formula(4, '=ROUND(3.5)');
  151. this._test_simple_formula(4, '=ROUND(4)');
  152. this._test_simple_formula(-3, '=ROUND(-3.1)');
  153. this._test_simple_formula(-3, '=ROUND(-3.5)');
  154. this._test_simple_formula(-4, '=ROUND(-3.9)');
  155. this._test_simple_formula(3.1, '=ROUND(3.1415926535, 1)');
  156. this._test_simple_formula(3.14, '=ROUND(3.1415926535, 2)');
  157. this._test_simple_formula(30, '=ROUND(31.415926535, -1)');
  158. }
  159. test_func_sqrt() {
  160. this._test_simple_formula(4, '=SQRT(16)');
  161. }
  162. test_func_substitute() {
  163. this._test_simple_formula('cot sot on the mot', '=SUBSTITUTE("cat sat on the mat", "at", "ot")');
  164. this._test_simple_formula('cot sot on the mot', '=SUBSTITUTE("cAt saT on the mat", "at", "ot")');
  165. this._test_simple_formula('cot sot on the mot', '=SUBSTITUTE("c.*t s.*t on the m.*t", ".*t", "ot")');
  166. }
  167. test_func_sum() {
  168. this._test_simple_formula(15, '=SUM(1, 2, 3, 4, 5)');
  169. }
  170. test_func_upper() {
  171. this._test_simple_formula('MIXED', '=UPPER("mIxEd")');
  172. }
  173. test_func_xor() {
  174. this._test_simple_formula(false, '=XOR(FALSE, FALSE)');
  175. this._test_simple_formula(true, '=XOR(FALSE, TRUE)');
  176. this._test_simple_formula(true, '=XOR(TRUE, FALSE)');
  177. this._test_simple_formula(false, '=XOR(TRUE, TRUE)');
  178. this._test_simple_formula(true, '=XOR(FALSE, FALSE, TRUE)');
  179. this._test_simple_formula(false, '=XOR(TRUE, FALSE, TRUE)');
  180. }
  181. test_format() {
  182. this._test_simple_formula(new CellValue('2.718', 2.718281828459045, 'number', 3), '=2.718281828459045 ; number 3');
  183. this._test_simple_formula(new CellValue('271.83%', 2.718281828459045, 'percent', 2), '=2.718281828459045 ; percent 2');
  184. this._test_simple_formula(new CellValue('$2.72', 2.718281828459045, 'currency', 2), '=2.718281828459045 ; currency 2');
  185. }
  186. #iterateCharacters(formula, testFinal=false) {
  187. for (var i = 1; i < formula.length; i++) {
  188. const portion = formula.substring(0, i);
  189. const grid = new SpreadsheetGrid(1, 1);
  190. grid.cells[0][0].originalValue = CellValue.fromCellString(portion);
  191. const expressionSet = new CellExpressionSet(grid);
  192. expressionSet.calculateCells();
  193. }
  194. if (testFinal) {
  195. const grid = new SpreadsheetGrid(1, 1);
  196. grid.cells[0][0].originalValue = CellValue.fromCellString(formula);
  197. const expressionSet = new CellExpressionSet(grid);
  198. expressionSet.calculateCells();
  199. const actual = grid.cells[0][0].outputValue;
  200. if (actual === null) {
  201. this.fail(`Expected \"${formula}\" to evaluate to a value, got null`);
  202. } else if (actual.type === CellValue.TYPE_ERROR) {
  203. this.fail(`Expected \"${formula}\" to evaluate to a value, got error ${actual.value.message}`);
  204. }
  205. }
  206. }
  207. test_partialAndBrokenSyntax() {
  208. // Like `BrokenSyntaxTests`, this tries parsing a bunch of cell values
  209. // character by character like an author typing them in and makes sure
  210. // they don't throw exceptions. The `true` flag will do a simple check
  211. // that the final complete expression evaluates to something other than
  212. // `null` or an error.
  213. this.#iterateCharacters("123", true);
  214. this.#iterateCharacters("-123", true);
  215. this.#iterateCharacters("true", true);
  216. this.#iterateCharacters("false", true);
  217. this.#iterateCharacters("TrUe", true);
  218. this.#iterateCharacters("fAlSe", true);
  219. this.#iterateCharacters("$123.45", true);
  220. this.#iterateCharacters("-$123.45", true);
  221. this.#iterateCharacters("12.34%", true);
  222. this.#iterateCharacters("-12.34%", true);
  223. this.#iterateCharacters("'01234", true);
  224. this.#iterateCharacters("' ", true);
  225. this.#iterateCharacters("Foo bar", true);
  226. this.#iterateCharacters("=====");
  227. this.#iterateCharacters("=1*2*3*4", true);
  228. this.#iterateCharacters("=1**2");
  229. this.#iterateCharacters("=**123");
  230. this.#iterateCharacters("=6+*3");
  231. this.#iterateCharacters("=)))");
  232. this.#iterateCharacters("=(3*)");
  233. this.#iterateCharacters("=(*3)");
  234. this.#iterateCharacters("=(((");
  235. this.#iterateCharacters("=\")");
  236. this.#iterateCharacters("=-123", true);
  237. this.#iterateCharacters("=--123");
  238. this.#iterateCharacters("=---123");
  239. this.#iterateCharacters("=-\"str\"");
  240. this.#iterateCharacters("=\"str\"&3", true);
  241. this.#iterateCharacters("=1<3", true);
  242. this.#iterateCharacters("=1<=3", true);
  243. this.#iterateCharacters("=1>3", true);
  244. this.#iterateCharacters("=1>=3", true);
  245. this.#iterateCharacters("=1==3", true);
  246. this.#iterateCharacters("=1!=3", true);
  247. this.#iterateCharacters("=>=3");
  248. this.#iterateCharacters("=<=3");
  249. this.#iterateCharacters("=!=3");
  250. this.#iterateCharacters("=MAX)");
  251. this.#iterateCharacters("=MAX(3,,)");
  252. this.#iterateCharacters("=MAX(\"a\",\"b\")");
  253. this.#iterateCharacters("=ZZ999");
  254. this.#iterateCharacters("=$ZZ999");
  255. this.#iterateCharacters("=ZZ$999");
  256. this.#iterateCharacters("=$ZZ$999");
  257. this.#iterateCharacters("=1+2", true);
  258. this.#iterateCharacters("=1-2", true);
  259. this.#iterateCharacters("=1*2", true);
  260. this.#iterateCharacters("=1/2", true);
  261. this.#iterateCharacters("=A1(1)");
  262. this.#iterateCharacters("=$A$1(1)");
  263. }
  264. }