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.

TokenTests.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. class TokenTests extends BaseTest {
  2. test_findFirstTokens() {
  3. const tokens = [
  4. new MDToken('Lorem', MDTokenType.Text),
  5. new MDToken(' ', MDTokenType.Whitespace),
  6. new MDToken('_', MDTokenType.Underscore),
  7. new MDToken('ipsum', MDTokenType.Text),
  8. new MDToken('_', MDTokenType.Underscore),
  9. new MDToken(' ', MDTokenType.Whitespace),
  10. new MDToken('dolor', MDTokenType.Text),
  11. new MDToken('_', MDTokenType.Underscore),
  12. new MDToken('sit', MDTokenType.Text),
  13. new MDToken('_', MDTokenType.Underscore),
  14. ];
  15. const pattern = [
  16. MDTokenType.Underscore,
  17. ];
  18. const result = MDToken.findFirstTokens(tokens, pattern);
  19. const expected = {
  20. tokens: [ tokens[2] ],
  21. index: 2,
  22. };
  23. this.assertEqual(result, expected);
  24. }
  25. test_findFirstTokens_optionalWhitespace1() {
  26. const tokens = [
  27. new MDToken('Lorem', MDTokenType.Text),
  28. new MDToken(' ', MDTokenType.Whitespace),
  29. new MDToken('[ipsum]', MDTokenType.Label, 'ipsum'),
  30. new MDToken('(link.html)', MDTokenType.URL, 'link.html'),
  31. new MDToken(' ', MDTokenType.Whitespace),
  32. new MDToken('dolor', MDTokenType.Text),
  33. ];
  34. const pattern = [
  35. MDTokenType.Label,
  36. MDTokenType.META_OptionalWhitespace,
  37. MDTokenType.URL,
  38. ];
  39. const result = MDToken.findFirstTokens(tokens, pattern);
  40. const expected = {
  41. tokens: [ tokens[2], tokens[3] ],
  42. index: 2,
  43. };
  44. this.assertEqual(result, expected);
  45. }
  46. test_findFirstTokens_optionalWhitespace2() {
  47. const tokens = [
  48. new MDToken('Lorem', MDTokenType.Text),
  49. new MDToken(' ', MDTokenType.Whitespace),
  50. new MDToken('[ipsum]', MDTokenType.Label, 'ipsum'),
  51. new MDToken(' ', MDTokenType.Whitespace),
  52. new MDToken('(link.html)', MDTokenType.URL, 'link.html'),
  53. new MDToken(' ', MDTokenType.Whitespace),
  54. new MDToken('dolor', MDTokenType.Text),
  55. ];
  56. const pattern = [
  57. MDTokenType.Label,
  58. MDTokenType.META_OptionalWhitespace,
  59. MDTokenType.URL,
  60. ];
  61. const result = MDToken.findFirstTokens(tokens, pattern);
  62. const expected = {
  63. tokens: [ tokens[2], tokens[3], tokens[4] ],
  64. index: 2,
  65. };
  66. this.assertEqual(result, expected);
  67. }
  68. test_findPairedTokens() {
  69. const tokens = [
  70. new MDToken('Lorem', MDTokenType.Text),
  71. new MDToken(' ', MDTokenType.Whitespace),
  72. new MDToken('_', MDTokenType.Underscore),
  73. new MDToken('ipsum', MDTokenType.Text),
  74. new MDToken('_', MDTokenType.Underscore),
  75. new MDToken(' ', MDTokenType.Whitespace),
  76. new MDToken('dolor', MDTokenType.Text),
  77. new MDToken('_', MDTokenType.Underscore),
  78. new MDToken('sit', MDTokenType.Text),
  79. new MDToken('_', MDTokenType.Underscore),
  80. ];
  81. const pattern = [
  82. MDTokenType.Underscore,
  83. ];
  84. const result = MDToken.findPairedTokens(tokens, pattern, pattern);
  85. const expected = {
  86. startTokens: [ tokens[2] ],
  87. contentTokens: [ tokens[3] ],
  88. endTokens: [ tokens[4] ],
  89. startIndex: 2,
  90. contentIndex: 3,
  91. endIndex: 4,
  92. totalLength: 3,
  93. }
  94. this.assertEqual(result, expected);
  95. }
  96. }