PHP and Javascript implementations of a simple markdown parser
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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