Skip to content

Commit d955d4b

Browse files
committed
Improved unit test validation for DocElementParser
1 parent 330ff28 commit d955d4b

2 files changed

Lines changed: 48 additions & 5 deletions

File tree

‎libraries/api-extractor/src/definitions/test/ApiDocumentation.test.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ describe('ApiDocumentation tests', function (): void {
6565
* - testInputs/example2/folder/MyDocumentedClass (10 errors)
6666
*/
6767

68-
console.log(JSON.stringify(capturedErrors));
69-
7068
assert.equal(capturedErrors.length, 8);
7169
assert.equal(capturedErrors[0].message, 'Cannot provide summary in JsDoc if @inheritdoc tag is given');
7270
assert.equal(capturedErrors[1].message, 'Unknown JSDoc tag "@badJsDocTag"');

‎libraries/api-extractor/src/test/DocElementParser.test.ts‎

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import ApiDocumentation from '../definitions/ApiDocumentation';
1212
import Extractor from './../Extractor';
1313
import Tokenizer from './../Tokenizer';
1414

15-
const capturedErrors: {
15+
let capturedErrors: {
1616
message: string;
1717
fileName: string;
1818
lineNumber: number;
@@ -22,6 +22,15 @@ function testErrorHandler(message: string, fileName: string, lineNumber: number)
2222
capturedErrors.push({ message, fileName, lineNumber });
2323
}
2424

25+
function clearCapturedErrors(): void {
26+
capturedErrors = [];
27+
}
28+
29+
function assertCapturedErrors(expectedMessages: string[]): void {
30+
assert.deepEqual(capturedErrors.map(x => x.message), expectedMessages,
31+
'The captured errors did not match the expected output.');
32+
}
33+
2534
const inputFolder: string = './testInputs/example2';
2635
let myDocumentedClass: ApiStructuredType;
2736

@@ -74,6 +83,7 @@ describe('DocElementParser tests', function (): void {
7483

7584
describe('Basic Tests', (): void => {
7685
it('Should parse basic doc comment stream', (): void => {
86+
clearCapturedErrors();
7787
const apiDoc: TestApiDocumentation = new TestApiDocumentation();
7888

7989
const docs: string = 'This function parses docTokens for the apiLint website ' +
@@ -129,9 +139,11 @@ describe('DocElementParser tests', function (): void {
129139
JsonFile.saveJsonFile('./lib/paramDocExpected.json', JSON.stringify(expectedParam));
130140
JsonFile.saveJsonFile('./lib/paramDocActual.json', JSON.stringify(actualParam));
131141
TestFileComparer.assertFileMatchesExpected('./lib/paramDocActual.json', './lib/paramDocExpected.json');
142+
assertCapturedErrors([]);
132143
});
133144

134145
it('Should parse @deprecated correctly', (): void => {
146+
clearCapturedErrors();
135147
const docs: string = '@deprecated - description of the deprecation';
136148
const tokenizer: Tokenizer = new Tokenizer(docs, console.log);
137149

@@ -144,9 +156,11 @@ describe('DocElementParser tests', function (): void {
144156
JsonFile.saveJsonFile('./lib/deprecatedDocExpected.json', JSON.stringify(expectedDeprecated));
145157
JsonFile.saveJsonFile('./lib/deprecatedDocActual.json', JSON.stringify(actualDeprecated));
146158
TestFileComparer.assertFileMatchesExpected('./lib/deprecatedDocActual.json', './lib/deprecatedDocExpected.json');
159+
assertCapturedErrors([]);
147160
});
148161

149162
it('Should parse @see with nested link and/or text', (): void => {
163+
clearCapturedErrors();
150164
const docs: string = 'Text describing the function’s purpose/nuances/context. \n' +
151165
'@see {@link https://github.com/OfficeDev/office-ui-fabric-react | The link will provide context}';
152166
const tokenizer: Tokenizer = new Tokenizer(docs, console.log);
@@ -170,9 +184,11 @@ describe('DocElementParser tests', function (): void {
170184
JsonFile.saveJsonFile('./lib/seeDocExpected.json', JSON.stringify(expectedSummary));
171185
JsonFile.saveJsonFile('./lib/seeDocActual.json', JSON.stringify(actualSummary));
172186
TestFileComparer.assertFileMatchesExpected('./lib/seeDocExpected.json', './lib/seeDocActual.json');
187+
assertCapturedErrors([]);
173188
});
174189

175190
it('Should parse @param with nested link and/or text', (): void => {
191+
clearCapturedErrors();
176192
const apiDoc: TestApiDocumentation = new TestApiDocumentation();
177193

178194
// Don't include the "@param" in the doc string, parseParam() expects this to be processed in a
@@ -202,9 +218,11 @@ describe('DocElementParser tests', function (): void {
202218
'./lib/nestedParamDocActual.json',
203219
'./lib/nestedParamDocExpected.json'
204220
);
221+
assertCapturedErrors([]);
205222
});
206223

207224
it('Should parse @link with url', (): void => {
225+
clearCapturedErrors();
208226
const docs: string = '{@link https://microsoft.com}';
209227
const tokenizer: Tokenizer = new Tokenizer(docs, console.log);
210228

@@ -222,11 +240,13 @@ describe('DocElementParser tests', function (): void {
222240
assert.equal(linkDocElement.referenceType, 'href');
223241
assert.equal(linkDocElement.targetUrl, 'https://microsoft.com');
224242
assert.equal(linkDocElement.value, '');
243+
assertCapturedErrors([]);
225244
});
226245

227246
it('Should parse @link with url and text', (): void => {
228-
const docs: string = '{@link https://microsoft.com | microsoft home}';
229-
const tokenizer: Tokenizer = new Tokenizer(docs, console.log);
247+
clearCapturedErrors();
248+
const docs: string = '{@link https://microsoft.com | microsoft home}';
249+
const tokenizer: Tokenizer = new Tokenizer(docs, console.log);
230250

231251
let docElements: IDocElement[];
232252
/* tslint:disable-next-line:no-any */
@@ -242,9 +262,29 @@ describe('DocElementParser tests', function (): void {
242262
assert.equal(linkDocElement.referenceType, 'href');
243263
assert.equal(linkDocElement.targetUrl, 'https://microsoft.com');
244264
assert.equal(linkDocElement.value, 'microsoft home');
265+
assertCapturedErrors([]);
266+
});
267+
268+
it('Should reject @link with missing pipe', (): void => {
269+
clearCapturedErrors();
270+
271+
const docs: string = '{@link https://microsoft.com microsoft home}';
272+
const tokenizer: Tokenizer = new Tokenizer(docs, console.log);
273+
274+
let docElements: IDocElement[];
275+
/* tslint:disable-next-line:no-any */
276+
let errorMessage: any;
277+
try {
278+
docElements = DocElementParser.parse(myDocumentedClass.documentation, tokenizer);
279+
} catch (error) {
280+
errorMessage = error;
281+
}
282+
assert.isUndefined(errorMessage);
283+
assertCapturedErrors(['Invalid @link parameter, url must be a single string.']);
245284
});
246285

247286
it('Should parse @link with API defintion reference', (): void => {
287+
clearCapturedErrors();
248288
const docs: string = '{@link @microsoft/sp-core-library:Guid.equals}';
249289
const tokenizer: Tokenizer = new Tokenizer(docs, console.log);
250290

@@ -264,9 +304,11 @@ describe('DocElementParser tests', function (): void {
264304
assert.equal(linkDocElement.packageName, 'sp-core-library');
265305
assert.equal(linkDocElement.exportName, 'Guid');
266306
assert.equal(linkDocElement.memberName, 'equals');
307+
assertCapturedErrors([]);
267308
});
268309

269310
it('Should parse @link with API defintion reference and text', (): void => {
311+
clearCapturedErrors();
270312
const docs: string = '{@link @microsoft/sp-core-library:Guid.equals | Guid equals}';
271313
const tokenizer: Tokenizer = new Tokenizer(docs, console.log);
272314

@@ -287,9 +329,11 @@ describe('DocElementParser tests', function (): void {
287329
assert.equal(linkDocElement.exportName, 'Guid');
288330
assert.equal(linkDocElement.memberName, 'equals');
289331
assert.equal(linkDocElement.value, 'Guid equals');
332+
assertCapturedErrors([]);
290333
});
291334

292335
it('Should report errors @link', (): void => {
336+
clearCapturedErrors();
293337
const docs: string = '{@link @microsoft/sp-core-library:Guid.equals | Guid equals | something}';
294338
const tokenizer: Tokenizer = new Tokenizer(docs, console.log);
295339

@@ -302,6 +346,7 @@ describe('DocElementParser tests', function (): void {
302346
errorMessage = error;
303347
}
304348
assert.isNotNull(errorMessage);
349+
assertCapturedErrors(['Invalid @link parameters, at most one pipe character allowed.']);
305350
});
306351
});
307352
});

0 commit comments

Comments
 (0)