Here are a couple more tests that fail with array and mixed array/object (because of a stack overflow):
test('array with references to self wont loop', function(){
var circularA = [],
circularB = [];
circularA.push(circularA);
circularB.push(circularB);
equals(QUnit.equiv(circularA, circularB), true, "Should not repeat test on array (ambigous test)");
circularA.push( 'abc' );
circularB.push( 'abc' );
equals(QUnit.equiv(circularA, circularB), true, "Should not repeat test on array (ambigous test)");
circularA.push( 'hello' );
circularB.push( 'goodbye' );
equals(QUnit.equiv(circularA, circularB), false, "Should not repeat test on array (unambigous test)");
});
test('mixed object/array with references to self wont loop', function(){
var circularA = [{abc:null}],
circularB = [{abc:null}];
circularA[0].abc = circularA;
circularB[0].abc = circularB;
circularA.push(circularA);
circularB.push(circularB);
equals(QUnit.equiv(circularA, circularB), true, "Should not repeat test on object/array (ambigous test)");
circularA[0].def = 1;
circularB[0].def = 1;
equals(QUnit.equiv(circularA, circularB), true, "Should not repeat test on object/array (ambigous test)");
circularA[0].def = 1;
circularB[0].def = 0;
equals(QUnit.equiv(circularA, circularB), false, "Should not repeat test on object/array (unambigous test)");
});
And here is a patch:
// Determine what is o.
@@ -788,7 +789,7 @@ QUnit.equiv = function () {
},
"array": function (b, a) {
- var i;
+ var i, j, loop;
var len;
// b could be an object literal here
@@ -800,16 +801,26 @@ QUnit.equiv = function () {
if (len !== b.length) { // safe and faster
return false;
}
+
+ parents.push(a);
for (i = 0; i < len; i++) {
- if ( ! innerEquiv(a[i], b[i])) {
+ loop = false;
+ for(j=0;j<parents.length;j++){
+ if(parents[j] === a[i]){
+ loop = true;//dont rewalk array
+ }
+ }
+ if (!loop && ! innerEquiv(a[i], b[i])) {
+ parents.pop();
return false;
}
}
+ parents.pop();
return true;
},