PHP and Javascript implementations of a simple markdown parser
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ExpressionSetTests.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. }
  199. ?>