PHP and Javascript implementations of a simple markdown parser
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

SpreadsheetMarkdownIntegrationTests.php 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. }
  68. ?>