123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- import descriptionTokenizer from '../../src/parser/tokenizers/description';
- import nameTokenizer from '../../src/parser/tokenizers/name';
- import tagTokenizer from '../../src/parser/tokenizers/tag';
- import typeTokenizer from '../../src/parser/tokenizers/type';
- import getParser from '../../src/parser/spec-parser';
- import { seedTokens, seedSpec } from '../../src/util';
- import { Spec, Problem } from '../../src/primitives';
-
- const parse = getParser({
- tokenizers: [
- tagTokenizer(),
- typeTokenizer(),
- nameTokenizer(),
- descriptionTokenizer(),
- ],
- });
-
- const tokenizer = (message: string, critical = false) => {
- const problem: Problem = {
- code: 'custom',
- line: 1,
- message,
- critical,
- };
- return (spec: Spec) => ({
- ...spec,
- problems: [...spec.problems, problem],
- });
- };
-
- test('all tokens', () => {
- const parsed = parse([
- {
- number: 1,
- source: '...',
- tokens: seedTokens({
- description: '@param {type} [name=value] description',
- }),
- },
- ]);
- expect(parsed).toEqual(
- seedSpec({
- tag: 'param',
- type: 'type',
- name: 'name',
- default: 'value',
- optional: true,
- description: 'description',
- source: [
- {
- number: 1,
- source: '...',
- tokens: seedTokens({
- tag: '@param',
- postTag: ' ',
- type: '{type}',
- postType: ' ',
- name: '[name=value]',
- postName: ' ',
- description: 'description',
- }),
- },
- ],
- })
- );
- });
-
- test('quotes in description', () => {
- const parsed = parse([
- {
- number: 1,
- source: '...',
- tokens: seedTokens({
- description:
- '@param {type} [name=value] description with "quoted" word',
- }),
- },
- ]);
- expect(parsed).toEqual(
- seedSpec({
- tag: 'param',
- type: 'type',
- name: 'name',
- default: 'value',
- optional: true,
- description: 'description with "quoted" word',
- source: [
- {
- number: 1,
- source: '...',
- tokens: seedTokens({
- tag: '@param',
- postTag: ' ',
- type: '{type}',
- postType: ' ',
- name: '[name=value]',
- postName: ' ',
- description: 'description with "quoted" word',
- }),
- },
- ],
- })
- );
- });
-
- test('collect non-critical errors', () => {
- const parse = getParser({
- tokenizers: [tokenizer('warning 1'), tokenizer('warning 2')],
- });
-
- const parsed = parse([
- {
- number: 1,
- source: '...',
- tokens: seedTokens({ description: 'description' }),
- },
- ]);
-
- expect(parsed).toEqual({
- tag: '',
- name: '',
- optional: false,
- type: '',
- description: '',
- problems: [
- {
- code: 'custom',
- line: 1,
- message: 'warning 1',
- critical: false,
- },
- {
- code: 'custom',
- line: 1,
- message: 'warning 2',
- critical: false,
- },
- ],
- source: [
- {
- number: 1,
- source: '...',
- tokens: seedTokens({ description: 'description' }),
- },
- ],
- });
- });
-
- test('stop on critical error', () => {
- const parse = getParser({
- tokenizers: [tokenizer('error 1', true), tokenizer('warning 2')],
- });
-
- const parsed = parse([
- {
- number: 1,
- source: '...',
- tokens: seedTokens({ description: 'description' }),
- },
- ]);
-
- expect(parsed).toEqual({
- tag: '',
- name: '',
- optional: false,
- type: '',
- description: '',
- problems: [
- {
- code: 'custom',
- line: 1,
- message: 'error 1',
- critical: true,
- },
- ],
- source: [
- {
- number: 1,
- source: '...',
- tokens: seedTokens({ description: 'description' }),
- },
- ],
- });
- });
|