diff options
| author | Ronen Babayoff <[email protected]> | 2015-08-23 21:57:46 -0400 |
|---|---|---|
| committer | Ronen Babayoff <[email protected]> | 2015-08-23 21:57:46 -0400 |
| commit | 66996e280c9fbbbc2e7db376549f568be32ad5cd (patch) | |
| tree | e93f412c4bceeb7ed0376e26113c9d31522af8b4 /test | |
| parent | cf0bd70d5fca9c0169414f5d2c16ca32431a3fd9 (diff) | |
| parent | d8f8108ac5dbec7e2b7ea9a23dd19aa42255e3fb (diff) | |
| download | faker-66996e280c9fbbbc2e7db376549f568be32ad5cd.tar.xz faker-66996e280c9fbbbc2e7db376549f568be32ad5cd.zip | |
Merge v3.0.1 into practicalmeteor:faker package branch
Diffstat (limited to 'test')
| -rw-r--r-- | test/address.unit.js | 62 | ||||
| -rw-r--r-- | test/all.functional.js | 4 | ||||
| -rw-r--r-- | test/commerce.unit.js | 115 | ||||
| -rw-r--r-- | test/company.unit.js | 12 | ||||
| -rw-r--r-- | test/date.unit.js | 85 | ||||
| -rw-r--r-- | test/helpers.unit.js | 18 | ||||
| -rw-r--r-- | test/internet.unit.js | 50 | ||||
| -rw-r--r-- | test/lorem.unit.js | 4 | ||||
| -rw-r--r-- | test/name.unit.js | 33 | ||||
| -rw-r--r-- | test/random.unit.js | 48 |
10 files changed, 401 insertions, 30 deletions
diff --git a/test/address.unit.js b/test/address.unit.js index b03a3be4..b5df1d24 100644 --- a/test/address.unit.js +++ b/test/address.unit.js @@ -63,7 +63,7 @@ describe("address.js", function () { assert.ok(faker.address.citySuffix.calledOnce); }); }); - + describe("streetName()", function () { beforeEach(function () { @@ -73,7 +73,6 @@ describe("address.js", function () { }); afterEach(function () { - faker.random.number.restore(); faker.name.firstName.restore(); faker.name.lastName.restore(); faker.address.streetSuffix.restore(); @@ -87,6 +86,8 @@ describe("address.js", function () { assert.ok(!faker.name.firstName.called); assert.ok(faker.name.lastName.calledOnce); assert.ok(faker.address.streetSuffix.calledOnce); + + faker.random.number.restore(); }); it("occasionally returns first name + suffix", function () { @@ -98,10 +99,20 @@ describe("address.js", function () { assert.ok(faker.name.firstName.calledOnce); assert.ok(!faker.name.lastName.called); assert.ok(faker.address.streetSuffix.calledOnce); + + faker.random.number.restore(); + }); + + it("trims trailing whitespace from the name", function() { + faker.address.streetSuffix.restore(); + + sinon.stub(faker.address, 'streetSuffix').returns("") + var street_name = faker.address.streetName(); + assert.ok(!street_name.match(/ $/)); }); }); - - + + describe("streetAddress()", function () { beforeEach(function () { @@ -157,11 +168,11 @@ describe("address.js", function () { }); }); }); - + describe("secondaryAddress()", function () { it("randomly chooses an Apt or Suite number", function () { - sinon.spy(faker.random, 'array_element'); + sinon.spy(faker.random, 'arrayElement'); var address = faker.address.secondaryAddress(); @@ -171,8 +182,8 @@ describe("address.js", function () { ]; assert.ok(address); - assert.ok(faker.random.array_element.calledWith(expected_array)); - faker.random.array_element.restore(); + assert.ok(faker.random.arrayElement.calledWith(expected_array)); + faker.random.arrayElement.restore(); }); }); @@ -196,6 +207,16 @@ describe("address.js", function () { }); }); + describe("countryCode()", function () { + it("returns random countryCode", function () { + sinon.spy(faker.address, 'countryCode'); + var countryCode = faker.address.countryCode(); + assert.ok(countryCode); + assert.ok(faker.address.countryCode.called); + faker.address.countryCode.restore(); + }); + }); + describe("state()", function () { it("returns random state", function () { sinon.spy(faker.address, 'state'); @@ -206,6 +227,31 @@ describe("address.js", function () { }); }); + describe("zipCode()", function () { + it("returns random zipCode", function () { + sinon.spy(faker.address, 'zipCode'); + var zipCode = faker.address.zipCode(); + assert.ok(zipCode); + assert.ok(faker.address.zipCode.called); + faker.address.zipCode.restore(); + }); + + it("returns random zipCode - user specified format", function () { + var zipCode = faker.address.zipCode("?#? #?#"); + assert.ok(zipCode.match(/^[A-Za-z]\d[A-Za-z]\s\d[A-Za-z]\d$/)); + // try another format + zipCode = faker.address.zipCode("###-###"); + assert.ok(zipCode.match(/^\d{3}-\d{3}$/)); + }); + + it("returns zipCode with proper locale format", function () { + // we'll use the en_CA locale.. + faker.locale = "en_CA"; + var zipCode = faker.address.zipCode(); + assert.ok(zipCode.match(/^[A-Za-z]\d[A-Za-z]\s?\d[A-Za-z]\d$/)); + }); + }); + describe("latitude()", function () { it("returns random latitude", function () { for (var i = 0; i < 100; i++) { diff --git a/test/all.functional.js b/test/all.functional.js index 1a2e836a..605aad2a 100644 --- a/test/all.functional.js +++ b/test/all.functional.js @@ -18,11 +18,13 @@ var modules = { lorem: ['words', 'sentence', 'sentences', 'paragraph', 'paragraphs'], - name: ['firstName', 'lastName', 'findName'], + name: ['firstName', 'lastName', 'findName', 'jobTitle'], phone: ['phoneNumber'], finance: ['account', 'accountName', 'mask', 'amount', 'transactionType', 'currencyCode', 'currencyName', 'currencySymbol'] + +// commerce: ['color', 'department', 'productName', 'price'] }; describe("functional tests", function () { diff --git a/test/commerce.unit.js b/test/commerce.unit.js new file mode 100644 index 00000000..06445e34 --- /dev/null +++ b/test/commerce.unit.js @@ -0,0 +1,115 @@ +if (typeof module !== 'undefined') { + var assert = require('assert'), + sinon = require('sinon'), + faker = require('../index'); +} + +describe("commerce.js", function() { + + describe("color()", function() { + it("returns random value from commerce.color array", function() { + var color = faker.commerce.color(); + assert.ok(faker.definitions.commerce.color.indexOf(color) !== -1); + }); + }); + + describe("department(max, fixedValue)", function() { + + it("should use the default amounts when not passing arguments", function() { + var department = faker.commerce.department(); + assert.ok(department.split(" ").length === 1); + }); + + /* + + it("should return only one value if we specify a maximum of one", function() { + sinon.spy(faker.random, 'arrayElement'); + + var department = faker.commerce.department(1); + + assert.strictEqual(department.split(" ").length, 1); + assert.ok(faker.random.arrayElement.calledOnce); + + faker.random.arrayElement.restore(); + }); + + it("should return the maxiumum value if we specify the fixed value", function() { + sinon.spy(faker.random, 'arrayElement'); + + var department = faker.commerce.department(5, true); + + console.log(department); + + // account for the separator + assert.strictEqual(department.split(" ").length, 6); + // Sometimes it will generate duplicates that aren't used in the final string, + // so we check if arrayElement has been called exactly or more than 5 times + assert.ok(faker.random.arrayElement.callCount >= 5); + + faker.random.arrayElement.restore(); + }); + */ + }); + + describe("productName()", function() { + it("returns name comprising of an adjective, material and product", function() { + sinon.spy(faker.random, 'arrayElement'); + sinon.spy(faker.commerce, 'productAdjective'); + sinon.spy(faker.commerce, 'productMaterial'); + sinon.spy(faker.commerce, 'product'); + var name = faker.commerce.productName(); + + assert.ok(name.split(' ').length >= 3); + assert.ok(faker.random.arrayElement.calledThrice); + assert.ok(faker.commerce.productAdjective.calledOnce); + assert.ok(faker.commerce.productMaterial.calledOnce); + assert.ok(faker.commerce.product.calledOnce); + + faker.random.arrayElement.restore(); + faker.commerce.productAdjective.restore(); + faker.commerce.productMaterial.restore(); + faker.commerce.product.restore(); + }); + }); + + describe("price(min, max, dec, symbol", function() { + it("should use the default amounts when not passing arguments", function() { + var price = faker.commerce.price(); + + assert.ok(price); + assert.equal((price > 0), true, "the amount should be greater than 0"); + assert.equal((price < 1001), true, "the amount should be less than 1000"); + }); + + it("should use the default decimal location when not passing arguments", function() { + var price = faker.commerce.price(); + + var decimal = "."; + var expected = price.length - 3; + var actual = price.indexOf(decimal); + + assert.equal(actual, expected, "The expected location of the decimal is " + expected + " but it was " + actual + " amount " + price); + }); + + it("should not include a currency symbol by default", function () { + + var amount = faker.commerce.price(); + + var regexp = new RegExp(/[0-9.]/); + + var expected = true; + var actual = regexp.test(amount); + + assert.equal(actual, expected, 'The expected match should not include a currency symbol'); + }); + + it("it should handle negative amounts, but return 0", function () { + + var amount = faker.commerce.price(-200, -1); + + assert.ok(amount); + assert.equal((amount == 0.00), true, "the amount should equal 0"); + }); + }); + +});
\ No newline at end of file diff --git a/test/company.unit.js b/test/company.unit.js index c324a4ba..f863d83d 100644 --- a/test/company.unit.js +++ b/test/company.unit.js @@ -59,19 +59,19 @@ describe("company.js", function () { describe("catchPhrase()", function () { it("returns phrase comprising of a catch phrase adjective, descriptor, and noun", function () { - sinon.spy(faker.random, 'array_element'); + sinon.spy(faker.random, 'arrayElement'); sinon.spy(faker.company, 'catchPhraseAdjective'); sinon.spy(faker.company, 'catchPhraseDescriptor'); sinon.spy(faker.company, 'catchPhraseNoun'); var phrase = faker.company.catchPhrase(); assert.ok(phrase.split(' ').length >= 3); - assert.ok(faker.random.array_element.calledThrice); + assert.ok(faker.random.arrayElement.calledThrice); assert.ok(faker.company.catchPhraseAdjective.calledOnce); assert.ok(faker.company.catchPhraseDescriptor.calledOnce); assert.ok(faker.company.catchPhraseNoun.calledOnce); - faker.random.array_element.restore(); + faker.random.arrayElement.restore(); faker.company.catchPhraseAdjective.restore(); faker.company.catchPhraseDescriptor.restore(); faker.company.catchPhraseNoun.restore(); @@ -80,19 +80,19 @@ describe("company.js", function () { describe("bs()", function () { it("returns phrase comprising of a BS adjective, buzz, and noun", function () { - sinon.spy(faker.random, 'array_element'); + sinon.spy(faker.random, 'arrayElement'); sinon.spy(faker.company, 'bsAdjective'); sinon.spy(faker.company, 'bsBuzz'); sinon.spy(faker.company, 'bsNoun'); var bs = faker.company.bs(); assert.ok(typeof bs === 'string'); - assert.ok(faker.random.array_element.calledThrice); + assert.ok(faker.random.arrayElement.calledThrice); assert.ok(faker.company.bsAdjective.calledOnce); assert.ok(faker.company.bsBuzz.calledOnce); assert.ok(faker.company.bsNoun.calledOnce); - faker.random.array_element.restore(); + faker.random.arrayElement.restore(); faker.company.bsAdjective.restore(); faker.company.bsBuzz.restore(); faker.company.bsNoun.restore(); diff --git a/test/date.unit.js b/test/date.unit.js index cc0cddc7..ffbc3201 100644 --- a/test/date.unit.js +++ b/test/date.unit.js @@ -78,4 +78,89 @@ describe("date.js", function () { assert.ok(date > from && date < to); }); }); + + describe("month()", function () { + it("returns random value from date.month.wide array by default", function () { + var month = faker.date.month(); + assert.ok(faker.definitions.date.month.wide.indexOf(month) !== -1); + }); + + it("returns random value from date.month.wide_context array for context option", function () { + var month = faker.date.month({ context: true }); + assert.ok(faker.definitions.date.month.wide_context.indexOf(month) !== -1); + }); + + it("returns random value from date.month.abbr array for abbr option", function () { + var month = faker.date.month({ abbr: true }); + assert.ok(faker.definitions.date.month.abbr.indexOf(month) !== -1); + }); + + it("returns random value from date.month.abbr_context array for abbr and context option", function () { + var month = faker.date.month({ abbr: true, context: true }); + assert.ok(faker.definitions.date.month.abbr_context.indexOf(month) !== -1); + }); + + it("returns random value from date.month.wide array for context option when date.month.wide_context array is missing", function () { + var backup_wide_context = faker.definitions.date.month.wide_context; + faker.definitions.date.month.wide_context = undefined; + + var month = faker.date.month({ context: true }); + assert.ok(faker.definitions.date.month.wide.indexOf(month) !== -1); + + faker.definitions.date.month.wide_context = backup_wide_context; + }); + + it("returns random value from date.month.abbr array for abbr and context option when date.month.abbr_context array is missing", function () { + var backup_abbr_context = faker.definitions.date.month.abbr_context; + faker.definitions.date.month.abbr_context = undefined; + + var month = faker.date.month({ abbr: true, context: true }); + assert.ok(faker.definitions.date.month.abbr.indexOf(month) !== -1); + + faker.definitions.date.month.abbr_context = backup_abbr_context; + }); + }); + + describe("weekday()", function () { + it("returns random value from date.weekday.wide array by default", function () { + var weekday = faker.date.weekday(); + assert.ok(faker.definitions.date.weekday.wide.indexOf(weekday) !== -1); + }); + + it("returns random value from date.weekday.wide_context array for context option", function () { + var weekday = faker.date.weekday({ context: true }); + assert.ok(faker.definitions.date.weekday.wide_context.indexOf(weekday) !== -1); + }); + + it("returns random value from date.weekday.abbr array for abbr option", function () { + var weekday = faker.date.weekday({ abbr: true }); + assert.ok(faker.definitions.date.weekday.abbr.indexOf(weekday) !== -1); + }); + + it("returns random value from date.weekday.abbr_context array for abbr and context option", function () { + var weekday = faker.date.weekday({ abbr: true, context: true }); + assert.ok(faker.definitions.date.weekday.abbr_context.indexOf(weekday) !== -1); + }); + + it("returns random value from date.weekday.wide array for context option when date.weekday.wide_context array is missing", function () { + var backup_wide_context = faker.definitions.date.weekday.wide_context; + faker.definitions.date.weekday.wide_context = undefined; + + var weekday = faker.date.weekday({ context: true }); + assert.ok(faker.definitions.date.weekday.wide.indexOf(weekday) !== -1); + + faker.definitions.date.weekday.wide_context = backup_wide_context; + }); + + it("returns random value from date.weekday.abbr array for abbr and context option when date.weekday.abbr_context array is missing", function () { + var backup_abbr_context = faker.definitions.date.weekday.abbr_context; + faker.definitions.date.weekday.abbr_context = undefined; + + var weekday = faker.date.weekday({ abbr: true, context: true }); + assert.ok(faker.definitions.date.weekday.abbr.indexOf(weekday) !== -1); + + faker.definitions.date.weekday.abbr_context = backup_abbr_context; + }); + }); + }); diff --git a/test/helpers.unit.js b/test/helpers.unit.js index 425167e2..fa2f4a90 100644 --- a/test/helpers.unit.js +++ b/test/helpers.unit.js @@ -21,6 +21,16 @@ describe("helpers.js", function () { }); }); + describe("shuffle()", function () { + it("the output is the same length as the input", function () { + sinon.spy(faker.random, 'number'); + var shuffled = faker.helpers.shuffle(["a", "b"]); + assert.ok(shuffled.length === 2); + assert.ok(faker.random.number.calledWith(1)); + faker.random.number.restore(); + }); + }); + describe("slugify()", function () { it("removes unwanted characters from URI string", function () { assert.equal(faker.helpers.slugify("Aiden.HarÂȘann"), "Aiden.Harann"); @@ -43,14 +53,6 @@ describe("helpers.js", function () { }); // Make sure we keep this function for backward-compatibility. - describe("randomNumber()", function () { - it("returns an integer", function () { - var num = faker.helpers.randomNumber(); - assert.ok(typeof num === 'number'); - }); - }); - - // Make sure we keep this function for backward-compatibility. describe("randomize()", function () { it("returns a random element from an array", function () { var arr = ['a', 'b', 'c']; diff --git a/test/internet.unit.js b/test/internet.unit.js index 2fc0db10..9218a56e 100644 --- a/test/internet.unit.js +++ b/test/internet.unit.js @@ -33,17 +33,18 @@ describe("internet.js", function () { sinon.stub(faker.random, 'number').returns(1); sinon.spy(faker.name, 'firstName'); sinon.spy(faker.name, 'lastName'); - sinon.spy(faker.random, 'array_element'); + sinon.spy(faker.random, 'arrayElement'); var username = faker.internet.userName(); assert.ok(username); assert.ok(faker.name.firstName.called); assert.ok(faker.name.lastName.called); - assert.ok(faker.random.array_element.calledWith(['.', '_'])); + assert.ok(faker.random.arrayElement.calledWith(['.', '_'])); faker.random.number.restore(); faker.name.firstName.restore(); faker.name.lastName.restore(); + faker.random.arrayElement.restore(); }); }); @@ -73,6 +74,44 @@ describe("internet.js", function () { }); }); + describe('protocol()', function () { + it('returns a valid protocol', function () { + var protocol = faker.internet.protocol(); + assert.ok(protocol); + }); + + it('should occasionally return http', function () { + sinon.stub(faker.random, 'number').returns(0); + var protocol = faker.internet.protocol(); + assert.ok(protocol); + assert.strictEqual(protocol, 'http'); + + faker.random.number.restore(); + }); + + it('should occasionally return https', function () { + sinon.stub(faker.random, 'number').returns(1); + var protocol = faker.internet.protocol(); + assert.ok(protocol); + assert.strictEqual(protocol, 'https'); + + faker.random.number.restore(); + }); + }); + + describe('url()', function () { + it('returns a valid url', function () { + sinon.stub(faker.internet,'protocol').returns('http'); + sinon.stub(faker.internet, 'domainWord').returns('bar'); + sinon.stub(faker.internet, 'domainSuffix').returns('net'); + + var url = faker.internet.url(); + + assert.ok(url); + assert.strictEqual(url,'http://bar.net'); + }); + }); + describe("ip()", function () { it("returns a random IP address with four parts", function () { var ip = faker.internet.ip(); @@ -94,4 +133,11 @@ describe("internet.js", function () { assert.ok(color.match(/^#[a-f0-9]{6}$/)); }); }); + + describe("mac()", function () { + it("returns a random MAC address with 6 hexadecimal digits", function () { + var mac = faker.internet.mac(); + assert.ok(mac.match(/^([a-f0-9]{2}:){5}[a-f0-9]{2}$/)); + }); + }); }); diff --git a/test/lorem.unit.js b/test/lorem.unit.js index 3b554d47..4fecb4ba 100644 --- a/test/lorem.unit.js +++ b/test/lorem.unit.js @@ -158,7 +158,7 @@ describe("lorem.js", function () { var paragraphs = faker.lorem.paragraphs(); assert.ok(typeof paragraphs === 'string'); - var parts = paragraphs.split('\n \r\t'); + var parts = paragraphs.split('\n \r'); assert.equal(parts.length, 3); assert.ok(faker.lorem.paragraph.calledThrice); @@ -172,7 +172,7 @@ describe("lorem.js", function () { var paragraphs = faker.lorem.paragraphs(5); assert.ok(typeof paragraphs === 'string'); - var parts = paragraphs.split('\n \r\t'); + var parts = paragraphs.split('\n \r'); assert.equal(parts.length, 5); faker.lorem.paragraph.restore(); diff --git a/test/name.unit.js b/test/name.unit.js index b34cbd1b..e1d2e871 100644 --- a/test/name.unit.js +++ b/test/name.unit.js @@ -67,4 +67,37 @@ describe("name.js", function () { var name = faker.name.findName(); }); }); + + describe("title()", function () { + it("returns a random title", function () { + sinon.stub(faker.name, 'title').returns('Lead Solutions Supervisor'); + + var title = faker.name.title(); + + assert.equal(title, 'Lead Solutions Supervisor'); + + faker.name.title.restore(); + }); + }); + + describe("jobTitle()", function () { + it("returns a job title consisting of a descriptor, area, and type", function () { + sinon.spy(faker.random, 'arrayElement'); + sinon.spy(faker.name, 'jobDescriptor'); + sinon.spy(faker.name, 'jobArea'); + sinon.spy(faker.name, 'jobType'); + var jobTitle = faker.name.jobTitle(); + + assert.ok(typeof jobTitle === 'string'); + assert.ok(faker.random.arrayElement.calledThrice); + assert.ok(faker.name.jobDescriptor.calledOnce); + assert.ok(faker.name.jobArea.calledOnce); + assert.ok(faker.name.jobType.calledOnce); + + faker.random.arrayElement.restore(); + faker.name.jobDescriptor.restore(); + faker.name.jobArea.restore(); + faker.name.jobType.restore(); + }); + }); }); diff --git a/test/random.unit.js b/test/random.unit.js index 6071767b..8f047ee5 100644 --- a/test/random.unit.js +++ b/test/random.unit.js @@ -14,10 +14,19 @@ describe("random.js", function () { assert.ok(faker.random.number(max) <= max); }); - it("returns a random number given a maximum value as Object", function() { var options = { max: 10 }; - assert.ok(faker.random.number(options) < options.max); + assert.ok(faker.random.number(options) <= options.max); + }); + + it("returns a random number given a maximum value of 0", function() { + var options = { max: 0 }; + assert.ok(faker.random.number(options) === 0); + }); + + it("returns a random number given a negative number minimum and maximum value of 0", function() { + var options = { min: -100, max: 0 }; + assert.ok(faker.random.number(options) <= options.max); }); it("returns a random number between a range", function() { @@ -56,9 +65,42 @@ describe("random.js", function () { }; faker.random.number(opts); - + assert.equal(opts.min, min); assert.equal(opts.max, max); }); + + it('should return deterministic results when seeded', function() { + faker.seed(100); + var name = faker.name.findName(); + assert.equal(name, 'Dulce Jenkins'); + }) + }); + + describe('arrayElement', function() { + it('returns a random element in the array', function() { + var testArray = ['hello', 'to', 'you', 'my', 'friend']; + assert.ok(testArray.indexOf(faker.random.arrayElement(testArray)) > -1); + }); + + it('returns a random element in the array when there is only 1', function() { + var testArray = ['hello']; + assert.ok(testArray.indexOf(faker.random.arrayElement(testArray)) > -1); + }); + }); + + describe('UUID', function() { + it('should generate a valid UUID', function() { + var UUID = faker.random.uuid(); + var RFC4122 = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/; + assert.ok(RFC4122.test(UUID)); + }) + }) + + describe('boolean', function() { + it('should generate a boolean value', function() { + var bool = faker.random.boolean(); + assert.ok(typeof bool == 'boolean'); + }); }); }); |
