From 28d5fb64e44237e21c01904d6e46b34626d66d33 Mon Sep 17 00:00:00 2001 From: Stephen Sugden Date: Sun, 17 Jun 2012 17:41:59 -0700 Subject: [PATCH] Add test for replacing objects with strings and vice-versa --- test/mutability.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/mutability.js b/test/mutability.js index 2236f56..dbea80c 100644 --- a/test/mutability.js +++ b/test/mutability.js @@ -250,3 +250,28 @@ exports.deleteMapRedux = function () { res, { a : 1, c : [ 3 ,, 5 ] } )); }; + +exports.objectToString = function () { + var obj = { a : 1, b : 2, c : [ 3, 4 ] }; + var res = Traverse(obj).forEach(function (x) { + if (typeof x === 'object' && !this.isRoot) { + this.update(JSON.stringify(x)); + } + }); + assert.deepEqual(obj, res); + assert.deepEqual(obj, { a : 1, b : 2, c : "[3,4]" }); +}; + +exports.stringToObject = function () { + var obj = { a : 1, b : 2, c : "[3,4]" }; + var res = Traverse(obj).forEach(function (x) { + if (typeof x === 'string') { + this.update(JSON.parse(x)); + } + else if (typeof x === 'number' && x % 2 === 0) { + this.update(x * 10); + } + }); + assert.deepEqual(obj, res); + assert.deepEqual(obj, { a : 1, b : 20, c : [ 3, 40 ] }); +};