From be54187b963d527348f6142f04e7d84c54af6ef2 Mon Sep 17 00:00:00 2001 From: cyanos3 Date: Tue, 5 Nov 2013 07:09:59 -0700 Subject: Add tree: 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__" --- test/tree.unit.js | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 test/tree.unit.js (limited to 'test/tree.unit.js') diff --git a/test/tree.unit.js b/test/tree.unit.js new file mode 100644 index 00000000..dc6c9530 --- /dev/null +++ b/test/tree.unit.js @@ -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(); + }); + + }); + +}); -- cgit v1.2.3 From 5aa4062dcabc2fdac1c93df09e71d702ec7ec81e Mon Sep 17 00:00:00 2001 From: cyanos3 Date: Tue, 5 Nov 2013 17:44:10 -0700 Subject: Tree: let the width parameter also be a function that returns an int --- test/tree.unit.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'test/tree.unit.js') diff --git a/test/tree.unit.js b/test/tree.unit.js index dc6c9530..3af144eb 100644 --- a/test/tree.unit.js +++ b/test/tree.unit.js @@ -91,6 +91,20 @@ describe("tree.js", function () { Faker.random.first_name.restore(); }); - }); + it("can accept a function for the width", function () { + var widthFuncCalled = 0; + var widthFunc = function () { + widthFuncCalled = widthFuncCalled + 1; + return 2; + }; + + var tree = Faker.Tree.createTree(2, widthFunc, proto); + assert.equal(widthFuncCalled, 3); + + Faker.random.first_name.restore(); -}); + }); + + + }); +}); \ No newline at end of file -- cgit v1.2.3 From c8f71cd072b74c66693cae927bdaf9a97ee15c1f Mon Sep 17 00:00:00 2001 From: cyanos3 Date: Tue, 5 Nov 2013 20:10:31 -0700 Subject: My take on separating male and female names. Tests passing, Coverage restored. --- test/tree.unit.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'test/tree.unit.js') diff --git a/test/tree.unit.js b/test/tree.unit.js index 3af144eb..666a16c9 100644 --- a/test/tree.unit.js +++ b/test/tree.unit.js @@ -101,10 +101,8 @@ describe("tree.js", function () { var tree = Faker.Tree.createTree(2, widthFunc, proto); assert.equal(widthFuncCalled, 3); - Faker.random.first_name.restore(); }); - }); -}); \ No newline at end of file +}); -- cgit v1.2.3