qunit-utils.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // Custom QUnit assertions.
  3. ///* global QUnit */
  4. import { SmartComparer } from './SmartComparer.js';
  5. import { ObjectLoader } from '../../../src/loaders/ObjectLoader.js';
  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 differingProp = getDifferingProp( geom, copy );
  65. QUnit.assert.ok( differingProp === undefined, 'properties are equal' );
  66. differingProp = getDifferingProp( copy, geom );
  67. QUnit.assert.ok( differingProp === undefined, 'properties are equal' );
  68. // json round trip with clone
  69. checkGeometryJsonRoundtrip( copy );
  70. }
  71. function getDifferingProp( geometryA, geometryB ) {
  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 ( cloneKeys.indexOf( key ) < 0 ) {
  78. differingProp = key;
  79. break;
  80. }
  81. }
  82. return differingProp;
  83. }
  84. // Compare json file with its source geometry.
  85. function checkGeometryJsonWriting( geom, json ) {
  86. QUnit.assert.equal( json.metadata.version, '4.5', 'check metadata version' );
  87. QUnit.assert.equalKey( geom, json, 'type' );
  88. QUnit.assert.equalKey( geom, json, 'uuid' );
  89. QUnit.assert.equal( json.id, undefined, 'should not persist id' );
  90. var params = geom.parameters;
  91. if ( ! params ) {
  92. return;
  93. }
  94. // All parameters from geometry should be persisted.
  95. var keys = Object.keys( params );
  96. for ( var i = 0, l = keys.length; i < l; i ++ ) {
  97. QUnit.assert.equalKey( params, json, keys[ i ] );
  98. }
  99. // All parameters from json should be transfered to the geometry.
  100. // json is flat. Ignore first level json properties that are not parameters.
  101. var notParameters = [ 'metadata', 'uuid', 'type' ];
  102. var keys = Object.keys( json );
  103. for ( var i = 0, l = keys.length; i < l; i ++ ) {
  104. var key = keys[ i ];
  105. if ( notParameters.indexOf( key ) === - 1 ) QUnit.assert.equalKey( params, json, key );
  106. }
  107. }
  108. // Check parsing and reconstruction of json geometry
  109. function checkGeometryJsonReading( json, geom ) {
  110. var wrap = [ json ];
  111. var loader = new ObjectLoader();
  112. var output = loader.parseGeometries( wrap );
  113. QUnit.assert.ok( output[ geom.uuid ], 'geometry matching source uuid not in output' );
  114. // QUnit.assert.smartEqual( output[ geom.uuid ], geom, 'Reconstruct geometry from ObjectLoader' );
  115. var differing = getDifferingProp( output[ geom.uuid ], geom, [ 'bones' ] );
  116. if ( differing ) console.log( differing );
  117. var excludedProperties = [ 'bones' ];
  118. var differingProp = getDifferingProp( output[ geom.uuid ], geom, excludedProperties );
  119. QUnit.assert.ok( differingProp === undefined, 'properties are equal' );
  120. differingProp = getDifferingProp( geom, output[ geom.uuid ], excludedProperties );
  121. QUnit.assert.ok( differingProp === undefined, 'properties are equal' );
  122. }
  123. // Verify geom -> json -> geom
  124. function checkGeometryJsonRoundtrip( geom ) {
  125. var json = geom.toJSON();
  126. checkGeometryJsonWriting( geom, json );
  127. checkGeometryJsonReading( json, geom );
  128. }
  129. // Run common geometry tests.
  130. function runStdGeometryTests( assert, geometries ) {
  131. for ( var i = 0, l = geometries.length; i < l; i ++ ) {
  132. var geom = geometries[ i ];
  133. // Clone
  134. checkGeometryClone( geom );
  135. // json round trip
  136. checkGeometryJsonRoundtrip( geom );
  137. }
  138. }
  139. //
  140. // LIGHT TEST HELPERS
  141. //
  142. // Run common light tests.
  143. function runStdLightTests( assert, lights ) {
  144. for ( var i = 0, l = lights.length; i < l; i ++ ) {
  145. var light = lights[ i ];
  146. // copy and clone
  147. checkLightCopyClone( assert, light );
  148. // THREE.Light doesn't get parsed by ObjectLoader as it's only
  149. // used as an abstract base class - so we skip the JSON tests
  150. if ( light.type !== 'Light' ) {
  151. // json round trip
  152. checkLightJsonRoundtrip( assert, light );
  153. }
  154. }
  155. }
  156. function checkLightCopyClone( assert, light ) {
  157. // copy
  158. var newLight = new light.constructor( 0xc0ffee );
  159. newLight.copy( light );
  160. QUnit.assert.notEqual( newLight.uuid, light.uuid, 'Copied light\'s UUID differs from original' );
  161. QUnit.assert.notEqual( newLight.id, light.id, 'Copied light\'s id differs from original' );
  162. QUnit.assert.smartEqual( newLight, light, 'Copied light is equal to original' );
  163. // real copy?
  164. newLight.color.setHex( 0xc0ffee );
  165. QUnit.assert.notStrictEqual(
  166. newLight.color.getHex(), light.color.getHex(), 'Copied light is independent from original'
  167. );
  168. // Clone
  169. var clone = light.clone(); // better get a new var
  170. QUnit.assert.notEqual( clone.uuid, light.uuid, 'Cloned light\'s UUID differs from original' );
  171. QUnit.assert.notEqual( clone.id, light.id, 'Clone light\'s id differs from original' );
  172. QUnit.assert.smartEqual( clone, light, 'Clone light is equal to original' );
  173. // real clone?
  174. clone.color.setHex( 0xc0ffee );
  175. QUnit.assert.notStrictEqual(
  176. clone.color.getHex(), light.color.getHex(), 'Clone light is independent from original'
  177. );
  178. if ( light.type !== 'Light' ) {
  179. // json round trip with clone
  180. checkLightJsonRoundtrip( assert, clone );
  181. }
  182. }
  183. // Compare json file with its source Light.
  184. function checkLightJsonWriting( assert, light, json ) {
  185. assert.equal( json.metadata.version, '4.5', 'check metadata version' );
  186. var object = json.object;
  187. assert.equalKey( light, object, 'type' );
  188. assert.equalKey( light, object, 'uuid' );
  189. assert.equal( object.id, undefined, 'should not persist id' );
  190. }
  191. // Check parsing and reconstruction of json Light
  192. function checkLightJsonReading( assert, json, light ) {
  193. var loader = new ObjectLoader();
  194. var outputLight = loader.parse( json );
  195. assert.smartEqual( outputLight, light, 'Reconstruct Light from ObjectLoader' );
  196. }
  197. // Verify light -> json -> light
  198. function checkLightJsonRoundtrip( assert, light ) {
  199. var json = light.toJSON();
  200. checkLightJsonWriting( assert, light, json );
  201. checkLightJsonReading( assert, json, light );
  202. }
  203. export { runStdLightTests, runStdGeometryTests };