test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. const {
  20. StringStream,
  21. LinesCommentParser,
  22. BlockCommentParser,
  23. CommentParser
  24. } = require('../main');
  25. const assert = async (label, fn) => {
  26. if ( ! await fn() ) {
  27. // TODO: strutil quot
  28. throw new Error(`assert: '${label}' failed`)
  29. }
  30. };
  31. describe('parsers', () => {
  32. describe('lines-comment-parser', () => {
  33. it ('basic test', async () => {
  34. const parser = LinesCommentParser({ prefix: '//' });
  35. let lines;
  36. const ss = StringStream(`
  37. // first line of first block
  38. // second line of first block
  39. // first line of second block
  40. function () {}
  41. `);
  42. const results = [];
  43. for (;;) {
  44. comment = await parser.parse(ss);
  45. if ( ! comment ) break;
  46. results.push(comment.lines);
  47. }
  48. console.log('results?', results);
  49. })
  50. })
  51. describe('block-comment-parser', () => {
  52. it ('basic test', async () => {
  53. const parser = BlockCommentParser({
  54. start: '/*',
  55. end: '*/',
  56. ignore_line_prefix: '*',
  57. });
  58. let lines;
  59. const ss = StringStream(`
  60. /*
  61. First block
  62. comment
  63. */
  64. /*
  65. * second block
  66. * comment
  67. */
  68. /**
  69. * third block
  70. * comment
  71. */
  72. function () {}
  73. `);
  74. const results = [];
  75. for (;;) {
  76. comment = await parser.parse(ss);
  77. if ( ! comment ) break;
  78. results.push(comment.lines);
  79. }
  80. console.log('results?', results);
  81. })
  82. it ('doesn\'t return anything for line comments', async () => {
  83. const parser = BlockCommentParser({
  84. start: '/*',
  85. end: '*/',
  86. ignore_line_prefix: '*',
  87. });
  88. let lines;
  89. const ss = StringStream(`
  90. // this comment should not be parsed
  91. // by the block comment parser
  92. function () {}
  93. `);
  94. const results = [];
  95. for (;;) {
  96. comment = await parser.parse(ss);
  97. if ( ! comment ) break;
  98. results.push(comment.lines);
  99. }
  100. console.log('results?', results);
  101. })
  102. })
  103. describe('extract_top_comments', () => {
  104. it ('basic test', async () => {
  105. const parser = CommentParser();
  106. const filename = 'test.js';
  107. const source = `
  108. // First lines comment
  109. // second line of lines comment
  110. /*
  111. First block comment
  112. second line of block comment
  113. */
  114. `;
  115. const results = await parser.extract_top_comments({
  116. filename,
  117. source,
  118. });
  119. console.log('results?', results);
  120. })
  121. })
  122. describe('StringStream', () => {
  123. describe('fork', () => {
  124. it('works', async () => {
  125. const ss = StringStream('asdf');
  126. const ss_ = ss.fork();
  127. ss_.fwd();
  128. await assert('fwd worked', async () => {
  129. return await ss_.get_char() === 's';
  130. });
  131. await assert('upstream state is same', async () => {
  132. return await ss.get_char() === 'a';
  133. });
  134. })
  135. })
  136. })
  137. });