@alterior/annotations 3.4.0 → 3.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,711 +0,0 @@
1
- /// <reference types="reflect-metadata" />
2
- import { __decorate, __metadata, __param } from "tslib";
3
- import { Annotation, METHOD_PARAMETER_ANNOTATIONS_KEY, ANNOTATIONS_KEY, PROPERTY_ANNOTATIONS_KEY, CONSTRUCTOR_PARAMETERS_ANNOTATIONS_KEY, AnnotationTargetError, Annotations, MetadataName } from './annotations';
4
- import { expect, assert } from 'chai';
5
- import { suite } from 'razmin';
6
- const META_NAME = 'altTests:Label';
7
- const LABEL = 'this is the label';
8
- const REGULAR_PROPERTY_VALUE = 123;
9
- let LabelAnnotation = class LabelAnnotation extends Annotation {
10
- constructor(text) {
11
- super();
12
- this.text = text;
13
- this.regularProperty = REGULAR_PROPERTY_VALUE;
14
- }
15
- };
16
- LabelAnnotation = __decorate([
17
- MetadataName(META_NAME),
18
- __metadata("design:paramtypes", [String])
19
- ], LabelAnnotation);
20
- const Label = LabelAnnotation.decorator();
21
- function assertClone(annotation, clone) {
22
- assert(annotation !== clone);
23
- assert(Object.getPrototypeOf(annotation) === Object.getPrototypeOf(clone));
24
- assert(clone.regularProperty === annotation.regularProperty);
25
- assert(clone.text === annotation.text);
26
- }
27
- suite(describe => {
28
- describe('Annotations', it => {
29
- it("should be able to list class annotations", () => {
30
- let MultiLabel = LabelAnnotation.decorator({ allowMultiple: true });
31
- let TestSubject = class TestSubject {
32
- };
33
- TestSubject = __decorate([
34
- MultiLabel(LABEL),
35
- MultiLabel(`foobar`)
36
- ], TestSubject);
37
- class EmptySubject {
38
- }
39
- assert(Annotations.getClassAnnotations(EmptySubject));
40
- let annotations = Annotations.getClassAnnotations(TestSubject);
41
- let matchingAnnotations = annotations.filter(x => x.$metadataName == META_NAME);
42
- assert(matchingAnnotations.length === 2);
43
- assert(matchingAnnotations.find((x) => x.text === 'foobar'));
44
- assert(matchingAnnotations.find((x) => x.text === LABEL));
45
- });
46
- it("should be able to list both subclass and superclass annotations", () => {
47
- let MultiLabel = LabelAnnotation.decorator({ allowMultiple: true });
48
- let TestSubject = class TestSubject {
49
- };
50
- TestSubject = __decorate([
51
- MultiLabel(LABEL),
52
- MultiLabel(`foobar`)
53
- ], TestSubject);
54
- let SubTestSubject = class SubTestSubject extends TestSubject {
55
- };
56
- SubTestSubject = __decorate([
57
- MultiLabel(`thing2`)
58
- ], SubTestSubject);
59
- let annotations = Annotations.getClassAnnotations(SubTestSubject);
60
- let matchingAnnotations = annotations.filter(x => x.$metadataName == META_NAME);
61
- assert(matchingAnnotations.length === 3);
62
- assert(matchingAnnotations.find((x) => x.text === 'foobar'));
63
- assert(matchingAnnotations.find((x) => x.text === LABEL));
64
- assert(matchingAnnotations.find((x) => x.text === 'thing2'));
65
- });
66
- it("should not list subclass annotations on superclasses", () => {
67
- let MultiLabel = LabelAnnotation.decorator({ allowMultiple: true });
68
- let TestSubject = class TestSubject {
69
- };
70
- TestSubject = __decorate([
71
- MultiLabel(LABEL),
72
- MultiLabel(`foobar`)
73
- ], TestSubject);
74
- let SubTestSubject = class SubTestSubject extends TestSubject {
75
- };
76
- SubTestSubject = __decorate([
77
- MultiLabel(`thing2`)
78
- ], SubTestSubject);
79
- let annotations = Annotations.getClassAnnotations(TestSubject);
80
- let matchingAnnotations = annotations.filter(x => x.$metadataName == META_NAME);
81
- assert(matchingAnnotations.length === 2);
82
- assert(matchingAnnotations.find((x) => x.text === 'foobar'));
83
- assert(matchingAnnotations.find((x) => x.text === LABEL));
84
- });
85
- it("should pass an instanceof check", () => {
86
- let MultiLabel = LabelAnnotation.decorator({ allowMultiple: true });
87
- let TestSubject = class TestSubject {
88
- };
89
- TestSubject = __decorate([
90
- MultiLabel(LABEL)
91
- ], TestSubject);
92
- let annotations = Annotations.getClassAnnotations(TestSubject);
93
- expect(annotations.filter(x => x instanceof LabelAnnotation).length).to.equal(1);
94
- let annotation = annotations.find(x => x instanceof LabelAnnotation);
95
- expect(annotation.text).to.equal(LABEL);
96
- });
97
- it("should be able to list method annotations", () => {
98
- let MultiLabel = LabelAnnotation.decorator({ allowMultiple: true });
99
- class TestSubject {
100
- helloWorld() {
101
- }
102
- }
103
- __decorate([
104
- MultiLabel(LABEL),
105
- MultiLabel(`foobar`),
106
- __metadata("design:type", Function),
107
- __metadata("design:paramtypes", []),
108
- __metadata("design:returntype", void 0)
109
- ], TestSubject.prototype, "helloWorld", null);
110
- class EmptySubject {
111
- helloWorld() { }
112
- }
113
- expect(Annotations.getMethodAnnotations(EmptySubject, 'helloWorld')).to.exist;
114
- let annotations = Annotations.getMethodAnnotations(TestSubject, 'helloWorld');
115
- let matchingAnnotations = annotations.filter(x => x.$metadataName == META_NAME);
116
- expect(matchingAnnotations.length).to.equal(2);
117
- expect(matchingAnnotations.find((x) => x.text === 'foobar')).to.exist;
118
- expect(matchingAnnotations.find((x) => x.text === LABEL)).to.exist;
119
- });
120
- it("should be able to list both subclass and superclass method annotations", () => {
121
- let MultiLabel = LabelAnnotation.decorator({ allowMultiple: true });
122
- class TestSubject {
123
- stuff() {
124
- return 123;
125
- }
126
- }
127
- __decorate([
128
- MultiLabel(LABEL),
129
- MultiLabel(`foobar`),
130
- __metadata("design:type", Function),
131
- __metadata("design:paramtypes", []),
132
- __metadata("design:returntype", Number)
133
- ], TestSubject.prototype, "stuff", null);
134
- class SubTestSubject extends TestSubject {
135
- stuff() {
136
- return 321;
137
- }
138
- }
139
- __decorate([
140
- MultiLabel(`thing2`),
141
- __metadata("design:type", Function),
142
- __metadata("design:paramtypes", []),
143
- __metadata("design:returntype", Number)
144
- ], SubTestSubject.prototype, "stuff", null);
145
- let annotations = Annotations.getMethodAnnotations(SubTestSubject, 'stuff');
146
- let matchingAnnotations = annotations.filter(x => x.$metadataName == META_NAME);
147
- expect(matchingAnnotations.length).to.equal(3);
148
- expect(matchingAnnotations.find((x) => x.text === 'foobar')).to.exist;
149
- expect(matchingAnnotations.find((x) => x.text === LABEL)).to.exist;
150
- expect(matchingAnnotations.find((x) => x.text === 'thing2')).to.exist;
151
- });
152
- it("should not list subclass method annotations on the superclass", () => {
153
- let MultiLabel = LabelAnnotation.decorator({ allowMultiple: true });
154
- class TestSubject {
155
- stuff() {
156
- return 123;
157
- }
158
- }
159
- __decorate([
160
- MultiLabel(LABEL),
161
- MultiLabel(`foobar`),
162
- __metadata("design:type", Function),
163
- __metadata("design:paramtypes", []),
164
- __metadata("design:returntype", void 0)
165
- ], TestSubject.prototype, "stuff", null);
166
- class SubTestSubject extends TestSubject {
167
- stuff() {
168
- return 321;
169
- }
170
- }
171
- __decorate([
172
- MultiLabel(`thing2`),
173
- __metadata("design:type", Function),
174
- __metadata("design:paramtypes", []),
175
- __metadata("design:returntype", void 0)
176
- ], SubTestSubject.prototype, "stuff", null);
177
- let annotations = Annotations.getMethodAnnotations(TestSubject, 'stuff');
178
- let matchingAnnotations = annotations.filter(x => x.$metadataName == META_NAME);
179
- expect(matchingAnnotations.length).to.equal(2);
180
- expect(matchingAnnotations.find((x) => x.text === 'foobar')).to.exist;
181
- expect(matchingAnnotations.find((x) => x.text === LABEL)).to.exist;
182
- });
183
- it("should be able to list property annotations", () => {
184
- let MultiLabel = LabelAnnotation.decorator({ allowMultiple: true });
185
- class TestSubject {
186
- constructor() {
187
- this.stuff = 123;
188
- }
189
- }
190
- __decorate([
191
- MultiLabel(LABEL),
192
- MultiLabel(`foobar`),
193
- __metadata("design:type", Number)
194
- ], TestSubject.prototype, "stuff", void 0);
195
- class EmptySubject {
196
- constructor() {
197
- this.stuff = 123;
198
- }
199
- }
200
- expect(Annotations.getMethodAnnotations(EmptySubject, 'stuff')).to.exist;
201
- let annotations = Annotations.getMethodAnnotations(TestSubject, 'stuff');
202
- let matchingAnnotations = annotations.filter(x => x.$metadataName == META_NAME);
203
- expect(matchingAnnotations.length).to.equal(2);
204
- expect(matchingAnnotations.find((x) => x.text === 'foobar')).to.exist;
205
- expect(matchingAnnotations.find((x) => x.text === LABEL)).to.exist;
206
- });
207
- it("should be able to list both subclass and superclass property annotations", () => {
208
- let MultiLabel = LabelAnnotation.decorator({ allowMultiple: true });
209
- class TestSubject {
210
- constructor() {
211
- this.stuff = 123;
212
- }
213
- }
214
- __decorate([
215
- MultiLabel(LABEL),
216
- MultiLabel(`foobar`),
217
- __metadata("design:type", Number)
218
- ], TestSubject.prototype, "stuff", void 0);
219
- class SubTestSubject extends TestSubject {
220
- constructor() {
221
- super(...arguments);
222
- this.stuff = 321;
223
- }
224
- }
225
- __decorate([
226
- MultiLabel(`thing2`),
227
- __metadata("design:type", Number)
228
- ], SubTestSubject.prototype, "stuff", void 0);
229
- let annotations = Annotations.getPropertyAnnotations(SubTestSubject, 'stuff');
230
- let matchingAnnotations = annotations.filter(x => x.$metadataName == META_NAME);
231
- expect(matchingAnnotations.length).to.equal(3);
232
- expect(matchingAnnotations.find((x) => x.text === 'foobar')).to.exist;
233
- expect(matchingAnnotations.find((x) => x.text === LABEL)).to.exist;
234
- expect(matchingAnnotations.find((x) => x.text === 'thing2')).to.exist;
235
- });
236
- it("should not list subclass property annotations on the superclass", () => {
237
- let MultiLabel = LabelAnnotation.decorator({ allowMultiple: true });
238
- class TestSubject {
239
- constructor() {
240
- this.stuff = 123;
241
- }
242
- }
243
- __decorate([
244
- MultiLabel(LABEL),
245
- MultiLabel(`foobar`),
246
- __metadata("design:type", Number)
247
- ], TestSubject.prototype, "stuff", void 0);
248
- class SubTestSubject extends TestSubject {
249
- constructor() {
250
- super(...arguments);
251
- this.stuff = 321;
252
- }
253
- }
254
- __decorate([
255
- MultiLabel(`thing2`),
256
- __metadata("design:type", Number)
257
- ], SubTestSubject.prototype, "stuff", void 0);
258
- let annotations = Annotations.getPropertyAnnotations(TestSubject, 'stuff');
259
- let matchingAnnotations = annotations.filter(x => x.$metadataName == META_NAME);
260
- expect(matchingAnnotations.length).to.equal(2);
261
- expect(matchingAnnotations.find((x) => x.text === 'foobar')).to.exist;
262
- expect(matchingAnnotations.find((x) => x.text === LABEL)).to.exist;
263
- });
264
- it("should be able to list method parameter annotations", () => {
265
- class TestSubject {
266
- helloStrings(param1, param2) {
267
- console.log(`hello ${param1}, ${param2}`);
268
- }
269
- }
270
- __decorate([
271
- __param(0, Label(LABEL)),
272
- __param(1, Label(`foobar`)),
273
- __metadata("design:type", Function),
274
- __metadata("design:paramtypes", [String, String]),
275
- __metadata("design:returntype", void 0)
276
- ], TestSubject.prototype, "helloStrings", null);
277
- class EmptySubject {
278
- helloStrings(param1, param2) {
279
- }
280
- }
281
- expect(Annotations.getParameterAnnotations(EmptySubject, 'helloStrings')).to.exist;
282
- let annotations = Annotations.getParameterAnnotations(TestSubject, 'helloStrings');
283
- expect(annotations.length).to.equal(2);
284
- expect(annotations[0].find((x) => x.text === LABEL)).to.exist;
285
- expect(annotations[1].find((x) => x.text === 'foobar')).to.exist;
286
- });
287
- it("should be able to list method parameter annotations on methods named as array methods", () => {
288
- class TestSubject {
289
- splice(param1, param2) {
290
- console.log(`hello ${param1}, ${param2}`);
291
- }
292
- }
293
- __decorate([
294
- __param(0, Label(LABEL)),
295
- __param(1, Label(`foobar`)),
296
- __metadata("design:type", Function),
297
- __metadata("design:paramtypes", [String, String]),
298
- __metadata("design:returntype", void 0)
299
- ], TestSubject.prototype, "splice", null);
300
- class EmptySubject {
301
- splice(param1, param2) {
302
- }
303
- }
304
- expect(Annotations.getParameterAnnotations(EmptySubject, 'splice')).to.exist;
305
- let annotations = Annotations.getParameterAnnotations(TestSubject, 'splice');
306
- expect(annotations.length).to.equal(2);
307
- expect(annotations[0].find((x) => x.text === LABEL)).to.exist;
308
- expect(annotations[1].find((x) => x.text === 'foobar')).to.exist;
309
- });
310
- it("should be able to list both subclass and superclass method parameter annotations", () => {
311
- let MultiLabel = LabelAnnotation.decorator({ allowMultiple: true });
312
- class TestSubject {
313
- helloStrings5(param1, param2) {
314
- console.log(`hello ${param1}, ${param2}`);
315
- }
316
- }
317
- __decorate([
318
- __param(0, MultiLabel(LABEL)),
319
- __param(0, MultiLabel('other')),
320
- __param(1, MultiLabel(`foobar`)),
321
- __metadata("design:type", Function),
322
- __metadata("design:paramtypes", [String, String]),
323
- __metadata("design:returntype", void 0)
324
- ], TestSubject.prototype, "helloStrings5", null);
325
- class SubTestSubject extends TestSubject {
326
- helloStrings5(param1, param2, param3) {
327
- }
328
- }
329
- __decorate([
330
- __param(0, MultiLabel(`thing2`)),
331
- __param(2, MultiLabel(`thing3`)),
332
- __metadata("design:type", Function),
333
- __metadata("design:paramtypes", [String, String, String]),
334
- __metadata("design:returntype", void 0)
335
- ], SubTestSubject.prototype, "helloStrings5", null);
336
- let annotations = Annotations.getParameterAnnotations(SubTestSubject, 'helloStrings5');
337
- expect(annotations).to.exist;
338
- expect(annotations.length).to.equal(3);
339
- expect(annotations[0].length).to.equal(3);
340
- expect(annotations[0].find((x) => x.text === LABEL)).to.exist;
341
- expect(annotations[0].find((x) => x.text === 'other')).to.exist;
342
- expect(annotations[0].find((x) => x.text === 'thing2')).to.exist;
343
- expect(annotations[1].length).to.equal(1);
344
- expect(annotations[1].find((x) => x.text === 'foobar')).to.exist;
345
- expect(annotations[2].length).to.equal(1);
346
- expect(annotations[2].find((x) => x.text === 'thing3')).to.exist;
347
- });
348
- it("should not list subclass method parameter annotations on the superclass", () => {
349
- let MultiLabel = LabelAnnotation.decorator({ allowMultiple: true });
350
- class TestSubject {
351
- helloStrings2(param1, param2) {
352
- console.log(`hello ${param1}, ${param2}`);
353
- }
354
- }
355
- __decorate([
356
- __param(0, MultiLabel(LABEL)),
357
- __param(1, MultiLabel(`foobar`)),
358
- __metadata("design:type", Function),
359
- __metadata("design:paramtypes", [String, String]),
360
- __metadata("design:returntype", void 0)
361
- ], TestSubject.prototype, "helloStrings2", null);
362
- class EmptySubject {
363
- helloStrings2(param1, param2, param3) {
364
- }
365
- }
366
- __decorate([
367
- __param(0, MultiLabel(`thing2`)),
368
- __param(2, MultiLabel(`thing3`)),
369
- __metadata("design:type", Function),
370
- __metadata("design:paramtypes", [String, String, String]),
371
- __metadata("design:returntype", void 0)
372
- ], EmptySubject.prototype, "helloStrings2", null);
373
- expect(Annotations.getParameterAnnotations(TestSubject, 'helloStrings2')).to.exist;
374
- let annotations = Annotations.getParameterAnnotations(TestSubject, 'helloStrings2');
375
- expect(annotations.length).to.equal(2);
376
- expect(annotations[0].length).to.equal(1);
377
- expect(annotations[0].find((x) => x.text === LABEL)).to.exist;
378
- expect(annotations[1].length).to.equal(1);
379
- expect(annotations[1].find((x) => x.text === 'foobar')).to.exist;
380
- });
381
- it("should be able to list constructor parameter annotations", () => {
382
- let TestSubject = class TestSubject {
383
- constructor(param1, param2) {
384
- }
385
- };
386
- TestSubject = __decorate([
387
- __param(0, Label(LABEL)),
388
- __param(1, Label(`foobar`)),
389
- __metadata("design:paramtypes", [String, String])
390
- ], TestSubject);
391
- class EmptySubject {
392
- constructor(param1, param2) {
393
- }
394
- }
395
- expect(Annotations.getConstructorParameterAnnotations(EmptySubject)).to.exist;
396
- let annotations = Annotations.getConstructorParameterAnnotations(TestSubject);
397
- expect(annotations.length).to.equal(2);
398
- expect(annotations[0].find((x) => x.text === LABEL)).to.exist;
399
- expect(annotations[1].find((x) => x.text === 'foobar')).to.exist;
400
- });
401
- it("should be able to clone an IAnnotation", () => {
402
- let annotation1 = new LabelAnnotation(LABEL);
403
- let annotation2 = Annotations.clone(annotation1);
404
- assertClone(annotation1, annotation2);
405
- });
406
- });
407
- describe('Annotation', it => {
408
- it("should be cloneable", () => {
409
- let annotation1 = new LabelAnnotation(LABEL);
410
- let annotation2 = annotation1.clone();
411
- assertClone(annotation1, annotation2);
412
- });
413
- it.skip("should not complain when an ApplyOnce decorator is applied to both superclass and subclass", () => {
414
- let Superclass = class Superclass {
415
- };
416
- Superclass = __decorate([
417
- Label('foo')
418
- ], Superclass);
419
- let Subclass = class Subclass {
420
- };
421
- Subclass = __decorate([
422
- Label('foo2')
423
- ], Subclass);
424
- let annotations = Annotations.getClassAnnotations(Subclass);
425
- expect(annotations.length).to.equal(1); // ???
426
- });
427
- it("should apply to classes", () => {
428
- class TestSubject {
429
- }
430
- let annotation = new LabelAnnotation(LABEL);
431
- annotation.applyToClass(TestSubject);
432
- expect(TestSubject[ANNOTATIONS_KEY]).to.exist;
433
- expect(TestSubject[ANNOTATIONS_KEY].length > 0).to.exist;
434
- let installedAnnotations = TestSubject[ANNOTATIONS_KEY]
435
- .filter(x => x.$metadataName == META_NAME);
436
- expect(installedAnnotations.length).to.equal(1);
437
- expect(installedAnnotations[0]).to.be.ok;
438
- let installedAnnotation = installedAnnotations[0];
439
- let annotationP = installedAnnotation;
440
- expect(annotationP.$metadataName).to.equal(META_NAME);
441
- expect(annotationP.constructor).to.equal(LabelAnnotation);
442
- expect(annotationP.regularProperty).to.equal(REGULAR_PROPERTY_VALUE);
443
- assertClone(annotation, annotationP);
444
- });
445
- it("should apply to methods", () => {
446
- class TestSubject {
447
- helloWorld() {
448
- console.log('hello world');
449
- }
450
- }
451
- let annotation = new LabelAnnotation(LABEL);
452
- annotation.applyToMethod(TestSubject, 'helloWorld');
453
- expect(TestSubject[PROPERTY_ANNOTATIONS_KEY]).to.exist;
454
- expect(TestSubject[PROPERTY_ANNOTATIONS_KEY]['helloWorld']).to.exist;
455
- let installedAnnotations = TestSubject[PROPERTY_ANNOTATIONS_KEY]['helloWorld']
456
- .filter(x => x.$metadataName == META_NAME);
457
- expect(installedAnnotations.length).to.equal(1);
458
- expect(installedAnnotations[0]).to.be.ok;
459
- let installedAnnotation = installedAnnotations[0];
460
- let annotationP = installedAnnotation;
461
- expect(annotationP.$metadataName).to.equal(META_NAME);
462
- expect(annotationP.constructor).to.equal(LabelAnnotation);
463
- expect(annotationP.regularProperty).to.equal(REGULAR_PROPERTY_VALUE);
464
- assertClone(annotation, annotationP);
465
- });
466
- it("should apply to properties", () => {
467
- class TestSubject {
468
- helloWorld() {
469
- console.log('hello world');
470
- }
471
- }
472
- let annotation = new LabelAnnotation(LABEL);
473
- annotation.applyToMethod(TestSubject, 'helloWorld');
474
- expect(TestSubject[PROPERTY_ANNOTATIONS_KEY]).to.exist;
475
- expect(TestSubject[PROPERTY_ANNOTATIONS_KEY]['helloWorld']).to.exist;
476
- let installedAnnotations = TestSubject[PROPERTY_ANNOTATIONS_KEY]['helloWorld']
477
- .filter(x => x.$metadataName == META_NAME);
478
- expect(installedAnnotations.length).to.equal(1);
479
- expect(installedAnnotations[0]).to.be.ok;
480
- let installedAnnotation = installedAnnotations[0];
481
- let annotationP = installedAnnotation;
482
- expect(annotationP.$metadataName).to.equal(META_NAME);
483
- expect(annotationP.constructor).to.equal(LabelAnnotation);
484
- expect(annotationP.regularProperty).to.equal(REGULAR_PROPERTY_VALUE);
485
- assertClone(annotation, annotationP);
486
- });
487
- it("should apply to method parameters", () => {
488
- class TestSubject {
489
- helloString(param1) {
490
- console.log(`hello ${param1}`);
491
- }
492
- }
493
- let annotation = new LabelAnnotation(LABEL);
494
- annotation.applyToParameter(TestSubject, 'helloString', 0);
495
- expect(TestSubject[METHOD_PARAMETER_ANNOTATIONS_KEY]).to.exist;
496
- expect(TestSubject[METHOD_PARAMETER_ANNOTATIONS_KEY]['helloString']).to.exist;
497
- let paramAnnotations = TestSubject[METHOD_PARAMETER_ANNOTATIONS_KEY]['helloString'];
498
- expect(paramAnnotations.length)
499
- .to.equal(1, `Should be 1 entry to match 1 parameter on TestSubject.helloString(), `
500
- + `not ${paramAnnotations.length}`);
501
- let installedAnnotations = paramAnnotations[0]
502
- .filter(x => x.$metadataName === META_NAME);
503
- expect(installedAnnotations.length).to.equal(1);
504
- expect(installedAnnotations[0]).to.be.ok;
505
- let installedAnnotation = installedAnnotations[0];
506
- let annotationP = installedAnnotation;
507
- expect(annotationP.$metadataName).to.equal(META_NAME);
508
- expect(annotationP.constructor).to.equal(LabelAnnotation);
509
- expect(annotationP.regularProperty).to.equal(REGULAR_PROPERTY_VALUE);
510
- assertClone(annotation, annotationP);
511
- });
512
- it("should fill missing parameter metadata slots with null", () => {
513
- class TestSubject {
514
- helloStrings3(param1, param2) {
515
- console.log(`hello ${param1}, ${param2}`);
516
- }
517
- }
518
- let annotation = new LabelAnnotation(LABEL);
519
- annotation.applyToParameter(TestSubject, 'helloStrings3', 1);
520
- expect(TestSubject[METHOD_PARAMETER_ANNOTATIONS_KEY]).to.exist;
521
- expect(TestSubject[METHOD_PARAMETER_ANNOTATIONS_KEY]['helloStrings3']).to.exist;
522
- let paramAnnotations = TestSubject[METHOD_PARAMETER_ANNOTATIONS_KEY]['helloStrings3'];
523
- expect(paramAnnotations.length)
524
- .to.equal(2, `Should be 2 entries to match 2 parameters on TestSubject.helloStrings(), `
525
- + `not ${paramAnnotations.length}`);
526
- expect(paramAnnotations[0]).to.be.null;
527
- expect(paramAnnotations[2]).to.be.undefined;
528
- let installedAnnotations = paramAnnotations[1]
529
- .filter(x => x.$metadataName === META_NAME);
530
- expect(installedAnnotations.length).to.equal(1);
531
- expect(installedAnnotations[0]).to.be.ok;
532
- let installedAnnotation = installedAnnotations[0];
533
- let annotationP = installedAnnotation;
534
- expect(annotationP.$metadataName).to.equal(META_NAME);
535
- expect(annotationP.constructor).to.equal(LabelAnnotation);
536
- expect(annotationP.regularProperty).to.equal(REGULAR_PROPERTY_VALUE);
537
- assertClone(annotation, annotationP);
538
- });
539
- it("should apply to constructor parameters", () => {
540
- class TestSubject {
541
- constructor(param1) {
542
- console.log(`hello ${param1}`);
543
- }
544
- }
545
- let annotation = new LabelAnnotation(LABEL);
546
- annotation.applyToConstructorParameter(TestSubject, 0);
547
- expect(TestSubject[CONSTRUCTOR_PARAMETERS_ANNOTATIONS_KEY]).to.exist;
548
- let paramAnnotations = TestSubject[CONSTRUCTOR_PARAMETERS_ANNOTATIONS_KEY];
549
- expect(paramAnnotations.length)
550
- .to.equal(1, `Should be 1 entry to match 1 parameter on TestSubject.helloString(), `
551
- + `not ${paramAnnotations.length}`);
552
- let installedAnnotations = paramAnnotations[0]
553
- .filter(x => x.$metadataName === META_NAME);
554
- expect(installedAnnotations.length).to.equal(1);
555
- expect(installedAnnotations[0]).to.be.ok;
556
- let installedAnnotation = installedAnnotations[0];
557
- let annotationP = installedAnnotation;
558
- expect(annotationP.$metadataName).to.equal(META_NAME);
559
- expect(annotationP.constructor).to.equal(LabelAnnotation);
560
- expect(annotationP.regularProperty).to.equal(REGULAR_PROPERTY_VALUE);
561
- assertClone(annotation, annotationP);
562
- });
563
- it("should fill missing constructor parameter metadata slots with null", () => {
564
- class TestSubject {
565
- constructor(param1, param2) {
566
- console.log(`hello ${param1}, ${param2}`);
567
- }
568
- }
569
- let annotation = new LabelAnnotation(LABEL);
570
- annotation.applyToConstructorParameter(TestSubject, 1);
571
- expect(TestSubject[CONSTRUCTOR_PARAMETERS_ANNOTATIONS_KEY]).to.exist;
572
- let paramAnnotations = TestSubject[CONSTRUCTOR_PARAMETERS_ANNOTATIONS_KEY];
573
- expect(paramAnnotations.length)
574
- .to.equal(2, `Should be 2 entries to match 2 parameters on new TestSubject(param1, param2), `
575
- + `not ${paramAnnotations.length}`);
576
- expect(paramAnnotations[0]).to.be.null;
577
- expect(paramAnnotations[2]).to.be.undefined;
578
- let installedAnnotations = paramAnnotations[1]
579
- .filter(x => x.$metadataName === META_NAME);
580
- expect(installedAnnotations.length).to.equal(1);
581
- expect(installedAnnotations[0]).to.be.ok;
582
- let installedAnnotation = installedAnnotations[0];
583
- let annotationP = installedAnnotation;
584
- expect(annotationP.$metadataName).to.equal(META_NAME);
585
- expect(annotationP.constructor).to.equal(LabelAnnotation);
586
- expect(annotationP.regularProperty).to.equal(REGULAR_PROPERTY_VALUE);
587
- assertClone(annotation, annotationP);
588
- });
589
- it("should be able to construct a viable decorator", () => {
590
- let decorator = LabelAnnotation.decorator();
591
- expect(typeof decorator).to.equal('function');
592
- let annotationDecorator = decorator(LABEL);
593
- let obj = {};
594
- annotationDecorator(obj);
595
- expect(obj[ANNOTATIONS_KEY]).to.exist;
596
- expect(obj[ANNOTATIONS_KEY].filter(x => x.$metadataName === META_NAME).length === 1).to.exist;
597
- expect(obj[ANNOTATIONS_KEY].find(x => x.$metadataName === META_NAME).text === LABEL).to.exist;
598
- });
599
- it("should handle class decorators", () => {
600
- let decorator = LabelAnnotation.decorator();
601
- let TestSubject = class TestSubject {
602
- };
603
- TestSubject = __decorate([
604
- decorator(LABEL)
605
- ], TestSubject);
606
- expect(TestSubject[ANNOTATIONS_KEY]).to.exist;
607
- expect(TestSubject[ANNOTATIONS_KEY].filter(x => x.$metadataName === META_NAME).length === 1).to.exist;
608
- expect(TestSubject[ANNOTATIONS_KEY].find(x => x.$metadataName === META_NAME).text === LABEL).to.exist;
609
- });
610
- it("should handle method decorators", () => {
611
- let decorator = LabelAnnotation.decorator();
612
- class TestSubject {
613
- helloWorld() {
614
- console.log('hello world');
615
- }
616
- }
617
- __decorate([
618
- decorator(LABEL),
619
- __metadata("design:type", Function),
620
- __metadata("design:paramtypes", []),
621
- __metadata("design:returntype", void 0)
622
- ], TestSubject.prototype, "helloWorld", null);
623
- expect(TestSubject.prototype[PROPERTY_ANNOTATIONS_KEY]).to.exist;
624
- expect(TestSubject.prototype[PROPERTY_ANNOTATIONS_KEY]['helloWorld']).to.exist;
625
- expect(TestSubject.prototype[PROPERTY_ANNOTATIONS_KEY]['helloWorld'].filter(x => x.$metadataName === META_NAME).length === 1).to.exist;
626
- expect(TestSubject.prototype[PROPERTY_ANNOTATIONS_KEY]['helloWorld'].find(x => x.$metadataName === META_NAME).text === LABEL).to.exist;
627
- });
628
- it("should handle property decorators", () => {
629
- let decorator = LabelAnnotation.decorator();
630
- class TestSubject {
631
- constructor() {
632
- this.stuff = 123;
633
- }
634
- }
635
- __decorate([
636
- decorator(LABEL),
637
- __metadata("design:type", Number)
638
- ], TestSubject.prototype, "stuff", void 0);
639
- expect(TestSubject.prototype[PROPERTY_ANNOTATIONS_KEY]).to.exist;
640
- expect(TestSubject.prototype[PROPERTY_ANNOTATIONS_KEY]['stuff']).to.exist;
641
- expect(TestSubject.prototype[PROPERTY_ANNOTATIONS_KEY]['stuff'].filter(x => x.$metadataName === META_NAME).length === 1).to.exist;
642
- expect(TestSubject.prototype[PROPERTY_ANNOTATIONS_KEY]['stuff'].find(x => x.$metadataName === META_NAME).text === LABEL).to.exist;
643
- });
644
- it("should handle method parameters decorators", () => {
645
- let decorator = LabelAnnotation.decorator();
646
- class TestSubject {
647
- helloWorld(stuff) {
648
- }
649
- }
650
- __decorate([
651
- __param(0, decorator(LABEL)),
652
- __metadata("design:type", Function),
653
- __metadata("design:paramtypes", [String]),
654
- __metadata("design:returntype", void 0)
655
- ], TestSubject.prototype, "helloWorld", null);
656
- expect(TestSubject.prototype[METHOD_PARAMETER_ANNOTATIONS_KEY]).to.exist;
657
- expect(TestSubject.prototype[METHOD_PARAMETER_ANNOTATIONS_KEY]['helloWorld']).to.exist;
658
- let parameters = TestSubject.prototype[METHOD_PARAMETER_ANNOTATIONS_KEY]['helloWorld'];
659
- expect(parameters.length).to.equal(1);
660
- let paramAnnotations = parameters[0];
661
- expect(paramAnnotations.filter(x => x.$metadataName === META_NAME).length).to.equal(1);
662
- expect(paramAnnotations.find(x => x.$metadataName === META_NAME).text).to.equal(LABEL);
663
- });
664
- it("should handle constructor parameters decorators", () => {
665
- let decorator = LabelAnnotation.decorator();
666
- let TestSubject = class TestSubject {
667
- constructor(stuff) {
668
- }
669
- };
670
- TestSubject = __decorate([
671
- __param(0, decorator(LABEL)),
672
- __metadata("design:paramtypes", [String])
673
- ], TestSubject);
674
- expect(TestSubject[CONSTRUCTOR_PARAMETERS_ANNOTATIONS_KEY]).to.exist;
675
- let parameters = TestSubject[CONSTRUCTOR_PARAMETERS_ANNOTATIONS_KEY];
676
- expect(parameters.length).to.equal(1);
677
- let paramAnnotations = parameters[0];
678
- expect(paramAnnotations.filter(x => x.$metadataName === META_NAME).length === 1).to.exist;
679
- expect(paramAnnotations.find(x => x.$metadataName === META_NAME).text === LABEL).to.exist;
680
- });
681
- it("should ensure a decorator is applied only to supported targets", () => {
682
- let decorator = LabelAnnotation.decorator({
683
- validTargets: ['class', 'parameter']
684
- });
685
- let TestSubject = class TestSubject {
686
- constructor(stuff) { }
687
- };
688
- TestSubject = __decorate([
689
- decorator(LABEL),
690
- __param(0, decorator(LABEL)),
691
- __metadata("design:paramtypes", [String])
692
- ], TestSubject);
693
- try {
694
- class BrokenSubject {
695
- helloWorld() { }
696
- }
697
- __decorate([
698
- decorator(LABEL),
699
- __metadata("design:type", Function),
700
- __metadata("design:paramtypes", []),
701
- __metadata("design:returntype", void 0)
702
- ], BrokenSubject.prototype, "helloWorld", null);
703
- throw new Error("The decorator allowed usage on 'method' when only 'class' and 'parameter' should be allowed.");
704
- }
705
- catch (e) {
706
- expect(e).to.be.instanceOf(AnnotationTargetError);
707
- }
708
- });
709
- });
710
- });
711
- //# sourceMappingURL=annotations.test.js.map