qunit-utils.js 8.0 KB

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