PHP and Javascript implementations of a simple markdown parser
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UtilsTests.php 4.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. declare(strict_types=1);
  3. use PHPUnit\Framework\TestCase;
  4. require_once __DIR__ . '/../php/markdown.php';
  5. final class UtilsTests extends TestCase {
  6. public function test_stripIndent() {
  7. $this->assertSame('', MDUtils::stripIndent(''));
  8. $this->assertSame('', MDUtils::stripIndent(' '));
  9. $this->assertSame('foo', MDUtils::stripIndent('foo'));
  10. $this->assertSame('foo', MDUtils::stripIndent(' foo'));
  11. $this->assertSame('foo', MDUtils::stripIndent(' foo'));
  12. $this->assertSame('foo', MDUtils::stripIndent(' foo'));
  13. $this->assertSame('foo', MDUtils::stripIndent(' foo'));
  14. $this->assertSame(' foo', MDUtils::stripIndent(' foo'));
  15. $this->assertSame('foo', MDUtils::stripIndent("\tfoo"));
  16. $this->assertSame("\tfoo", MDUtils::stripIndent("\t\tfoo"));
  17. $this->assertSame('foo', MDUtils::stripIndent("\t\tfoo", 2));
  18. $this->assertSame('foo', MDUtils::stripIndent(' foo', 2));
  19. }
  20. public function test_countIndents() {
  21. $this->assertSame(0, MDUtils::countIndents(''));
  22. $this->assertSame(1, MDUtils::countIndents(' '));
  23. $this->assertSame(1, MDUtils::countIndents(' '));
  24. $this->assertSame(0, MDUtils::countIndents('foo'));
  25. $this->assertSame(0, MDUtils::countIndents('foo'));
  26. $this->assertSame(1, MDUtils::countIndents(' foo'));
  27. $this->assertSame(1, MDUtils::countIndents(' foo'));
  28. $this->assertSame(1, MDUtils::countIndents(' foo'));
  29. $this->assertSame(1, MDUtils::countIndents(' foo'));
  30. $this->assertSame(2, MDUtils::countIndents(' foo'));
  31. $this->assertSame(1, MDUtils::countIndents("\tfoo"));
  32. $this->assertSame(2, MDUtils::countIndents("\t\tfoo"));
  33. $this->assertSame(0, MDUtils::countIndents('', true));
  34. $this->assertSame(0, MDUtils::countIndents(' ', true));
  35. $this->assertSame(1, MDUtils::countIndents(' ', true));
  36. $this->assertSame(0, MDUtils::countIndents('foo', true));
  37. $this->assertSame(0, MDUtils::countIndents(' foo', true));
  38. $this->assertSame(0, MDUtils::countIndents(' foo', true));
  39. $this->assertSame(0, MDUtils::countIndents(' foo', true));
  40. $this->assertSame(1, MDUtils::countIndents(' foo', true));
  41. $this->assertSame(1, MDUtils::countIndents(' foo', true));
  42. $this->assertSame(1, MDUtils::countIndents("\tfoo", true));
  43. $this->assertSame(2, MDUtils::countIndents("\t\tfoo", true));
  44. }
  45. public function test_tokenizeLabel() {
  46. // Escapes are preserved
  47. $this->assertSame([ '[foo]', 'foo' ], MDToken::tokenizeLabel('[foo] bar'));
  48. $this->assertSame([ '[foo\\[]', 'foo\\[' ], MDToken::tokenizeLabel('[foo\\[] bar'));
  49. $this->assertSame([ '[foo\\]]', 'foo\\]' ], MDToken::tokenizeLabel('[foo\\]] bar'));
  50. $this->assertSame([ '[foo[]]', 'foo[]' ], MDToken::tokenizeLabel('[foo[]] bar'));
  51. $this->assertSame([ '[foo\\(]', 'foo\\(' ], MDToken::tokenizeLabel('[foo\\(] bar'));
  52. $this->assertSame([ '[foo\\)]', 'foo\\)' ], MDToken::tokenizeLabel('[foo\\)] bar'));
  53. $this->assertSame([ '[foo()]', 'foo()' ], MDToken::tokenizeLabel('[foo()] bar'));
  54. $this->assertSame(null, MDToken::tokenizeLabel('foo bar'));
  55. $this->assertSame(null, MDToken::tokenizeLabel('[foo\\] bar'));
  56. $this->assertSame(null, MDToken::tokenizeLabel('[foo bar'));
  57. $this->assertSame(null, MDToken::tokenizeLabel('[foo[] bar'));
  58. }
  59. public function test_tokenizeURL() {
  60. $this->assertSame([ '(page.html)', 'page.html', null ], MDToken::tokenizeURL('(page.html) foo'));
  61. $this->assertSame([ '(page.html "link title")', 'page.html', 'link title' ], MDToken::tokenizeURL('(page.html "link title") foo'));
  62. $this->assertSame([ '(https://example.com/path/page.html?query=foo&bar=baz#fragment)', 'https://example.com/path/page.html?query=foo&bar=baz#fragment', null ], MDToken::tokenizeURL('(https://example.com/path/page.html?query=foo&bar=baz#fragment) foo'));
  63. $this->assertSame(null, MDToken::tokenizeURL('page.html foo'));
  64. $this->assertSame(null, MDToken::tokenizeURL('(page.html foo'));
  65. $this->assertSame(null, MDToken::tokenizeURL('page.html) foo'));
  66. $this->assertSame(null, MDToken::tokenizeURL('(page.html "title) foo'));
  67. $this->assertSame(null, MDToken::tokenizeURL('(page .html) foo'));
  68. $this->assertSame(null, MDToken::tokenizeURL('(user@example.com) foo'));
  69. $this->assertSame(null, MDToken::tokenizeURL('(user@example.com "title") foo'));
  70. }
  71. public function test_tokenizeEmail() {
  72. $this->assertSame([ '(user@example.com)', 'user@example.com', null ], MDToken::tokenizeEmail('(user@example.com)'));
  73. $this->assertSame([ '(user@example.com "link title")', 'user@example.com', 'link title' ], MDToken::tokenizeEmail('(user@example.com "link title")'));
  74. $this->assertSame(null, MDToken::tokenizeEmail('(https://example.com) foo'));
  75. $this->assertSame(null, MDToken::tokenizeEmail('(https://example.com "link title") foo'));
  76. $this->assertSame(null, MDToken::tokenizeEmail('(user@example.com "link title) foo'));
  77. $this->assertSame(null, MDToken::tokenizeEmail('(user@example.com foo'));
  78. $this->assertSame(null, MDToken::tokenizeEmail('user@example.com) foo'));
  79. }
  80. }
  81. ?>