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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 CellValueTests extends TestCase {
  7. public function test_fromCellString_blank() {
  8. $value = CellValue::fromCellString('');
  9. $this->assertSame(CellValue::TYPE_BLANK, $value->type);
  10. $this->assertSame('', $value->formattedValue);
  11. $this->assertSame(null, $value->value);
  12. $value = CellValue::fromCellString(' ');
  13. $this->assertSame(CellValue::TYPE_BLANK, $value->type);
  14. $this->assertSame('', $value->formattedValue);
  15. $this->assertSame(null, $value->value);
  16. }
  17. public function test_fromCellString_number() {
  18. $value = CellValue::fromCellString('123');
  19. $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
  20. $this->assertSame('123', $value->formattedValue);
  21. $this->assertEqualsWithDelta(123, $value->value, 0.000_001);
  22. $this->assertSame(0, $value->decimals);
  23. $value = CellValue::fromCellString('-0');
  24. $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
  25. $this->assertSame('0', $value->formattedValue);
  26. $this->assertEqualsWithDelta(0, $value->value, 0.000_001);
  27. $this->assertSame(0, $value->decimals);
  28. $value = CellValue::fromCellString('1,234');
  29. $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
  30. $this->assertSame('1,234', $value->formattedValue);
  31. $this->assertEqualsWithDelta(1234, $value->value, 0.000_001);
  32. $this->assertSame(0, $value->decimals);
  33. $value = CellValue::fromCellString('-1,234,567.89');
  34. $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
  35. $this->assertSame('-1,234,567.89', $value->formattedValue);
  36. $this->assertEqualsWithDelta(-1234567.89, $value->value, 0.000_001);
  37. $this->assertSame(2, $value->decimals);
  38. }
  39. public function test_fromCellString_percent() {
  40. $value = CellValue::fromCellString('123%');
  41. $this->assertSame(CellValue::TYPE_PERCENT, $value->type);
  42. $this->assertSame('123%', $value->formattedValue);
  43. $this->assertEqualsWithDelta(1.23, $value->value, 0.000_001);
  44. $this->assertSame(0, $value->decimals);
  45. $value = CellValue::fromCellString('-12.3%');
  46. $this->assertSame(CellValue::TYPE_PERCENT, $value->type);
  47. $this->assertSame('-12.3%', $value->formattedValue);
  48. $this->assertEqualsWithDelta(-0.123, $value->value, 0.000_001);
  49. $this->assertSame(1, $value->decimals);
  50. }
  51. public function test_fromCellString_currency() {
  52. $value = CellValue::fromCellString('$123');
  53. $this->assertSame(CellValue::TYPE_CURRENCY, $value->type);
  54. $this->assertSame('$123', $value->formattedValue);
  55. $this->assertEqualsWithDelta(123, $value->value, 0.000_001);
  56. $this->assertSame(0, $value->decimals);
  57. $value = CellValue::fromCellString('-$12.34');
  58. $this->assertSame(CellValue::TYPE_CURRENCY, $value->type);
  59. $this->assertSame('-$12.34', $value->formattedValue);
  60. $this->assertEqualsWithDelta(-12.34, $value->value, 0.000_001);
  61. $this->assertSame(2, $value->decimals);
  62. }
  63. public function test_fromCellString_boolean() {
  64. $value = CellValue::fromCellString('true');
  65. $this->assertSame(CellValue::TYPE_BOOLEAN, $value->type);
  66. $this->assertSame('TRUE', $value->formattedValue);
  67. $this->assertSame(true, $value->value);
  68. $value = CellValue::fromCellString('false');
  69. $this->assertSame(CellValue::TYPE_BOOLEAN, $value->type);
  70. $this->assertSame('FALSE', $value->formattedValue);
  71. $this->assertSame(false, $value->value);
  72. }
  73. public function test_fromCellString_string() {
  74. $value = CellValue::fromCellString('some text');
  75. $this->assertSame(CellValue::TYPE_STRING, $value->type);
  76. $this->assertSame('some text', $value->formattedValue);
  77. $this->assertSame('some text', $value->value);
  78. $value = CellValue::fromCellString("'0123");
  79. $this->assertSame(CellValue::TYPE_STRING, $value->type);
  80. $this->assertSame('0123', $value->formattedValue);
  81. $this->assertSame('0123', $value->value);
  82. $value = CellValue::fromCellString("'=123");
  83. $this->assertSame(CellValue::TYPE_STRING, $value->type);
  84. $this->assertSame('=123', $value->formattedValue);
  85. $this->assertSame('=123', $value->value);
  86. }
  87. public function test_fromCellString_formula() {
  88. $value = CellValue::fromCellString('=A*B');
  89. $this->assertSame(CellValue::TYPE_FORMULA, $value->type);
  90. $this->assertSame('=A*B', $value->formattedValue);
  91. $this->assertSame('=A*B', $value->value);
  92. $value = CellValue::fromCellString('=MAX(A, 3)');
  93. $this->assertSame(CellValue::TYPE_FORMULA, $value->type);
  94. $this->assertSame('=MAX(A, 3)', $value->formattedValue);
  95. $this->assertSame('=MAX(A, 3)', $value->value);
  96. }
  97. public function test_fromValue_null() {
  98. $value = CellValue::fromValue(null);
  99. $this->assertSame(CellValue::TYPE_BLANK, $value->type);
  100. }
  101. public function test_fromValue_number() {
  102. $value = CellValue::fromValue(123);
  103. $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
  104. $this->assertSame('123', $value->formattedValue);
  105. $value = CellValue::fromValue(3.141592);
  106. $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
  107. $this->assertSame('3.141592', $value->formattedValue);
  108. $value = CellValue::fromValue(123456789);
  109. $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
  110. $this->assertSame('123,456,789', $value->formattedValue);
  111. }
  112. public function test_fromValue_boolean() {
  113. $value = CellValue::fromValue(true);
  114. $this->assertSame(CellValue::TYPE_BOOLEAN, $value->type);
  115. $this->assertSame('TRUE', $value->formattedValue);
  116. $value = CellValue::fromValue(false);
  117. $this->assertSame(CellValue::TYPE_BOOLEAN, $value->type);
  118. $this->assertSame('FALSE', $value->formattedValue);
  119. }
  120. public function test_fromValue_string() {
  121. $value = CellValue::fromValue('foo');
  122. $this->assertSame(CellValue::TYPE_STRING, $value->type);
  123. $this->assertSame('foo', $value->formattedValue);
  124. $value = CellValue::fromValue('0123456');
  125. $this->assertSame(CellValue::TYPE_STRING, $value->type);
  126. $this->assertSame('0123456', $value->formattedValue);
  127. }
  128. public function test_fromValue_formula() {
  129. $value = CellValue::fromValue('=A*B');
  130. $this->assertSame(CellValue::TYPE_FORMULA, $value->type);
  131. $this->assertSame('=A*B', $value->formattedValue);
  132. }
  133. private function assertSameCellValue(CellValue $expected, CellValue $actual) {
  134. $this->assertSame($expected->formattedValue, $actual->formattedValue);
  135. $this->assertSame($expected->value, $actual->value);
  136. $this->assertSame($expected->type, $actual->type);
  137. $this->assertSame($expected->decimals, $actual->decimals);
  138. }
  139. public function test_operation_add() {
  140. $a = CellValue::fromValue(3);
  141. $b = CellValue::fromValue(4);
  142. $actual = $a->add($b);
  143. $expected = new CellValue('7', 7, CellValue::TYPE_NUMBER, 0);
  144. $this->assertSameCellValue($expected, $actual);
  145. $a = CellValue::fromCellString('100%');
  146. $b = CellValue::fromCellString('50%');
  147. $actual = $a->add($b);
  148. $expected = new CellValue('150%', 1.5, CellValue::TYPE_PERCENT, 0);
  149. $this->assertSameCellValue($expected, $actual);
  150. $a = CellValue::fromCellString('$123');
  151. $b = CellValue::fromCellString('$321');
  152. $actual = $a->add($b);
  153. $expected = new CellValue('$444.00', 444.0, CellValue::TYPE_CURRENCY, 2);
  154. $this->assertSameCellValue($expected, $actual);
  155. }
  156. public function test_operation_subtract() {
  157. $a = CellValue::fromValue(9);
  158. $b = CellValue::fromValue(4);
  159. $actual = $a->subtract($b);
  160. $expected = new CellValue('5', 5, CellValue::TYPE_NUMBER, 0);
  161. $this->assertSameCellValue($expected, $actual);
  162. $a = CellValue::fromCellString('100%');
  163. $b = CellValue::fromCellString('50%');
  164. $actual = $a->subtract($b);
  165. $expected = new CellValue('50%', 0.5, CellValue::TYPE_PERCENT, 0);
  166. $this->assertSameCellValue($expected, $actual);
  167. $a = CellValue::fromCellString('$321');
  168. $b = CellValue::fromCellString('$123');
  169. $actual = $a->subtract($b);
  170. $expected = new CellValue('$198.00', 198.0, CellValue::TYPE_CURRENCY, 2);
  171. $this->assertSameCellValue($expected, $actual);
  172. }
  173. public function test_operation_multiply() {
  174. $a = CellValue::fromValue(3);
  175. $b = CellValue::fromValue(4);
  176. $actual = $a->multiply($b);
  177. $expected = new CellValue('12', 12, CellValue::TYPE_NUMBER, 0);
  178. $this->assertSameCellValue($expected, $actual);
  179. $a = CellValue::fromCellString('150%');
  180. $b = CellValue::fromCellString('50%');
  181. $actual = $a->multiply($b);
  182. $expected = new CellValue('75%', 0.75, CellValue::TYPE_PERCENT, 0);
  183. $this->assertSameCellValue($expected, $actual);
  184. $a = CellValue::fromCellString('$321');
  185. $b = CellValue::fromCellString('50%');
  186. $actual = $a->multiply($b);
  187. $expected = new CellValue('$160.50', 160.50, CellValue::TYPE_CURRENCY, 2);
  188. $this->assertSameCellValue($expected, $actual);
  189. }
  190. public function test_operation_divide() {
  191. $a = CellValue::fromValue(12);
  192. $b = CellValue::fromValue(4);
  193. $actual = $a->divide($b);
  194. $expected = new CellValue('3', 3, CellValue::TYPE_NUMBER, 0);
  195. $this->assertSameCellValue($expected, $actual);
  196. $a = CellValue::fromCellString('150%');
  197. $b = CellValue::fromCellString('50%');
  198. $actual = $a->divide($b);
  199. $expected = new CellValue('300%', 3.0, CellValue::TYPE_PERCENT, 0);
  200. $this->assertSameCellValue($expected, $actual);
  201. $a = CellValue::fromCellString('$321');
  202. $b = CellValue::fromCellString('200%');
  203. $actual = $a->divide($b);
  204. $expected = new CellValue('$160.50', 160.50, CellValue::TYPE_CURRENCY, 2);
  205. $this->assertSameCellValue($expected, $actual);
  206. }
  207. public function test_operation_modulo() {
  208. $a = CellValue::fromValue(7);
  209. $b = CellValue::fromValue(4);
  210. $actual = $a->modulo($b);
  211. $expected = new CellValue('3', 3, CellValue::TYPE_NUMBER, 0);
  212. $this->assertSameCellValue($expected, $actual);
  213. $a = CellValue::fromCellString('175%');
  214. $b = CellValue::fromCellString('50%');
  215. $actual = $a->modulo($b);
  216. $expected = new CellValue('25%', 0.25, CellValue::TYPE_PERCENT, 0);
  217. $this->assertSameCellValue($expected, $actual);
  218. $a = CellValue::fromCellString('$327');
  219. $b = CellValue::fromCellString('$20');
  220. $actual = $a->modulo($b);
  221. $expected = new CellValue('$7.00', 7.00, CellValue::TYPE_CURRENCY, 2);
  222. $this->assertSameCellValue($expected, $actual);
  223. }
  224. public function test_operation_unaryNot() {
  225. $this->assertSameCellValue(CellValue::fromValue(false), CellValue::fromValue(true)->not());
  226. $this->assertSameCellValue(CellValue::fromValue(true), CellValue::fromValue(false)->not());
  227. }
  228. public function test_operation_comparators() {
  229. $a = CellValue::fromValue(3);
  230. $b = CellValue::fromValue(4);
  231. $t = CellValue::fromValue(true);
  232. $f = CellValue::fromValue(false);
  233. $this->assertSameCellValue($t, $a->lt($b));
  234. $this->assertSameCellValue($t, $a->lte($b));
  235. $this->assertSameCellValue($f, $a->gt($b));
  236. $this->assertSameCellValue($f, $a->gte($b));
  237. $this->assertSameCellValue($f, $a->eq($b));
  238. $this->assertSameCellValue($t, $a->neq($b));
  239. $this->assertSameCellValue($f, $b->lt($a));
  240. $this->assertSameCellValue($f, $b->lte($a));
  241. $this->assertSameCellValue($t, $b->gt($a));
  242. $this->assertSameCellValue($t, $b->gte($a));
  243. $this->assertSameCellValue($f, $b->eq($a));
  244. $this->assertSameCellValue($t, $b->neq($a));
  245. $this->assertSameCellValue($f, $a->lt($a));
  246. $this->assertSameCellValue($t, $a->lte($a));
  247. $this->assertSameCellValue($f, $a->gt($a));
  248. $this->assertSameCellValue($t, $a->gte($a));
  249. $this->assertSameCellValue($t, $a->eq($a));
  250. $this->assertSameCellValue($f, $a->neq($a));
  251. }
  252. public function test_operation_concatenate() {
  253. $a = CellValue::fromValue('abc');
  254. $b = CellValue::fromValue('xyz');
  255. $actual = $a->concatenate($b);
  256. $expected = CellValue::fromValue('abcxyz');
  257. $this->assertSameCellValue($expected, $actual);
  258. }
  259. }
  260. ?>