Skip to content

Commit 1a635e0

Browse files
committed
initial address.unit.js
1 parent 0bdf6cb commit 1a635e0

8 files changed

+129
-23
lines changed

.jshintignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
lib/definitions.js
22
lib/version.js
3+
lib/helpers.js

Makefile

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ BUSTER = ./node_modules/.bin/buster test
88
main: lint test
99

1010
cover:
11-
$(ISTANBUL) cover test/run.js
11+
$(ISTANBUL) cover test/run.js --root ./lib -- -T unit,functional
1212

1313
check-coverage:
1414
$(ISTANBUL) check-coverage $(COVERAGE_OPTS)
1515

1616
test: cover check-coverage
1717

18-
1918
test-cov: cover check-coverage
2019
open coverage/lcov-report/index.html
2120

lib/address.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var Helpers = require('./helpers');
2+
var random = require('./random');
23
var definitions = require('../lib/definitions');
34

45
exports.zipCode = function () {
@@ -10,31 +11,31 @@ exports.zipCodeFormat = function (format) {
1011
};
1112

1213
exports.city = function () {
13-
switch (Helpers.randomNumber(3)) {
14+
switch (random.number(3)) {
1415
case 0:
15-
return Helpers.randomize(definitions.city_prefix()) + " " + Helpers.randomize(definitions.first_name()) + Helpers.randomize(definitions.city_suffix());
16+
return random.city_prefix() + " " + random.first_name() + random.city_suffix();
1617
case 1:
17-
return Helpers.randomize(definitions.city_prefix()) + " " + Helpers.randomize(definitions.first_name());
18+
return random.city_prefix() + " " + random.first_name();
1819
case 2:
19-
return Helpers.randomize(definitions.first_name()) + Helpers.randomize(definitions.city_suffix());
20+
return random.first_name() + random.city_suffix();
2021
case 3:
21-
return Helpers.randomize(definitions.last_name()) + Helpers.randomize(definitions.city_suffix());
22+
return random.last_name() + random.city_suffix();
2223
}
2324
};
2425

2526
exports.streetName = function () {
26-
switch (Helpers.randomNumber(1)) {
27+
switch (random.number(1)) {
2728
case 0:
28-
return Helpers.randomize(definitions.last_name()) + " " + Helpers.randomize(definitions.street_suffix());
29+
return random.last_name() + " " + random.street_suffix();
2930
case 1:
30-
return Helpers.randomize(definitions.first_name()) + " " + Helpers.randomize(definitions.street_suffix());
31+
return random.first_name() + " " + random.street_suffix();
3132
}
3233
};
3334

3435
exports.streetAddress = function (i) {
3536
if (typeof i == 'undefined') { i = false; }
3637
var address = "";
37-
switch (Helpers.randomNumber(2)) {
38+
switch (random.number(2)) {
3839
case 0:
3940
address = Helpers.replaceSymbolWithNumber("#####") + " " + this.streetName();
4041
break;

lib/definitions.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/helpers.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ exports.randomize = function (array) {
1414
// parses string for a symbol and replace it with a random number from 1-10
1515
exports.replaceSymbolWithNumber = function (string, symbol) {
1616
// default symbol is '#'
17-
if (typeof symbol == 'undefined') {
17+
if (symbol === undefined) {
1818
symbol = '#';
1919
}
2020

2121
var str = '';
2222
for (var i = 0; i < string.length; i++) {
2323
if (string[i] == symbol) {
2424
str += Math.floor(Math.random() * 10);
25-
}
26-
else {
25+
} else {
2726
str += string[i];
2827
}
2928
}
@@ -32,9 +31,7 @@ exports.replaceSymbolWithNumber = function (string, symbol) {
3231

3332
// takes an array and returns it randomized
3433
exports.shuffle = function (o) {
35-
for (var j, x, i = o.length; i; j = parseInt(Math.random() * i, 10), x = o[--i], o[i] = o[j], o[j] = x) {
36-
return null; // dummy for lint. This one-liner is a bad way to write a function.
37-
}
34+
for (var j, x, i = o.length; i; j = parseInt(Math.random() * i, 10), x = o[--i], o[i] = o[j], o[j] = x);
3835
return o;
3936
};
4037

lib/lorem.js

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var definitions = require('../lib/definitions');
44
exports.words = function (num) {
55
if (typeof num == 'undefined') { num = 3; }
66
return Helpers.shuffle(definitions.lorem()).slice(0, num);
7-
//Words.shuffle[0, num]
87
};
98

109
exports.sentence = function (wordCount) {

lib/random.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ var random = {
1717
* Generate methods that return random elements from definition arrays.
1818
*/
1919
var method_names = [
20+
// Address
21+
'city_prefix',
22+
'city_suffix',
23+
'street_suffix',
24+
2025
// Name
2126
'first_name',
2227
'last_name',
@@ -32,11 +37,10 @@ var method_names = [
3237
'bs_noun'
3338
];
3439

35-
for (var i = 0; i < method_names.length; i++) {
36-
var method_name = method_names[i];
40+
method_names.forEach(function (method_name) {
3741
random[method_name] = function () {
3842
return random.array_rand(definitions[method_name]());
3943
};
40-
}
44+
});
4145

4246
module.exports = random;

test/address.unit.js

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
var assert = require('assert');
2+
var sinon = require('sinon');
3+
var Faker = require('../index');
4+
var random = require('../lib/random');
5+
6+
describe("name.js", function () {
7+
describe("city()", function () {
8+
beforeEach(function () {
9+
sinon.spy(random, 'city_prefix');
10+
sinon.spy(random, 'first_name');
11+
sinon.spy(random, 'last_name');
12+
sinon.spy(random, 'city_suffix');
13+
});
14+
15+
afterEach(function () {
16+
random.number.restore();
17+
random.city_prefix.restore();
18+
random.first_name.restore();
19+
random.last_name.restore();
20+
random.city_suffix.restore();
21+
});
22+
23+
it("occasionally returns prefix + first name + suffix", function () {
24+
sinon.stub(random, 'number').returns(0);
25+
26+
var city = Faker.Address.city();
27+
assert.ok(city);
28+
29+
assert.ok(random.city_prefix.calledOnce);
30+
assert.ok(random.first_name.calledOnce);
31+
assert.ok(random.city_suffix.calledOnce);
32+
});
33+
34+
it("occasionally returns prefix + first name", function () {
35+
sinon.stub(random, 'number').returns(1);
36+
37+
var city = Faker.Address.city();
38+
assert.ok(city);
39+
40+
assert.ok(random.city_prefix.calledOnce);
41+
assert.ok(random.first_name.calledOnce);
42+
assert.ok(!random.city_suffix.called);
43+
});
44+
45+
it("occasionally returns first name + suffix", function () {
46+
sinon.stub(random, 'number').returns(2);
47+
48+
var city = Faker.Address.city();
49+
assert.ok(city);
50+
51+
assert.ok(!random.city_prefix.called);
52+
assert.ok(random.first_name.calledOnce);
53+
assert.ok(random.city_suffix.calledOnce);
54+
});
55+
56+
it("occasionally returns last name + suffix", function () {
57+
sinon.stub(random, 'number').returns(3);
58+
59+
var city = Faker.Address.city();
60+
assert.ok(city);
61+
62+
assert.ok(!random.city_prefix.called);
63+
assert.ok(!random.first_name.called);
64+
assert.ok(random.last_name.calledOnce);
65+
assert.ok(random.city_suffix.calledOnce);
66+
});
67+
});
68+
69+
describe("streetName()", function () {
70+
beforeEach(function () {
71+
sinon.spy(random, 'first_name');
72+
sinon.spy(random, 'last_name');
73+
sinon.spy(random, 'street_suffix');
74+
});
75+
76+
afterEach(function () {
77+
random.number.restore();
78+
random.first_name.restore();
79+
random.last_name.restore();
80+
random.street_suffix.restore();
81+
});
82+
83+
it("occasionally returns last name + suffix", function () {
84+
sinon.stub(random, 'number').returns(0);
85+
86+
var street_name = Faker.Address.streetName();
87+
assert.ok(street_name);
88+
89+
assert.ok(!random.first_name.called);
90+
assert.ok(random.last_name.calledOnce);
91+
assert.ok(random.street_suffix.calledOnce);
92+
});
93+
94+
it("occasionally returns first name + suffix", function () {
95+
sinon.stub(random, 'number').returns(1);
96+
97+
var street_name = Faker.Address.streetName();
98+
assert.ok(street_name);
99+
100+
assert.ok(random.first_name.calledOnce);
101+
assert.ok(!random.last_name.called);
102+
assert.ok(random.street_suffix.calledOnce);
103+
});
104+
});
105+
});

0 commit comments

Comments
 (0)