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.

SpreadsheetMarkdownIntegrationTests.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 SpreadsheetMarkdownIntegrationTests extends TestCase {
  7. private ?Markdown $parser;
  8. protected function setUp(): void {
  9. $this->parser = new Markdown(array_merge(Markdown::allReaders(), [ new MDSpreadsheetReader() ]));
  10. }
  11. private function md(string $markdown): string {
  12. return $this->normalizeWhitespace($this->parser->toHTML($markdown));
  13. }
  14. private function normalizeWhitespace(string $str): string {
  15. $str = mb_eregi_replace('\\s+', ' ', $str);
  16. $str = mb_eregi_replace('>\\s+<', '><', $str);
  17. return trim($str);
  18. }
  19. public function test_integration() {
  20. $markdown = "| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | =A*B |";
  21. $expected = '<table><thead>' .
  22. '<tr><th>A</th><th>B</th><th>C</th></tr>' .
  23. '</thead><tbody><tr>' .
  24. '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3">3</td>' .
  25. '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4">4</td>' .
  26. '<td class="calculated spreadsheet-type-number" data-numeric-value="12" data-string-value="12">12</td>' .
  27. '</tr></tbody></table>';
  28. $actual = $this->md($markdown);
  29. $this->assertSame($expected, $actual);
  30. }
  31. public function test_bailOut() {
  32. // If no formulas found table isn't modified
  33. $markdown = "| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | A*B |";
  34. $expected = '<table><thead>' .
  35. '<tr><th>A</th><th>B</th><th>C</th></tr>' .
  36. '</thead><tbody><tr>' .
  37. '<td>3</td>' .
  38. '<td>4</td>' .
  39. '<td>A*B</td>' .
  40. '</tr></tbody></table>';
  41. $actual = $this->md($markdown);
  42. $this->assertSame($expected, $actual);
  43. }
  44. public function test_observedError1() {
  45. // Saw "Uncaught Error: columnIndex must be number, got string" from this
  46. $markdown = "| Unit Price | Qty | Subtotal |\n| ---: | ---: | ---: |\n| $1.23 | 2 | =A*B FILL |\n| $4.99 | 6 | |\n| Total | | =SUM(C:C) |";
  47. $expected = '<table><thead><tr>' .
  48. '<th style="text-align: right;">Unit Price</th>' .
  49. '<th style="text-align: right;">Qty</th>' .
  50. '<th>Subtotal</th>' .
  51. '</tr></thead><tbody><tr>' .
  52. '<td class="spreadsheet-type-currency" style="text-align: right;" data-numeric-value="1.23" data-string-value="1.23">$1.23</td>' .
  53. '<td class="spreadsheet-type-number" style="text-align: right;" data-numeric-value="2" data-string-value="2">2</td>' .
  54. '<td class="calculated spreadsheet-type-currency" data-numeric-value="2.46" data-string-value="2.46">$2.46</td>' .
  55. '</tr><tr>' .
  56. '<td class="spreadsheet-type-currency" style="text-align: right;" data-numeric-value="4.99" data-string-value="4.99">$4.99</td>' .
  57. '<td class="spreadsheet-type-number" style="text-align: right;" data-numeric-value="6" data-string-value="6">6</td>' .
  58. '<td class="calculated spreadsheet-type-currency" data-numeric-value="29.94" data-string-value="29.94">$29.94</td>' .
  59. '</tr><tr>' .
  60. '<td class="spreadsheet-type-string" style="text-align: right;" data-string-value="Total">Total</td>' .
  61. '<td class="spreadsheet-type-blank" style="text-align: right;" data-numeric-value="0" data-string-value=""></td>' .
  62. '<td class="calculated spreadsheet-type-currency" data-numeric-value="32.4" data-string-value="32.4">$32.40</td>' .
  63. '</tr></tbody></table>';
  64. $actual = $this->md($markdown);
  65. $this->assertSame($expected, $actual);
  66. }
  67. public function test_observedError2() {
  68. // A FILLed difference formula in row 2 of `=B2-B1` threw an exception
  69. // due to a transposed address to the row above of `=B1-B0`.
  70. $markdown = "| TB | Price | $/TB | Incr |\n| --- | --- | --- | --- |\n| 2 | $99.99 | =B/A FILL | |\n| 4 | $186.41 | | =B2-B1 FILL |\n| 6 | $209.99 | | |";
  71. $actual = $this->md($markdown);
  72. $this->assertTrue(true);
  73. }
  74. public function test_styledValue() {
  75. $markdown = "| A | B | C |\n| --- | --- | --- |\n| **3** | _4_ | =A*B |";
  76. $expected = '<table><thead>' .
  77. '<tr><th>A</th><th>B</th><th>C</th></tr>' .
  78. '</thead><tbody><tr>' .
  79. '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3"><strong>3</strong></td>' .
  80. '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4"><em>4</em></td>' .
  81. '<td class="calculated spreadsheet-type-number" data-numeric-value="12" data-string-value="12">12</td>' .
  82. '</tr></tbody></table>';
  83. $actual = $this->md($markdown);
  84. $this->assertSame($expected, $actual);
  85. }
  86. public function test_styledFormula() {
  87. $markdown = "| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | **=A*B** |";
  88. $expected = '<table><thead>' .
  89. '<tr><th>A</th><th>B</th><th>C</th></tr>' .
  90. '</thead><tbody><tr>' .
  91. '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3">3</td>' .
  92. '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4">4</td>' .
  93. '<td class="calculated spreadsheet-type-number" data-numeric-value="12" data-string-value="12"><strong>12</strong></td>' .
  94. '</tr></tbody></table>';
  95. $actual = $this->md($markdown);
  96. $this->assertSame($expected, $actual);
  97. }
  98. public function test_styledFormula_multiple() {
  99. $markdown = "| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | ~~**=A*B**~~ |";
  100. $expected = '<table><thead>' .
  101. '<tr><th>A</th><th>B</th><th>C</th></tr>' .
  102. '</thead><tbody><tr>' .
  103. '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3">3</td>' .
  104. '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4">4</td>' .
  105. '<td class="calculated spreadsheet-type-number" data-numeric-value="12" data-string-value="12"><s><strong>12</strong></s></td>' .
  106. '</tr></tbody></table>';
  107. $actual = $this->md($markdown);
  108. $this->assertSame($expected, $actual);
  109. }
  110. public function test_preservedOperators() {
  111. // Regular markdown will want to convert *B* into emphasized B
  112. $markdown = "| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | =A*B*4 |";
  113. $expected = '<table><thead>' .
  114. '<tr><th>A</th><th>B</th><th>C</th></tr>' .
  115. '</thead><tbody><tr>' .
  116. '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3">3</td>' .
  117. '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4">4</td>' .
  118. '<td class="calculated spreadsheet-type-number" data-numeric-value="48" data-string-value="48">48</td>' .
  119. '</tr></tbody></table>';
  120. $actual = $this->md($markdown);
  121. $this->assertSame($expected, $actual);
  122. }
  123. }
  124. ?>