-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathproperty.test.ts
More file actions
645 lines (584 loc) · 24.8 KB
/
property.test.ts
File metadata and controls
645 lines (584 loc) · 24.8 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
import {expect, use} from '@esm-bundle/chai';
import {chaiAlmost} from './chai/almost.js';
import {
Component,
Type,
Property,
ComponentProperty,
Object3D,
Skin,
Material,
Texture,
Mesh,
inheritProperties,
PropertyKeys,
ComponentConstructor,
LogLevel,
} from '@wonderlandengine/api';
import {property} from '@wonderlandengine/api/decorators.js';
import {init, reset, WL} from './setup.js';
use(chaiAlmost());
before(init);
beforeEach(reset);
/**
* Adding a new property test requires to modify multiple things:
* 1. Add the property type to {@link TestProperties}
* 2. Add the definition in {@link TestComponentProperties.PropertiesDefinition}
* 3. Add the definition as a decorator in {@link TestComponentPropertiesDecorator}
* 3. Add a test for default value in {@link TestComponentProperties.assertDefaults}
* 4. Add a test for missing default value in {@link TestComponentProperties.assertNoDefaults}
*/
/**
* TypeScript can't figure out that the `Properties` entries will get added on the instance.
*
* This interface represents the instantiated properties.
*/
interface TestProperties {
propBool: boolean;
propInt: number;
propFloat: number;
propString: string;
propEnum: number;
propEnumDefaultMissing: number;
propEnumDefaultNumber: number;
propEnumDefaultNumberOutOfRange: number;
propEnumNoValues: number;
propEnumEmptyValues: number;
propObject: Object3D | null;
propMesh: Mesh | null;
propTexture: Texture | null;
propMaterial: Material | null;
propAnimation: Animation | null;
propSkin: Skin | null;
propColor: Float32Array;
propVector2: Float32Array;
propVector3: Float32Array;
propVector4: Float32Array;
}
/**
* Basic component type to check for properties.
*
* Tests will inherit this component to get proper typings.
*/
class TestComponentProperties extends Component {
/**
* Property name to definition map.
*
* This is different than the original {@link Component.Properties} object.
* This allows to automatically test properties created with {@link Property} function.
*/
static PropertiesDefinition = {
propBool: {name: 'Bool', default: true},
propInt: {name: 'Int', default: 12},
propFloat: {name: 'Float', default: 3.75},
propString: {name: 'String', default: 'hello'},
propEnum: {name: 'Enum', default: 'snake', values: ['serpent', 'snake']},
propEnumDefaultMissing: {
name: 'Enum',
default: 'monke',
values: ['serpent', 'snake'],
},
propEnumDefaultNumber: {name: 'Enum', default: 1, values: ['serpent', 'snake']},
propEnumDefaultNumberOutOfRange: {
name: 'Enum',
default: 2,
values: ['serpent', 'snake'],
},
propEnumNoValues: {name: 'Enum', default: 'snake'},
propEnumEmptyValues: {name: 'Enum', values: [], default: 'snake'},
propObject: {name: 'Object', default: null},
propMesh: {name: 'Mesh', default: null},
propTexture: {name: 'Texture', default: null},
propMaterial: {name: 'Material', default: null},
propAnimation: {name: 'Animation', default: null},
propSkin: {name: 'Skin', default: null},
propColor: {name: 'Color', default: [1, 0, 0.5, 0.75]},
propVector2: {name: 'Vector2', default: [1, 2.5]},
propVector3: {name: 'Vector3', default: [2, 2.5, 0.5]},
propVector4: {name: 'Vector4', default: [3, 2.5, 0.5, 12.0]},
};
/**
* Create properties using the literal syntax `{ ... }`.
*
* @param setupDefaults If `true`, add defaults
* @returns The properties to list in {@link Component.Properties}
*/
static createLiteralProperties(
setupDefaults: boolean
): Record<string, ComponentProperty> {
const result = {} as Record<string, ComponentProperty>;
for (const [name, definition] of Object.entries(
TestComponentProperties.PropertiesDefinition
)) {
result[name] = {
type: Type[definition.name as keyof typeof Type],
values: 'values' in definition ? definition['values'] : undefined,
default: undefined,
};
if (setupDefaults) result[name].default = definition.default;
}
return result;
}
/**
* Create properties using the functor syntax `Property.type()`.
*
* @param setupDefaults If `true`, add defaults
* @returns The properties to list in {@link Component.Properties}
*/
static createFunctorProperties(
setupDefaults: boolean
): Record<string, ComponentProperty> {
const result = {} as Record<string, ComponentProperty>;
for (const [name, definition] of Object.entries(
TestComponentProperties.PropertiesDefinition
)) {
const values = 'values' in definition ? definition['values'] : undefined;
const type = definition.name.toLowerCase();
const functor: (...args: any[]) => ComponentProperty =
Property[type as keyof typeof Property];
expect(functor).to.be.instanceOf(Function, `Property '${type}' doesn't exist`);
const defaults = [];
if (setupDefaults) {
defaults.push(
...(Array.isArray(definition.default)
? definition.default
: [definition.default])
);
}
const args = type === 'enum' ? [values, ...defaults] : [...defaults];
result[name] = functor(...args);
}
return result;
}
/**
* Assert that each property has the proper value set to
* the default value defined in the property list.
*
* @param instance The instance to read from
*/
static assertDefaults(instance: TestComponentProperties) {
const message = `failed on component '${instance.constructor.name}'`;
const properties = (instance.constructor as ComponentConstructor).Properties;
for (const name in properties) {
const propertyMessage = message + `, property '${name}'`;
const property = properties[name];
const value = (instance as any)[name];
/* Default values are set on the instance */
/* chai has an overload that doesn't check value, but it's broken */
expect(instance).to.have.own.property(name, value, propertyMessage);
if (ArrayBuffer.isView(value)) {
expect(value).to.not.equal(property.default, propertyMessage);
}
}
expect(instance.propBool).to.equal(true, message);
expect(instance.propInt).to.equal(12, message);
expect(instance.propFloat).to.equal(3.75, message);
expect(instance.propString).to.equal('hello', message);
expect(instance.propEnum).to.equal(1, message);
expect(instance.propEnumDefaultMissing).to.equal(0, message);
expect(instance.propEnumDefaultNumber).to.equal(1, message);
expect(instance.propEnumDefaultNumberOutOfRange).to.equal(0, message);
expect(instance.propEnumNoValues).to.equal(undefined, message);
expect(instance.propEnumEmptyValues).to.equal(undefined, message);
expect(instance.propObject).to.equal(null, message);
expect(instance.propMesh).to.equal(null, message);
expect(instance.propTexture).to.equal(null, message);
expect(instance.propMaterial).to.equal(null, message);
expect(instance.propAnimation).to.equal(null, message);
expect(instance.propSkin).to.equal(null, message);
expect(instance.propColor).to.be.instanceOf(Float32Array, message);
expect(instance.propColor).to.deep.almost([1, 0, 0.5, 0.75], undefined, message);
expect(instance.propVector2).to.be.instanceOf(Float32Array, message);
expect(instance.propVector2).to.deep.almost([1, 2.5], undefined, message);
expect(instance.propVector3).to.be.instanceOf(Float32Array, message);
expect(instance.propVector3).to.deep.almost([2, 2.5, 0.5], undefined, message);
expect(instance.propVector4).to.be.instanceOf(Float32Array, message);
expect(instance.propVector4).to.deep.almost(
[3, 2.5, 0.5, 12.0],
undefined,
message
);
}
/**
* Assert that each property has the proper global default value **per type**.
*
* @param instance The instance to read from
*/
static assertNoDefaults(instance: TestComponentProperties) {
const properties = (instance.constructor as ComponentConstructor).Properties;
for (const name in properties) {
const property = properties[name];
/* Missing default is replaced with global default */
expect(property).to.have.own.property('default');
const value = (instance as any)[name];
/* Global default values are set on the instance */
expect(instance).to.have.own.property(name);
if (ArrayBuffer.isView(value)) {
expect(value).to.not.equal(property.default);
}
}
expect(instance.propBool).to.equal(false);
expect(instance.propInt).to.equal(0);
expect(instance.propFloat).to.equal(0.0);
expect(instance.propString).to.equal('');
expect(instance.propEnum).to.equal(0);
expect(instance.propEnumNoValues).to.equal(undefined);
expect(instance.propEnumEmptyValues).to.equal(undefined);
expect(instance.propObject).to.equal(null);
expect(instance.propMesh).to.equal(null);
expect(instance.propTexture).to.equal(null);
expect(instance.propMaterial).to.equal(null);
expect(instance.propAnimation).to.equal(null);
expect(instance.propSkin).to.equal(null);
expect(instance.propColor).to.be.instanceOf(Float32Array);
expect(instance.propColor).to.deep.almost([0, 0, 0, 1]);
expect(instance.propVector2).to.be.instanceOf(Float32Array);
expect(instance.propVector2).to.deep.almost([0, 0]);
expect(instance.propVector3).to.be.instanceOf(Float32Array);
expect(instance.propVector3).to.deep.almost([0, 0, 0]);
expect(instance.propVector4).to.be.instanceOf(Float32Array);
expect(instance.propVector4).to.deep.almost([0, 0, 0, 0]);
}
}
/*
* This is required because adding the property type directly in the class
* would shadow the default value. The TypeScript compiler would compile
* the class into something like:
*
* ```js
* class TestComponentProperties {
* propObject;
* }
* ```
*
* Thus `propBool` would override the default `false` value with `undefined`.
*/
interface TestComponentProperties extends TestProperties {}
/**
* Copy of {@link TestComponentProperties} for decorators.
*/
class TestComponentPropertiesDecorator extends Component {
static TypeName = 'decorator-with-defaults';
@property.bool(true)
propBool!: boolean;
@property.int(12)
propInt!: number;
@property.float(3.75)
propFloat!: number;
@property.string('hello')
propString!: string;
@property.enum(['serpent', 'snake'], 'snake')
propEnum!: number;
@property.enum(['serpent', 'snake'], 'monkey')
propEnumDefaultMissing!: number;
@property.enum(['serpent', 'snake'], 1)
propEnumDefaultNumber!: number;
@property.enum(['serpent', 'snake'], 2)
propEnumDefaultNumberOutOfRange!: number;
@property.enum(undefined as any, 'snake')
propEnumNoValues!: number;
@property.enum([], 'snake')
propEnumEmptyValues!: number;
@property.object()
propObject!: Object3D;
@property.mesh()
propMesh!: Mesh;
@property.texture()
propTexture!: Texture | null;
@property.material()
propMaterial!: Material | null;
@property.animation()
propAnimation!: Animation | null;
@property.skin()
propSkin!: Skin | null;
@property.color(1, 0, 0.5, 0.75)
propColor!: Float32Array;
@property.vector2(1, 2.5)
propVector2!: Float32Array;
@property.vector3(2, 2.5, 0.5)
propVector3!: Float32Array;
@property.vector4(3, 2.5, 0.5, 12.0)
propVector4!: Float32Array;
}
describe('Properties', function () {
it('Component.resetProperties()', function () {
class TestComponent extends TestComponentProperties {
static TypeName = 'test-component';
static Properties = TestComponentProperties.createLiteralProperties(true);
}
WL.registerComponent(TestComponent);
const comp = WL.scene.addObject().addComponent(TestComponent)!;
for (const name in TestComponent.Properties) {
(comp as any)[name] = null;
}
comp.resetProperties();
/* Also tests that properties are overwritten on the instance, not
* deleted to use the prototype */
TestComponentProperties.assertDefaults(comp);
});
for (const prop of [
'object',
'skin',
'texture',
'mesh',
'material',
'animation',
] as PropertyKeys[]) {
it('Component.validateProperties() - ' + prop, function () {
class TestOptionalProperty extends Component {
static TypeName = 'test-optional-property';
static Properties = {
prop: (Property[prop] as typeof Property.object)({required: false}),
};
}
class TestRequiredProperty extends Component {
static TypeName = 'test-required-property';
static Properties = {
prop: (Property[prop] as typeof Property.object)({required: true}),
};
}
WL.registerComponent(TestOptionalProperty, TestRequiredProperty);
const optionalPropComp = new TestOptionalProperty(WL.scene);
const requiredPropComp = new TestRequiredProperty(WL.scene);
expect(() => optionalPropComp.validateProperties()).to.not.throw;
/* `validateProperties` only check for non-null references for now.
* Thus, there is no need to set the good type of reference (texture, etc...).
*
* Disable the error logs to avoid spamming the test output */
WL.log.levels.disable(LogLevel.Error);
expect(() => requiredPropComp.validateProperties()).to.throw(
`Property 'prop' is required but was not initialized`
);
});
}
it('required property should deactivate component if validation fails', function () {
class TestComponent extends Component {
static TypeName = 'test-component';
static Properties = {
notRequired: Property.object(),
obj: Property.object({required: true}),
};
}
class TestDecorator extends Component {
static TypeName = 'test-decorator';
@property.object()
notRequired: Object3D = null!;
@property.object({required: true})
obj: Object3D = null!;
}
WL.registerComponent(TestComponent, TestDecorator);
/* Disable the error logs to avoid spamming the test output */
WL.log.levels.disable(LogLevel.Error);
const obj = WL.scene.addObject();
for (const CompClass of [TestComponent, TestDecorator]) {
const failed = obj.addComponent(CompClass, {active: true})!;
expect(failed.active).to.be.false;
const success = obj.addComponent(CompClass, {active: true, obj})!;
expect(success.active).to.be.true;
}
});
it('property decorator should not modify parent constructor', function () {
class ParentComponent extends Component {
static TypeName = 'parent';
@property.string('parent')
parentProp: string = '';
}
class _ extends ParentComponent {
static TypeName = 'child';
@property.string('child')
childProp: string = '';
}
expect(Object.keys(ParentComponent.Properties)).to.have.lengthOf(1);
expect(ParentComponent.Properties.childProp).to.be.undefined;
});
it('Properties order', function () {
class A extends Component {
static TypeName = 'A';
@property.string()
someProp: string = '';
}
class B extends A {
static TypeName = 'B';
@property.float()
anotherProp: number = 0.0;
@property.mesh()
theLastProp: null = null;
}
WL.registerComponent(B);
const order = (B as ComponentConstructor)._propertyOrder;
/* Sorted by name to match editor serialization order */
expect(order).to.deep.equal(['anotherProp', 'someProp', 'theLastProp']);
});
describe('Merge Properties', function () {
class GrandParent extends Component {
static TypeName = 'grand-parent';
@property.string('grand-parent')
grandParentProp: string = '';
}
class Parent extends GrandParent {
static TypeName = 'parent';
@property.string('parent')
parentProp: string = '';
}
it('inheritProperties() direct parenting', function () {
class Child extends Parent {
static TypeName = 'child';
@property.string('child')
childProp: string = '';
}
inheritProperties(Child);
expect(Object.keys(Child.Properties)).to.have.lengthOf(3);
expect(Child.Properties.grandParentProp.default).to.equal('grand-parent');
expect(Child.Properties.parentProp.default).to.equal('parent');
expect(Child.Properties.childProp.default).to.equal('child');
});
it('inheritProperties() returns new .Properties reference when not set', function () {
class SingleParent extends Component {
static TypeName = 'single-parent';
@property.string('parent')
prop: string = '';
}
class Child extends SingleParent {
static TypeName = 'child';
/* Leave the child without any properties */
}
inheritProperties(Child);
expect(Child.Properties).to.not.equal(Parent.Properties);
expect(Object.keys(Child.Properties)).to.deep.equal(['prop']);
});
it('inheritProperties() override', function () {
class Child extends Parent {
static TypeName = 'child';
@property.string('42')
grandParentProp: string = '';
@property.string('43')
parentProp: string = '';
}
inheritProperties(Child);
expect(Object.keys(Child.Properties)).to.have.lengthOf(2);
expect(Child.Properties.grandParentProp.default).to.equal('42');
expect(Child.Properties.parentProp.default).to.equal('43');
});
it('inheritProperties() missing TypeName', function () {
class Interleaved extends Parent {
@property.string('interleaved')
interleavedProp: string = '';
}
class Child extends Interleaved {
static TypeName = 'child';
@property.string('child')
childProp: string = '';
}
inheritProperties(Child);
expect([
Child.Properties.grandParentProp.default,
Child.Properties.parentProp.default,
Child.Properties.interleavedProp.default,
Child.Properties.childProp.default,
]).to.deep.equal(['grand-parent', 'parent', 'interleaved', 'child']);
});
it('inheritProperties() stops at first InheritProperties = false encountered', function () {
class ParentStop extends GrandParent {
static TypeName = 'parent-stop';
static InheritProperties = false;
@property.string('parent')
parentProp: string = '';
}
class Child extends ParentStop {
static TypeName = 'child';
}
inheritProperties(Child);
expect(Object.keys(Child.Properties)).to.deep.equal(['parentProp']);
});
it('registerComponent() with InheritProperties = true', function () {
class Child extends Parent {
static TypeName = 'child';
static InheritProperties = true;
@property.string('child')
childProp: string = '';
}
WL.registerComponent(Child);
expect(Object.keys(Child.Properties)).to.have.lengthOf(3);
expect(Child.Properties.childProp.default).to.equal('child');
expect(Child.Properties.parentProp.default).to.equal('parent');
expect(Child.Properties.grandParentProp.default).to.equal('grand-parent');
WL.registerComponent(Parent);
expect(Object.keys(Parent.Properties)).to.have.lengthOf(2);
expect(Parent.Properties.parentProp.default).to.equal('parent');
expect(Parent.Properties.grandParentProp.default).to.equal('grand-parent');
});
it('Component.InheritProperties = false', function () {
class Parent extends Component {
static TypeName = 'parent';
@property.string('parent')
parentProp: string = '';
}
class Child extends Parent {
static TypeName = 'child';
static InheritProperties = false;
@property.string('child')
childProp: string = '';
}
WL.registerComponent(Child);
expect(Object.keys(Child.Properties)).to.have.lengthOf(1);
expect(Child.Properties.parentProp).to.be.undefined;
});
});
describe('Component Instantiation', function () {
it('defaults when constructor define properties (#1341)', function () {
class TestComponent extends Component {
static TypeName = 'test-component';
static Properties = {
propStr: {type: Type.String, default: 'Hello'},
};
propStr!: string;
}
WL.registerComponent(TestComponent);
const comp = WL.scene.addObject().addComponent(TestComponent);
expect((comp as any).propStr).to.equal('Hello');
});
it('literal properties with defaults', function () {
class LiteralWithDefaults extends TestComponentProperties {
static TypeName = 'literal-with-defaults';
static Properties = TestComponentProperties.createLiteralProperties(true);
}
WL.registerComponent(LiteralWithDefaults);
const comp = WL.scene.addObject().addComponent(LiteralWithDefaults)!;
TestComponentProperties.assertDefaults(comp);
});
it('literal properties no defaults', function () {
class LiteralNoDefaults extends TestComponentProperties {
static TypeName = 'literal-no-defaults';
static Properties = TestComponentProperties.createLiteralProperties(false);
}
WL.registerComponent(LiteralNoDefaults);
const comp = WL.scene.addObject().addComponent(LiteralNoDefaults)!;
TestComponentProperties.assertNoDefaults(comp);
});
it('functor properties with defaults', function () {
class FunctorWithDefaults extends TestComponentProperties {
static TypeName = 'functor-with-defaults';
static Properties = TestComponentProperties.createFunctorProperties(true);
}
WL.registerComponent(FunctorWithDefaults);
const comp = WL.scene.addObject().addComponent(FunctorWithDefaults)!;
TestComponentProperties.assertDefaults(comp);
});
it('functor properties no defaults', function () {
class FunctorNoDefaults extends TestComponentProperties {
static TypeName = 'functor-no-defaults';
static Properties = TestComponentProperties.createFunctorProperties(false);
}
WL.registerComponent(FunctorNoDefaults);
const comp = WL.scene.addObject().addComponent(FunctorNoDefaults)!;
TestComponentProperties.assertNoDefaults(comp);
});
it('decorator properties with defaults', function () {
WL.registerComponent(TestComponentPropertiesDecorator);
const comp = WL.scene
.addObject()
.addComponent(TestComponentPropertiesDecorator)!;
TestComponentProperties.assertDefaults(comp);
});
});
});