qunit-utils.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. const 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. const actual = obj[ key ];
  36. const expected = ref[ key ];
  37. const 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. const cmp = new SmartComparer();
  47. const same = cmp.areEqual( actual, expected );
  48. const 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. const 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. let 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. const geometryKeys = Object.keys( geometryA );
  73. const cloneKeys = Object.keys( geometryB );
  74. let differingProp = undefined;
  75. for ( let i = 0, l = geometryKeys.length; i < l; i ++ ) {
  76. const 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.6', '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. const params = geom.parameters;
  91. if ( ! params ) {
  92. return;
  93. }
  94. // All parameters from geometry should be persisted.
  95. let keys = Object.keys( params );
  96. for ( let 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. const notParameters = [ 'metadata', 'uuid', 'type' ];
  102. keys = Object.keys( json );
  103. for ( let i = 0, l = keys.length; i < l; i ++ ) {
  104. const 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. const wrap = [ json ];
  111. const loader = new ObjectLoader();
  112. const 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. const differing = getDifferingProp( output[ geom.uuid ], geom );
  116. if ( differing ) console.log( differing );
  117. let differingProp = getDifferingProp( output[ geom.uuid ], geom );
  118. QUnit.assert.ok( differingProp === undefined, 'properties are equal' );
  119. differingProp = getDifferingProp( geom, output[ geom.uuid ] );
  120. QUnit.assert.ok( differingProp === undefined, 'properties are equal' );
  121. }
  122. // Verify geom -> json -> geom
  123. function checkGeometryJsonRoundtrip( geom ) {
  124. const json = geom.toJSON();
  125. checkGeometryJsonWriting( geom, json );
  126. checkGeometryJsonReading( json, geom );
  127. }
  128. // Run common geometry tests.
  129. function runStdGeometryTests( assert, geometries ) {
  130. for ( let i = 0, l = geometries.length; i < l; i ++ ) {
  131. const geom = geometries[ i ];
  132. // Clone
  133. checkGeometryClone( geom );
  134. // json round trip
  135. checkGeometryJsonRoundtrip( geom );
  136. }
  137. }
  138. //
  139. // LIGHT TEST HELPERS
  140. //
  141. // Run common light tests.
  142. function runStdLightTests( assert, lights ) {
  143. for ( let i = 0, l = lights.length; i < l; i ++ ) {
  144. const light = lights[ i ];
  145. // copy and clone
  146. checkLightCopyClone( assert, light );
  147. // THREE.Light doesn't get parsed by ObjectLoader as it's only
  148. // used as an abstract base class - so we skip the JSON tests
  149. if ( light.type !== 'Light' ) {
  150. // json round trip
  151. checkLightJsonRoundtrip( assert, light );
  152. }
  153. }
  154. }
  155. function checkLightCopyClone( assert, light ) {
  156. // copy
  157. const newLight = new light.constructor( 0xc0ffee );
  158. newLight.copy( light );
  159. QUnit.assert.notEqual( newLight.uuid, light.uuid, 'Copied light\'s UUID differs from original' );
  160. QUnit.assert.notEqual( newLight.id, light.id, 'Copied light\'s id differs from original' );
  161. QUnit.assert.smartEqual( newLight, light, 'Copied light is equal to original' );
  162. // real copy?
  163. newLight.color.setHex( 0xc0ffee );
  164. QUnit.assert.notStrictEqual(
  165. newLight.color.getHex(), light.color.getHex(), 'Copied light is independent from original'
  166. );
  167. // Clone
  168. const clone = light.clone(); // better get a new clone
  169. QUnit.assert.notEqual( clone.uuid, light.uuid, 'Cloned light\'s UUID differs from original' );
  170. QUnit.assert.notEqual( clone.id, light.id, 'Clone light\'s id differs from original' );
  171. QUnit.assert.smartEqual( clone, light, 'Clone light is equal to original' );
  172. // real clone?
  173. clone.color.setHex( 0xc0ffee );
  174. QUnit.assert.notStrictEqual(
  175. clone.color.getHex(), light.color.getHex(), 'Clone light is independent from original'
  176. );
  177. if ( light.type !== 'Light' ) {
  178. // json round trip with clone
  179. checkLightJsonRoundtrip( assert, clone );
  180. }
  181. }
  182. // Compare json file with its source Light.
  183. function checkLightJsonWriting( assert, light, json ) {
  184. assert.equal( json.metadata.version, '4.6', 'check metadata version' );
  185. const object = json.object;
  186. assert.equalKey( light, object, 'type' );
  187. assert.equalKey( light, object, 'uuid' );
  188. assert.equal( object.id, undefined, 'should not persist id' );
  189. }
  190. // Check parsing and reconstruction of json Light
  191. function checkLightJsonReading( assert, json, light ) {
  192. const loader = new ObjectLoader();
  193. const outputLight = loader.parse( json );
  194. assert.smartEqual( outputLight, light, 'Reconstruct Light from ObjectLoader' );
  195. }
  196. // Verify light -> json -> light
  197. function checkLightJsonRoundtrip( assert, light ) {
  198. const json = light.toJSON();
  199. checkLightJsonWriting( assert, light, json );
  200. checkLightJsonReading( assert, json, light );
  201. }
  202. export { runStdLightTests, runStdGeometryTests };