-
Notifications
You must be signed in to change notification settings - Fork 779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prop equal against circular structures #776
Changes from 11 commits
a32095d
13b67ca
798bd66
03e63e0
33bdee1
dd19af0
dfec366
397002e
858367c
c714dae
8f01e8a
c3224c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,25 @@ | |
// Author: Philippe Rathé <[email protected]> | ||
QUnit.equiv = (function() { | ||
|
||
/** | ||
* Gets the keys of an object which are both enumerable and own | ||
* | ||
* @param {Object} obj | ||
* @return {Array} Array with the enumerable and own keys of the object given | ||
*/ | ||
function ownObjectKeys( obj ) { | ||
var i, | ||
keys = []; | ||
|
||
for ( i in obj ) { | ||
if ( hasOwn.call( obj, i ) ) { | ||
keys.push( i ); | ||
} | ||
} | ||
|
||
return keys; | ||
} | ||
|
||
// Call the o related callback with the given arguments. | ||
function bindCallbacks( o, callbacks, args ) { | ||
var prop = QUnit.objectType( o ); | ||
|
@@ -17,6 +36,12 @@ QUnit.equiv = (function() { | |
// the real equiv function | ||
var innerEquiv, | ||
|
||
// the function that performs deep equivalence checks | ||
deepEquiv, | ||
|
||
// the function that performs property equivalence checks | ||
propEquiv, | ||
|
||
// stack to decide between skip/abort functions | ||
callers = [], | ||
|
||
|
@@ -116,7 +141,7 @@ QUnit.equiv = (function() { | |
} | ||
} | ||
} | ||
if ( !loop && !innerEquiv( a[ i ], b[ i ] ) ) { | ||
if ( !loop && !deepEquiv( a[ i ], b[ i ] ) ) { | ||
parents.pop(); | ||
parentsB.pop(); | ||
return false; | ||
|
@@ -171,7 +196,7 @@ QUnit.equiv = (function() { | |
} | ||
} | ||
aProperties.push( i ); | ||
if ( !loop && !innerEquiv( a[ i ], b[ i ] ) ) { | ||
if ( !loop && !deepEquiv( a[ i ], b[ i ] ) ) { | ||
eq = false; | ||
break; | ||
} | ||
|
@@ -186,12 +211,11 @@ QUnit.equiv = (function() { | |
} | ||
|
||
// Ensures identical properties name | ||
return eq && innerEquiv( aProperties.sort(), bProperties.sort() ); | ||
return eq && deepEquiv( aProperties.sort(), bProperties.sort() ); | ||
} | ||
}; | ||
}()); | ||
|
||
innerEquiv = function() { // can take multiple arguments | ||
deepEquiv = function ( ) { // can take multiple arguments | ||
var args = [].slice.apply( arguments ); | ||
if ( args.length < 2 ) { | ||
return true; // end transition | ||
|
@@ -212,8 +236,75 @@ QUnit.equiv = (function() { | |
|
||
// apply transition with (1..n) arguments | ||
}( args[ 0 ], args[ 1 ] ) ) && | ||
innerEquiv.apply( this, args.splice( 1, args.length - 1 ) ) ); | ||
}; | ||
deepEquiv.apply( this, args.splice( 1, args.length - 1 ) ) ); | ||
}, | ||
propEquiv = function ( ) { | ||
var args = [].slice.apply( arguments ); | ||
if ( args.length < 2 ) { | ||
return true; | ||
} | ||
|
||
return ( (function ( a, b ) { | ||
if ( !QUnit.is( "object", a ) || !QUnit.is( "object", b ) ) { | ||
// if the test author submits parameters that are not objects, check for deepEquiv | ||
return deepEquiv( a, b ); | ||
} else { | ||
/*jshint forin:false */ | ||
var i, j, loop, aCircular, bCircular, currentProperty, subEquiv, | ||
// Default to true | ||
eq = true, | ||
aProperties = ownObjectKeys( a ), | ||
bProperties = ownObjectKeys( b ); | ||
|
||
// stack constructor before traversing properties | ||
callers.push( a.constructor ); | ||
|
||
// track reference to avoid circular references | ||
parents.push( a ); | ||
parentsB.push( b ); | ||
|
||
for ( i = 0; i < aProperties.length; i++ ){ | ||
currentProperty = aProperties[i]; | ||
loop = false; | ||
for ( j = 0; j < parents.length; j++ ) { | ||
|
||
aCircular = parents[j] === a[currentProperty]; | ||
bCircular = parentsB[j] === b[currentProperty]; | ||
|
||
if ( aCircular || bCircular ){ | ||
if ( a[ currentProperty ] === b[ currentProperty ] || | ||
aCircular && bCircular ){ | ||
loop = true; | ||
} else { | ||
eq = false; | ||
break; | ||
} | ||
} | ||
|
||
} | ||
|
||
// we'll only use prop equivalence to compare objects | ||
subEquiv = QUnit.is( "object", a[ currentProperty ] ) ? | ||
propEquiv : deepEquiv; | ||
|
||
if ( !loop && !subEquiv( a[ currentProperty ], b[ currentProperty ] ) ) { | ||
eq = false; | ||
break; | ||
} | ||
} | ||
parents.pop(); | ||
parentsB.pop(); | ||
callers.pop(); // unstack, we are done | ||
|
||
// ensure both objects have the same properties | ||
return eq && deepEquiv(aProperties.sort(), bProperties.sort()); | ||
} | ||
}( args[ 0 ], args[ 1 ] ) ) && | ||
propEquiv.apply(this, args.splice( 1, args.length - 1 ) ) ); | ||
|
||
}, | ||
innerEquiv = deepEquiv; // default equivalence check | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might as well drop There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure that would totally make sense. How could Does that make any sense? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sticking with |
||
innerEquiv.props = propEquiv; | ||
|
||
return innerEquiv; | ||
}()); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
QUnit.module( "equiv" ); | ||
QUnit.module( "deepEquiv" ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's revert this - there is no exported method called "deepEquiv". |
||
|
||
QUnit.test( "Primitive types and constants", function( assert ) { | ||
assert.equal( QUnit.equiv( null, null ), true, "null" ); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not add more exceptions. We should fix any issues in new files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added test/propEqual.js to the exceptions because the tests in both files are almost the same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, but we shouldn't duplicate crappy code.