|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- import getParser, { Parser } from '../../src/parser/source-parser';
- import { Line } from '../../src/primitives';
- import { splitLines, seedBlock, seedTokens } from '../../src/util';
-
- let _parse: Parser;
-
- const nulls = (n: number): null[] => Array(n).fill(null);
- const parse = (source: string): Array<Line[] | null> =>
- splitLines(source).map(_parse);
-
- beforeEach(() => {
- _parse = getParser();
- });
-
- test('multi-line block', () => {
- const parsed = parse(`
- /**
- * description 0
- *
- * description 1
- *
- * @param {string} value value description 0
- \`\`\`
- @sample code
- \`\`\`
- * description 1
- */`);
-
- const block = [
- {
- number: 1,
- source: ' /**',
- tokens: seedTokens({
- start: ' ',
- delimiter: '/**',
- postDelimiter: '',
- description: '',
- end: '',
- }),
- },
- {
- number: 2,
- source: ' * description 0',
- tokens: seedTokens({
- start: ' ',
- delimiter: '*',
- postDelimiter: ' ',
- description: 'description 0',
- end: '',
- }),
- },
- {
- number: 3,
- source: ' *',
- tokens: seedTokens({
- start: ' ',
- delimiter: '*',
- postDelimiter: '',
- description: '',
- end: '',
- }),
- },
- {
- number: 4,
- source: ' * description 1',
- tokens: seedTokens({
- start: ' ',
- delimiter: '*',
- postDelimiter: ' ',
- description: 'description 1',
- end: '',
- }),
- },
- {
- number: 5,
- source: ' *',
- tokens: seedTokens({
- start: ' ',
- delimiter: '*',
- postDelimiter: '',
- description: '',
- end: '',
- }),
- },
- {
- number: 6,
- source: ' * @param {string} value value description 0',
- tokens: seedTokens({
- start: ' ',
- delimiter: '*',
- postDelimiter: ' ',
- description: '@param {string} value value description 0',
- end: '',
- }),
- },
- {
- number: 7,
- source: ' ```',
- tokens: seedTokens({
- start: ' ',
- delimiter: '',
- postDelimiter: '',
- description: '```',
- end: '',
- }),
- },
- {
- number: 8,
- source: ' @sample code',
- tokens: seedTokens({
- start: ' ',
- delimiter: '',
- postDelimiter: '',
- description: '@sample code',
- end: '',
- }),
- },
- {
- number: 9,
- source: ' ```',
- tokens: seedTokens({
- start: ' ',
- delimiter: '',
- postDelimiter: '',
- description: '```',
- end: '',
- }),
- },
- {
- number: 10,
- source: ' * description 1',
- tokens: seedTokens({
- start: ' ',
- delimiter: '*',
- postDelimiter: ' ',
- description: 'description 1',
- end: '',
- }),
- },
- {
- number: 11,
- source: ' */',
- tokens: seedTokens({
- start: ' ',
- delimiter: '',
- postDelimiter: '',
- description: '',
- end: '*/',
- }),
- },
- ];
-
- expect(parsed).toEqual([...nulls(11), block]);
- });
-
- test('one-line block', () => {
- const parsed = parse(`
- /** description */
- `);
-
- const block = [
- {
- number: 1,
- source: ' /** description */',
- tokens: seedTokens({
- start: ' ',
- delimiter: '/**',
- postDelimiter: ' ',
- description: 'description ',
- end: '*/',
- }),
- },
- ];
-
- expect(parsed).toEqual([null, block, null]);
- });
-
- test('multiple blocks', () => {
- const parsed = parse(`
- /** description 0 */
-
- /** description 1 */
- `);
-
- const block0 = [
- {
- number: 1,
- source: ' /** description 0 */',
- tokens: seedTokens({
- start: ' ',
- delimiter: '/**',
- postDelimiter: ' ',
- description: 'description 0 ',
- end: '*/',
- }),
- },
- ];
-
- const block1 = [
- {
- number: 3,
- source: ' /** description 1 */',
- tokens: seedTokens({
- start: ' ',
- delimiter: '/**',
- postDelimiter: ' ',
- description: 'description 1 ',
- end: '*/',
- }),
- },
- ];
-
- expect(parsed).toEqual([null, block0, null, block1, null]);
- });
-
- test('start line number', () => {
- const source = splitLines(`
- /** description */`);
-
- const parsed = source.map(getParser({ startLine: 5 }));
-
- const block = [
- {
- number: 6,
- source: ' /** description */',
- tokens: seedTokens({
- start: ' ',
- delimiter: '/**',
- postDelimiter: ' ',
- description: 'description ',
- end: '*/',
- }),
- },
- ];
-
- expect(parsed).toEqual([null, block]);
- });
-
- test('carriage returns', () => {
- const source = splitLines(
- ['/**', ' * description', ' *', ' */', ''].join('\r\n')
- );
-
- const parse = getParser();
- const parsed = source.map(parse);
-
- const block = [
- {
- number: 0,
- source: '/**\r',
- tokens: seedTokens({
- delimiter: '/**',
- lineEnd: '\r',
- }),
- },
- {
- number: 1,
- source: ' * description\r',
- tokens: seedTokens({
- start: ' ',
- delimiter: '*',
- postDelimiter: ' ',
- description: 'description',
- lineEnd: '\r',
- }),
- },
- {
- number: 2,
- source: ' *\r',
- tokens: seedTokens({
- start: ' ',
- delimiter: '*',
- lineEnd: '\r',
- }),
- },
- {
- number: 3,
- source: ' */\r',
- tokens: seedTokens({
- start: ' ',
- end: '*/',
- lineEnd: '\r',
- }),
- },
- ];
-
- expect(parsed).toEqual([...nulls(3), block, null]);
- });
|