Skip to content

Commit

Permalink
refactoring so we do not need to use Helpers.randomize() everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanDonovan committed Jan 6, 2013
1 parent 8a6ad8d commit 0b3cfbe
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 50 deletions.
21 changes: 10 additions & 11 deletions lib/company.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
var Helpers = require('./helpers');
var Name = require('./name');
var definitions = require('../lib/definitions');
var random = require('../lib/random');

var company = {
suffixes: ["Inc", "and Sons", "LLC", "Group", "and Daughters"],

companyName: function (format) {
switch ((format ? format : Helpers.randomNumber(3))) {
switch ((format ? format : random.number(3))) {
case 0:
return Name.lastName() + " " + this.companySuffix();
case 1:
return Name.lastName() + "-" + Name.lastName();
case 2:
return Name.lastName() + "," + Name.lastName() + " and " + Name.lastName();
return Name.lastName() + ", " + Name.lastName() + " and " + Name.lastName();
}
},

companySuffix: function () {
return Helpers.randomize(this.suffixes);
return random.array_rand(this.suffixes);
},

catchPhrase: function () {
return Helpers.randomize(definitions.catch_phrase_adjective()) + " " +
Helpers.randomize(definitions.catch_phrase_descriptor()) + " " +
Helpers.randomize(definitions.catch_phrase_noun());
return random.catch_phrase_adjective() + " " +
random.catch_phrase_descriptor() + " " +
random.catch_phrase_noun();
},

bs: function () {
return Helpers.randomize(definitions.bs_adjective()) + " " +
Helpers.randomize(definitions.bs_buzz()) + " " +
Helpers.randomize(definitions.bs_noun());
return random.bs_adjective() + " " +
random.bs_buzz() + " " +
random.bs_noun();
}
};

Expand Down
11 changes: 5 additions & 6 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
var Faker = require('../Faker');
var random = require('./random');

// returns a single random number based on a range
// temporary backword-compatibility
exports.randomNumber = function (range) {
var r = Math.floor(Math.random() * range);
return r;
return random.number(range);
};

// takes an array and returns the array randomly sorted
// temporary backword-compatibility
exports.randomize = function (array) {
var r = Math.floor(Math.random() * array.length);
return array[r];
return random.array_rand(array);
};

// parses string for a symbol and replace it with a random number from 1-10
Expand Down
34 changes: 34 additions & 0 deletions lib/random.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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;
},

// takes an array and returns the array randomly sorted
array_rand: function (array) {
var r = Math.floor(Math.random() * array.length);
return array[r];
}
};

var method_names = [
'catch_phrase_adjective',
'catch_phrase_descriptor',
'catch_phrase_noun',
'bs_adjective',
'bs_buzz',
'bs_noun'
];

for (var i = 0; i < method_names.length; i++) {
var method_name = method_names[i];
random[method_name] = function () {
return random.array_rand(definitions[method_name]());
};
}

module.exports = random;
67 changes: 34 additions & 33 deletions test/company.unit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var assert = require('assert');
var sinon = require('sinon');
var Faker = require('../index');
var random = require('../lib/random');

describe("company.js", function () {
describe("companyName()", function () {
Expand All @@ -16,42 +17,42 @@ describe("company.js", function () {

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

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

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

it("sometimes returns two last names separated by a hyphen", function () {
sinon.spy(Faker.definitions, 'last_name');
sinon.stub(Faker.Helpers, 'randomNumber').returns(1);
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);

Faker.Helpers.randomNumber.restore();
random.number.restore();
Faker.definitions.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.stub(Faker.Helpers, 'randomNumber').returns(0);
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(Faker.Company.companySuffix.calledOnce);

Faker.Helpers.randomNumber.restore();
random.number.restore();
Faker.definitions.last_name.restore();
Faker.Company.companySuffix.restore();
});
Expand All @@ -66,43 +67,43 @@ describe("company.js", function () {

describe("catchPhrase()", function () {
it("returns phrase comprising of a catch phrase adjective, descriptor, and noun", function () {
sinon.spy(Faker.Helpers, 'randomize');
sinon.spy(Faker.definitions, 'catch_phrase_adjective');
sinon.spy(Faker.definitions, 'catch_phrase_descriptor');
sinon.spy(Faker.definitions, 'catch_phrase_noun');
sinon.spy(random, 'array_rand');
sinon.spy(random, 'catch_phrase_adjective');
sinon.spy(random, 'catch_phrase_descriptor');
sinon.spy(random, 'catch_phrase_noun');
var phrase = Faker.Company.catchPhrase();

assert.ok(phrase.split(' ').length >= 3);
assert.ok(Faker.Helpers.randomize.calledThrice);
assert.ok(Faker.definitions.catch_phrase_adjective.calledOnce);
assert.ok(Faker.definitions.catch_phrase_descriptor.calledOnce);
assert.ok(Faker.definitions.catch_phrase_noun.calledOnce);

Faker.Helpers.randomize.restore();
Faker.definitions.catch_phrase_adjective.restore();
Faker.definitions.catch_phrase_descriptor.restore();
Faker.definitions.catch_phrase_noun.restore();
assert.ok(random.array_rand.calledThrice);
assert.ok(random.catch_phrase_adjective.calledOnce);
assert.ok(random.catch_phrase_descriptor.calledOnce);
assert.ok(random.catch_phrase_noun.calledOnce);

random.array_rand.restore();
random.catch_phrase_adjective.restore();
random.catch_phrase_descriptor.restore();
random.catch_phrase_noun.restore();
});
});

describe("bs()", function () {
it("returns phrase comprising of a BS adjective, buzz, and noun", function () {
sinon.spy(Faker.Helpers, 'randomize');
sinon.spy(Faker.definitions, 'bs_adjective');
sinon.spy(Faker.definitions, 'bs_buzz');
sinon.spy(Faker.definitions, 'bs_noun');
sinon.spy(random, 'array_rand');
sinon.spy(random, 'bs_adjective');
sinon.spy(random, 'bs_buzz');
sinon.spy(random, 'bs_noun');
var bs = Faker.Company.bs();

assert.ok(typeof bs === 'string');
assert.ok(Faker.Helpers.randomize.calledThrice);
assert.ok(Faker.definitions.bs_adjective.calledOnce);
assert.ok(Faker.definitions.bs_buzz.calledOnce);
assert.ok(Faker.definitions.bs_noun.calledOnce);

Faker.Helpers.randomize.restore();
Faker.definitions.bs_adjective.restore();
Faker.definitions.bs_buzz.restore();
Faker.definitions.bs_noun.restore();
assert.ok(random.array_rand.calledThrice);
assert.ok(random.bs_adjective.calledOnce);
assert.ok(random.bs_buzz.calledOnce);
assert.ok(random.bs_noun.calledOnce);

random.array_rand.restore();
random.bs_adjective.restore();
random.bs_buzz.restore();
random.bs_noun.restore();
});
});
});

0 comments on commit 0b3cfbe

Please sign in to comment.