-
Notifications
You must be signed in to change notification settings - Fork 678
/
converter.test.ts
207 lines (173 loc) · 7.17 KB
/
converter.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
import { Application, resetReflectionID, normalizePath, ProjectReflection } from '..';
import * as FS from 'fs';
import * as Path from 'path';
import Assert = require('assert');
function compareReflections(fixture, spec, path?: string) {
path = (path ? path + '/' : '') + spec.name;
Assert.deepEqual(fixture, spec);
for (let key in spec) {
if (!spec.hasOwnProperty(key)) {
continue;
}
Assert(fixture.hasOwnProperty(key), path + ': Missing property "' + key + '"');
}
for (let key in fixture) {
if (!fixture.hasOwnProperty(key) || typeof fixture[key] === 'undefined') {
continue;
}
Assert(spec.hasOwnProperty(key), path + ': Unknown property "' + key + '"');
const a = fixture[key];
const b = spec[key];
Assert(a instanceof Object === b instanceof Object, path + ': Property "' + key + '" type mismatch');
if (a instanceof Object) {
switch (key) {
case 'signatures':
case 'typeParameters':
case 'children':
compareChildren(a, b, path);
break;
case 'indexSignature':
case 'getSignature':
case 'setSignature':
compareReflections(a, b, path);
break;
default:
Assert.deepEqual(a, b, path + ': Property "' + key + '" value mismatch');
}
} else {
Assert(a === b, path + ': Property "' + key + '" value mismatch');
}
}
}
function compareChildren(fixture, spec, path) {
const a = fixture.map(function(child) { return child.id; });
const b = spec.map(function(child) { return child.id; });
Assert(a.length === b.length, path + ': Number of children differs');
Assert(a.every(function(u, i) { return u === b[i]; }), path + ': Children are different');
fixture.forEach(function(a, index) {
compareReflections(a, spec[index], path);
});
}
describe('Converter', function() {
const base = Path.join(__dirname, 'converter');
let app: Application;
before('constructs', function() {
app = new Application({
mode: 'Modules',
logger: 'none',
target: 'ES5',
module: 'CommonJS',
experimentalDecorators: true,
jsx: 'react'
});
});
FS.readdirSync(base).forEach(function (directory) {
const path = Path.join(base, directory);
if (!FS.lstatSync(path).isDirectory()) {
return;
}
describe(directory, function() {
let result: ProjectReflection | undefined;
it('converts fixtures', function() {
resetReflectionID();
result = app.convert(app.expandInputFiles([path]));
Assert(result instanceof ProjectReflection, 'No reflection returned');
});
it('matches specs', function() {
const specs = JSON.parse(FS.readFileSync(Path.join(path, 'specs.json')).toString());
let data = JSON.stringify(app.serializer.toObject(result), null, ' ');
data = data.split(normalizePath(base)).join('%BASE%');
compareReflections(JSON.parse(data), specs);
});
});
});
});
describe('Converter with categorizeByGroup=false', function() {
const base = Path.join(__dirname, 'converter');
const categoryDir = Path.join(base, 'category');
const classDir = Path.join(base, 'class');
let app: Application;
before('constructs', function() {
app = new Application({
mode: 'Modules',
logger: 'none',
target: 'ES5',
module: 'CommonJS',
experimentalDecorators: true,
categorizeByGroup: false,
jsx: 'react'
});
});
let result: ProjectReflection | undefined;
describe('category', () => {
it('converts fixtures', function() {
resetReflectionID();
result = app.convert(app.expandInputFiles([categoryDir]));
Assert(result instanceof ProjectReflection, 'No reflection returned');
});
it('matches specs', function() {
const specs = JSON.parse(FS.readFileSync(Path.join(categoryDir, 'specs-with-lump-categories.json')).toString());
let data = JSON.stringify(result!.toObject(), null, ' ');
data = data.split(normalizePath(base)).join('%BASE%');
compareReflections(JSON.parse(data), specs);
});
});
// verify that no categories are used when not specified during lump categorization
describe('class', () => {
it('converts fixtures', function() {
resetReflectionID();
result = app.convert(app.expandInputFiles([classDir]));
Assert(result instanceof ProjectReflection, 'No reflection returned');
});
it('matches specs', function() {
const specs = JSON.parse(FS.readFileSync(Path.join(classDir, 'specs.json')).toString());
let data = JSON.stringify(result!.toObject(), null, ' ');
data = data.split(normalizePath(base)).join('%BASE%');
compareReflections(JSON.parse(data), specs);
});
});
});
describe('Converter with excludeNotExported=true', function() {
const base = Path.join(__dirname, 'converter');
const exportWithLocalDir = Path.join(base, 'export-with-local');
const classDir = Path.join(base, 'class');
let app: Application;
before('constructs', function() {
app = new Application({
mode: 'Modules',
logger: 'none',
target: 'ES5',
module: 'CommonJS',
experimentalDecorators: true,
excludeNotExported: true,
jsx: 'react'
});
});
let result: ProjectReflection | undefined;
describe('export-with-local', () => {
it('converts fixtures', function() {
resetReflectionID();
result = app.convert(app.expandInputFiles([exportWithLocalDir]));
Assert(result instanceof ProjectReflection, 'No reflection returned');
});
it('matches specs', function() {
const specs = JSON.parse(FS.readFileSync(Path.join(exportWithLocalDir, 'specs-without-exported.json')).toString());
let data = JSON.stringify(result!.toObject(), null, ' ');
data = data.split(normalizePath(base)).join('%BASE%');
compareReflections(JSON.parse(data), specs);
});
});
describe('class', () => {
it('converts fixtures', function() {
resetReflectionID();
result = app.convert(app.expandInputFiles([classDir]));
Assert(result instanceof ProjectReflection, 'No reflection returned');
});
it('matches specs', function() {
const specs = JSON.parse(FS.readFileSync(Path.join(classDir, 'specs-without-exported.json')).toString());
let data = JSON.stringify(result!.toObject(), null, ' ');
data = data.split(normalizePath(base)).join('%BASE%');
compareReflections(JSON.parse(data), specs);
});
});
});