-
Notifications
You must be signed in to change notification settings - Fork 0
/
trait.test.ts
321 lines (285 loc) · 10.1 KB
/
trait.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
// tslint:disable:no-shadowed-variable
import { Ctor, Trait, traitSelector, Use } from './trait';
const GETIMAGETRAIT_GETIMAGE = 'GetImageTrait.getImage' as const;
const COMPUTETRAIT_COMPUTE = 'ComputeTrait.compute' as const;
const COMPUTETRAIT_GETIMAGE = 'ComputeTrait.getImage' as const;
// Setup
class GetImageTrait extends Trait {
public static INSTANCE = 'foo';
public static S1 = 'Static1';
public static S2 = 'Static2';
public getImage(p1?: string, p2 = true) {
return GETIMAGETRAIT_GETIMAGE;
}
public fn1() {}
}
class ComputeTrait extends Trait {
public static INSTANCE = 'bar';
public compute() {
return COMPUTETRAIT_COMPUTE;
}
public getImage() {
return COMPUTETRAIT_GETIMAGE;
}
}
declare module './trait' {
interface TraitsRegister {
GetImageTrait: typeof GetImageTrait;
ComputeTrait: typeof ComputeTrait;
}
}
it('should works with an annotation', () => {
@Use(GetImageTrait)
class Controller {
public that = (this as unknown) as Controller & GetImageTrait;
public foo() {
return this.that.getImage();
}
}
const controller = new Controller();
expect(controller.foo()).toBe(GETIMAGETRAIT_GETIMAGE);
});
it('should works with an abstract class', () => {
abstract class AbstractController {
public that = (this as unknown) as AbstractController & GetImageTrait;
public foo() {
return this.that.getImage();
}
public abstract bar(): string;
}
class Controller extends Use(GetImageTrait)(AbstractController as Ctor<AbstractController>) {
public bar() {
return this.getImage();
}
}
const controller = new Controller();
expect(controller.foo()).toBe(GETIMAGETRAIT_GETIMAGE);
expect(controller.bar()).toBe(GETIMAGETRAIT_GETIMAGE);
});
it('should works as function decorator', () => {
const Controller = Use(
GetImageTrait,
ComputeTrait,
)(
class Controller {
public foo(this: Controller & GetImageTrait) {
return this.getImage();
}
},
);
const controller = new Controller();
expect(controller.foo()).toBe(GETIMAGETRAIT_GETIMAGE);
});
it('should not override class members', () => {
@Use(GetImageTrait)
class Controller {
public getImage() {
return 'foo';
}
}
expect(new Controller().getImage()).toBe('foo');
});
describe('traitSelector()', () => {
it('should produce a valid token', () => {
expect(traitSelector(GetImageTrait, 'getImage')).toBe('GetImageTrait.getImage');
expect(traitSelector(GetImageTrait, 'INSTANCE', true)).toBe('GetImageTrait::INSTANCE');
});
it('should throw if selected prop is not found', () => {
expect(() => traitSelector(GetImageTrait, 'aaaaaa' as any)).toThrow(/^Error on TraitSelector/);
expect(() => traitSelector(GetImageTrait, 'aaaaaa' as any, true)).toThrow(/^Error on TraitSelector/);
});
});
describe('Trait rules', () => {
it('should alias a property with another', () => {
@Use(GetImageTrait, ComputeTrait, {
as: {
[traitSelector(GetImageTrait, 'S1', true)]: 'protected FOO',
'GetImageTrait::S2': 'S3',
},
})
class Controller {}
expect((Controller as any).S1).toBeUndefined();
expect((Controller as any).FOO).toBe('Static1');
expect((Controller as any).S2).toBeUndefined();
expect((Controller as any).S3).toBe('Static2');
});
it('should throws when selectors are malformed', () => {
expect(
Use(GetImageTrait, {
as: {
'GetImageTrait getImage': 'foo',
},
}),
).toThrow(/^The selector "GetImageTrait getImage" is malformed/);
});
it('should throws when property is not found', () => {
expect(
Use(GetImageTrait, {
as: {
'GetImageTrait.foo': 'bar',
},
}),
).toThrow(
'In selector "GetImageTrait.foo", the property or method "foo" was not found on the trait "GetImageTrait" prototype.',
);
expect(
Use(GetImageTrait, {
as: {
'GetImageTrait::foo': 'bar',
},
}),
).toThrow(
'In selector "GetImageTrait::foo", the static property or method "foo" was not found on the trait "GetImageTrait".',
);
});
describe('AS', () => {
it('should make the initial prop undefined', () => {
@Use(GetImageTrait, {
as: {
'GetImageTrait.getImage': 'foo',
},
})
class Controller {}
expect((new Controller() as any).getImage).toBeUndefined();
});
it('should work with no scope', () => {
@Use(GetImageTrait, {
as: {
'GetImageTrait.getImage': 'foo',
},
})
class Controller {}
expect((new Controller() as any).foo()).toBe(GETIMAGETRAIT_GETIMAGE);
});
it('should work with only scope', () => {
@Use(GetImageTrait, {
as: {
'GetImageTrait.getImage': 'public',
},
})
class Controller {}
expect((new Controller() as any).getImage()).toBe(GETIMAGETRAIT_GETIMAGE);
});
it('should throws if scope is not known', () => {
expect(
Use(GetImageTrait, {
as: {
'GetImageTrait.getImage': 'synchronized foo',
},
}),
).toThrow(
'In the "as"-part "synchronized foo", the scope have to be one of these: public, protected, private',
);
});
it('should throws if the property name is not good for JS', () => {
expect(
Use(GetImageTrait, {
as: {
'GetImageTrait.getImage': '0foo',
},
}),
).toThrow(/^In the "as"-part "0foo", the name "0foo" is not a valid property/);
});
it('should throws if we add more than a scope and a name', () => {
expect(
Use(GetImageTrait, {
as: {
'GetImageTrait.getImage': 'public static foo',
},
}),
).toThrow(/^The "as"-part "public static foo" is not valid/);
});
// -----------
// After parse
// -----------
// Can't overlap trait static method
it('should throws if a collision with an existing prop is found', () => {
expect(
Use(GetImageTrait, {
as: {
'GetImageTrait::INSTANCE': 'S1',
},
}),
).toThrow(
'Collision on "as" trait rule. "GetImageTrait.S1" (static) already exists and can\'t be overlap.',
);
// Can't overlap trait instance method
expect(() =>
Use(GetImageTrait, {
as: {
'GetImageTrait.getImage': 'fn1',
},
})(class Controller {}),
).toThrow('Collision on "as" trait rule. "GetImageTrait.fn1" already exists and can\'t be overlap.');
// Can't overlap traited class static method
expect(() =>
Use(GetImageTrait, {
as: {
'GetImageTrait::INSTANCE': 'FOO',
},
})(
class Controller {
public static FOO = 'FOO';
},
),
).toThrow('Collision on "as" trait rule. "Controller.FOO" (static) already exists and can\'t be overlap.');
// Can't overlap traited class instance method
expect(() =>
Use(GetImageTrait, {
as: {
'GetImageTrait.getImage': 'foo',
},
})(
class Controller {
public foo() {}
},
),
).toThrow('Collision on "as" trait rule. "Controller.foo" already exists and can\'t be overlap.');
});
it('should works with an empty config', () => {
@Use(GetImageTrait, {
as: {},
})
class Controller {}
expect((new Controller() as any).getImage()).toBe(GETIMAGETRAIT_GETIMAGE);
});
it('should throws with an weird config', () => {
expect(
Use(GetImageTrait, {
as: {
'GetImageTrait.getImage': null as any,
},
}),
).toThrow();
});
});
describe('INSTEAD OF', () => {
it('should replace a trait method by another from another trait', () => {
@Use(GetImageTrait, ComputeTrait, {
insteadOf: {
'ComputeTrait.getImage': [GetImageTrait],
'ComputeTrait::INSTANCE': [GetImageTrait],
},
})
class Controller {}
expect((new Controller() as any).getImage()).toBe(COMPUTETRAIT_GETIMAGE);
expect((Controller as any).INSTANCE).toBe('bar');
});
it('should works with an empty config', () => {
@Use(GetImageTrait, {
insteadOf: {},
})
class Controller {}
expect((new Controller() as any).getImage()).toBe(GETIMAGETRAIT_GETIMAGE);
});
it('should throws with an weird config', () => {
expect(
Use(GetImageTrait, {
insteadOf: {
'GetImageTrait.getImage': null as any,
},
}),
).toThrow();
});
});
});