qunit-utils.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // Custom QUnit assertions.
  3. //
  4. QUnit.assert.success = function( message ) {
  5. // Equivalent to assert( true, message );
  6. QUnit.assert.push( true, undefined, undefined, message );
  7. };
  8. QUnit.assert.fail = function( message ) {
  9. // Equivalent to assert( false, message );
  10. QUnit.assert.push( false, undefined, undefined, message );
  11. };
  12. QUnit.assert.numEqual = function( actual, expected, message ) {
  13. var diff = Math.abs(actual - expected);
  14. message = message || ( actual + " should be equal to " + expected );
  15. QUnit.assert.push( diff < 0.1, actual, expected, message );
  16. };
  17. QUnit.assert.equalKey = function( obj, ref, key ) {
  18. var actual = obj[key];
  19. var expected = ref[key];
  20. var message = actual + ' should be equal to ' + expected + ' for key "' + key + '"';
  21. QUnit.assert.push( actual == expected, actual, expected, message );
  22. };
  23. QUnit.assert.smartEqual = function( actual, expected, message ) {
  24. var cmp = new SmartComparer();
  25. var same = cmp.areEqual(actual, expected);
  26. var msg = cmp.getDiagnostic() || message;
  27. QUnit.assert.push( same, actual, expected, msg );
  28. };
  29. //
  30. // GEOMETRY TEST HELPERS
  31. //
  32. function checkGeometryClone( geom ) {
  33. // Clone
  34. var copy = geom.clone();
  35. QUnit.assert.notEqual( copy.uuid, geom.uuid, "clone uuid should differ from original" );
  36. QUnit.assert.notEqual( copy.id, geom.id, "clone id should differ from original" );
  37. var excludedProperties = [ 'parameters', 'widthSegments', 'heightSegments', 'depthSegments' ];
  38. var differingProp = getDifferingProp( geom, copy, excludedProperties );
  39. var differingProp = getDifferingProp( copy, geom, excludedProperties );
  40. ok( differingProp === undefined, 'properties are equal' );
  41. // json round trip with clone
  42. checkGeometryJsonRoundtrip( copy );
  43. }
  44. function getDifferingProp( geometryA, geometryB, excludedProperties ) {
  45. var geometryKeys = Object.keys( geometryA );
  46. var cloneKeys = Object.keys( geometryB );
  47. var keysWhichAreNotChecked = [ 'parameters', 'widthSegments', 'heightSegments', 'depthSegments' ];
  48. var differingProp = undefined;
  49. for ( var i = 0, l = geometryKeys.length; i < l; i++ ) {
  50. var key = geometryKeys[ i ];
  51. if ( excludedProperties.indexOf(key) >= 0 ) {
  52. continue;
  53. }
  54. if ( cloneKeys.indexOf( key ) < 0 ) {
  55. differingProp = key;
  56. break;
  57. }
  58. }
  59. return differingProp;
  60. }
  61. // Compare json file with its source geometry.
  62. function checkGeometryJsonWriting( geom, json ) {
  63. QUnit.assert.equal( json.metadata.version, "4.4", "check metadata version" );
  64. QUnit.assert.equalKey( geom, json, 'type' );
  65. QUnit.assert.equalKey( geom, json, 'uuid' );
  66. QUnit.assert.equal( json.id, undefined, "should not persist id" );
  67. var params = geom.parameters;
  68. if ( !params ) {
  69. return;
  70. }
  71. // All parameters from geometry should be persisted.
  72. var keys = Object.keys( params );
  73. for ( var i = 0, l = keys.length; i < l; i++ ) {
  74. QUnit.assert.equalKey( params, json, keys[ i ] );
  75. }
  76. // All parameters from json should be transfered to the geometry.
  77. // json is flat. Ignore first level json properties that are not parameters.
  78. var notParameters = [ "metadata", "uuid", "type" ];
  79. var keys = Object.keys( json );
  80. for ( var i = 0, l = keys.length; i < l; i++ ) {
  81. var key = keys[ i ];
  82. if ( notParameters.indexOf( key) === -1 ) QUnit.assert.equalKey( params, json, key );
  83. }
  84. }
  85. // Check parsing and reconstruction of json geometry
  86. function checkGeometryJsonReading( json, geom ) {
  87. var wrap = [ json ];
  88. var loader = new THREE.ObjectLoader();
  89. var output = loader.parseGeometries( wrap );
  90. QUnit.assert.ok( output[ geom.uuid ], 'geometry matching source uuid not in output' );
  91. QUnit.assert.smartEqual( output[ geom.uuid ], geom, 'Reconstruct geometry from ObjectLoader' );
  92. }
  93. // Verify geom -> json -> geom
  94. function checkGeometryJsonRoundtrip( geom ) {
  95. var json = geom.toJSON();
  96. checkGeometryJsonWriting( geom, json );
  97. checkGeometryJsonReading( json, geom );
  98. }
  99. // Look for undefined and NaN values in numerical fieds.
  100. function checkFinite( geom ) {
  101. var allVerticesAreFinite = true;
  102. var vertices = geom.vertices || [];
  103. for ( var i = 0, l = vertices.length; i < l; i++ ) {
  104. var v = geom.vertices[ i ];
  105. if ( !( isFinite( v.x ) || isFinite( v.y ) || isFinite( v.z ) ) ) {
  106. allVerticesAreFinite = false;
  107. break;
  108. }
  109. }
  110. // TODO Buffers, normal, etc.
  111. QUnit.assert.ok( allVerticesAreFinite, "contains only finite coordinates" );
  112. }
  113. // Run common geometry tests.
  114. function runStdGeometryTests( assert, geometries ) {
  115. for ( var i = 0, l = geometries.length; i < l; i++ ) {
  116. var geom = geometries[ i ];
  117. checkFinite( geom );
  118. // Clone
  119. checkGeometryClone( geom );
  120. // json round trip
  121. checkGeometryJsonRoundtrip( geom );
  122. }
  123. }
  124. //
  125. // LIGHT TEST HELPERS
  126. //
  127. // Run common light tests.
  128. function runStdLightTests( assert, lights ) {
  129. for ( var i = 0, l = lights.length; i < l; i++ ) {
  130. var light = lights[i];
  131. // Clone
  132. checkLightClone( light );
  133. // json round trip
  134. checkLightJsonRoundtrip( light );
  135. }
  136. }
  137. function checkLightClone( light ) {
  138. // Clone
  139. var copy = light.clone();
  140. QUnit.assert.notEqual( copy.uuid, light.uuid, "clone uuid should differ from original" );
  141. QUnit.assert.notEqual( copy.id, light.id, "clone id should differ from original" );
  142. QUnit.assert.smartEqual( copy, light, "clone is equal to original" );
  143. // json round trip with clone
  144. checkLightJsonRoundtrip( copy );
  145. }
  146. // Compare json file with its source Light.
  147. function checkLightJsonWriting( light, json ) {
  148. QUnit.assert.equal( json.metadata.version, "4.4", "check metadata version" );
  149. var object = json.object;
  150. QUnit.assert.equalKey( light, object, 'type' );
  151. QUnit.assert.equalKey( light, object, 'uuid' );
  152. QUnit.assert.equal( object.id, undefined, "should not persist id" );
  153. }
  154. // Check parsing and reconstruction of json Light
  155. function checkLightJsonReading( json, light ) {
  156. var loader = new THREE.ObjectLoader();
  157. var outputLight = loader.parse( json );
  158. QUnit.assert.smartEqual( outputLight, light, 'Reconstruct Light from ObjectLoader' );
  159. }
  160. // Verify light -> json -> light
  161. function checkLightJsonRoundtrip( light ) {
  162. var json = light.toJSON();
  163. checkLightJsonWriting( light, json );
  164. checkLightJsonReading( json, light );
  165. }