qunit-utils.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. QUnit.assert.smartEqual( copy, geom, "clone is equal to original" );
  38. // json round trip with clone
  39. checkGeometryJsonRoundtrip( copy );
  40. }
  41. // Compare json file with its source geometry.
  42. function checkGeometryJsonWriting( geom, json ) {
  43. QUnit.assert.equal( json.metadata.version, "4.4", "check metadata version" );
  44. QUnit.assert.equalKey( geom, json, 'type' );
  45. QUnit.assert.equalKey( geom, json, 'uuid' );
  46. QUnit.assert.equal( json.id, undefined, "should not persist id" );
  47. var params = geom.parameters;
  48. if ( !params ) {
  49. return;
  50. }
  51. // All parameters from geometry should be persisted.
  52. var keys = Object.keys( params );
  53. for ( var i = 0, l = keys.length; i < l; i++ ) {
  54. QUnit.assert.equalKey( params, json, keys[ i ] );
  55. }
  56. // All parameters from json should be transfered to the geometry.
  57. // json is flat. Ignore first level json properties that are not parameters.
  58. var notParameters = [ "metadata", "uuid", "type" ];
  59. var keys = Object.keys( json );
  60. for ( var i = 0, l = keys.length; i < l; i++ ) {
  61. var key = keys[ i ];
  62. if ( notParameters.indexOf( key) === -1 ) QUnit.assert.equalKey( params, json, key );
  63. }
  64. }
  65. // Check parsing and reconstruction of json geometry
  66. function checkGeometryJsonReading( json, geom ) {
  67. var wrap = [ json ];
  68. var loader = new THREE.ObjectLoader();
  69. var output = loader.parseGeometries( wrap );
  70. QUnit.assert.ok( output[ geom.uuid ], 'geometry matching source uuid not in output' );
  71. QUnit.assert.smartEqual( output[ geom.uuid ], geom, 'Reconstruct geometry from ObjectLoader' );
  72. }
  73. // Verify geom -> json -> geom
  74. function checkGeometryJsonRoundtrip( geom ) {
  75. var json = geom.toJSON();
  76. checkGeometryJsonWriting( geom, json );
  77. checkGeometryJsonReading( json, geom );
  78. }
  79. // Look for undefined and NaN values in numerical fieds.
  80. function checkFinite( geom ) {
  81. var allVerticesAreFinite = true;
  82. var vertices = geom.vertices || [];
  83. for ( var i = 0, l = vertices.length; i < l; i++ ) {
  84. var v = geom.vertices[ i ];
  85. if ( !( isFinite( v.x ) || isFinite( v.y ) || isFinite( v.z ) ) ) {
  86. allVerticesAreFinite = false;
  87. break;
  88. }
  89. }
  90. // TODO Buffers, normal, etc.
  91. QUnit.assert.ok( allVerticesAreFinite, "contains only finite coordinates" );
  92. }
  93. // Run common geometry tests.
  94. function runStdGeometryTests( assert, geometries ) {
  95. for ( var i = 0, l = geometries.length; i < l; i++ ) {
  96. var geom = geometries[ i ];
  97. checkFinite( geom );
  98. // Clone
  99. checkGeometryClone( geom );
  100. // json round trip
  101. checkGeometryJsonRoundtrip( geom );
  102. }
  103. }
  104. //
  105. // LIGHT TEST HELPERS
  106. //
  107. // Run common light tests.
  108. function runStdLightTests( assert, lights ) {
  109. for ( var i = 0, l = lights.length; i < l; i++ ) {
  110. var light = lights[i];
  111. // Clone
  112. checkLightClone( light );
  113. // json round trip
  114. checkLightJsonRoundtrip( light );
  115. }
  116. }
  117. function checkLightClone( light ) {
  118. // Clone
  119. var copy = light.clone();
  120. QUnit.assert.notEqual( copy.uuid, light.uuid, "clone uuid should differ from original" );
  121. QUnit.assert.notEqual( copy.id, light.id, "clone id should differ from original" );
  122. QUnit.assert.smartEqual( copy, light, "clone is equal to original" );
  123. // json round trip with clone
  124. checkLightJsonRoundtrip( copy );
  125. }
  126. // Compare json file with its source Light.
  127. function checkLightJsonWriting( light, json ) {
  128. QUnit.assert.equal( json.metadata.version, "4.4", "check metadata version" );
  129. var object = json.object;
  130. QUnit.assert.equalKey( light, object, 'type' );
  131. QUnit.assert.equalKey( light, object, 'uuid' );
  132. QUnit.assert.equal( object.id, undefined, "should not persist id" );
  133. }
  134. // Check parsing and reconstruction of json Light
  135. function checkLightJsonReading( json, light ) {
  136. var loader = new THREE.ObjectLoader();
  137. var outputLight = loader.parse( json );
  138. QUnit.assert.smartEqual( outputLight, light, 'Reconstruct Light from ObjectLoader' );
  139. }
  140. // Verify light -> json -> light
  141. function checkLightJsonRoundtrip( light ) {
  142. var json = light.toJSON();
  143. checkLightJsonWriting( light, json );
  144. checkLightJsonReading( json, light );
  145. }