-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathexport_test.ts
382 lines (353 loc) · 12.4 KB
/
export_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import merge from '../src/index';
import { expect } from 'chai';
import 'mocha';
describe('Merging exports', () => {
describe('should accumulate exports from', () => {
describe('disjunct export sets', () => {
const base = `export { a } from 'b';
export { thit as that } from 'e';`,
patch = `export { c } from 'd';
export { thit as thot } from 'somewhere';`;
it('by default.', () => {
const result: String[] = merge(base, patch, false)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
expect(result.length, 'to contain all exports').to.equal(4);
expect(
result
.map((value) => value == "export { a } from 'b';")
.reduce((previous, current) => previous || current, false),
'to contain the first export from base',
).to.equal(true);
expect(
result
.map((value) => value == "export { thit as that } from 'e';")
.reduce((previous, current) => previous || current, false),
'to contain the second export from base',
).to.equal(true);
expect(
result
.map((value) => value == "export { c } from 'd';")
.reduce((previous, current) => previous || current, false),
'to contain the export from patch',
).to.equal(true);
expect(
result
.map(
(value) => value == "export { thit as thot } from 'somewhere';",
)
.reduce((previous, current) => previous || current, false),
'to contain the export from patch',
).to.equal(true);
});
it('with patchOverride. (Should not make any difference).', () => {
const result: String[] = merge(base, patch, true)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
expect(result.length, 'to contain all exports').to.equal(4);
expect(
result
.map((value) => value == "export { a } from 'b';")
.reduce((previous, current) => previous || current, false),
'to contain the first export from base',
).to.equal(true);
expect(
result
.map((value) => value == "export { thit as that } from 'e';")
.reduce((previous, current) => previous || current, false),
'to contain the second export from base',
).to.equal(true);
expect(
result
.map((value) => value == "export { c } from 'd';")
.reduce((previous, current) => previous || current, false),
'to contain the export from patch',
).to.equal(true);
expect(
result
.map(
(value) => value == "export { thit as thot } from 'somewhere';",
)
.reduce((previous, current) => previous || current, false),
'to contain the export from patch',
).to.equal(true);
});
});
describe('overlapping export sets', () => {
const base = `export { a } from 'b';
export { c } from 'd';`,
patch = `export { e } from 'f';
export { a } from 'b';`;
it('by default.', () => {
const result: String[] = merge(base, patch, false)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
expect(result.length, 'to contain unique exports').to.equal(3);
expect(
result.filter((value) => value == "export { a } from 'b';").length,
).to.equal(1);
});
it('with patchOverride. (Should not make any difference)', () => {
const result: String[] = merge(base, patch, true)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
expect(result.length, 'to contain unique exports').to.equal(3);
expect(
result.filter((value) => value == "export { a } from 'b';").length,
).to.equal(1);
});
});
describe('the same source in a single export statement', () => {
const base = `export { a } from 'somewhere';`,
patch = `export { b } from 'somewhere';`;
it('by default.', () => {
const result: String[] = merge(base, patch, false)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
expect(result.length, 'to contain only one export statement').to.equal(
1,
);
let exportRegex = new RegExp('.*{ [a,b], [a,b] }.*');
expect(exportRegex.test(result[0].toString())).to.be.true;
});
it('with patchOverride. (Should not make any difference)', () => {
const result: String[] = merge(base, patch, true)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
expect(result.length, 'to contain only one export statement').to.equal(
1,
);
let exportRegex = new RegExp('.*{ [a,b], [a,b] }.*');
expect(
exportRegex.test(result[0].toString()),
'to contain both exported artifacts from the module',
).to.be.true;
});
});
describe('the base if the patch is empty', () => {
const base = `export { a } from 'b';`,
patch = ``;
it('by default. This is a borderline case.', () => {
const result: String[] = merge(base, patch, false)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
expect(result.length).to.be.equal(1);
});
it('and patchOverride is set. This is a borderline case.', () => {
const result: String[] = merge(base, patch, true)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
expect(result.length).to.be.equal(1);
});
});
describe('the patch if the base is empty', () => {
const base = ``,
patch = `export { a } from 'b';`;
it('by default. This is a borderline case.', () => {
const result: String[] = merge(base, patch, false)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
expect(result.length).to.be.equal(1);
});
it('and patchOverride is set. This is a borderline case.', () => {
const result: String[] = merge(base, patch, true)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
expect(result.length).to.be.equal(1);
});
});
describe('the same source', () => {
const base = `export a from 'b';
export c from 'd';`,
patch = `export e from 'd';`;
it('by default.', () => {
const result: String[] = merge(base, patch, false)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
let exportRegex = /export\s*\{\s*c\s*,\s*e\s*\}\s*from\s*'d'\s*[;]?/;
expect(
result.filter((value) => exportRegex.test(value.toString())).length,
).equal(1);
});
it('when patchOverride is set (should not make a difference).', () => {
const result: String[] = merge(base, patch, true)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
let exportRegex = /export\s*\{\s*c\s*,\s*e\s*\}\s*from\s*'d'\s*[;]?/;
expect(
result.filter((value) => exportRegex.test(value.toString())).length,
).equal(1);
});
});
});
describe('Should merge exports with *', () => {
const base = `export * from 'b';`,
patch = `export * from 'b';
export * from 'd';`;
it('by default.', () => {
const result: String[] = merge(base, patch, false)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
let exportRegex = /export\s*\*\s*from\s*'b'\s*[;]?/;
expect(
result.filter((value) => exportRegex.test(value.toString())).length,
).equal(1);
exportRegex = /export\s*\*\s*from\s*'d'\s*[;]?/;
expect(
result.filter((value) => exportRegex.test(value.toString())).length,
).equal(1);
});
it('when patchOverride is set (should not make a difference).', () => {
const result: String[] = merge(base, patch, true)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
let exportRegex = /export\s*\*\s*from\s*'b'\s*[;]?/;
expect(
result.filter((value) => exportRegex.test(value.toString())).length,
).equal(1);
exportRegex = /export\s*\*\s*from\s*'d'\s*[;]?/;
expect(
result.filter((value) => exportRegex.test(value.toString())).length,
).equal(1);
});
});
describe('Should not mix imports and exports', () => {
const base = `import { O } from 'OS';
import { N } from 'NS';
export 'OC';
export const APIS = [ O, N ];`,
patch = `import { G } from 'GS';
export a, b from 'bla'
export const APIS = [G];`;
xit('by default.', () => {
/**
* currently not supported
*/
const result: String[] = merge(base, patch, false)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
let exportRegex = /export\s*{\s*\w,\s*\w\s*}\s*from\s*'bla';/;
expect(
result.filter((value) => exportRegex.test(value.toString())).length,
).equal(1);
});
xit('when patchOverride is set (should not make a difference).', () => {
/**
* currently not supported
*/
const result: String[] = merge(base, patch, true)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
let exportRegex = /export\s*{\s*\w,\s*\w\s*}\s*from\s*'bla';/;
expect(
result.filter((value) => exportRegex.test(value.toString())).length,
).equal(1);
});
});
describe('in case of conflicts of the export name', () => {
const base = `export { a as b } from 'c';`,
patch = `export { a as d } from 'c';`;
xit('should prefer the base by default.', () => {
/**
* currently not supported
*/
const result: String[] = merge(base, patch, false)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
expect(result.length).to.be.equal(1);
let exportRegex = new RegExp('.*{ a as b }.*');
expect(exportRegex.test(result[0].toString())).to.be.true;
});
xit('should prefer the patch if patchOverride==true.', () => {
/**
* currently not supported
*/
const result: String[] = merge(base, patch, true)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
expect(result.length).to.be.equal(1);
let exportRegex = new RegExp('.*{ a as d }.*');
expect(exportRegex.test(result[0].toString())).to.be.true;
});
});
describe('in case of conflicts of exports', () => {
const base = `export { a as b } from 'c';`,
patch = `export { d as b } from 'c';`;
xit('should prefer the base by default.', () => {
/**
* currently not supported
*/
const result: String[] = merge(base, patch, false)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
expect(result.length).to.be.equal(1);
let exportRegex = new RegExp('.*{ a as b }.*');
expect(exportRegex.test(result[0].toString())).to.be.true;
});
xit('should prefer the patch if patchOverride==true.', () => {
/**
* currently not supported
*/
const result: String[] = merge(base, patch, true)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
expect(result.length).to.be.equal(1);
let exportRegex = new RegExp('.*{ d as b }.*');
expect(exportRegex.test(result[0].toString())).to.be.true;
});
});
describe('check correct syntax after merge', () => {
const base = `export const test: { b: boolean; } = { b: false };`,
patch = `export const test = { b: true, c: false };`;
it('should merge with proper syntax.', () => {
const result: String[] = merge(base, patch, false)
.split('\n')
.filter((r) => {
return r.trim() != '';
});
expect(
result.filter((value) => value == "export const test: { b: boolean } = {").length,
).to.equal(1);
});
});
});