qunit-utils.js 7.8 KB

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