浏览代码

fist try of kicking out lodash

simonThiele 9 年之前
父节点
当前提交
065d3dc667
共有 3 个文件被更改,包括 104 次插入45 次删除
  1. 62 22
      test/unit/SmartComparer.js
  2. 41 20
      test/unit/qunit-utils.js
  3. 1 3
      test/unit/unittests_three.html

+ 62 - 22
test/unit/SmartComparer.js

@@ -9,9 +9,6 @@ function SmartComparer() {
 	// Diagnostic message, when comparison fails.
 	var message;
 
-	// Keys to skip during object comparison.
-	var omitKeys = [ 'id', 'uuid' ];
-
 	return {
 
 		areEqual: areEqual,
@@ -47,7 +44,7 @@ function SmartComparer() {
 		}
 
 		// Don't compare functions.
-		if ( _.isFunction( val1 ) && _.isFunction( val2 ) ) return true;
+		if ( isFunction( val1 ) && isFunction( val2 ) ) return true;
 
 		// Array comparison.
 		var arrCmp = compareArrays( val1, val2 );
@@ -68,13 +65,30 @@ function SmartComparer() {
 
 		// if (JSON.stringify( val1 ) == JSON.stringify( val2 ) ) return true;
 
-		// Continue with default comparison.
-		if ( _.isEqual( val1, val2 ) ) return true;
-
 		// Object differs (unknown reason).
 		return makeFail( 'Values differ', val1, val2 );
 	}
 
+	function isFunction(value) {
+
+    // The use of `Object#toString` avoids issues with the `typeof` operator
+    // in Safari 8 which returns 'object' for typed array constructors, and
+    // PhantomJS 1.9 which returns 'function' for `NodeList` instances.
+    var tag = isObject(value) ? Object.prototype.toString.call(value) : '';
+
+		return tag == '[object Function]' || tag == '[object GeneratorFunction]';
+
+  }
+
+	function isObject(value) {
+
+		// Avoid a V8 JIT bug in Chrome 19-20.
+    // See https://code.google.com/p/v8/issues/detail?id=2291 for more details.
+    var type = typeof value;
+
+		return !!value && (type == 'object' || type == 'function');
+
+  }
 
 	function compareArrays( val1, val2 ) {
 
@@ -107,8 +121,8 @@ function SmartComparer() {
 
 	function compareObjects( val1, val2 ) {
 
-		var isObj1 = _.isObject( val1 );
-		var isObj2 = _.isObject( val2 );
+		var isObj1 = isObject( val1 );
+		var isObj2 = isObject( val2 );
 
 		// Compare type.
 		if ( isObj1 !== isObj2 ) return makeFail( 'Values are not both objects' );
@@ -117,38 +131,64 @@ function SmartComparer() {
 		if ( !isObj1 ) return undefined;
 
 		// Compare keys.
-		var keys1 = _( val1 ).keys().difference( omitKeys ).value();
-		var keys2 = _( val2 ).keys().difference( omitKeys ).value();
+		var keys1 = Object.keys( val1 );
+		var keys2 = Object.keys( val2 );
 
-		var missingActual = _.difference( keys1, keys2 );
+		var keys1T = _( val1 ).keys().value();
+		var keys2T = _( val2 ).keys().value();
+		var missingActual = _.difference( keys1T, keys2T );
 		if ( missingActual.length !== 0 ) {
+			console.log(missingActual, keys1T, keys2T);
+		}
+
+		for ( var i = 0, l = keys1.length; i < l; i++ ) {
+
+			if (keys2.indexOf(keys1[ i ]) < 0) {
+
+				return makeFail( 'Property "' + keys1[ i ] + '" is unexpected.' );
 
-			return makeFail( 'Property "' + missingActual[0] + '" is unexpected.' );
+			}
 
 		}
 
-		var missingExpected = _.difference( keys2, keys1 );
-		if ( missingExpected.length !== 0 ) {
+		for ( var i = 0, l = keys2.length; i < l; i++ ) {
+
+			if (keys1.indexOf(keys2[ i ]) < 0) {
 
-			return makeFail( 'Property "' + missingExpected[0] + '" is missing.' );
+				return makeFail( 'Property "' + keys2[ i ] + '" is missing.' );
+
+			}
 
 		}
 
 		// Keys are the same. For each key, compare content until a difference is found.
-		var hadDifference = _.any( keys1, function ( key ) {
+		var hadDifference = false;
+
+		for ( var i = 0, l = keys1.length; i < l; i++ ) {
+
+			var key = keys1[ i ];
+
+			if (key === "uuid" || key === "id") {
 
-			var prop1 = val1[key];
-			var prop2 = val2[key];
+				continue;
+
+			}
+
+			var prop1 = val1[ key ];
+			var prop2 = val2[ key ];
 
 			// Compare property content.
 			var eq = areEqual( prop1, prop2 );
 
 			// In case of failure, an message should already be set.
 			// Add context to low level message.
-			if ( !eq ) addContext( 'property "' + key + '"' );
-			return !eq;
+			if ( !eq ) {
 
-		});
+				addContext( 'property "' + key + '"' );
+				hadDifference = true;
+
+			}
+		}
 
 		return ! hadDifference;
 

+ 41 - 20
test/unit/qunit-utils.js

@@ -72,22 +72,31 @@ function checkGeometryJsonWriting( geom, json ) {
 	QUnit.assert.equalKey( geom, json, 'uuid' );
 	QUnit.assert.equal( json.id, undefined, "should not persist id" );
 
+	var params = geom.parameters;
+	if ( !params ) {
+
+		return;
+
+	}
+
 	// All parameters from geometry should be persisted.
-	_.forOwn( geom.parameters, function ( val, key ) {
+	var keys = Object.keys( params );
+	for ( var i = 0, l = keys.length; i < l; i++ ) {
 
-		QUnit.assert.equalKey( geom.parameters, json, key );
+		QUnit.assert.equalKey( params, json, keys[ i ] );
 
-	});
+	}
 
 	// All parameters from json should be transfered to the geometry.
 	// json is flat. Ignore first level json properties that are not parameters.
 	var notParameters = [ "metadata", "uuid", "type" ];
-	_.forOwn( json, function ( val, key ) {
-
-		if ( notParameters.indexOf( key) === -1 ) QUnit.assert.equalKey( geom.parameters, json, key );
+	var keys = Object.keys( json );
+	for ( var i = 0, l = keys.length; i < l; i++ ) {
 
-	});
+		var key = keys[ i ];
+		if ( notParameters.indexOf( key) === -1 ) QUnit.assert.equalKey( params, json, key );
 
+	}
 }
 
 // Check parsing and reconstruction of json geometry
@@ -98,9 +107,8 @@ function checkGeometryJsonReading( json, geom ) {
 	var loader = new THREE.ObjectLoader();
 	var output = loader.parseGeometries( wrap );
 
-	QUnit.assert.ok( output[geom.uuid], 'geometry matching source uuid not in output' );
-	QUnit.assert.smartEqual( output[geom.uuid], geom, 'Reconstruct geometry from ObjectLoader' );
-
+	QUnit.assert.ok( output[ geom.uuid ], 'geometry matching source uuid not in output' );
+	QUnit.assert.smartEqual( output[ geom.uuid ], geom, 'Reconstruct geometry from ObjectLoader' );
 }
 
 // Verify geom -> json -> geom
@@ -115,22 +123,35 @@ function checkGeometryJsonRoundtrip( geom ) {
 // Look for undefined and NaN values in numerical fieds.
 function checkFinite( geom ) {
 
-	var isNotFinite = _.any( geom.vertices, function ( v ) {
+	var allVerticesAreFinite = true;
+
+	var vertices = geom.vertices || [];
+
+	for ( var i = 0, l = vertices.length; i < l; i++ ) {
 
-		return ! ( _.isFinite( v.x ) || _.isFinite( v.y ) || _.isFinite( v.z ) );
+		var v = geom.vertices[ i ];
 
-	});
+		if ( !( isFinite( v.x ) || isFinite( v.y ) || isFinite( v.z ) ) ) {
+
+			allVerticesAreFinite = false;
+			break;
+
+		}
+
+	}
 
 	// TODO Buffers, normal, etc.
 
-	QUnit.assert.ok( isNotFinite === false, "contains non-finite coordinates" );
+	QUnit.assert.ok( allVerticesAreFinite, "contains non-finite coordinates" );
 
 }
 
 // Run common geometry tests.
 function runStdGeometryTests( assert, geometries ) {
 
-	_.each( geometries, function( geom ) {
+	for ( var i = 0, l = geometries.length; i < l; i++ ) {
+
+		var geom = geometries[ i ];
 
 		checkFinite( geom );
 
@@ -140,7 +161,7 @@ function runStdGeometryTests( assert, geometries ) {
 		// json round trip
 		checkGeometryJsonRoundtrip( geom );
 
-	});
+	}
 
 }
 
@@ -154,15 +175,16 @@ function runStdGeometryTests( assert, geometries ) {
 // Run common light tests.
 function runStdLightTests( assert, lights ) {
 
-	_.each( lights, function( light ) {
+	for ( var i = 0, l = lights.length; i < l; i++ ) {
+
+		const light = lights[i];
 
 		// Clone
 		checkLightClone( light );
 
 		// json round trip
 		checkLightJsonRoundtrip( light );
-
-	});
+	}
 
 }
 
@@ -209,4 +231,3 @@ function checkLightJsonRoundtrip( light ) {
 	checkLightJsonReading( json, light );
 
 }
-

+ 1 - 3
test/unit/unittests_three.html

@@ -7,6 +7,7 @@
 </head>
 <body>
   <div id="qunit"></div>
+  <script src="../../node_modules/lodash/index.js"></script>
   <script src="qunit-1.18.0.js"></script>
   <script src="qunit-utils.js"></script>
   <script src="SmartComparer.js"></script>
@@ -27,9 +28,6 @@
   <script src="core/InterleavedBuffer.js"></script>
   <script src="core/InterleavedBufferAttribute.js"></script>
 
-  <script src="core/Raycaster.js"></script>
-  <script src="core/Face3.js"></script>
-  <script src="core/Geometry.js"></script>
   <script src="core/BufferAttribute.js"></script>
   <script src="core/BufferGeometry.js"></script>
   <script src="core/Clock.js"></script>