2
0

qunit-utils.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //
  2. // Custom QUnit assertions.
  3. //
  4. QUnit.assert.success = function( message ) {
  5. // Equivalent to assert( true, message );
  6. QUnit.assert.push( true, undefined, undefined, message );
  7. };
  8. QUnit.assert.fail = function( message ) {
  9. // Equivalent to assert( false, message );
  10. QUnit.assert.push( false, undefined, undefined, message );
  11. };
  12. QUnit.assert.numEqual = function( actual, expected, message ) {
  13. var diff = Math.abs(actual - expected);
  14. message = message || ( actual + " should be equal to " + expected );
  15. QUnit.assert.push( diff < 0.1, actual, expected, message );
  16. };
  17. QUnit.assert.equalKey = function( obj, ref, key ) {
  18. var actual = obj[key];
  19. var expected = ref[key];
  20. var message = actual + ' should be equal to ' + expected + ' for key "' + key + '"';
  21. QUnit.assert.push( actual == expected, actual, expected, message );
  22. };
  23. QUnit.assert.smartEqual = function( actual, expected, message ) {
  24. var cmp = new SmartComparer();
  25. var same = cmp.areEqual(actual, expected);
  26. var msg = cmp.getDiagnostic() || message;
  27. QUnit.assert.push( same, actual, expected, msg );
  28. };
  29. //
  30. // GEOMETRY TEST HELPERS
  31. //
  32. function checkGeometryClone( geom ) {
  33. // Clone
  34. var copy = geom.clone();
  35. QUnit.assert.notEqual( copy.uuid, geom.uuid, "clone uuid should differ from original" );
  36. QUnit.assert.notEqual( copy.id, geom.id, "clone id should differ from original" );
  37. var excludedProperties = [ 'parameters', 'widthSegments', 'heightSegments', 'depthSegments' ];
  38. var differingProp = getDifferingProp( geom, copy, excludedProperties );
  39. ok( differingProp === undefined, 'properties are equal' );
  40. differingProp = getDifferingProp( copy, geom, excludedProperties );
  41. ok( differingProp === undefined, 'properties are equal' );
  42. // json round trip with clone
  43. checkGeometryJsonRoundtrip( copy );
  44. }
  45. function getDifferingProp( geometryA, geometryB, excludedProperties) {
  46. excludedProperties = excludedProperties || [];
  47. var geometryKeys = Object.keys( geometryA );
  48. var cloneKeys = Object.keys( geometryB );
  49. var keysWhichAreNotChecked = [ 'parameters', 'widthSegments', 'heightSegments', 'depthSegments' ];
  50. var differingProp = undefined;
  51. for ( var i = 0, l = geometryKeys.length; i < l; i++ ) {
  52. var key = geometryKeys[ i ];
  53. if ( excludedProperties.indexOf(key) >= 0 ) {
  54. continue;
  55. }
  56. if ( cloneKeys.indexOf( key ) < 0 ) {
  57. differingProp = key;
  58. break;
  59. }
  60. }
  61. return differingProp;
  62. }
  63. // Compare json file with its source geometry.
  64. function checkGeometryJsonWriting( geom, json ) {
  65. QUnit.assert.equal( json.metadata.version, "4.4", "check metadata version" );
  66. QUnit.assert.equalKey( geom, json, 'type' );
  67. QUnit.assert.equalKey( geom, json, 'uuid' );
  68. QUnit.assert.equal( json.id, undefined, "should not persist id" );
  69. var params = geom.parameters;
  70. if ( !params ) {
  71. return;
  72. }
  73. // All parameters from geometry should be persisted.
  74. var keys = Object.keys( params );
  75. for ( var i = 0, l = keys.length; i < l; i++ ) {
  76. QUnit.assert.equalKey( params, json, keys[ i ] );
  77. }
  78. // All parameters from json should be transfered to the geometry.
  79. // json is flat. Ignore first level json properties that are not parameters.
  80. var notParameters = [ "metadata", "uuid", "type" ];
  81. var keys = Object.keys( json );
  82. for ( var i = 0, l = keys.length; i < l; i++ ) {
  83. var key = keys[ i ];
  84. if ( notParameters.indexOf( key) === -1 ) QUnit.assert.equalKey( params, json, key );
  85. }
  86. }
  87. // Check parsing and reconstruction of json geometry
  88. function checkGeometryJsonReading( json, geom ) {
  89. var wrap = [ json ];
  90. var loader = new THREE.ObjectLoader();
  91. var output = loader.parseGeometries( wrap );
  92. QUnit.assert.ok( output[ geom.uuid ], 'geometry matching source uuid not in output' );
  93. // QUnit.assert.smartEqual( output[ geom.uuid ], geom, 'Reconstruct geometry from ObjectLoader' );
  94. var differing = getDifferingProp(output[ geom.uuid ], geom, ['bones']);
  95. if (differing) {
  96. console.log(differing);
  97. }
  98. var excludedProperties = [ 'bones' ];
  99. var differingProp = getDifferingProp( output[ geom.uuid ], geom, excludedProperties );
  100. ok( differingProp === undefined, 'properties are equal' );
  101. differingProp = getDifferingProp( geom, output[ geom.uuid ], excludedProperties );
  102. ok( differingProp === undefined, 'properties are equal' );
  103. }
  104. // Verify geom -> json -> geom
  105. function checkGeometryJsonRoundtrip( geom ) {
  106. var json = geom.toJSON();
  107. checkGeometryJsonWriting( geom, json );
  108. checkGeometryJsonReading( json, geom );
  109. }
  110. // Look for undefined and NaN values in numerical fieds.
  111. function checkFinite( geom ) {
  112. var allVerticesAreFinite = true;
  113. var vertices = geom.vertices || [];
  114. for ( var i = 0, l = vertices.length; i < l; i++ ) {
  115. var v = geom.vertices[ i ];
  116. if ( !( isFinite( v.x ) || isFinite( v.y ) || isFinite( v.z ) ) ) {
  117. allVerticesAreFinite = false;
  118. break;
  119. }
  120. }
  121. // TODO Buffers, normal, etc.
  122. QUnit.assert.ok( allVerticesAreFinite, "contains only finite coordinates" );
  123. }
  124. // Run common geometry tests.
  125. function runStdGeometryTests( assert, geometries ) {
  126. for ( var i = 0, l = geometries.length; i < l; i++ ) {
  127. var geom = geometries[ i ];
  128. checkFinite( geom );
  129. // Clone
  130. checkGeometryClone( geom );
  131. // json round trip
  132. checkGeometryJsonRoundtrip( geom );
  133. }
  134. }
  135. //
  136. // LIGHT TEST HELPERS
  137. //
  138. // Run common light tests.
  139. function runStdLightTests( assert, lights ) {
  140. for ( var i = 0, l = lights.length; i < l; i++ ) {
  141. var light = lights[i];
  142. // Clone
  143. checkLightClone( light );
  144. // json round trip
  145. checkLightJsonRoundtrip( light );
  146. }
  147. }
  148. function checkLightClone( light ) {
  149. // Clone
  150. var copy = light.clone();
  151. QUnit.assert.notEqual( copy.uuid, light.uuid, "clone uuid should differ from original" );
  152. QUnit.assert.notEqual( copy.id, light.id, "clone id should differ from original" );
  153. QUnit.assert.smartEqual( copy, light, "clone is equal to original" );
  154. // json round trip with clone
  155. checkLightJsonRoundtrip( copy );
  156. }
  157. // Compare json file with its source Light.
  158. function checkLightJsonWriting( light, json ) {
  159. QUnit.assert.equal( json.metadata.version, "4.4", "check metadata version" );
  160. var object = json.object;
  161. QUnit.assert.equalKey( light, object, 'type' );
  162. QUnit.assert.equalKey( light, object, 'uuid' );
  163. QUnit.assert.equal( object.id, undefined, "should not persist id" );
  164. }
  165. // Check parsing and reconstruction of json Light
  166. function checkLightJsonReading( json, light ) {
  167. var loader = new THREE.ObjectLoader();
  168. var outputLight = loader.parse( json );
  169. QUnit.assert.smartEqual( outputLight, light, 'Reconstruct Light from ObjectLoader' );
  170. }
  171. // Verify light -> json -> light
  172. function checkLightJsonRoundtrip( light ) {
  173. var json = light.toJSON();
  174. checkLightJsonWriting( light, json );
  175. checkLightJsonReading( json, light );
  176. }