1 | import { TSDocParser } from '../TSDocParser';
|
2 | import { DocErrorText, DocPlainText, DocExcerpt } from '../../nodes';
|
3 | import { TSDocConfiguration } from '../../configuration/TSDocConfiguration';
|
4 | import { TokenCoverageChecker } from './TokenCoverageChecker';
|
5 | var TestHelpers = (function () {
|
6 | function TestHelpers() {
|
7 | }
|
8 | |
9 |
|
10 |
|
11 | TestHelpers.formatLineSpan = function (line, range) {
|
12 | if (range.pos < line.pos || range.end > line.end) {
|
13 | throw new Error('Range must fall within the associated line');
|
14 | }
|
15 | var paddedSpace = ['', ' ', ' ', ' ', ' '];
|
16 | var paddedLArrow = ['', '>', ' >', ' >', ' >'];
|
17 | var paddedRArrow = ['', '<', '< ', '< ', '< '];
|
18 | var buffer = line.buffer;
|
19 | var span = '';
|
20 | if (line.end > 0) {
|
21 | var i = line.pos - 1;
|
22 | while (i < range.pos - 1) {
|
23 | span += paddedSpace[TestHelpers.getEscaped(buffer[i]).length];
|
24 | ++i;
|
25 | }
|
26 | span += paddedLArrow[TestHelpers.getEscaped(buffer[i]).length];
|
27 | ++i;
|
28 | while (i < range.end) {
|
29 | span += paddedSpace[TestHelpers.getEscaped(buffer[i]).length];
|
30 | ++i;
|
31 | }
|
32 | if (i === line.end) {
|
33 | span += '<';
|
34 | }
|
35 | else {
|
36 | span += paddedRArrow[TestHelpers.getEscaped(buffer[i]).length];
|
37 | ++i;
|
38 | while (i < line.end) {
|
39 | span += paddedSpace[TestHelpers.getEscaped(buffer[i]).length];
|
40 | ++i;
|
41 | }
|
42 | }
|
43 | }
|
44 | return span;
|
45 | };
|
46 | |
47 |
|
48 |
|
49 | TestHelpers.getEscaped = function (s) {
|
50 | return s
|
51 | .replace(/\n/g, '[n]')
|
52 | .replace(/\r/g, '[r]')
|
53 | .replace(/\t/g, '[t]')
|
54 | .replace(/\f/g, '[f]')
|
55 | .replace(/\\/g, '[b]')
|
56 | .replace(/\"/g, '[q]')
|
57 | .replace(/`/g, '[c]')
|
58 | .replace(/\</g, '[<]')
|
59 | .replace(/\>/g, '[>]');
|
60 | };
|
61 | |
62 |
|
63 |
|
64 | TestHelpers.parseAndMatchNodeParserSnapshot = function (buffer, config) {
|
65 | var configuration = config !== null && config !== void 0 ? config : new TSDocConfiguration();
|
66 |
|
67 | configuration.validation.ignoreUndefinedTags = true;
|
68 | var tsdocParser = new TSDocParser(configuration);
|
69 | var parserContext = tsdocParser.parseString(buffer);
|
70 | expect({
|
71 | buffer: TestHelpers.getEscaped(buffer),
|
72 | lines: parserContext.lines.map(function (x) { return TestHelpers.getEscaped(x.toString()); }),
|
73 | logMessages: parserContext.log.messages.map(function (message) { return message.text; }),
|
74 | nodes: TestHelpers.getDocNodeSnapshot(parserContext.docComment),
|
75 | gaps: this._getTokenCoverageGapsSnapshot(parserContext)
|
76 | }).toMatchSnapshot();
|
77 | TestHelpers._getTokenCoverageGapsSnapshot(parserContext);
|
78 | };
|
79 | |
80 |
|
81 |
|
82 | TestHelpers.parseAndMatchDocCommentSnapshot = function (buffer, configuration) {
|
83 | var tsdocParser = new TSDocParser(configuration);
|
84 | var parserContext = tsdocParser.parseString(buffer);
|
85 | var docComment = parserContext.docComment;
|
86 | expect({
|
87 | s00_lines: parserContext.lines.map(function (x) { return TestHelpers.getEscaped(x.toString()); }),
|
88 | s01_gaps: this._getTokenCoverageGapsSnapshot(parserContext),
|
89 | s02_summarySection: TestHelpers.getDocNodeSnapshot(docComment.summarySection),
|
90 | s03_remarksBlock: TestHelpers.getDocNodeSnapshot(docComment.remarksBlock),
|
91 | s04_privateRemarksBlock: TestHelpers.getDocNodeSnapshot(docComment.privateRemarks),
|
92 | s05_deprecatedBlock: TestHelpers.getDocNodeSnapshot(docComment.deprecatedBlock),
|
93 | s06_paramBlocks: docComment.params.blocks.map(function (x) { return TestHelpers.getDocNodeSnapshot(x); }),
|
94 | s07_typeParamBlocks: docComment.typeParams.blocks.map(function (x) { return TestHelpers.getDocNodeSnapshot(x); }),
|
95 | s08_returnsBlock: TestHelpers.getDocNodeSnapshot(docComment.returnsBlock),
|
96 | s09_customBlocks: docComment.customBlocks.map(function (x) { return TestHelpers.getDocNodeSnapshot(x); }),
|
97 | s10_inheritDocTag: TestHelpers.getDocNodeSnapshot(docComment.inheritDocTag),
|
98 | s11_modifierTags: docComment.modifierTagSet.nodes.map(function (x) { return TestHelpers.getDocNodeSnapshot(x); }),
|
99 | s12_logMessages: parserContext.log.messages.map(function (message) { return message.text; })
|
100 | }).toMatchSnapshot();
|
101 | return parserContext;
|
102 | };
|
103 | |
104 |
|
105 |
|
106 | TestHelpers.getDocNodeSnapshot = function (docNode) {
|
107 | if (!docNode) {
|
108 | return undefined;
|
109 | }
|
110 | var item = {
|
111 | kind: docNode.kind
|
112 | };
|
113 | if (docNode instanceof DocExcerpt) {
|
114 | item.kind += ': ' + docNode.excerptKind;
|
115 | item.nodeExcerpt = TestHelpers.getEscaped(docNode.content.toString());
|
116 | }
|
117 | if (docNode instanceof DocPlainText) {
|
118 | var docPlainText = docNode;
|
119 | if (docPlainText.textExcerpt === undefined) {
|
120 | item.nodePlainText = TestHelpers.getEscaped(docPlainText.text);
|
121 | }
|
122 | }
|
123 | if (docNode instanceof DocErrorText) {
|
124 | item.errorMessage = TestHelpers.getEscaped(docNode.errorMessage);
|
125 | item.errorLocation = TestHelpers.getEscaped(docNode.errorLocation.toString());
|
126 | if (docNode.errorLocation.startIndex > 0) {
|
127 |
|
128 |
|
129 | item.errorLocationPrecedingToken = docNode.errorLocation.parserContext.tokens[docNode.errorLocation.startIndex - 1].toString();
|
130 | }
|
131 | }
|
132 | if (docNode.getChildNodes().length > 0) {
|
133 | item.nodes = docNode.getChildNodes().map(function (x) { return TestHelpers.getDocNodeSnapshot(x); });
|
134 | }
|
135 | return item;
|
136 | };
|
137 | TestHelpers._getTokenCoverageGapsSnapshot = function (parserContext) {
|
138 | var tokenCoverageChecker = new TokenCoverageChecker(parserContext);
|
139 | return tokenCoverageChecker.getGaps(parserContext.docComment).map(function (x) { return x.toString(); });
|
140 | };
|
141 | return TestHelpers;
|
142 | }());
|
143 | export { TestHelpers };
|
144 |
|
\ | No newline at end of file |