diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/date.unit.js | 65 | ||||
| -rw-r--r-- | test/image.unit.js | 14 | ||||
| -rw-r--r-- | test/internet.unit.js | 7 | ||||
| -rw-r--r-- | test/name.unit.js | 28 | ||||
| -rw-r--r-- | test/tree.unit.js | 108 |
5 files changed, 215 insertions, 7 deletions
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); + }); + }); +}); 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')); }) }); }); 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}$/)); + }); + }); }); 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 new file mode 100644 index 00000000..666a16c9 --- /dev/null +++ b/test/tree.unit.js @@ -0,0 +1,108 @@ +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(); + }); + + 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); + + + }); + + }); +}); |
