Skip to content

Commit

Permalink
fix: return type incorrectly checking periods (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere authored and MarshallOfSound committed Dec 6, 2019
1 parent 110b288 commit f11cfe0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
74 changes: 74 additions & 0 deletions src/__tests__/markdown-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import MarkdownIt from 'markdown-it';
import {
safelyJoinTokens,
extractStringEnum,
extractReturnType,
rawTypeToTypeInformation,
parseHeadingTags,
findNextList,
Expand Down Expand Up @@ -366,6 +367,79 @@ foo`),
});
});

describe('extractReturnType()', () => {
it('should handle simple return types with descriptions', () => {
const intTokens = getTokens(`Returns \`Integer\` - The request id used for the request.`);
const intRet = extractReturnType(intTokens);
expect(intRet.parsedReturnType).toEqual({
collection: false,
type: 'Integer',
});

const stringTokens = getTokens(`Returns \`String\` - Returns the WebRTC IP Handling Policy.`);
const stringRet = extractReturnType(stringTokens);
expect(stringRet.parsedReturnType).toEqual({
collection: false,
possibleValues: null,
type: 'String',
});
});

it('should handle Promises with void inner types', () => {
const promiseTokens = getTokens(
`Returns \`Promise<void>\` - Indicates whether the snapshot has been created successfully.`,
);
const promiseRet = extractReturnType(promiseTokens);
expect(promiseRet.parsedReturnType).toEqual({
collection: false,
innerTypes: [
{
collection: false,
type: 'void',
},
],
type: 'Promise',
});
});

it('should handle Promises with non-void inner types', () => {
const promiseTokens = getTokens(
`Returns \`Promise<Buffer>\` - Resolves with the generated PDF data.`,
);
const promiseRet = extractReturnType(promiseTokens);
expect(promiseRet.parsedReturnType).toEqual({
collection: false,
innerTypes: [
{
collection: false,
type: 'Buffer',
},
],
type: 'Promise',
});
});

it('should handle custom return types', () => {
const customTokens = getTokens(
`Returns \`WebContents\` - A WebContents instance with the given ID.`,
);
const customRet = extractReturnType(customTokens);
expect(customRet.parsedReturnType).toEqual({
collection: false,
type: 'WebContents',
});
});

it('should handle return types with no descriptions', () => {
const printerTokens = getTokens(`Returns [\`PrinterInfo[]\`](structures/printer-info.md)`);
const printerRet = extractReturnType(printerTokens);
expect(printerRet.parsedReturnType).toEqual({
collection: true,
type: 'PrinterInfo',
});
});
});

describe('getTopLevelGenericType()', () => {
it('should return null if there is no generic in the type', () => {
expect(getTopLevelGenericType('Foo')).toEqual(null);
Expand Down
2 changes: 1 addition & 1 deletion src/markdown-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ export const extractReturnType = (
}

const returnsWithNewLineMatch = description.match(
new RegExp(`${prefix} \`([^\`]+?)\`:?(\. |\n|$)`),
new RegExp(`${prefix} \`([^\`]+?)\`:?(\. |\.\n|\n|$)`),
);
const returnsWithHyphenMatch = description.match(new RegExp(`${prefix} \`([^\`]+?)\` - `));
const returnsWithContinousSentence = description.match(new RegExp(`${prefix} \`([^\`]+?)\` `));
Expand Down

0 comments on commit f11cfe0

Please sign in to comment.