Skip to content

Commit

Permalink
mutability tests all pass
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Feb 18, 2011
1 parent 7a0969f commit 36df874
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions test/mutability.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
var assert = require('assert');
var Traverse = require('traverse');

exports.mutate = function () {
var obj = { a : 1, b : 2, c : [ 3, 4 ] };
var res = Traverse(obj).forEach(function (x) {
if (typeof x === 'number' && x % 2 === 0) {
this.update(x * 10);
}
});
assert.eql(obj, res);
assert.eql(obj, { a : 1, b : 20, c : [ 3, 40 ] });
};

exports.mutateT = function () {
var obj = { a : 1, b : 2, c : [ 3, 4 ] };
var res = Traverse.forEach(obj, function (x) {
if (typeof x === 'number' && x % 2 === 0) {
this.update(x * 10);
}
});
assert.eql(obj, res);
assert.eql(obj, { a : 1, b : 20, c : [ 3, 40 ] });
};

exports.map = function () {
var obj = { a : 1, b : 2, c : [ 3, 4 ] };
var res = Traverse(obj).map(function (x) {
if (typeof x === 'number' && x % 2 === 0) {
this.update(x * 10);
}
});
assert.eql(obj, { a : 1, b : 2, c : [ 3, 4 ] });
assert.eql(res, { a : 1, b : 20, c : [ 3, 40 ] });
};

exports.mapT = function () {
var obj = { a : 1, b : 2, c : [ 3, 4 ] };
var res = Traverse.map(obj, function (x) {
if (typeof x === 'number' && x % 2 === 0) {
this.update(x * 10);
}
});
assert.eql(obj, { a : 1, b : 2, c : [ 3, 4 ] });
assert.eql(res, { a : 1, b : 20, c : [ 3, 40 ] });
};

exports.clone = function () {
var obj = { a : 1, b : 2, c : [ 3, 4 ] };
var res = Traverse(obj).clone();
assert.eql(obj, res);
assert.ok(obj !== res);
obj.a ++;
assert.eql(res.a, 1);
obj.c.push(5);
assert.eql(res.c, [ 3, 4 ]);
};

exports.cloneT = function () {
var obj = { a : 1, b : 2, c : [ 3, 4 ] };
var res = Traverse.clone(obj);
assert.eql(obj, res);
assert.ok(obj !== res);
obj.a ++;
assert.eql(res.a, 1);
obj.c.push(5);
assert.eql(res.c, [ 3, 4 ]);
};

0 comments on commit 36df874

Please sign in to comment.