package tests
{
public class ObjectComparator
{
public static function equals( obj1:*, obj2:* ) : Boolean
{
if( (obj1 == undefined || obj1 == null) &&
(obj2 == undefined || obj2 == null) )
return true;
if( typeof( obj1 ) == "Number" && typeof( obj2 ) == "Number" )
return obj1 == obj2
if( typeof( obj1 ) == "function" &&
typeof( obj2 ) == "function" &&
obj1.toString() == obj2.toString() )
return true;
if( typeof( obj1 ) != typeof( obj2 ) )
{
trace( "object types do not match." );
trace( "object1 type " + typeof( obj1 ) );
trace( "object2 type " + typeof( obj2 ) );
trace( "" );
return false;
}
if( typeof( obj1 ) == "Array" && typeof( obj2 ) == "Array" )
{
if( obj1.length != obj2.length )
{
trace( "object1 and object2 are arrays of different lengths" );
return false;
}
else
{
for( var i:int = 0; i < obj1.length; i++ )
if( !equals( obj1[ i ], obj2[ i ] ) )
{
trace( "elements at index " + i + " in the object1 and object2 arrays do not match" );
trace( "object1 value - " + obj1[ i ] );
trace( "object2 value - " + obj2[ i ] );
return false;
}
return true;
}
}
for( var name:String in obj1 )
{
if( obj2[ name ] == undefined && obj1[ name ] != undefined )
{
trace( "object2 does not have field from object1. field name " + name );
return false;
}
if( !( equals( obj1[ name ], obj2[ name ] ) ) )
{
trace( "values assigned to the field " + name + " do not match" );
trace( "value in object1 - " + obj1[ name ] );
trace( "value in object2 - " + obj2[ name ] );
trace( "" );
return false;
}
}
if( typeof( obj1 ) != "object" )
{
if( obj1 == obj2 )
return true;
else
{
trace( "object1 and object2 are non-equal object references" );
return false;
}
}
else
{
return true;
}
}
}
}