qunit-utils.js 7.8 KB

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