-
Notifications
You must be signed in to change notification settings - Fork 6
/
qunit-utils.js
331 lines (218 loc) · 7.81 KB
/
qunit-utils.js
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
//
// Custom QUnit assertions.
//
QUnit.assert.success = function ( message ) {
// Equivalent to assert( true, message );
this.pushResult( {
result: true,
actual: undefined,
expected: undefined,
message: message
} );
};
QUnit.assert.fail = function ( message ) {
// Equivalent to assert( false, message );
this.pushResult( {
result: false,
actual: undefined,
expected: undefined,
message: message
} );
};
QUnit.assert.numEqual = function ( actual, expected, message ) {
var diff = Math.abs( actual - expected );
message = message || ( actual + " should be equal to " + expected );
this.pushResult( {
result: diff < 0.1,
actual: actual,
expected: expected,
message: message
} );
};
QUnit.assert.equalKey = function ( obj, ref, key ) {
var actual = obj[ key ];
var expected = ref[ key ];
var message = actual + ' should be equal to ' + expected + ' for key "' + key + '"';
this.pushResult( {
result: actual == expected,
actual: actual,
expected: expected,
message: message
} );
};
QUnit.assert.smartEqual = function ( actual, expected, message ) {
var cmp = new SmartComparer();
var same = cmp.areEqual( actual, expected );
var msg = cmp.getDiagnostic() || message;
this.pushResult( {
result: same,
actual: actual,
expected: expected,
message: msg
} );
};
//
// GEOMETRY TEST HELPERS
//
function checkGeometryClone( geom ) {
// Clone
var copy = geom.clone();
QUnit.assert.notEqual( copy.uuid, geom.uuid, "clone uuid should differ from original" );
QUnit.assert.notEqual( copy.id, geom.id, "clone id should differ from original" );
var excludedProperties = [ 'parameters', 'widthSegments', 'heightSegments', 'depthSegments' ];
var differingProp = getDifferingProp( geom, copy, excludedProperties );
QUnit.assert.ok( differingProp === undefined, 'properties are equal' );
differingProp = getDifferingProp( copy, geom, excludedProperties );
QUnit.assert.ok( differingProp === undefined, 'properties are equal' );
// json round trip with clone
checkGeometryJsonRoundtrip( copy );
}
function getDifferingProp( geometryA, geometryB, excludedProperties ) {
excludedProperties = excludedProperties || [];
var geometryKeys = Object.keys( geometryA );
var cloneKeys = Object.keys( geometryB );
var differingProp = undefined;
for ( var i = 0, l = geometryKeys.length; i < l; i ++ ) {
var key = geometryKeys[ i ];
if ( excludedProperties.indexOf( key ) >= 0 ) continue;
if ( cloneKeys.indexOf( key ) < 0 ) {
differingProp = key;
break;
}
}
return differingProp;
}
// Compare json file with its source geometry.
function checkGeometryJsonWriting( geom, json ) {
QUnit.assert.equal( json.metadata.version, "4.5", "check metadata version" );
QUnit.assert.equalKey( geom, json, 'type' );
QUnit.assert.equalKey( geom, json, 'uuid' );
QUnit.assert.equal( json.id, undefined, "should not persist id" );
var params = geom.parameters;
if ( ! params ) {
return;
}
// All parameters from geometry should be persisted.
var keys = Object.keys( params );
for ( var i = 0, l = keys.length; i < l; i ++ ) {
QUnit.assert.equalKey( params, json, keys[ i ] );
}
// All parameters from json should be transfered to the geometry.
// json is flat. Ignore first level json properties that are not parameters.
var notParameters = [ "metadata", "uuid", "type" ];
var keys = Object.keys( json );
for ( var i = 0, l = keys.length; i < l; i ++ ) {
var key = keys[ i ];
if ( notParameters.indexOf( key ) === - 1 ) QUnit.assert.equalKey( params, json, key );
}
}
// Check parsing and reconstruction of json geometry
function checkGeometryJsonReading( json, geom ) {
var wrap = [ json ];
var loader = new THREE.ObjectLoader();
var output = loader.parseGeometries( wrap );
QUnit.assert.ok( output[ geom.uuid ], 'geometry matching source uuid not in output' );
// QUnit.assert.smartEqual( output[ geom.uuid ], geom, 'Reconstruct geometry from ObjectLoader' );
var differing = getDifferingProp( output[ geom.uuid ], geom, [ 'bones' ] );
if ( differing ) console.log( differing );
var excludedProperties = [ 'bones' ];
var differingProp = getDifferingProp( output[ geom.uuid ], geom, excludedProperties );
QUnit.assert.ok( differingProp === undefined, 'properties are equal' );
differingProp = getDifferingProp( geom, output[ geom.uuid ], excludedProperties );
QUnit.assert.ok( differingProp === undefined, 'properties are equal' );
}
// Verify geom -> json -> geom
function checkGeometryJsonRoundtrip( geom ) {
var json = geom.toJSON();
checkGeometryJsonWriting( geom, json );
checkGeometryJsonReading( json, geom );
}
// Look for undefined and NaN values in numerical fieds.
function checkFinite( geom ) {
var allVerticesAreFinite = true;
var vertices = geom.vertices || [];
for ( var i = 0, l = vertices.length; i < l; i ++ ) {
var v = geom.vertices[ i ];
if ( ! ( isFinite( v.x ) || isFinite( v.y ) || isFinite( v.z ) ) ) {
allVerticesAreFinite = false;
break;
}
}
// TODO Buffers, normal, etc.
QUnit.assert.ok( allVerticesAreFinite, "contains only finite coordinates" );
}
// Run common geometry tests.
function runStdGeometryTests( assert, geometries ) {
for ( var i = 0, l = geometries.length; i < l; i ++ ) {
var geom = geometries[ i ];
checkFinite( geom );
// Clone
checkGeometryClone( geom );
// json round trip
checkGeometryJsonRoundtrip( geom );
}
}
//
// LIGHT TEST HELPERS
//
// Run common light tests.
function runStdLightTests( assert, lights ) {
for ( var i = 0, l = lights.length; i < l; i ++ ) {
var light = lights[ i ];
// copy and clone
checkLightCopyClone( assert, light );
// THREE.Light doesn't get parsed by ObjectLoader as it's only
// used as an abstract base class - so we skip the JSON tests
if ( light.type !== "Light" ) {
// json round trip
checkLightJsonRoundtrip( assert, light );
}
}
}
function checkLightCopyClone( assert, light ) {
// copy
var newLight = new light.constructor( 0xc0ffee );
newLight.copy( light );
QUnit.assert.notEqual( newLight.uuid, light.uuid, "Copied light's UUID differs from original" );
QUnit.assert.notEqual( newLight.id, light.id, "Copied light's id differs from original" );
QUnit.assert.smartEqual( newLight, light, "Copied light is equal to original" );
// real copy?
newLight.color.setHex( 0xc0ffee );
QUnit.assert.notStrictEqual(
newLight.color.getHex(), light.color.getHex(), "Copied light is independent from original"
);
// Clone
var clone = light.clone(); // better get a new var
QUnit.assert.notEqual( clone.uuid, light.uuid, "Cloned light's UUID differs from original" );
QUnit.assert.notEqual( clone.id, light.id, "Clone light's id differs from original" );
QUnit.assert.smartEqual( clone, light, "Clone light is equal to original" );
// real clone?
clone.color.setHex( 0xc0ffee );
QUnit.assert.notStrictEqual(
clone.color.getHex(), light.color.getHex(), "Clone light is independent from original"
);
if ( light.type !== "Light" ) {
// json round trip with clone
checkLightJsonRoundtrip( assert, clone );
}
}
// Compare json file with its source Light.
function checkLightJsonWriting( assert, light, json ) {
assert.equal( json.metadata.version, "4.5", "check metadata version" );
var object = json.object;
assert.equalKey( light, object, 'type' );
assert.equalKey( light, object, 'uuid' );
assert.equal( object.id, undefined, "should not persist id" );
}
// Check parsing and reconstruction of json Light
function checkLightJsonReading( assert, json, light ) {
var loader = new THREE.ObjectLoader();
var outputLight = loader.parse( json );
assert.smartEqual( outputLight, light, 'Reconstruct Light from ObjectLoader' );
}
// Verify light -> json -> light
function checkLightJsonRoundtrip( assert, light ) {
var json = light.toJSON();
checkLightJsonWriting( assert, light, json );
checkLightJsonReading( assert, json, light );
}