Skip to content

Commit

Permalink
refactoring to use random.js
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanDonovan committed Jan 6, 2013
1 parent 0b3cfbe commit 71884c8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 45 deletions.
13 changes: 6 additions & 7 deletions lib/name.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
var Helpers = require('./helpers');
var definitions = require('./definitions');
var random = require('./random');

var _name = {
firstName: function () {
return Helpers.randomize(definitions.first_name());
return random.first_name();
},

lastName: function () {
return Helpers.randomize(definitions.last_name());
return random.last_name();
},

findName: function () {
var r = Helpers.randomNumber(8);
var r = random.number(8);
switch (r) {
case 0:
return Helpers.randomize(definitions.name_prefix()) + " " + _name.firstName() + " " + _name.lastName();
return random.name_prefix() + " " + _name.firstName() + " " + _name.lastName();
case 1:
return _name.firstName() + " " + _name.lastName() + " " + Helpers.randomize(definitions.name_suffix);
return _name.firstName() + " " + _name.lastName() + " " + random.name_suffix();
}

return _name.firstName() + " " + _name.lastName();
Expand Down
14 changes: 11 additions & 3 deletions lib/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ var definitions = require('./definitions');
var random = {
// returns a single random number based on a range
number: function (range) {
var r = Math.floor(Math.random() * range);
console.log(r);
return r;
return Math.floor(Math.random() * range);
},

// takes an array and returns the array randomly sorted
Expand All @@ -15,7 +13,17 @@ var random = {
}
};

/**
* Generate methods that return random elements from definition arrays.
*/
var method_names = [
// Name
'first_name',
'last_name',
'name_prefix',
'name_suffix',

// Company
'catch_phrase_adjective',
'catch_phrase_descriptor',
'catch_phrase_noun',
Expand Down
20 changes: 10 additions & 10 deletions test/company.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,44 @@ describe("company.js", function () {
});

it("sometimes returns three last names", function () {
sinon.spy(Faker.definitions, 'last_name');
sinon.spy(random, 'last_name');
sinon.stub(random, 'number').returns(2);
var name = Faker.Company.companyName();
var parts = name.split(' ');

assert.strictEqual(parts.length, 4); // account for word 'and'
assert.ok(Faker.definitions.last_name.calledThrice);
assert.ok(random.last_name.calledThrice);

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

it("sometimes returns two last names separated by a hyphen", function () {
sinon.spy(Faker.definitions, 'last_name');
sinon.spy(random, 'last_name');
sinon.stub(random, 'number').returns(1);
var name = Faker.Company.companyName();
var parts = name.split('-');

assert.strictEqual(parts.length, 2);
assert.ok(Faker.definitions.last_name.calledTwice);
assert.ok(parts.length >= 2);
assert.ok(random.last_name.calledTwice);

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

it("sometimes returns a last name with a company suffix", function () {
sinon.spy(Faker.Company, 'companySuffix');
sinon.spy(Faker.definitions, 'last_name');
sinon.spy(random, 'last_name');
sinon.stub(random, 'number').returns(0);
var name = Faker.Company.companyName();
var parts = name.split(' ');

assert.ok(parts.length >= 2);
assert.ok(Faker.definitions.last_name.calledOnce);
assert.ok(random.last_name.calledOnce);
assert.ok(Faker.Company.companySuffix.calledOnce);

random.number.restore();
Faker.definitions.last_name.restore();
random.last_name.restore();
Faker.Company.companySuffix.restore();
});
});
Expand Down
46 changes: 21 additions & 25 deletions test/name.unit.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,62 @@
var assert = require('assert');
var sinon = require('sinon');
var Faker = require('../index');
var random = require('../lib/random');

describe("name.js", function () {
describe("firstName()", function () {
it("returns a random name from definitions", function (done) {
sinon.stub(Faker.definitions, 'first_name').returns(['foo']);
sinon.spy(Faker.Helpers, 'randomize');

it("returns a random name", function () {
sinon.stub(random, 'first_name').returns('foo');
var first_name = Faker.Name.firstName();

assert.ok(Faker.Helpers.randomize.calledWith(['foo']));
assert.equal(first_name, 'foo');

Faker.definitions.first_name.restore();
Faker.Helpers.randomize.restore();

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

describe("lastName()", function () {
it("returns a random name from definitions", function (done) {
sinon.stub(Faker.definitions, 'last_name').returns(['foo']);
sinon.spy(Faker.Helpers, 'randomize');
it("returns a random name", function () {
sinon.stub(random, 'last_name').returns('foo');

var last_name = Faker.Name.lastName();

assert.ok(Faker.Helpers.randomize.calledWith(['foo']));
assert.equal(last_name, 'foo');

Faker.definitions.last_name.restore();
Faker.Helpers.randomize.restore();

done();
random.last_name.restore();
});
});

describe("findName()", function () {
it("usually returns a first name and last name", function () {
sinon.stub(Faker.Helpers, 'randomNumber').returns(5);
sinon.stub(random, 'number').returns(5);
var name = Faker.Name.findName();
assert.ok(name);
var parts = name.split(' ');

assert.strictEqual(parts.length, 2);
Faker.Helpers.randomNumber.restore();

random.number.restore();
});

it("occasionally returns a first name and last name with a prefix", function () {
sinon.stub(Faker.Helpers, 'randomNumber').returns(0);
sinon.stub(random, 'number').returns(0);
var name = Faker.Name.findName();
var parts = name.split(' ');
assert.strictEqual(parts.length, 3);
Faker.Helpers.randomNumber.restore();

assert.ok(parts.length >= 3);

random.number.restore();
});

it("occasionally returns a first name and last name with a suffix", function () {
sinon.stub(Faker.Helpers, 'randomNumber').returns(1);
sinon.stub(random, 'number').returns(1);
var name = Faker.Name.findName();
var parts = name.split(' ');
assert.strictEqual(parts.length, 3);
Faker.Helpers.randomNumber.restore();

assert.ok(parts.length >= 3);

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

0 comments on commit 71884c8

Please sign in to comment.