Debian Codegraph @

test/test.js

raw
1 const fs = require( 'fs' );
2 const assert = require( 'assert' );
3 const { describe, it } = require( 'mocha' );
4
5 require( 'source-map-support' ).install();
6 require( 'console-group' ).install();
7
8 const tippex = require( '../' );
9
10 let samples = {};
11 fs.readdirSync( 'test/samples' ).forEach( file => {
12 samples[ file.replace( /\.jsx?$/, '' ) ] = fs.readFileSync( `test/samples/${file}`, 'utf-8' );
13 });
14
15 describe( 'tippex', () => {
16 describe( 'find', () => {
17 let found;
18
19 before( () => {
20 found = tippex.find( samples.misc );
21 });
22
23 it( 'finds line comments', () => {
24 const lines = found.filter( chunk => chunk.type === 'line' );
25
26 const start = samples.misc.indexOf( '//' ) + 2;
27 const end = samples.misc.indexOf( '\n', start );
28
29 assert.equal( lines.length, 1 );
30 assert.deepEqual( lines[0], {
31 start,
32 end,
33 value: ' line comment',
34 type: 'line'
35 });
36 });
37
38 it( 'finds block comments', () => {
39 const blocks = found.filter( chunk => chunk.type === 'block' );
40
41 const start = samples.misc.indexOf( '/*' ) + 2;
42 const end = samples.misc.indexOf( '*/' );
43
44 const comment = samples.misc.slice( start, end );
45
46 assert.equal( blocks.length, 1 );
47 assert.deepEqual( blocks[0], {
48 start,
49 end,
50 value: comment,
51 type: 'block'
52 });
53 });
54
55 it( 'finds regular expressions', () => {
56 const regexes = found.filter( chunk => chunk.type === 'regex' );
57
58 const start = samples.misc.indexOf( '/you' ) + 1;
59 const end = samples.misc.indexOf( 'cool/' ) + 4;
60 const regex = samples.misc.slice( start, end );
61
62 assert.equal( regexes.length, 2 );
63 assert.deepEqual( regexes[0], {
64 start,
65 end,
66 value: regex,
67 type: 'regex'
68 });
69 });
70
71 it( 'finds template strings', () => {
72 const templateStrings = found.filter( chunk => chunk.type.slice( 0, 8 ) === 'template' );
73
74 assert.equal( templateStrings.length, 2 );
75
76 let start = samples.misc.indexOf( '`the' ) + 1;
77 let end = samples.misc.indexOf( '${' );
78 let section = samples.misc.slice( start, end );
79
80 assert.deepEqual( templateStrings[0], {
81 start,
82 end,
83 value: section,
84 type: 'templateChunk'
85 });
86
87 start = samples.misc.indexOf( '}.' ) + 1;
88 end = samples.misc.indexOf( '\\``' ) + 2;
89 section = samples.misc.slice( start, end );
90
91 assert.deepEqual( templateStrings[1], {
92 start,
93 end,
94 value: section,
95 type: 'templateEnd'
96 });
97 });
98
99 it( 'finds normal strings', () => {
100 const strings = found.filter( chunk => chunk.type === 'string' );
101
102 assert.equal( strings.length, 2 );
103
104 let start = samples.misc.indexOf( "'" ) + 1;
105 let end = samples.misc.indexOf( "';" );
106 let string = samples.misc.slice( start, end );
107
108 assert.deepEqual( strings[0], {
109 start,
110 end,
111 value: string,
112 type: 'string'
113 });
114
115 start = samples.misc.indexOf( '"' ) + 1;
116 end = samples.misc.indexOf( '";' );
117 string = samples.misc.slice( start, end );
118
119 assert.deepEqual( strings[1], {
120 start,
121 end,
122 value: string,
123 type: 'string'
124 });
125 });
126 });
127
128 describe( 'erase', () => {
129 const tests = {
130 'erases a line comment': [
131 `const answer = 42; // line comment`,
132 `const answer = 42; // `
133 ],
134
135 'erases a line comment at the start of a line': [
136 `// line comment`,
137 `// `
138 ],
139
140 'erases a line comment with parens (#8)': [
141 `//)\n//\n`,
142 `// \n//\n`
143 ],
144
145 'removes jsx contents': [
146 '<div>this should disappear</div>',
147 '<div> </div>'
148 ],
149
150 'removes jsx attributes': [
151 '<div x="y"></div>',
152 '<div x=" "></div>'
153 ],
154
155 'handles simple jsx syntax': [
156 `/**/<a>/**/{/**/}</a>/**/;`,
157 `/**/<a> {/**/}</a>/**/;`
158 ],
159
160 'erases block comments': [
161 `/*foo*/`,
162 `/* */`
163 ],
164
165 'erases regular expressions': [
166 `const regex = /you can ignore\\/[/]skip me, it's cool/;`,
167 `const regex = / /;`
168 ],
169
170 'erases template strings': [
171 "const templateString = `the answer is ${answer}. This is a backtick: \\``;",
172 "const templateString = ` ${answer} `;"
173 ],
174
175 'erases normal strings': [
176 `const singleQuotedString = 'i\\'m trying to escape';`,
177 `const singleQuotedString = ' ';`
178 ],
179
180 'handles double trailing asterisks in block comments': [
181 '/* double trailing asterisks **/',
182 '/* */'
183 ],
184
185 'handles comments before division': [
186 '1 /**/ / 2',
187 '1 /**/ / 2'
188 ],
189
190 'handles comments before regex': [
191 `foo = 'bar'; /**/ /bar/.test(foo)`,
192 `foo = ' '; /**/ / /.test(foo)`
193 ],
194
195 'removes dollar inside template string': [
196 'const a = `$`;',
197 'const a = ` `;'
198 ],
199
200 'removes escaped dollar inside template string': [
201 'const a = `\\$`;',
202 'const a = ` `;'
203 ],
204
205 'removes things that look like template expressions': [
206 'const c = `$ {}${ `${ `$` }` }`;',
207 'const c = ` ${ `${ ` ` }` }`;'
208 ],
209
210 'handles curlies inside a regex following export default (#1)': [
211 'export default /^}{/',
212 'export default / /'
213 ],
214
215 'handles / character after parenthesized expression': [
216 '( a + b ) / /x+/.exec( y )[0].length',
217 '( a + b ) / / /.exec( y )[0].length'
218 ],
219
220 'handles regex with escaped slash': [
221 'var regex = /\\//;',
222 'var regex = / /;'
223 ],
224
225 'handles slash after almost-keyword': [
226 `dodo / 'nonsense'`,
227 `dodo / ' '`
228 ]
229 };
230
231 Object.keys( tests ).forEach( key => {
232 ( key[0] === '-' ? it.only : it )( key, () => {
233 const [ before, after ] = tests[ key ];
234 assert.equal( tippex.erase( before ), after );
235 });
236 });
237
238 it( 'handles tricky regex/division cases', () => {
239 const erased = tippex.erase( samples.regexDivisionBefore );
240 assert.equal( erased, samples.regexDivisionAfter );
241 });
242
243 it( "handles jsx syntax", () => {
244 const erased = tippex.erase( samples.jsxBefore );
245 assert.equal( erased, samples.jsxAfter );
246 });
247
248 it( 'erases block comments', () => {
249 const erased = tippex.erase( samples.misc );
250 assert.equal( erased.indexOf( 'Multi' ), -1 );
251 });
252 });
253
254 describe( 'match', () => {
255 it( 'matches regular expressions against the original string', () => {
256 const importPattern = /import (\w+) from '([^']+)'/g;
257
258 let results = [];
259 tippex.match( samples.imports, importPattern, ( match, name, source ) => {
260 results.push({ match, name, source });
261 });
262
263 assert.deepEqual( results, [
264 {
265 match: "import a from './a.js'",
266 name: 'a',
267 source: './a.js'
268 },
269 {
270 match: "import c from './c.js'",
271 name: 'c',
272 source: './c.js'
273 }
274 ]);
275 });
276
277 it( 'matches regular expressions without the global flag', () => {
278 const importPattern = /import (\w+) from '([^']+)'/;
279
280 let results = [];
281 tippex.match( samples.imports, importPattern, ( match, name, source ) => {
282 results.push({ match, name, source });
283 });
284
285 assert.deepEqual( results, [
286 {
287 match: "import a from './a.js'",
288 name: 'a',
289 source: './a.js'
290 }
291 ]);
292 });
293 });
294
295 describe( 'replace', () => {
296 it( 'replaces a pattern', () => {
297 const importPattern = /import (\w+) from '([^']+)'/g;
298
299 var result = tippex.replace( samples.imports, importPattern, ( match, name, source ) => {
300 return `var ${name} = require('${source}')`;
301 });
302
303 assert.equal( result, samples.requires );
304 });
305 });
306 });