Skip to content

Commit

Permalink
double circular ref test failing, not aggressive enough
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Feb 18, 2011
1 parent 36df874 commit d190897
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions test/circular.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var assert = require('assert');
var Traverse = require('traverse');
var util = require('util');

exports.circular = function (assert) {
var obj = { x : 3 };
obj.y = obj;
var foundY = false;
Traverse(obj).forEach(function (x) {
if (this.path.join('') == 'y') {
assert.equal(
util.inspect(this.circular.node),
util.inspect(obj)
);
foundY = true;
}
});
assert.ok(foundY);
};

exports.deepCirc = function () {
var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] };
obj.y[2] = obj;

var times = 0;
Traverse(obj).forEach(function (x) {
if (this.circular) {
assert.eql(this.circular.path, []);
assert.eql(this.path, [ 'y', 2 ]);
times ++;
}
});

assert.eql(times, 1);
};

exports.doubleCirc = function () {
var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] };
obj.y[2] = obj;
obj.x.push(obj.y);
console.dir(obj);

var circs = [];
Traverse(obj).forEach(function (x) {
if (this.circular) {
circs.push({ circ : this.circular, self : this, node : x });
}
});

assert.eql(circs[0].self.path, [ 'x', 3 ]);
assert.eql(circs[0].circ.path, [ 'y' ]);

assert.eql(circs[1].self.path, [ 'y', 2 ]);
assert.eql(circs[1].circ.path, []);

assert.eql(circs.length, 2);
};

0 comments on commit d190897

Please sign in to comment.