-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplates.spec.ts
319 lines (284 loc) · 8.89 KB
/
templates.spec.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
import { assert } from 'chai';
import mockFs from 'mock-fs';
import { join } from 'path';
import { v4 as uuid } from 'uuid';
import { Configuration } from '../src/cli/configuration.js';
import { ClassInfo, EdmInfo, EnumInfo, ComplexTypeInfo, PropertyInfo, ComplexTypeInfoSet } from '../src/cli/shared.js';
import { EdmTemplate, EndpointTemplate } from '../src/cli/templates.js';
import { standardEndpoints } from './data.js';
describe('templates', function () {
describe('EndpointTemplate', function () {
it('Renders correctly', async function () {
try {
await Configuration.createFromCLIArgs(['--quoteStyle', 'single']);
const actual = new EndpointTemplate().render(standardEndpoints);
assert.strictEqual(actual,
`/**
* This is a generated file. Please don't change this manually.
*/
export const enum Endpoints {
People = 'People',
Foos = 'Foos',
Bar = 'fizzbazz',
}`
);
} finally {
Configuration.dispose();
}
});
it('Respects the given template', async function () {
try {
await Configuration.createFromCLIArgs(['--quoteStyle', 'single']);
const template = 'export const enum Endpoints {<% for(const endpoint of it.endpoints) { %> <%= endpoint.name %> = "<%= endpoint.url %>",<% } %>}';
const actual = new EndpointTemplate(template).render(standardEndpoints);
assert.strictEqual(actual, 'export const enum Endpoints { People = "People", Foos = "Foos", Bar = "fizzbazz",}');
} finally {
Configuration.dispose();
}
});
});
describe('EdmTemplate', function () {
it('Renders correctly', async function () {
try {
const basePath = join(process.cwd(), uuid());
mockFs({ [basePath]: {} }, { createCwd: true });
const config = await Configuration.createFromCLIArgs(['--outputDir', basePath, '--endpoint', 'https://api.example.com']);
const base1 = new ClassInfo(
'BaseOne',
[
new PropertyInfo('name', 'string', false, false),
],
);
const interface1 = new ComplexTypeInfo(
'ComplexType1',
[
new PropertyInfo('prop11', 'number', false, false),
new PropertyInfo('prop12', 'string', false, false),
],
false,
null
);
const info = new EdmInfo(
'Awesome.Possum',
// [],
[
new ClassInfo(
'Foo',
[
new PropertyInfo('id', 'number', false, true),
new PropertyInfo('name', 'string', false, false),
new PropertyInfo('isActive', 'boolean', false, false),
new PropertyInfo('optional', 'string', true, false),
],
'foos'
),
new ClassInfo(
'Bar',
[
new PropertyInfo('id', 'number', false, true),
new PropertyInfo('boolProp', 'boolean', false, false),
new PropertyInfo('strProp', 'string', false, false),
new PropertyInfo('optionalProp', 'number', true, false),
],
),
base1,
new ClassInfo(
'Child',
[
new PropertyInfo('id', 'number', false, true),
new PropertyInfo('strProp', 'string', false, false),
new PropertyInfo('optionalProp', 'number', true, false),
],
'Children',
base1,
),
],
new ComplexTypeInfoSet(
interface1,
new ComplexTypeInfo(
'ComplexType2',
[
new PropertyInfo('prop21', 'number', false, false),
new PropertyInfo('prop22', 'string', false, false),
new PropertyInfo('prop23', 'boolean', false, false),
],
false,
null
),
new ComplexTypeInfo(
'ChildComplexType',
[
new PropertyInfo('prop31', 'number', false, false),
new PropertyInfo('prop32', 'string', false, false),
new PropertyInfo('prop33', 'boolean', false, false),
],
false,
interface1,
),
),
[
new EnumInfo(
'EnumOne',
['member11', 'member12']
),
new EnumInfo(
'EnumTwo',
['member21', 'member22']
),
],
config.endpoints[0],
);
const actual = new EdmTemplate().render(info);
assert.strictEqual(
actual,
`/**
* This is a generated file. Please don't change this manually.
*/
import {
Class,
odataEndpoint,
odataType,
odataTypeKey,
} from '@netatwork/odata-edm-generator';
import {
Endpoints,
} from '../../Endpoints.js';
@odataEndpoint(Endpoints.foos)
export class Foo {
public static create<TFoo extends Foo = Foo>(this: Class<TFoo>, raw: Partial<TFoo>): TFoo {
if (raw === undefined || raw === null || raw instanceof this) { return raw as TFoo; }
return new this(
raw.id,
raw.name,
raw.isActive,
raw.optional,
);
}
public constructor(
public id: number,
public name: string,
public isActive: boolean,
public optional?: string,
) { }
}
export class Bar {
public static create<TBar extends Bar = Bar>(this: Class<TBar>, raw: Partial<TBar>): TBar {
if (raw === undefined || raw === null || raw instanceof this) { return raw as TBar; }
return new this(
raw.id,
raw.boolProp,
raw.strProp,
raw.optionalProp,
);
}
public constructor(
public id: number,
public boolProp: boolean,
public strProp: string,
public optionalProp?: number,
) { }
}
export class BaseOne {
public static create<TBaseOne extends BaseOne = BaseOne>(this: Class<TBaseOne>, raw: Partial<TBaseOne>): TBaseOne {
if (raw === undefined || raw === null || raw instanceof this) { return raw as TBaseOne; }
return new this(
raw.name,
);
}
public constructor(
public name: string,
) { }
}
@odataEndpoint(Endpoints.Children)
// @ts-ignore needed to avoid this issue: https://github.com/microsoft/TypeScript/issues/4628
export class Child extends BaseOne {
public static create<TChild extends Child = Child>(this: Class<TChild>, raw: Partial<TChild>): TChild {
if (raw === undefined || raw === null || raw instanceof this) { return raw as TChild; }
return new this(
raw.id,
raw.name,
raw.strProp,
raw.optionalProp,
);
}
public constructor(
public id: number,
public name: string,
public strProp: string,
public optionalProp?: number,
) {
super(
name,
);
}
}
export enum $$ComplexType1Types {
ChildComplexType = 'ChildComplexType',
}
export class ComplexType1 {
protected static get derivedTypes(): typeof ComplexType1[] {
return [
ChildComplexType,
] as unknown as typeof ComplexType1[];
}
public static create(raw: Partial<ComplexType1>): ComplexType1 {
if (raw === undefined || raw === null || raw instanceof this) { return raw as ComplexType1; }
const edmType = raw[odataTypeKey];
const ctor = this.derivedTypes.find((f) => f.canHandle(edmType));
if (!ctor) {
return raw as ComplexType1;
}
return ctor.create(raw);
}
protected static canHandle(_odataType: string): boolean { return false; }
public readonly $$type: $$ComplexType1Types;
public constructor(
public prop11: number,
public prop12: string,
) { }
}
export interface ComplexType2 {
prop21: number;
prop22: string;
prop23: boolean;
}
@odataType('#Awesome.Possum.ChildComplexType', $$ComplexType1Types.ChildComplexType, '$$type')
export class ChildComplexType extends ComplexType1 {
public constructor(
public prop11: number,
public prop12: string,
public prop31: number,
public prop32: string,
public prop33: boolean,
) {
super(
prop11,
prop12,
);
}
public static create(raw: Partial<ChildComplexType>): ChildComplexType {
return new ChildComplexType(
raw.prop11,
raw.prop12,
raw.prop31,
raw.prop32,
raw.prop33,
);
}
}
export enum EnumOne {
member11 = 'member11',
member12 = 'member12',
}
export enum EnumTwo {
member21 = 'member21',
member22 = 'member22',
}
` );
} finally {
Configuration.dispose();
mockFs.restore();
}
});
});
});