Skip to content

Commit

Permalink
100% test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanDonovan committed Jan 7, 2013
1 parent d3299b4 commit 0c84125
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 43 deletions.
4 changes: 2 additions & 2 deletions lib/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var definitions = require('../lib/definitions');

var address = {
zipCode: function () {
return Helpers.replaceSymbolWithNumber(Helpers.randomize(["#####", '#####-####']));
return Helpers.replaceSymbolWithNumber(Faker.random.array_rand(["#####", '#####-####']));
},

zipCodeFormat: function (format) {
Expand Down Expand Up @@ -54,7 +54,7 @@ var address = {
},

secondaryAddress: function () {
return Helpers.replaceSymbolWithNumber(Helpers.randomize(
return Helpers.replaceSymbolWithNumber(Faker.random.array_rand(
[
'Apt. ###',
'Suite ###'
Expand Down
12 changes: 2 additions & 10 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
var Faker = require('../index');

// temporary backword-compatibility
exports.randomNumber = function (range) {
return Faker.random.number(range);
};

// temporary backword-compatibility
exports.randomize = function (array) {
return Faker.random.array_rand(array);
};

// parses string for a symbol and replace it with a random number from 1-10
exports.replaceSymbolWithNumber = function (string, symbol) {
// default symbol is '#'
Expand Down Expand Up @@ -102,8 +92,10 @@ exports.userCard = function () {
};


/*
String.prototype.capitalize = function () { //v1.0
return this.replace(/\w+/g, function (a) {
return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
});
};
*/
59 changes: 31 additions & 28 deletions lib/internet.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
var Helpers = require('./helpers');
var definitions = require('./definitions');
var Faker = require('../index');

exports.email = function () {
return this.userName() + "@" + this.domainName();
};
var internet = {
email: function () {
return this.userName() + "@" + this.domainName();
},

exports.userName = function () {
switch (Helpers.randomNumber(2)) {
case 0:
return Helpers.randomize(definitions.first_name());
case 1:
return Helpers.randomize(definitions.first_name()) + Helpers.randomize([".", "_"]) + Helpers.randomize(definitions.last_name());
}
};
userName: function () {
switch (Faker.random.number(2)) {
case 0:
return Faker.random.first_name();
case 1:
return Faker.random.first_name() + Faker.random.array_rand([".", "_"]) + Faker.random.last_name();
}
},

exports.domainName = function () {
return this.domainWord() + "." + Helpers.randomize(definitions.domain_suffix());
};
domainName: function () {
return this.domainWord() + "." + Faker.random.domain_suffix();
},

exports.domainWord = function () {
return Helpers.randomize(definitions.first_name()).toLowerCase();
};
domainWord: function () {
return Faker.random.first_name().toLowerCase();
},

exports.ip = function () {
var randNum = function () {
return (Math.random() * 254 + 1).toFixed(0);
};
ip: function () {
var randNum = function () {
return (Math.random() * 254 + 1).toFixed(0);
};

var result = [];
for (var i = 0; i < 4; i++) {
result[i] = randNum();
}
var result = [];
for (var i = 0; i < 4; i++) {
result[i] = randNum();
}

return result.join(".");
return result.join(".");
}
};

module.exports = internet;
5 changes: 5 additions & 0 deletions lib/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ var random = {

phone_formats: function () {
return this.array_rand(definitions.phone_formats());
},

domain_suffix: function () {
return this.array_rand(definitions.domain_suffix());
}

};

module.exports = random;
6 changes: 3 additions & 3 deletions test/company.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ var random = require('../lib/random');
describe("company.js", function () {
describe("companyName()", function () {
it("lets you specify the type of name to return", function () {
sinon.spy(Faker.Helpers, 'randomNumber');
sinon.spy(Faker.random, 'number');
var name = Faker.Company.companyName(1);

assert.ok(name.match(/-/));

assert.ok(!Faker.Helpers.randomNumber.called);
Faker.Helpers.randomNumber.restore();
assert.ok(!Faker.random.number.called);
Faker.random.number.restore();
});

it("sometimes returns three last names", function () {
Expand Down
35 changes: 35 additions & 0 deletions test/helpers.unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var assert = require('assert');
var sinon = require('sinon');
var Faker = require('../index');

describe("helpers.js", function() {
describe("replaceSymbolWithNumber()", function() {
context("when no symbol passed in", function() {
it("uses '#' by default", function() {
var num = Faker.Helpers.replaceSymbolWithNumber('#AB');
assert.ok(num.match(/\dAB/));
});
});

context("when symbol passed in", function() {
it("replaces that symbol with integers", function() {
var num = Faker.Helpers.replaceSymbolWithNumber('#AB', 'A');
assert.ok(num.match(/#\dB/));
});
});
});

describe("createCard()", function() {
it("returns an object", function() {
var card = Faker.Helpers.createCard();
assert.ok(typeof card === 'object');
});
});

describe("userCard()", function() {
it("returns an object", function() {
var card = Faker.Helpers.userCard();
assert.ok(typeof card === 'object');
});
});
});
83 changes: 83 additions & 0 deletions test/internet.unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
var assert = require('assert');
var sinon = require('sinon');
var Faker = require('../index');

describe("internet.js", function () {
describe("email()", function () {
it("returns a userName@domainName", function () {
sinon.stub(Faker.Internet, 'userName').returns('foo');
sinon.stub(Faker.Internet, 'domainName').returns('bar.com');
var email = Faker.Internet.email();

assert.equal(email, '[email protected]');

Faker.Internet.userName.restore();
Faker.Internet.domainName.restore();
});
});

describe("userName()", function () {
it("occasionally returns a single firstName", function () {
sinon.stub(Faker.random, 'number').returns(0);
sinon.spy(Faker.random, 'first_name');
var username = Faker.Internet.userName();

assert.ok(username);
assert.ok(Faker.random.first_name.called);

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

it("occasionally returns a firstName with a period or hyphen and a lastName", function () {
sinon.stub(Faker.random, 'number').returns(1);
sinon.spy(Faker.random, 'first_name');
sinon.spy(Faker.random, 'last_name');
sinon.spy(Faker.random, 'array_rand');
var username = Faker.Internet.userName();

assert.ok(username);
assert.ok(Faker.random.first_name.called);
assert.ok(Faker.random.last_name.called);
assert.ok(Faker.random.array_rand.calledWith(['.', '_']));

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

describe("domainName()", function() {
it("returns a domainWord plus a random suffix", function() {
sinon.stub(Faker.Internet, 'domainWord').returns('bar');
sinon.stub(Faker.random, 'domain_suffix').returns('net');

var domain_name = Faker.Internet.domainName();

assert.equal(domain_name, 'bar.net');

Faker.Internet.domainWord.restore();
Faker.random.domain_suffix.restore();
});
});

describe("domainWord()", function() {
it("returns a lower-case firstName", function() {
sinon.stub(Faker.random, 'first_name').returns('FOO');
var domain_word = Faker.Internet.domainWord();

assert.ok(domain_word);
assert.strictEqual(domain_word, 'foo');

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

describe("ip()", function() {
it("returns a random IP address with four parts", function() {
var ip = Faker.Internet.ip();
var parts = ip.split('.');
assert.equal(parts.length, 4);
});
});
});

0 comments on commit 0c84125

Please sign in to comment.