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') 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 86e3fee620f4182bd3966b2b3743cdd652d4bc0f Mon Sep 17 00:00:00 2001 From: cyanos3 Date: Tue, 5 Nov 2013 07:43:27 -0700 Subject: Add random color to Internet.js --- test/internet.unit.js | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'test') diff --git a/test/internet.unit.js b/test/internet.unit.js index d7d823cf..44879c37 100644 --- a/test/internet.unit.js +++ b/test/internet.unit.js @@ -82,4 +82,11 @@ describe("internet.js", function () { assert.equal(parts.length, 4); }); }); + + describe("color()", function () { + it("returns a valid hex value (like #ffffff)", function () { + var color = Faker.Internet.color(100, 100, 100); + assert.ok(color.match(/^#[a-f0-9]{6}$/)); + }); + }); }); -- 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') 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/name.unit.js | 28 ++++++++++++++++++++++++++++ test/tree.unit.js | 4 +--- 2 files changed, 29 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/name.unit.js b/test/name.unit.js index 84e0ff90..b3865b4e 100644 --- a/test/name.unit.js +++ b/test/name.unit.js @@ -2,6 +2,7 @@ if (typeof module !== 'undefined') { var assert = require('assert'); var sinon = require('sinon'); var Faker = require('../index'); + } describe("name.js", function () { @@ -16,6 +17,33 @@ describe("name.js", function () { }); }); + describe("firstNameMale()", function () { + it("returns a random male name", function () { + var first_name = Faker.Name.firstNameMale(); + console.log(first_name); + assert.ok(first_name); + }); + }); + + + describe("firstNameFemale()", function () { + it("returns a random female name", function () { + var first_name = Faker.Name.firstNameFemale(); + console.log(first_name); + assert.ok(first_name); + + }); + }); + + + describe("gender()", function () { + it("returns a random gender", function () { + var gender = Faker.Name.gender(); + assert.ok(gender === 'male' || gender === 'female'); + + }); + }); + describe("lastName()", function () { it("returns a random name", function () { sinon.stub(Faker.random, 'last_name').returns('foo'); 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 From ff1c1942e5e1d46f00d8c87664909f88bde94d34 Mon Sep 17 00:00:00 2001 From: cyanos3 Date: Tue, 5 Nov 2013 21:26:06 -0700 Subject: Add Date functionality: past, future, recent, between Includes build --- test/date.unit.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 test/date.unit.js (limited to 'test') diff --git a/test/date.unit.js b/test/date.unit.js new file mode 100644 index 00000000..83b73ca9 --- /dev/null +++ b/test/date.unit.js @@ -0,0 +1,65 @@ +if (typeof module !== 'undefined') { + var assert = require('assert'); + var sinon = require('sinon'); + var Faker = require('../index'); +} + +describe("date.js", function () { + describe("past()", function () { + it("returns a date N years into the past", function () { + + var date = Faker.Date.past(75); + assert.ok(Date.parse(date) < new Date()); + }); + + it("returns a date N years before the date given", function () { + + var refDate = new Date(2120, 11, 9, 10, 0, 0, 0); // set the date beyond the usual calculation (to make sure this is working correctly) + + var date = Date.parse(Faker.Date.past(75, refDate.toJSON())); + + assert.ok(date < refDate && date > new Date()); // date should be before date given but after the current time + }); + + }); + + describe("future()", function () { + it("returns a date N years into the future", function () { + + var date = Faker.Date.future(75); + + assert.ok(Date.parse(date) > new Date()); + }); + + it("returns a date N years after the date given", function () { + + var refDate = new Date(1880, 11, 9, 10, 0, 0, 0); // set the date beyond the usual calculation (to make sure this is working correctly) + + var date = Date.parse(Faker.Date.future(75, refDate.toJSON())); + + assert.ok(date > refDate && date < new Date()); // date should be after the date given, but before the current time + }); + }); + + describe("recent()", function () { + it("returns a date N days from the recent past", function () { + + var date = Faker.Date.recent(30); + + assert.ok(Date.parse(date) < new Date()); + }); + + }); + + describe("between()", function () { + it("returns a random date between the dates given", function () { + + var from = new Date(1990, 5, 7, 9, 11, 0, 0); + var to = new Date(2000, 6, 8, 10, 12, 0, 0); + + var date = Date.parse(Faker.Date.between(from, to)); + + assert.ok(date > from && date < to); + }); + }); +}); -- cgit v1.2.3 From e80be6e92d27e129d9504b60562069bd83ef9dee Mon Sep 17 00:00:00 2001 From: FotoVerite Date: Mon, 17 Feb 2014 21:40:33 -0500 Subject: Fixes so build runs. --- test/image.unit.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'test') diff --git a/test/image.unit.js b/test/image.unit.js index 046291ad..3d25fe9a 100644 --- a/test/image.unit.js +++ b/test/image.unit.js @@ -7,32 +7,32 @@ if (typeof module !== 'undefined') { describe("image.js", function () { describe("imageUrl()", function () { it("returns a random image url from lorempixel", function () { - var imageUrl = Faker.Image.LoremPixel.imageUrl(); + var imageUrl = Faker.Image.imageUrl(); assert.equal(imageUrl, 'http://lorempixel.com/640/480'); }); it("returns a random image url from lorempixel with width and height", function () { - var imageUrl = Faker.Image.LoremPixel.imageUrl(100, 100); + var imageUrl = Faker.Image.imageUrl(100, 100); assert.equal(imageUrl, 'http://lorempixel.com/100/100'); }); it("returns a random image url in an abstract category", function () { - var imageUrl = Faker.Image.LoremPixel.imageUrl(100, 100, 'abstract'); + var imageUrl = Faker.Image.imageUrl(100, 100, 'abstract'); assert.equal(imageUrl, 'http://lorempixel.com/100/100/abstract'); }); it("returns a random image url in a category via proxy methods", function () { - var nightlife = Faker.Image.LoremPixel.nightlife(); + var nightlife = Faker.Image.nightlife(); assert.equal(nightlife, 'http://lorempixel.com/640/480/nightlife'); - var city = Faker.Image.LoremPixel.city(); + var city = Faker.Image.city(); assert.equal(city, 'http://lorempixel.com/640/480/city'); - var fashion = Faker.Image.LoremPixel.fashion(); + var fashion = Faker.Image.fashion(); assert.equal(fashion, 'http://lorempixel.com/640/480/fashion'); }); it("return a random avatar from UIFaces", function () { - assert.notEqual(-1, Faker.Image.UIFaces.avatar().indexOf('s3.amazonaws.com/uifaces/faces')); + assert.notEqual(-1, Faker.Image.avatar().indexOf('s3.amazonaws.com/uifaces/faces')); }) }); }); -- cgit v1.2.3