Skip to content

Commit

Permalink
Add tree:
Browse files Browse the repository at this point in the history
createTree(depth, width, obj) where
- depth is the distance from the root to the farthest node
- width is the number of children to add to each node
- each property/value pair in obj contains a value that will be evaluated to get the final result. The property that contains the array of child nodes should have the value of "__RECURSE__"
  • Loading branch information
jayliu50 committed Nov 5, 2013
1 parent 1bd1d73 commit be54187
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ exports.Internet = require('./lib/internet');
exports.Company = require('./lib/company');
exports.Lorem = require('./lib/lorem');
exports.Helpers = require('./lib/helpers');
exports.Tree = require('./lib/tree');
exports.random = require('./lib/random');
exports.definitions = require('./lib/definitions');
67 changes: 67 additions & 0 deletions lib/tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var Faker = require('../index');

var clone = function clone(obj) {
if (obj == null || typeof(obj) != 'object')
return obj;

var temp = obj.constructor(); // changed

for (var key in obj) {
temp[key] = clone(obj[key]);
}
return temp;
};


var createTree = function (depth, width, obj) {

if (!obj) {
throw {
name: "ObjectError",
message: "there needs to be an object passed in",
toString: function () {
return this.name + ": " + this.message
}
};
}

if (width <= 0) {
throw {
name: "TreeParamError",
message: "width must be greater than zero",
toString: function () {
return this.name + ": " + this.message
}
};
}

var newObj = clone(obj);

for (var prop in newObj) {
if (newObj.hasOwnProperty(prop)) {
var value = null;
if (newObj[prop] !== "__RECURSE__") {
value = eval(newObj[prop]);
}
else {
if (depth !== 0) {
value = [];
for (var i = 0; i < width; i++) {
value.push(createTree(depth - 1, width, obj));
}
}
}

newObj[prop] = value;
}
}

return newObj;
};

var tree = {
createTree: createTree
};


module.exports = tree;
96 changes: 96 additions & 0 deletions test/tree.unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var Faker = require('../index');
}

describe("tree.js", function () {
describe("createTree()", function () {

var proto = {
"firstname": "Faker.random.first_name()",
"children": "__RECURSE__"
};

it("requires the width to be at least one", function () {
sinon.spy(Faker.Tree, 'createTree');

try {
Faker.Tree.createTree(0, 0, {});
}
catch (e) {
}

assert.ok(Faker.Tree.createTree.threw);

Faker.Tree.createTree.restore();
});

it("requires that the object passed in should not be null", function () {
sinon.spy(Faker.Tree, 'createTree');

try {
Faker.Tree.createTree(1, 1, null);
}
catch (e) {
}

assert.ok(Faker.Tree.createTree.threw);

Faker.Tree.createTree.restore();

});

it("can create a trivial tree with one node", function () {
sinon.spy(Faker.random, 'first_name');

var tree = Faker.Tree.createTree(0, 1, proto);

assert.ok(Faker.random.first_name.calledOnce);

assert.ok(tree.children == null);

Faker.random.first_name.restore();
});

it("can create a deep tree with one node at each level", function () {
sinon.spy(Faker.random, 'first_name');
var tree = Faker.Tree.createTree(2, 1, proto);

assert.ok(Faker.random.first_name.calledThrice);

assert.ok(tree.firstname);
assert.ok(tree.children[0].firstname);
assert.ok(tree.children[0].children[0].firstname);

Faker.random.first_name.restore();
});

it("can create a basic N-tree", function () {
var n = 3;
sinon.spy(Faker.random, 'first_name');
var tree = Faker.Tree.createTree(1, n, proto);

assert.ok(Faker.random.first_name.callCount == 4);

assert.ok(tree.firstname);
assert.ok(tree.children[0].firstname);
assert.ok(tree.children[1].firstname);
assert.ok(tree.children[2].firstname);

Faker.random.first_name.restore();
});

it("can create a full N-tree", function () {
var n = 3;
sinon.spy(Faker.random, 'first_name');
var tree = Faker.Tree.createTree(2, n, proto);

assert.ok(Faker.random.first_name.callCount == 13);

Faker.random.first_name.restore();
});

});

});

0 comments on commit be54187

Please sign in to comment.