qunit-utils.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // All parameters from geometry should be persisted.
  48. _.forOwn( geom.parameters, function ( val, key ) {
  49. QUnit.assert.equalKey( geom.parameters, json, key );
  50. });
  51. // All parameters from json should be transfered to the geometry.
  52. // json is flat. Ignore first level json properties that are not parameters.
  53. var notParameters = [ "metadata", "uuid", "type" ];
  54. _.forOwn( json, function ( val, key ) {
  55. if ( notParameters.indexOf( key) === -1 ) QUnit.assert.equalKey( geom.parameters, json, key );
  56. });
  57. }
  58. // Check parsing and reconstruction of json geometry
  59. function checkGeometryJsonReading( json, geom ) {
  60. var wrap = [ json ];
  61. var loader = new THREE.ObjectLoader();
  62. var output = loader.parseGeometries( wrap );
  63. QUnit.assert.ok( output[geom.uuid], 'geometry matching source uuid not in output' );
  64. QUnit.assert.smartEqual( output[geom.uuid], geom, 'Reconstruct geometry from ObjectLoader' );
  65. }
  66. // Verify geom -> json -> geom
  67. function checkGeometryJsonRoundtrip( geom ) {
  68. var json = geom.toJSON();
  69. checkGeometryJsonWriting( geom, json );
  70. checkGeometryJsonReading( json, geom );
  71. }
  72. // Look for undefined and NaN values in numerical fieds.
  73. function checkFinite( geom ) {
  74. var isNotFinite = _.any( geom.vertices, function ( v ) {
  75. return ! ( _.isFinite( v.x ) || _.isFinite( v.y ) || _.isFinite( v.z ) );
  76. });
  77. // TODO Buffers, normal, etc.
  78. QUnit.assert.ok( isNotFinite === false, "contains non-finite coordinates" );
  79. }
  80. // Run common geometry tests.
  81. function runStdGeometryTests( assert, geometries ) {
  82. _.each( geometries, function( geom ) {
  83. checkFinite( geom );
  84. // Clone
  85. checkGeometryClone( geom );
  86. // json round trip
  87. checkGeometryJsonRoundtrip( geom );
  88. });
  89. }