Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

source-parser.spec.ts 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import getParser, { Parser } from '../../src/parser/source-parser';
  2. import { Line } from '../../src/primitives';
  3. import { splitLines, seedBlock, seedTokens } from '../../src/util';
  4. let _parse: Parser;
  5. const nulls = (n: number): null[] => Array(n).fill(null);
  6. const parse = (source: string): Array<Line[] | null> =>
  7. splitLines(source).map(_parse);
  8. beforeEach(() => {
  9. _parse = getParser();
  10. });
  11. test('multi-line block', () => {
  12. const parsed = parse(`
  13. /**
  14. * description 0
  15. *
  16. * description 1
  17. *
  18. * @param {string} value value description 0
  19. \`\`\`
  20. @sample code
  21. \`\`\`
  22. * description 1
  23. */`);
  24. const block = [
  25. {
  26. number: 1,
  27. source: ' /**',
  28. tokens: seedTokens({
  29. start: ' ',
  30. delimiter: '/**',
  31. postDelimiter: '',
  32. description: '',
  33. end: '',
  34. }),
  35. },
  36. {
  37. number: 2,
  38. source: ' * description 0',
  39. tokens: seedTokens({
  40. start: ' ',
  41. delimiter: '*',
  42. postDelimiter: ' ',
  43. description: 'description 0',
  44. end: '',
  45. }),
  46. },
  47. {
  48. number: 3,
  49. source: ' *',
  50. tokens: seedTokens({
  51. start: ' ',
  52. delimiter: '*',
  53. postDelimiter: '',
  54. description: '',
  55. end: '',
  56. }),
  57. },
  58. {
  59. number: 4,
  60. source: ' * description 1',
  61. tokens: seedTokens({
  62. start: ' ',
  63. delimiter: '*',
  64. postDelimiter: ' ',
  65. description: 'description 1',
  66. end: '',
  67. }),
  68. },
  69. {
  70. number: 5,
  71. source: ' *',
  72. tokens: seedTokens({
  73. start: ' ',
  74. delimiter: '*',
  75. postDelimiter: '',
  76. description: '',
  77. end: '',
  78. }),
  79. },
  80. {
  81. number: 6,
  82. source: ' * @param {string} value value description 0',
  83. tokens: seedTokens({
  84. start: ' ',
  85. delimiter: '*',
  86. postDelimiter: ' ',
  87. description: '@param {string} value value description 0',
  88. end: '',
  89. }),
  90. },
  91. {
  92. number: 7,
  93. source: ' ```',
  94. tokens: seedTokens({
  95. start: ' ',
  96. delimiter: '',
  97. postDelimiter: '',
  98. description: '```',
  99. end: '',
  100. }),
  101. },
  102. {
  103. number: 8,
  104. source: ' @sample code',
  105. tokens: seedTokens({
  106. start: ' ',
  107. delimiter: '',
  108. postDelimiter: '',
  109. description: '@sample code',
  110. end: '',
  111. }),
  112. },
  113. {
  114. number: 9,
  115. source: ' ```',
  116. tokens: seedTokens({
  117. start: ' ',
  118. delimiter: '',
  119. postDelimiter: '',
  120. description: '```',
  121. end: '',
  122. }),
  123. },
  124. {
  125. number: 10,
  126. source: ' * description 1',
  127. tokens: seedTokens({
  128. start: ' ',
  129. delimiter: '*',
  130. postDelimiter: ' ',
  131. description: 'description 1',
  132. end: '',
  133. }),
  134. },
  135. {
  136. number: 11,
  137. source: ' */',
  138. tokens: seedTokens({
  139. start: ' ',
  140. delimiter: '',
  141. postDelimiter: '',
  142. description: '',
  143. end: '*/',
  144. }),
  145. },
  146. ];
  147. expect(parsed).toEqual([...nulls(11), block]);
  148. });
  149. test('one-line block', () => {
  150. const parsed = parse(`
  151. /** description */
  152. `);
  153. const block = [
  154. {
  155. number: 1,
  156. source: ' /** description */',
  157. tokens: seedTokens({
  158. start: ' ',
  159. delimiter: '/**',
  160. postDelimiter: ' ',
  161. description: 'description ',
  162. end: '*/',
  163. }),
  164. },
  165. ];
  166. expect(parsed).toEqual([null, block, null]);
  167. });
  168. test('multiple blocks', () => {
  169. const parsed = parse(`
  170. /** description 0 */
  171. /** description 1 */
  172. `);
  173. const block0 = [
  174. {
  175. number: 1,
  176. source: ' /** description 0 */',
  177. tokens: seedTokens({
  178. start: ' ',
  179. delimiter: '/**',
  180. postDelimiter: ' ',
  181. description: 'description 0 ',
  182. end: '*/',
  183. }),
  184. },
  185. ];
  186. const block1 = [
  187. {
  188. number: 3,
  189. source: ' /** description 1 */',
  190. tokens: seedTokens({
  191. start: ' ',
  192. delimiter: '/**',
  193. postDelimiter: ' ',
  194. description: 'description 1 ',
  195. end: '*/',
  196. }),
  197. },
  198. ];
  199. expect(parsed).toEqual([null, block0, null, block1, null]);
  200. });
  201. test('start line number', () => {
  202. const source = splitLines(`
  203. /** description */`);
  204. const parsed = source.map(getParser({ startLine: 5 }));
  205. const block = [
  206. {
  207. number: 6,
  208. source: ' /** description */',
  209. tokens: seedTokens({
  210. start: ' ',
  211. delimiter: '/**',
  212. postDelimiter: ' ',
  213. description: 'description ',
  214. end: '*/',
  215. }),
  216. },
  217. ];
  218. expect(parsed).toEqual([null, block]);
  219. });
  220. test('carriage returns', () => {
  221. const source = splitLines(
  222. ['/**', ' * description', ' *', ' */', ''].join('\r\n')
  223. );
  224. const parse = getParser();
  225. const parsed = source.map(parse);
  226. const block = [
  227. {
  228. number: 0,
  229. source: '/**\r',
  230. tokens: seedTokens({
  231. delimiter: '/**',
  232. lineEnd: '\r',
  233. }),
  234. },
  235. {
  236. number: 1,
  237. source: ' * description\r',
  238. tokens: seedTokens({
  239. start: ' ',
  240. delimiter: '*',
  241. postDelimiter: ' ',
  242. description: 'description',
  243. lineEnd: '\r',
  244. }),
  245. },
  246. {
  247. number: 2,
  248. source: ' *\r',
  249. tokens: seedTokens({
  250. start: ' ',
  251. delimiter: '*',
  252. lineEnd: '\r',
  253. }),
  254. },
  255. {
  256. number: 3,
  257. source: ' */\r',
  258. tokens: seedTokens({
  259. start: ' ',
  260. end: '*/',
  261. lineEnd: '\r',
  262. }),
  263. },
  264. ];
  265. expect(parsed).toEqual([...nulls(3), block, null]);
  266. });