test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. const {
  2. StringStream,
  3. LinesCommentParser,
  4. BlockCommentParser,
  5. CommentParser
  6. } = require('../main');
  7. const assert = async (label, fn) => {
  8. if ( ! await fn() ) {
  9. // TODO: strutil quot
  10. throw new Error(`assert: '${label}' failed`)
  11. }
  12. };
  13. describe('parsers', () => {
  14. describe('lines-comment-parser', () => {
  15. it ('basic test', async () => {
  16. const parser = LinesCommentParser({ prefix: '//' });
  17. let lines;
  18. const ss = StringStream(`
  19. // first line of first block
  20. // second line of first block
  21. // first line of second block
  22. function () {}
  23. `);
  24. const results = [];
  25. for (;;) {
  26. comment = await parser.parse(ss);
  27. if ( ! comment ) break;
  28. results.push(comment.lines);
  29. }
  30. console.log('results?', results);
  31. })
  32. })
  33. describe('block-comment-parser', () => {
  34. it ('basic test', async () => {
  35. const parser = BlockCommentParser({
  36. start: '/*',
  37. end: '*/',
  38. ignore_line_prefix: '*',
  39. });
  40. let lines;
  41. const ss = StringStream(`
  42. /*
  43. First block
  44. comment
  45. */
  46. /*
  47. * second block
  48. * comment
  49. */
  50. /**
  51. * third block
  52. * comment
  53. */
  54. function () {}
  55. `);
  56. const results = [];
  57. for (;;) {
  58. comment = await parser.parse(ss);
  59. if ( ! comment ) break;
  60. results.push(comment.lines);
  61. }
  62. console.log('results?', results);
  63. })
  64. it ('doesn\'t return anything for line comments', async () => {
  65. const parser = BlockCommentParser({
  66. start: '/*',
  67. end: '*/',
  68. ignore_line_prefix: '*',
  69. });
  70. let lines;
  71. const ss = StringStream(`
  72. // this comment should not be parsed
  73. // by the block comment parser
  74. function () {}
  75. `);
  76. const results = [];
  77. for (;;) {
  78. comment = await parser.parse(ss);
  79. if ( ! comment ) break;
  80. results.push(comment.lines);
  81. }
  82. console.log('results?', results);
  83. })
  84. })
  85. describe('extract_top_comments', () => {
  86. it ('basic test', async () => {
  87. const parser = CommentParser();
  88. const filename = 'test.js';
  89. const source = `
  90. // First lines comment
  91. // second line of lines comment
  92. /*
  93. First block comment
  94. second line of block comment
  95. */
  96. `;
  97. const results = await parser.extract_top_comments({
  98. filename,
  99. source,
  100. });
  101. console.log('results?', results);
  102. })
  103. })
  104. describe('StringStream', () => {
  105. describe('fork', () => {
  106. it('works', async () => {
  107. const ss = StringStream('asdf');
  108. const ss_ = ss.fork();
  109. ss_.fwd();
  110. await assert('fwd worked', async () => {
  111. return await ss_.get_char() === 's';
  112. });
  113. await assert('upstream state is same', async () => {
  114. return await ss.get_char() === 'a';
  115. });
  116. })
  117. })
  118. })
  119. });