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.

UtilsTests.js 4.6KB

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