diff options
| author | Marak <[email protected]> | 2021-02-11 01:24:16 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-02-11 01:24:16 -0500 |
| commit | 9c8b3249ffa8412d305bea0522d5754f88f000ae (patch) | |
| tree | 665bb0c281ea8a170e5c8da414aed1b80e75d86b /test | |
| parent | 00f18703af6bf46b0435f4cc9ed1838054a79c1c (diff) | |
| parent | 388b6cd8513b3b73daab5278e539f8f6bb600e12 (diff) | |
| download | faker-9c8b3249ffa8412d305bea0522d5754f88f000ae.tar.xz faker-9c8b3249ffa8412d305bea0522d5754f88f000ae.zip | |
Merge branch 'master' into master
Diffstat (limited to 'test')
| -rw-r--r-- | test/address.unit.js | 96 | ||||
| -rw-r--r-- | test/commerce.unit.js | 22 | ||||
| -rw-r--r-- | test/database.unit.js | 12 | ||||
| -rw-r--r-- | test/date.unit.js | 13 | ||||
| -rw-r--r-- | test/finance.unit.js | 79 | ||||
| -rw-r--r-- | test/helpers.unit.js | 23 | ||||
| -rw-r--r-- | test/image.unit.js | 127 | ||||
| -rw-r--r-- | test/internet.unit.js | 43 | ||||
| -rw-r--r-- | test/locales.unit.js | 2 | ||||
| -rw-r--r-- | test/lorem.unit.js | 44 | ||||
| -rw-r--r-- | test/music.unit.js | 17 | ||||
| -rw-r--r-- | test/name.unit.js | 86 | ||||
| -rw-r--r-- | test/random.unit.js | 36 | ||||
| -rw-r--r-- | test/system.unit.js | 8 | ||||
| -rw-r--r-- | test/time.unit.js | 30 | ||||
| -rw-r--r-- | test/unique.unit.js | 8 | ||||
| -rw-r--r-- | test/vehicle.unit.js | 20 |
17 files changed, 520 insertions, 146 deletions
diff --git a/test/address.unit.js b/test/address.unit.js index d63cb78c..5b56266c 100644 --- a/test/address.unit.js +++ b/test/address.unit.js @@ -115,6 +115,11 @@ describe("address.js", function () { describe("streetAddress()", function () { + + var errorExpectDigits = function(expected){ + return "The street number should be had " + expected + " digits" + } + beforeEach(function () { sinon.spy(faker.address, 'streetName'); sinon.spy(faker.address, 'secondaryAddress'); @@ -128,9 +133,10 @@ describe("address.js", function () { it("occasionally returns a 5-digit street number", function () { sinon.stub(faker.random, 'number').returns(0); var address = faker.address.streetAddress(); + var expected = 5 var parts = address.split(' '); - assert.equal(parts[0].length, 5); + assert.strictEqual(parts[0].length, expected, errorExpectDigits(expected)); assert.ok(faker.address.streetName.called); faker.random.number.restore(); @@ -140,8 +146,9 @@ describe("address.js", function () { sinon.stub(faker.random, 'number').returns(1); var address = faker.address.streetAddress(); var parts = address.split(' '); + var expected = 4 - assert.equal(parts[0].length, 4); + assert.strictEqual(parts[0].length, expected, errorExpectDigits(expected)); assert.ok(faker.address.streetName.called); faker.random.number.restore(); @@ -151,8 +158,9 @@ describe("address.js", function () { sinon.stub(faker.random, 'number').returns(2); var address = faker.address.streetAddress(); var parts = address.split(' '); + var expected = 3 - assert.equal(parts[0].length, 3); + assert.strictEqual(parts[0].length, expected, errorExpectDigits(expected)); assert.ok(faker.address.streetName.called); assert.ok(!faker.address.secondaryAddress.called); @@ -161,9 +169,8 @@ describe("address.js", function () { context("when useFulladdress is true", function () { it("adds a secondary address to the result", function () { - var address = faker.address.streetAddress(true); - var parts = address.split(' '); - + faker.address.streetAddress(true); + assert.ok(faker.address.secondaryAddress.called); }); }); @@ -208,6 +215,7 @@ describe("address.js", function () { }); describe("countryCode()", function () { + it("returns random countryCode", function () { sinon.spy(faker.address, 'countryCode'); var countryCode = faker.address.countryCode(); @@ -215,6 +223,16 @@ describe("address.js", function () { assert.ok(faker.address.countryCode.called); faker.address.countryCode.restore(); }); + + it("returns random alpha-3 countryCode", function () { + sinon.spy(faker.address, 'countryCode'); + var countryCode = faker.address.countryCode("alpha-3"); + assert.ok(countryCode); + assert.ok(faker.address.countryCode.called); + assert.strictEqual(countryCode.length, 3, "The countryCode should be had 3 characters"); + faker.address.countryCode.restore(); + }); + }); describe("state()", function () { @@ -271,7 +289,7 @@ describe("address.js", function () { it("returns undefined if state is invalid", function () { var state = "XX"; sinon.spy(faker.address, 'zipCode'); - var zipCode = faker.address.zipCodeByState(state); + faker.address.zipCodeByState(state); assert.ok(faker.address.zipCode.called); faker.address.zipCode.restore(); }); @@ -280,7 +298,7 @@ describe("address.js", function () { faker.locale = "zh_CN"; var state = "IL"; sinon.spy(faker.address, 'zipCode'); - var zipCode = faker.address.zipCodeByState(state); + faker.address.zipCodeByState(state); assert.ok(faker.address.zipCode.called); faker.address.zipCode.restore(); }); @@ -305,7 +323,7 @@ describe("address.js", function () { sinon.spy(faker.random, 'number'); var latitude = faker.address.latitude(-5, 5); assert.ok(typeof latitude === 'string'); - assert.equal(latitude.split('.')[1].length, 4); + assert.strictEqual(latitude.split('.')[1].length, 4, "The precision of latitude should be had of 4 digits"); var latitude_float = parseFloat(latitude); assert.ok(latitude_float >= -5); assert.ok(latitude_float <= 5); @@ -319,7 +337,7 @@ describe("address.js", function () { sinon.spy(faker.random, 'number'); var latitude = faker.address.latitude(undefined, undefined, 7); assert.ok(typeof latitude === 'string'); - assert.equal(latitude.split('.')[1].length, 7); + assert.strictEqual(latitude.split('.')[1].length, 7, "The precision of latitude should be had of 7 digits"); var latitude_float = parseFloat(latitude); assert.ok(latitude_float >= -180); assert.ok(latitude_float <= 180); @@ -348,7 +366,7 @@ describe("address.js", function () { sinon.spy(faker.random, 'number'); var longitude = faker.address.longitude(100, -30); assert.ok(typeof longitude === 'string'); - assert.equal(longitude.split('.')[1].length, 4); + assert.strictEqual(longitude.split('.')[1].length, 4, "The precision of longitude should be had of 4 digits"); var longitude_float = parseFloat(longitude); assert.ok(longitude_float >= -30); assert.ok(longitude_float <= 100); @@ -362,7 +380,7 @@ describe("address.js", function () { sinon.spy(faker.random, 'number'); var longitude = faker.address.longitude(undefined, undefined, 7); assert.ok(typeof longitude === 'string'); - assert.equal(longitude.split('.')[1].length, 7); + assert.strictEqual(longitude.split('.')[1].length, 7, "The precision of longitude should be had of 7 digits"); var longitude_float = parseFloat(longitude); assert.ok(longitude_float >= -180); assert.ok(longitude_float <= 180); @@ -376,28 +394,34 @@ describe("address.js", function () { it("returns random direction", function () { sinon.stub(faker.address, 'direction').returns('North'); var direction = faker.address.direction(); + var expected = 'North'; - assert.equal(direction, 'North'); + assert.strictEqual(direction, expected, "The random direction should be equals " + expected); faker.address.direction.restore(); }) it("returns abbreviation when useAbbr is false", function () { sinon.stub(faker.address, 'direction').returns('N'); var direction = faker.address.direction(false); - assert.equal(direction, 'N'); + var expected = 'N'; + assert.strictEqual(direction, expected, "The abbreviation of direction when useAbbr is false should be equals " + expected+ ". Current is " + direction); faker.address.direction.restore(); }) it("returns abbreviation when useAbbr is true", function () { var direction = faker.address.direction(true); - assert.equal(typeof direction, 'string'); - assert.equal(direction.length <= 2, true); + var expectedType = 'string'; + var lengthDirection = direction.length + var prefixErrorMessage = "The abbreviation of direction when useAbbr is true should" + assert.strictEqual(typeof direction, expectedType, prefixErrorMessage + " be typeof string. Current is" + typeof direction); + assert.strictEqual(lengthDirection <= 2, true, prefixErrorMessage + " have a length less or equals 2. Current is " + lengthDirection); }) it("returns abbreviation when useAbbr is true", function () { sinon.stub(faker.address, 'direction').returns('N'); var direction = faker.address.direction(true); - assert.equal(direction, 'N'); + var expected = 'N'; + assert.strictEqual(direction, expected, "The abbreviation of direction when useAbbr is true should be equals " + expected + ". Current is " + direction); faker.address.direction.restore(); }) @@ -407,23 +431,29 @@ describe("address.js", function () { it("returns random ordinal direction", function () { sinon.stub(faker.address, 'ordinalDirection').returns('West'); var ordinalDirection = faker.address.ordinalDirection(); + var expected = 'West'; - assert.equal(ordinalDirection, 'West'); + assert.strictEqual(ordinalDirection, expected, "The ransom ordinal direction should be equals " + expected + ". Current is " + ordinalDirection); faker.address.ordinalDirection.restore(); }) it("returns abbreviation when useAbbr is true", function () { sinon.stub(faker.address, 'ordinalDirection').returns('W'); var ordinalDirection = faker.address.ordinalDirection(true); + var expected = 'W'; - assert.equal(ordinalDirection, 'W'); + assert.strictEqual(ordinalDirection, expected, "The ordinal direction when useAbbr is true should be equals " + expected + ". Current is " + ordinalDirection); faker.address.ordinalDirection.restore(); }) it("returns abbreviation when useAbbr is true", function () { var ordinalDirection = faker.address.ordinalDirection(true); - assert.equal(typeof ordinalDirection, 'string'); - assert.equal(ordinalDirection.length <= 2, true); + var expectedType = 'string'; + var ordinalDirectionLength = ordinalDirection.length; + var prefixErrorMessage = "The ordinal direction when useAbbr is true should" + + assert.strictEqual(typeof ordinalDirection, expectedType, prefixErrorMessage + " be had typeof equals " + expectedType + ".Current is " + typeof ordinalDirection); + assert.strictEqual(ordinalDirectionLength <= 2, true, prefixErrorMessage + " have a length less or equals 2. Current is " + ordinalDirectionLength); }) @@ -433,23 +463,29 @@ describe("address.js", function () { it("returns random cardinal direction", function () { sinon.stub(faker.address, 'cardinalDirection').returns('Northwest'); var cardinalDirection = faker.address.cardinalDirection(); + var expected = 'Northwest'; - assert.equal(cardinalDirection, 'Northwest'); + assert.strictEqual(cardinalDirection, expected, "The random cardinal direction should be equals " + expected + ". Current is " + cardinalDirection); faker.address.cardinalDirection.restore(); }) it("returns abbreviation when useAbbr is true", function () { sinon.stub(faker.address, 'cardinalDirection').returns('NW'); var cardinalDirection = faker.address.cardinalDirection(true); + var expected = 'NW'; - assert.equal(cardinalDirection, 'NW'); + assert.strictEqual(cardinalDirection, expected, "The cardinal direction when useAbbr is true should be equals " + expected + ". Current is " + cardinalDirection); faker.address.cardinalDirection.restore(); }) it("returns abbreviation when useAbbr is true", function () { var cardinalDirection = faker.address.cardinalDirection(true); - assert.equal(typeof cardinalDirection, 'string'); - assert.equal(cardinalDirection.length <= 2, true); + var expectedType = 'string'; + var cardinalDirectionLength = cardinalDirection.length; + var prefixErrorMessage = "The cardinal direction when useAbbr is true should" + + assert.strictEqual(typeof cardinalDirection, expectedType, prefixErrorMessage + " be had typeof equals " + expectedType + ".Current is " + typeof ordinalDirection); + assert.strictEqual(cardinalDirectionLength <= 2, true, prefixErrorMessage + " have a length less or equals 2. Current is " + cardinalDirectionLength); }) }) @@ -509,4 +545,14 @@ describe("address.js", function () { }); }); + describe("timeZone()", function () { + it("returns random timeZone", function () { + sinon.spy(faker.address, 'timeZone'); + var timeZone = faker.address.timeZone(); + assert.ok(timeZone); + assert.ok(faker.address.timeZone.called); + faker.address.timeZone.restore(); + }); + }); + }); diff --git a/test/commerce.unit.js b/test/commerce.unit.js index a0336c47..61161e07 100644 --- a/test/commerce.unit.js +++ b/test/commerce.unit.js @@ -77,8 +77,8 @@ describe("commerce.js", 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"); + assert.strictEqual((price > 0), true, "the amount should be greater than 0"); + assert.strictEqual((price < 1001), true, "the amount should be less than 1000"); }); it("should use the default decimal location when not passing arguments", function() { @@ -88,7 +88,7 @@ describe("commerce.js", function() { 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); + assert.strictEqual(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 () { @@ -100,7 +100,7 @@ describe("commerce.js", function() { var expected = true; var actual = regexp.test(amount); - assert.equal(actual, expected, 'The expected match should not include a currency symbol'); + assert.strictEqual(actual, expected, 'The expected match should not include a currency symbol'); }); it("it should handle negative amounts, but return 0", function () { @@ -108,7 +108,7 @@ describe("commerce.js", function() { var amount = faker.commerce.price(-200, -1); assert.ok(amount); - assert.equal((amount == 0.00), true, "the amount should equal 0"); + assert.strictEqual((amount == 0.00), true, "the amount should equal 0"); }); it("it should handle argument dec", function () { @@ -129,4 +129,16 @@ describe("commerce.js", function() { }); + describe("productDescription()", function() { + it("returns a random product description", function() { + sinon.spy(faker.commerce, 'productDescription'); + var description = faker.commerce.productDescription(); + + assert.ok(typeof description === 'string'); + assert.ok(faker.commerce.productDescription.calledOnce); + + faker.commerce.productDescription.restore(); + }); + }); + }); diff --git a/test/database.unit.js b/test/database.unit.js index bb0cd2a5..b3955fea 100644 --- a/test/database.unit.js +++ b/test/database.unit.js @@ -9,8 +9,9 @@ describe("database.js", function () { it("returns a column name", function () { sinon.stub(faker.database, 'column').returns('title'); var column = faker.database.column(); + var expected = 'title'; - assert.equal(column, 'title'); + assert.strictEqual(column, expected, "The column name should be equals " + expected + ". Current is " + column); faker.database.column.restore(); }); }); @@ -19,8 +20,9 @@ describe("database.js", function () { it("returns a collation", function () { sinon.stub(faker.database, 'collation').returns('utf8_bin'); var collation = faker.database.collation(); + var expected = 'utf8_bin'; - assert.equal(collation, 'utf8_bin'); + assert.strictEqual(collation, expected, "The collation should be equals " + expected + ". Current is " + collation); faker.database.collation.restore(); }); }); @@ -29,8 +31,9 @@ describe("database.js", function () { it("returns an engine", function () { sinon.stub(faker.database, 'engine').returns('InnoDB'); var engine = faker.database.engine(); + var expected = 'InnoDB'; - assert.equal(engine, 'InnoDB'); + assert.strictEqual(engine, expected, "The db engine should be equals " + expected + ". Current is " + engine); faker.database.engine.restore(); }); }); @@ -39,8 +42,9 @@ describe("database.js", function () { it("returns a column type", function () { sinon.stub(faker.database, 'type').returns('int'); var type = faker.database.type(); + var expected = 'int'; - assert.equal(type, 'int'); + assert.strictEqual(type, expected, "The column type should be equals " + expected + ". Current is " + type); faker.database.type.restore(); }); }); diff --git a/test/date.unit.js b/test/date.unit.js index c3f4eb6c..c4569d54 100644 --- a/test/date.unit.js +++ b/test/date.unit.js @@ -115,6 +115,19 @@ describe("date.js", function () { }); }); + describe("betweens()", function () { + it("returns an array of 3 dates ( by default ) of sorted randoms dates between the dates given", function () { + + var from = new Date(1990, 5, 7, 9, 11, 0, 0); + var to = new Date(2000, 6, 8, 10, 12, 0, 0); + + var dates = faker.date.betweens(from, to ); + + assert.ok(dates[0] > from && dates[0] < to); + assert.ok(dates[1] > dates[0] && dates[2] > dates[1]); + }); + }); + describe("month()", function () { it("returns random value from date.month.wide array by default", function () { var month = faker.date.month(); diff --git a/test/finance.unit.js b/test/finance.unit.js index bad85884..ae9db61c 100644 --- a/test/finance.unit.js +++ b/test/finance.unit.js @@ -4,6 +4,7 @@ if (typeof module !== 'undefined') { var faker = require('../index'); } +faker.seed(1234); describe('finance.js', function () { describe('account( length )', function () { @@ -15,7 +16,7 @@ describe('finance.js', function () { var expected = 8; var actual = account.length; - assert.equal(actual, expected, 'The expected default account length is ' + expected + ' but it was ' + actual); + assert.strictEqual(actual, expected, 'The expected default account length is ' + expected + ' but it was ' + actual); }); @@ -27,7 +28,7 @@ describe('finance.js', function () { var actual = account.length; - assert.equal(actual, expected, 'The expected default account length is ' + expected + ' but it was ' + actual); + assert.strictEqual(actual, expected, 'The expected default account length is ' + expected + ' but it was ' + actual); }); @@ -39,7 +40,7 @@ describe('finance.js', function () { var actual = account.length; - assert.equal(actual, expected, 'The expected default account length is ' + expected + ' but it was ' + actual); + assert.strictEqual(actual, expected, 'The expected default account length is ' + expected + ' but it was ' + actual); }); @@ -78,7 +79,7 @@ describe('finance.js', function () { var actual = mask.length; - assert.equal(actual, expected, 'The expected default mask length is ' + expected + ' but it was ' + actual); + assert.strictEqual(actual, expected, 'The expected default mask length is ' + expected + ' but it was ' + actual); }); @@ -92,7 +93,7 @@ describe('finance.js', function () { var actual = mask.length; //picks 4 if the random number generator picks 0 - assert.equal(actual, expected, 'The expected default mask length is ' + expected + ' but it was ' + actual); + assert.strictEqual(actual, expected, 'The expected default mask length is ' + expected + ' but it was ' + actual); }); @@ -100,11 +101,11 @@ describe('finance.js', function () { var expected = 4; - var mask = faker.finance.mask(0, false, false); + faker.finance.mask(0, false, false); var actual = 4; //picks 4 if the random number generator picks 0 - assert.equal(actual, expected, 'The expected default mask length is ' + expected + ' but it was ' + actual); + assert.strictEqual(actual, expected, 'The expected default mask length is ' + expected + ' but it was ' + actual); }); @@ -118,7 +119,7 @@ describe('finance.js', function () { var regexp = new RegExp(/(\(\d{4}?\))/); var actual = regexp.test(mask); - assert.equal(actual, expected, 'The expected match for parentheses is ' + expected + ' but it was ' + actual); + assert.strictEqual(actual, expected, 'The expected match for parentheses is ' + expected + ' but it was ' + actual); }); @@ -131,7 +132,7 @@ describe('finance.js', function () { var regexp = new RegExp(/(\.\.\.\d{4})/); var actual = regexp.test(mask); - assert.equal(actual, expected, 'The expected match for parentheses is ' + expected + ' but it was ' + actual); + assert.strictEqual(actual, expected, 'The expected match for parentheses is ' + expected + ' but it was ' + actual); }); @@ -155,22 +156,23 @@ describe('finance.js', function () { var amount = faker.finance.amount(); assert.ok(amount); - assert.equal((amount > 0), true, "the amount should be greater than 0"); - assert.equal((amount < 1001), true, "the amount should be greater than 0"); + assert.strictEqual((amount > 0), true, "the amount should be greater than 0"); + assert.strictEqual((amount < 1001), true, "the amount should be greater than 0"); }); - it("should use the defaul decimal location when not passing arguments", function () { + it("should use the default decimal location when not passing arguments", function () { var amount = faker.finance.amount(); var decimal = '.'; var expected = amount.length - 3; - var actual = amount.indexOf(decimal); + var amount = faker.finance.amount(100, 100, 1); - assert.equal(actual, expected, 'The expected location of the decimal is ' + expected + ' but it was ' + actual + ' amount ' + amount); + assert.ok(amount); + assert.strictEqual(amount , '100.0', "the amount should be equal 100.0"); }); - + //TODO: add support for more currency and decimal options it("should not include a currency symbol by default", function () { @@ -181,7 +183,7 @@ describe('finance.js', function () { var expected = true; var actual = regexp.test(amount); - assert.equal(actual, expected, 'The expected match should not include a currency symbol'); + assert.strictEqual(actual, expected, 'The expected match should not include a currency symbol'); }); @@ -190,8 +192,8 @@ describe('finance.js', function () { var amount = faker.finance.amount(-200, -1); assert.ok(amount); - assert.equal((amount < 0), true, "the amount should be greater than 0"); - assert.equal((amount > -201), true, "the amount should be greater than 0"); + assert.strictEqual((amount < 0), true, "the amount should be greater than 0"); + assert.strictEqual((amount > -201), true, "the amount should be greater than 0"); }); @@ -200,7 +202,7 @@ describe('finance.js', function () { var amount = faker.finance.amount(100, 100, 1); assert.ok(amount); - assert.strictEqual(amount , '100.0', "the amount should be equal 100.0"); + assert.strictEqual(amount , "100.0", "the amount should be equal 100.0"); }); it("it should handle argument dec = 0", function () { @@ -211,6 +213,16 @@ describe('finance.js', function () { assert.strictEqual(amount , '100', "the amount should be equal 100"); }); + it("it should return a string", function() { + + var amount = faker.finance.amount(100, 100, 0); + + var typeOfAmount = typeof amount; + + assert.ok(amount); + assert.strictEqual(typeOfAmount , "string", "the amount type should be number"); + }); + }); describe('transactionType()', function () { @@ -226,7 +238,7 @@ describe('finance.js', function () { it("returns a random currency code with a format", function () { var currencyCode = faker.finance.currencyCode(); - assert.ok(currencyCode.match(/[A-Z]{3}/)); + assert.ok(currencyCode.match(/^[A-Z]{3}$/)); }); }); @@ -243,6 +255,14 @@ describe('finance.js', function () { }); }); + describe("litecoinAddress()", function(){ + it("returns a random litecoin address", function(){ + var litecoinAddress = faker.finance.litecoinAddress(); + + assert.ok(litecoinAddress.match(/^[LM3][1-9a-km-zA-HJ-NP-Z]{25,32}$/)); + }); + }); + describe("ethereumAddress()", function(){ it("returns a random ethereum address", function(){ var ethereumAddress = faker.finance.ethereumAddress(); @@ -329,8 +349,19 @@ describe('finance.js', function () { var iban = faker.finance.iban(); var bban = iban.substring(4) + iban.substring(0, 4); + assert.strictEqual(ibanLib.mod97(ibanLib.toDigitString(bban)), 1, "the result should be equal to 1"); + }); + it("returns a specific and formally correct IBAN number", function () { + var iban = faker.finance.iban(false, "DE"); + var bban = iban.substring(4) + iban.substring(0, 4); + var countryCode = iban.substring(0, 2); + + assert.equal(countryCode, "DE"); assert.equal(ibanLib.mod97(ibanLib.toDigitString(bban)), 1, "the result should be equal to 1"); }); + it("throws an error if the passed country code is not supported", function () { + assert.throws(function() { faker.finance.iban(false, 'AA');}, /Country code AA not supported/); + }); }); describe("bic()", function () { @@ -342,4 +373,12 @@ describe('finance.js', function () { assert.ok(bic.match(expr)); }); }); + + describe("transactionDescription()", function() { + it("returns a random transaction description", function() { + var transactionDescription = faker.finance.transactionDescription(); + + assert.ok(transactionDescription); + }) + }) }); diff --git a/test/helpers.unit.js b/test/helpers.unit.js index 5c8b31d5..5bbe0fcc 100644 --- a/test/helpers.unit.js +++ b/test/helpers.unit.js @@ -43,30 +43,43 @@ describe("helpers.js", function () { var shuffled = faker.helpers.shuffle([]); assert.ok(shuffled.length === 0); }); + + it("mutates the input array in place", function () { + var input = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; + var shuffled = faker.helpers.shuffle(input); + assert.deepStrictEqual(shuffled, input); + }); + + it("all items shuffled as expected when seeded", function () { + var input = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; + faker.seed(100); + var shuffled = faker.helpers.shuffle(input); + assert.deepStrictEqual(shuffled, ["b", "e", "a", "d", "j", "i", "h", "c", "g", "f"]); + }); }); describe("slugify()", function () { it("removes unwanted characters from URI string", function () { - assert.equal(faker.helpers.slugify("Aiden.Harªann"), "Aiden.Harann"); - assert.equal(faker.helpers.slugify("d'angelo.net"), "dangelo.net"); + assert.strictEqual(faker.helpers.slugify("Aiden.Harªann"), "Aiden.Harann"); + assert.strictEqual(faker.helpers.slugify("d'angelo.net"), "dangelo.net"); }); }); describe("mustache()", function () { it("returns empty string with no arguments", function () { - assert.equal(faker.helpers.mustache(), ""); + assert.strictEqual(faker.helpers.mustache(), ""); }); }); describe("repeatString()", function () { it("returns empty string with no arguments", function () { - assert.equal(faker.helpers.repeatString(), ""); + assert.strictEqual(faker.helpers.repeatString(), ""); }); }); describe("replaceSymbols()", function () { it("returns empty string with no arguments", function () { - assert.equal(faker.helpers.replaceSymbols(), ""); + assert.strictEqual(faker.helpers.replaceSymbols(), ""); }); }); diff --git a/test/image.unit.js b/test/image.unit.js index 5f5e7b48..f0e37fdd 100644 --- a/test/image.unit.js +++ b/test/image.unit.js @@ -5,105 +5,170 @@ if (typeof module !== 'undefined') { } describe("image.js", function () { + describe("lorempicsum", function() { + describe("imageUrl()", function () { + it("returns a random image url from lorempixel", function () { + var imageUrl = faker.image.lorempicsum.imageUrl(); + + assert.strictEqual(imageUrl, 'https://picsum.photos/640/480'); + }); + it("returns a random image url from lorem picsum with width and height", function () { + var imageUrl = faker.image.lorempicsum.imageUrl(100, 100); + + assert.strictEqual(imageUrl, 'https://picsum.photos/100/100'); + }); + it("returns a random image url grayscaled", function () { + var imageUrl = faker.image.lorempicsum.imageUrl(100, 100, true); + + assert.strictEqual(imageUrl, 'https://picsum.photos/100/100?grayscale'); + }); + + it("returns a random image url grayscaled and blurred", function () { + var imageUrl = faker.image.lorempicsum.imageUrl(100, 100, true, 2); + + assert.strictEqual(imageUrl, 'https://picsum.photos/100/100?grayscale&blur=2'); + }); + + it("returns a random image url blurred", function () { + var imageUrl = faker.image.lorempicsum.imageUrl(100, 100, undefined, 2); + + assert.strictEqual(imageUrl, 'https://picsum.photos/100/100?blur=2'); + }); + + it("returns a random image url with seed", function () { + var imageUrl = faker.image.lorempicsum.imageUrl(100, 100, undefined, undefined, 'picsum'); + + assert.strictEqual(imageUrl, 'https://picsum.photos/seed/picsum/100/100'); + }); + }); + describe("avatar()", function () { + it("return a random avatar from UIFaces", function () { + assert.notStrictEqual(-1, faker.image.lorempicsum.avatar().indexOf('s3.amazonaws.com/uifaces/faces')); + }) + }); + + describe("imageGrayscale()", function () { + it("returns a random URL with grayscale image", function () { + var imageUrl = faker.image.lorempicsum.imageGrayscale(100, 100, true); + + assert.strictEqual(imageUrl, 'https://picsum.photos/100/100?grayscale'); + }); + }); + describe("imageBlurred()", function () { + it("returns a random image url blurred", function () { + var imageUrl = faker.image.lorempicsum.imageBlurred(100, 100, 2); + + assert.strictEqual(imageUrl, 'https://picsum.photos/100/100?blur=2'); + }); + }); + describe("imageRandomSeeded()", function () { + it("returns a random image url blurred", function () { + var imageUrl = faker.image.lorempicsum.imageRandomSeeded(100, 100, undefined, undefined, 'picsum'); + + assert.strictEqual(imageUrl, 'https://picsum.photos/seed/picsum/100/100'); + }); + }); + }); + describe("lorempixel", function() { describe("imageUrl()", function () { it("returns a random image url from lorempixel", function () { var imageUrl = faker.image.lorempixel.imageUrl(); - assert.equal(imageUrl, 'http://lorempixel.com/640/480'); + assert.strictEqual(imageUrl, 'https://lorempixel.com/640/480'); }); it("returns a random image url from lorempixel with width and height", function () { var imageUrl = faker.image.lorempixel.imageUrl(100, 100); - assert.equal(imageUrl, 'http://lorempixel.com/100/100'); + assert.strictEqual(imageUrl, 'https://lorempixel.com/100/100'); }); it("returns a random image url for a specified category", function () { var imageUrl = faker.image.lorempixel.imageUrl(100, 100, 'abstract'); - assert.equal(imageUrl, 'http://lorempixel.com/100/100/abstract'); + assert.strictEqual(imageUrl, 'https://lorempixel.com/100/100/abstract'); }); }); describe("avatar()", function () { it("return a random avatar from UIFaces", function () { - assert.notEqual(-1, faker.image.lorempixel.avatar().indexOf('s3.amazonaws.com/uifaces/faces')); + assert.notStrictEqual(-1, faker.image.lorempixel.avatar().indexOf('s3.amazonaws.com/uifaces/faces')); }) }); describe("abstract()", function () { it("returns a random abstract image url", function () { var abstract = faker.image.lorempixel.abstract(); - assert.equal(abstract, 'http://lorempixel.com/640/480/abstract'); + assert.strictEqual(abstract, 'https://lorempixel.com/640/480/abstract'); }); }); describe("animals()", function () { it("returns a random animals image url", function () { var animals = faker.image.lorempixel.animals(); - assert.equal(animals, 'http://lorempixel.com/640/480/animals'); + assert.strictEqual(animals, 'https://lorempixel.com/640/480/animals'); }); }); describe("business()", function () { it("returns a random business image url", function () { var business = faker.image.lorempixel.business(); - assert.equal(business, 'http://lorempixel.com/640/480/business'); + assert.strictEqual(business, 'https://lorempixel.com/640/480/business'); }); }); describe("cats()", function () { it("returns a random cats image url", function () { var cats = faker.image.lorempixel.cats(); - assert.equal(cats, 'http://lorempixel.com/640/480/cats'); + assert.strictEqual(cats, 'https://lorempixel.com/640/480/cats'); }); }); describe("city()", function () { it("returns a random city image url", function () { var city = faker.image.lorempixel.city(); - assert.equal(city, 'http://lorempixel.com/640/480/city'); + assert.strictEqual(city, 'https://lorempixel.com/640/480/city'); }); }); describe("food()", function () { it("returns a random food image url", function () { var food = faker.image.lorempixel.food(); - assert.equal(food, 'http://lorempixel.com/640/480/food'); + assert.strictEqual(food, 'https://lorempixel.com/640/480/food'); }); }); describe("nightlife()", function () { it("returns a random nightlife image url", function () { var nightlife = faker.image.lorempixel.nightlife(); - assert.equal(nightlife, 'http://lorempixel.com/640/480/nightlife'); + assert.strictEqual(nightlife, 'https://lorempixel.com/640/480/nightlife'); }); }); describe("fashion()", function () { it("returns a random fashion image url", function () { var fashion = faker.image.lorempixel.fashion(); - assert.equal(fashion, 'http://lorempixel.com/640/480/fashion'); + assert.strictEqual(fashion, 'https://lorempixel.com/640/480/fashion'); }); }); describe("people()", function () { it("returns a random people image url", function () { var people = faker.image.lorempixel.people(); - assert.equal(people, 'http://lorempixel.com/640/480/people'); + assert.strictEqual(people, 'https://lorempixel.com/640/480/people'); }); }); describe("nature()", function () { it("returns a random nature image url", function () { var nature = faker.image.lorempixel.nature(); - assert.equal(nature, 'http://lorempixel.com/640/480/nature'); + assert.strictEqual(nature, 'https://lorempixel.com/640/480/nature'); }); }); describe("sports()", function () { it("returns a random sports image url", function () { var sports = faker.image.lorempixel.sports(); - assert.equal(sports, 'http://lorempixel.com/640/480/sports'); + assert.strictEqual(sports, 'https://lorempixel.com/640/480/sports'); }); }); describe("technics()", function () { it("returns a random technics image url", function () { var technics = faker.image.lorempixel.technics(); - assert.equal(technics, 'http://lorempixel.com/640/480/technics'); + assert.strictEqual(technics, 'https://lorempixel.com/640/480/technics'); }); }); describe("transport()", function () { it("returns a random transport image url", function () { var transport = faker.image.lorempixel.transport(); - assert.equal(transport, 'http://lorempixel.com/640/480/transport'); + assert.strictEqual(transport, 'https://lorempixel.com/640/480/transport'); }); }); }); @@ -113,80 +178,80 @@ describe("image.js", function () { it("returns a random image url from unsplash", function () { var imageUrl = faker.image.unsplash.imageUrl(); - assert.equal(imageUrl, 'https://source.unsplash.com/640x480'); + assert.strictEqual(imageUrl, 'https://source.unsplash.com/640x480'); }); it("returns a random image url from unsplash with width and height", function () { var imageUrl = faker.image.unsplash.imageUrl(100, 100); - assert.equal(imageUrl, 'https://source.unsplash.com/100x100'); + assert.strictEqual(imageUrl, 'https://source.unsplash.com/100x100'); }); it("returns a random image url for a specified category", function () { var imageUrl = faker.image.unsplash.imageUrl(100, 100, 'food'); - assert.equal(imageUrl, 'https://source.unsplash.com/category/food/100x100'); + assert.strictEqual(imageUrl, 'https://source.unsplash.com/category/food/100x100'); }); it("returns a random image url with correct keywords for a specified category", function () { var imageUrl = faker.image.unsplash.imageUrl(100, 100, 'food', 'keyword1,keyword2'); - assert.equal(imageUrl, 'https://source.unsplash.com/category/food/100x100?keyword1,keyword2'); + assert.strictEqual(imageUrl, 'https://source.unsplash.com/category/food/100x100?keyword1,keyword2'); }); it("returns a random image url without keyword which format is wrong for a specified category", function () { var imageUrl = faker.image.unsplash.imageUrl(100, 100, 'food', 'keyword1,?ds)0123$*908932409'); - assert.equal(imageUrl, 'https://source.unsplash.com/category/food/100x100'); + assert.strictEqual(imageUrl, 'https://source.unsplash.com/category/food/100x100'); }); }); describe("image()", function() { it("returns a searching image url with keyword", function () { var food = faker.image.unsplash.image(100, 200, 'keyword1,keyword2,keyword3'); - assert.equal(food, 'https://source.unsplash.com/100x200?keyword1,keyword2,keyword3'); + assert.strictEqual(food, 'https://source.unsplash.com/100x200?keyword1,keyword2,keyword3'); }); }) describe("food()", function () { it("returns a random food image url", function () { var food = faker.image.unsplash.food(); - assert.equal(food, 'https://source.unsplash.com/category/food/640x480'); + assert.strictEqual(food, 'https://source.unsplash.com/category/food/640x480'); }); }); describe("people()", function () { it("returns a random people image url", function () { var people = faker.image.unsplash.people(); - assert.equal(people, 'https://source.unsplash.com/category/people/640x480'); + assert.strictEqual(people, 'https://source.unsplash.com/category/people/640x480'); }); }); describe("nature()", function () { it("returns a random nature image url", function () { var nature = faker.image.unsplash.nature(); - assert.equal(nature, 'https://source.unsplash.com/category/nature/640x480'); + assert.strictEqual(nature, 'https://source.unsplash.com/category/nature/640x480'); }); }); describe("technology()", function () { it("returns a random technology image url", function () { var transport = faker.image.unsplash.technology(); - assert.equal(transport, 'https://source.unsplash.com/category/technology/640x480'); + assert.strictEqual(transport, 'https://source.unsplash.com/category/technology/640x480'); }); }); describe("objects()", function () { it("returns a random objects image url", function () { var transport = faker.image.unsplash.objects(); - assert.equal(transport, 'https://source.unsplash.com/category/objects/640x480'); + assert.strictEqual(transport, 'https://source.unsplash.com/category/objects/640x480'); }); }); describe("buildings()", function () { it("returns a random buildings image url", function () { var transport = faker.image.unsplash.buildings(); - assert.equal(transport, 'https://source.unsplash.com/category/buildings/640x480'); + assert.strictEqual(transport, 'https://source.unsplash.com/category/buildings/640x480'); }); }); }); describe("dataUri", function () { it("returns a blank data", function () { var dataUri = faker.image.dataUri(200,300); - assert.equal(dataUri, 'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20baseProfile%3D%22full%22%20width%3D%22200%22%20height%3D%22300%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22grey%22%2F%3E%3Ctext%20x%3D%22100%22%20y%3D%22150%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E200x300%3C%2Ftext%3E%3C%2Fsvg%3E'); + assert.strictEqual(dataUri, 'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20baseProfile%3D%22full%22%20width%3D%22200%22%20height%3D%22300%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22grey%22%2F%3E%3Ctext%20x%3D%22100%22%20y%3D%22150%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E200x300%3C%2Ftext%3E%3C%2Fsvg%3E'); }); it("returns a customed background color data URI", function () { var dataUri = faker.image.dataUri(200, 300, 'red'); - assert.equal(dataUri, 'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20baseProfile%3D%22full%22%20width%3D%22200%22%20height%3D%22300%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22red%22%2F%3E%3Ctext%20x%3D%22100%22%20y%3D%22150%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E200x300%3C%2Ftext%3E%3C%2Fsvg%3E'); + assert.strictEqual(dataUri, 'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20baseProfile%3D%22full%22%20width%3D%22200%22%20height%3D%22300%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22red%22%2F%3E%3Ctext%20x%3D%22100%22%20y%3D%22150%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E200x300%3C%2Ftext%3E%3C%2Fsvg%3E'); }); }); }); diff --git a/test/internet.unit.js b/test/internet.unit.js index 0c9bbd30..cceaacc8 100644 --- a/test/internet.unit.js +++ b/test/internet.unit.js @@ -11,7 +11,16 @@ describe("internet.js", function () { var email = faker.internet.email("Aiden.Harann55"); var res = email.split("@"); res = res[0]; - assert.equal(res, 'Aiden.Harann55'); + assert.strictEqual(res, 'Aiden.Harann55'); + faker.internet.userName.restore(); + }); + + it("returns an email with japanese characters", function () { + sinon.stub(faker.internet, 'userName').returns('思源_唐3'); + var email = faker.internet.email("思源_唐3"); + var res = email.split("@"); + res = res[0]; + assert.equal(res, '思源_唐3'); faker.internet.userName.restore(); }); }); @@ -22,7 +31,7 @@ describe("internet.js", function () { var email = faker.internet.email("Aiden.Harann55"); var res = email.split("@"); res = res[0]; - assert.equal(res, 'Aiden.Harann55'); + assert.strictEqual(res, 'Aiden.Harann55'); faker.internet.userName.restore(); }); @@ -71,7 +80,7 @@ describe("internet.js", function () { var domain_name = faker.internet.domainName(); - assert.equal(domain_name, 'bar.net'); + assert.strictEqual(domain_name, 'bar.net'); faker.internet.domainWord.restore(); faker.internet.domainSuffix.restore(); @@ -125,6 +134,14 @@ describe("internet.js", function () { }); }); + describe('httpMethod()', function () { + it('returns a valid http method', function () { + var httpMethods = ['GET','POST', 'PUT', 'DELETE', 'PATCH']; + var method = faker.internet.httpMethod(); + assert.ok(httpMethods.includes(method)); + }); + }); + describe('url()', function () { it('returns a valid url', function () { sinon.stub(faker.internet,'protocol').returns('http'); @@ -142,7 +159,7 @@ describe("internet.js", 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); + assert.strictEqual(parts.length, 4); }); }); @@ -150,7 +167,15 @@ describe("internet.js", function () { it("returns a random IPv6 address with eight parts", function () { var ip = faker.internet.ipv6(); var parts = ip.split(':'); - assert.equal(parts.length, 8); + assert.strictEqual(parts.length, 8); + }); + }); + + describe("port()", function () { + it("returns a random port number", function () { + var port = faker.internet.port(); + assert.ok(Number.isInteger(port)); + assert.ok(0 <= port && port <= 65535); }); }); @@ -159,6 +184,14 @@ describe("internet.js", function () { var ua = faker.internet.userAgent(); assert.ok(ua); }); + + it('is deterministic', function () { + faker.seed(1); + var ua1 = faker.internet.userAgent(); + faker.seed(1); + var ua2 = faker.internet.userAgent(); + assert.strictEqual(ua1, ua2); + }); }); describe("color()", function () { diff --git a/test/locales.unit.js b/test/locales.unit.js index 932369f4..8f8be2d7 100644 --- a/test/locales.unit.js +++ b/test/locales.unit.js @@ -13,7 +13,7 @@ describe("locale", function () { it("setLocale() changes faker.locale", function () { for(var locale in faker.locales) { faker.setLocale(locale) - assert.equal(faker.locale, locale); + assert.strictEqual(faker.locale, locale); } }); }); diff --git a/test/lorem.unit.js b/test/lorem.unit.js index 300786b8..d7853a4b 100644 --- a/test/lorem.unit.js +++ b/test/lorem.unit.js @@ -5,6 +5,24 @@ if (typeof module !== 'undefined') { } describe("lorem.js", function () { + describe("word()", function () { + + context("when no 'length' param passed in", function () { + it("returns a word with a random length", function () { + var str = faker.lorem.word(); + assert.ok(typeof str === 'string'); + }); + }); + + context("when 'length' param passed in", function () { + it("returns a word with the requested length", function () { + var str = faker.lorem.word(5); + assert.ok(typeof str === 'string'); + assert.strictEqual(str.length, 5); + }); + }); + }); + describe("words()", function () { beforeEach(function () { sinon.spy(faker.helpers, 'shuffle'); @@ -19,7 +37,7 @@ describe("lorem.js", function () { var str = faker.lorem.words(); var words = str.split(' '); assert.ok(Array.isArray(words)); - assert.equal(true, words.length >= 3); + assert.strictEqual(true, words.length >= 3); // assert.ok(faker.helpers.shuffle.called); }); }); @@ -29,7 +47,7 @@ describe("lorem.js", function () { var str = faker.lorem.words(7); var words = str.split(' '); assert.ok(Array.isArray(words)); - assert.equal(words.length, 7); + assert.strictEqual(words.length, 7); }); }); }); @@ -44,8 +62,8 @@ describe("lorem.js", function () { }); var validateSlug = function (wordCount, str) { - assert.equal(1, str.match(/^[a-z][a-z-]*[a-z]$/).length); - assert.equal(wordCount - 1, str.match(/-/g).length); + assert.strictEqual(1, str.match(/^[a-z][a-z-]*[a-z]$/).length); + assert.strictEqual(wordCount - 1, str.match(/-/g).length); }; context("when no 'wordCount' param passed in", function () { @@ -73,7 +91,7 @@ describe("lorem.js", function () { var sentence = faker.lorem.sentence(); assert.ok(typeof sentence === 'string'); var parts = sentence.split(' '); - assert.equal(parts.length, 5); // default 3 plus stubbed 2. + assert.strictEqual(parts.length, 5); // default 3 plus stubbed 2. assert.ok(faker.lorem.words.calledWith(5)); faker.lorem.words.restore(); @@ -89,7 +107,7 @@ describe("lorem.js", function () { assert.ok(typeof sentence === 'string'); var parts = sentence.split(' '); - assert.equal(parts.length, 12); // requested 10 plus stubbed 2. + assert.strictEqual(parts.length, 12); // requested 10 plus stubbed 2. assert.ok(faker.lorem.words.calledWith(12)); faker.lorem.words.restore(); @@ -106,7 +124,7 @@ describe("lorem.js", function () { assert.ok(typeof sentence === 'string'); var parts = sentence.split(' '); - assert.equal(parts.length, 14); // requested 10 plus stubbed 4. + assert.strictEqual(parts.length, 14); // requested 10 plus stubbed 4. assert.ok(faker.random.number.calledWith(4)); // random.number should be called with the 'range' we passed. assert.ok(faker.lorem.words.calledWith(14)); @@ -127,7 +145,7 @@ describe("lorem.js", function () { assert.ok(typeof sentences === 'string'); var parts = sentences.split('\n'); - assert.equal(parts.length, 3); + assert.strictEqual(parts.length, 3); assert.ok(faker.lorem.sentence.calledThrice); faker.lorem.sentence.restore(); @@ -141,7 +159,7 @@ describe("lorem.js", function () { assert.ok(typeof sentences === 'string'); var parts = sentences.split('\n'); - assert.equal(parts.length, 5); + assert.strictEqual(parts.length, 5); faker.lorem.sentence.restore(); }); @@ -158,7 +176,7 @@ describe("lorem.js", function () { assert.ok(typeof paragraph === 'string'); var parts = paragraph.split('\n'); - assert.equal(parts.length, 5); // default 3 plus stubbed 2. + assert.strictEqual(parts.length, 5); // default 3 plus stubbed 2. assert.ok(faker.lorem.sentences.calledWith(5)); faker.lorem.sentences.restore(); @@ -174,7 +192,7 @@ describe("lorem.js", function () { assert.ok(typeof paragraph === 'string'); var parts = paragraph.split('\n'); - assert.equal(parts.length, 12); // requested 10 plus stubbed 2. + assert.strictEqual(parts.length, 12); // requested 10 plus stubbed 2. assert.ok(faker.lorem.sentences.calledWith(12)); faker.lorem.sentences.restore(); @@ -194,7 +212,7 @@ describe("lorem.js", function () { assert.ok(typeof paragraphs === 'string'); var parts = paragraphs.split('\n \r'); - assert.equal(parts.length, 3); + assert.strictEqual(parts.length, 3); assert.ok(faker.lorem.paragraph.calledThrice); faker.lorem.paragraph.restore(); @@ -208,7 +226,7 @@ describe("lorem.js", function () { assert.ok(typeof paragraphs === 'string'); var parts = paragraphs.split('\n \r'); - assert.equal(parts.length, 5); + assert.strictEqual(parts.length, 5); faker.lorem.paragraph.restore(); }); diff --git a/test/music.unit.js b/test/music.unit.js new file mode 100644 index 00000000..3abc0787 --- /dev/null +++ b/test/music.unit.js @@ -0,0 +1,17 @@ +if (typeof module !== 'undefined') { + var assert = require('assert'); + var sinon = require('sinon'); + var faker = require('../index'); +} + +describe("music.js", function () { + describe("genre()", function () { + it("returns a genre", function () { + sinon.stub(faker.music, 'genre').returns('Rock'); + var genre = faker.music.genre(); + + assert.strictEqual(genre, 'Rock'); + faker.music.genre.restore(); + }); + }); +}); diff --git a/test/name.unit.js b/test/name.unit.js index d6b311a3..40291d2b 100644 --- a/test/name.unit.js +++ b/test/name.unit.js @@ -5,16 +5,39 @@ if (typeof module !== 'undefined') { } +function assertInArray(value, array) { + var idx = array.indexOf(value); + assert.notEqual(idx, -1); +} + describe("name.js", function () { describe("firstName()", function () { it("returns a random name", function () { sinon.stub(faker.name, 'firstName').returns('foo'); var first_name = faker.name.firstName(); - assert.equal(first_name, 'foo'); + assert.strictEqual(first_name, 'foo'); faker.name.firstName.restore(); }); + + it("returns a gender-specific name when passed a number", function () { + for (var q = 0; q < 30; q++) { + var gender = Math.floor(Math.random() * 2); + var name = faker.name.firstName(gender); + if (gender === 0) assertInArray(name, faker.definitions.name.male_first_name); + else assertInArray(name, faker.definitions.name.female_first_name); + } + }); + + it("returns a gender-specific name when passed a string", function () { + for (var q = 0; q < 30; q++) { + var gender = Math.floor(Math.random() * 2); + var genderString = (gender === 0 ? 'male' : 'female'); + var name = faker.name.firstName(genderString); + assertInArray(name, faker.definitions.name[genderString + '_first_name']); + } + }); }); describe("lastName()", function () { @@ -23,12 +46,57 @@ describe("name.js", function () { var last_name = faker.name.lastName(); - assert.equal(last_name, 'foo'); + assert.strictEqual(last_name, 'foo'); faker.name.lastName.restore(); }); }); + describe("middleName()", function () { + + it("returns a random middle name", function () { + sinon.stub(faker.name, 'middleName').returns('foo'); + + var middle_name = faker.name.middleName(); + + assert.strictEqual(middle_name, 'foo'); + + faker.name.middleName.restore(); + }); + + describe('when using a locale with gender specific middle names', function () { + beforeEach(function(){ + this.oldLocale = faker.locale; + faker.locale = 'TEST'; + + faker.locales['TEST'] = { + name: { + male_middle_name: ['Genaddiesvich'], + female_middle_name: ['Genaddievna'] + } + }; + }); + + afterEach(function () { + faker.locale = this.oldLocale; + delete faker.locale['TEST']; + }) + + it("returns male prefix", function () { + var middle_name = faker.name.middleName(0); + + assert.strictEqual(middle_name, 'Genaddiesvich') + }); + + it("returns female prefix", function () { + var middle_name = faker.name.middleName(1); + + assert.strictEqual(middle_name, 'Genaddievna'); + }); + }); + }); + + describe("findName()", function () { it("usually returns a first name and last name", function () { sinon.stub(faker.random, 'number').returns(5); @@ -62,7 +130,7 @@ describe("name.js", function () { var name = faker.name.findName(); - assert.equal(name, 'X Y Z'); + assert.strictEqual(name, 'X Y Z'); faker.random.number.restore(); faker.name.prefix.restore(); @@ -81,7 +149,7 @@ describe("name.js", function () { var name = faker.name.findName(); - assert.equal(name, 'J K L'); + assert.strictEqual(name, 'J K L'); faker.random.number.restore(); faker.name.prefix.restore(); @@ -96,7 +164,7 @@ describe("name.js", function () { var parts = name.split(' '); assert.ok(parts.length >= 3); - assert.equal(parts[parts.length-1], 'Jr.'); + assert.strictEqual(parts[parts.length-1], 'Jr.'); faker.name.suffix.restore(); faker.random.number.restore(); @@ -115,7 +183,7 @@ describe("name.js", function () { var title = faker.name.title(); - assert.equal(title, 'Lead Solutions Supervisor'); + assert.strictEqual(title, 'Lead Solutions Supervisor'); faker.name.title.restore(); }); @@ -163,13 +231,13 @@ describe("name.js", function () { it("returns male prefix", function () { var prefix = faker.name.prefix(0); - assert.equal(prefix, 'Mp') + assert.strictEqual(prefix, 'Mp') }); it("returns female prefix", function () { var prefix = faker.name.prefix(1); - assert.equal(prefix, 'Fp'); + assert.strictEqual(prefix, 'Fp'); }); it("returns either prefix", function () { @@ -199,7 +267,7 @@ describe("name.js", function () { it("returns a prefix", function () { var prefix = faker.name.prefix(); - assert.equal(prefix, 'P'); + assert.strictEqual(prefix, 'P'); }); }); }); diff --git a/test/random.unit.js b/test/random.unit.js index cdeb07f1..ce2c6925 100644 --- a/test/random.unit.js +++ b/test/random.unit.js @@ -52,8 +52,8 @@ describe("random.js", function () { assert.ok(_.includes(results, 0.5)); assert.ok(_.includes(results, 1.0)); - assert.equal(results[0], 0); - assert.equal(_.last(results), 1.5); + assert.strictEqual(results[0], 0); + assert.strictEqual(_.last(results), 1.5); }); @@ -61,7 +61,7 @@ describe("random.js", function () { var options = { min: 0.5, max: 0.99, precision: 0.01 }; for(var i = 0; i < 100; i++) { var number = faker.random.number(options); - assert.equal(number, Number(number.toFixed(2))); + assert.strictEqual(number, Number(number.toFixed(2))); } }); @@ -75,26 +75,32 @@ describe("random.js", function () { faker.random.number(opts); - assert.equal(opts.min, min); - assert.equal(opts.max, max); + assert.strictEqual(opts.min, min); + assert.strictEqual(opts.max, max); }); it('should return deterministic results when seeded with integer', function() { faker.seed(100); var name = faker.name.findName(); - assert.equal(name, 'Eva Jenkins'); + assert.strictEqual(name, 'Eva Jenkins'); + }) + + it('should return deterministic results when seeded with 0', function() { + faker.seed(0); + var name = faker.name.findName(); + assert.strictEqual(name, 'Lola Sporer'); }) it('should return deterministic results when seeded with array - one element', function() { faker.seed([10]); var name = faker.name.findName(); - assert.equal(name, 'Duane Kub'); + assert.strictEqual(name, 'Duane Kub'); }) it('should return deterministic results when seeded with array - multiple elements', function() { faker.seed([10, 100, 1000]); var name = faker.name.findName(); - assert.equal(name, 'Alma Shanahan'); + assert.strictEqual(name, 'Alma Shanahan'); }) }); @@ -103,12 +109,12 @@ describe("random.js", function () { it("returns a random float with a default precision value (0.01)", function() { var number = faker.random.float(); - assert.equal(number, Number(number.toFixed(2))); + assert.strictEqual(number, Number(number.toFixed(2))); }); it("returns a random float given a precision value", function() { var number = faker.random.float(0.001); - assert.equal(number, Number(number.toFixed(3))); + assert.strictEqual(number, Number(number.toFixed(3))); }); it("returns a random number given a maximum value as Object", function() { @@ -148,8 +154,8 @@ describe("random.js", function () { assert.ok(_.includes(results, 0.5)); assert.ok(_.includes(results, 1.0)); - assert.equal(results[0], 0); - assert.equal(_.last(results), 1.5); + assert.strictEqual(results[0], 0); + assert.strictEqual(_.last(results), 1.5); }); @@ -157,7 +163,7 @@ describe("random.js", function () { var options = { min: 0.5, max: 0.99, precision: 0.01 }; for(var i = 0; i < 100; i++) { var number = faker.random.float(options); - assert.equal(number, Number(number.toFixed(2))); + assert.strictEqual(number, Number(number.toFixed(2))); } }); @@ -171,8 +177,8 @@ describe("random.js", function () { faker.random.float(opts); - assert.equal(opts.min, min); - assert.equal(opts.max, max); + assert.strictEqual(opts.min, min); + assert.strictEqual(opts.max, max); }); }); diff --git a/test/system.unit.js b/test/system.unit.js index dfcfcb0b..0198af62 100644 --- a/test/system.unit.js +++ b/test/system.unit.js @@ -9,7 +9,7 @@ describe("system.js", function () { it("returns unix fs directory full path", function () { sinon.stub(faker.random, 'words').returns('24/7'); var directoryPath = faker.system.directoryPath(); - assert.equal(directoryPath.indexOf('/'), 0, 'generated directoryPath should start with /'); + assert.strictEqual(directoryPath.indexOf('/'), 0, 'generated directoryPath should start with /'); faker.random.words.restore(); }); @@ -19,7 +19,7 @@ describe("system.js", function () { it("returns unix fs file full path", function () { sinon.stub(faker.random, 'words').returns('24/7'); var filePath = faker.system.filePath(); - assert.equal(filePath.indexOf('/'), 0, 'generated filePath should start with /'); + assert.strictEqual(filePath.indexOf('/'), 0, 'generated filePath should start with /'); faker.random.words.restore(); }); @@ -29,7 +29,7 @@ describe("system.js", function () { it("returns filenames without system path seperators", function () { sinon.stub(faker.random, 'words').returns('24/7'); var fileName = faker.system.fileName(); - assert.equal(fileName.indexOf('/'), -1, 'generated fileNames should not have path seperators'); + assert.strictEqual(fileName.indexOf('/'), -1, 'generated fileNames should not have path seperators'); faker.random.words.restore(); }); @@ -39,7 +39,7 @@ describe("system.js", function () { it("returns filenames without system path seperators", function () { sinon.stub(faker.random, 'words').returns('24/7'); var fileName = faker.system.commonFileName(); - assert.equal(fileName.indexOf('/'), -1, 'generated commonFileNames should not have path seperators'); + assert.strictEqual(fileName.indexOf('/'), -1, 'generated commonFileNames should not have path seperators'); faker.random.words.restore(); }); diff --git a/test/time.unit.js b/test/time.unit.js new file mode 100644 index 00000000..7b2c7bae --- /dev/null +++ b/test/time.unit.js @@ -0,0 +1,30 @@ +if (typeof module !== 'undefined') { + var assert = require('assert'); + var sinon = require('sinon'); + var faker = require('../index'); +} + +faker.seed(1234); + +describe("time.js", function () { + describe("recent()", function () { + it("returns the recent timestamp in Unix time format", function () { + var date = faker.time.recent(); + assert.ok(typeof date === 'number'); + // assert.ok(date == new Date().getTime()); + }); + + it("returns the recent timestamp in full time string format", function () { + var date = faker.time.recent('wide'); + assert.ok(typeof date === 'string'); + // assert.ok(date == new Date().toTimeString()); + }); + + it("returns the recent timestamp in abbreviated string format", function () { + var date = faker.time.recent('abbr'); + assert.ok(typeof date === 'string'); + // assert.ok(date == new Date().toLocaleTimeString()); + }); + }); + +}); diff --git a/test/unique.unit.js b/test/unique.unit.js index 47b38d9d..3681e0b3 100644 --- a/test/unique.unit.js +++ b/test/unique.unit.js @@ -9,7 +9,7 @@ describe("unique.js", function () { it("is able to call a function with no arguments and return a result", function () { var result = faker.unique(faker.internet.email); - assert.equal(typeof result, 'string'); + assert.strictEqual(typeof result, 'string'); }); it("is able to call a function with arguments and return a result", function () { @@ -24,7 +24,7 @@ describe("unique.js", function () { it("is able to exclude results as array", function () { var result = faker.unique(faker.internet.protocol, [], { exclude: ['https'] }); - assert.equal(result, 'http'); + assert.strictEqual(result, 'http'); }); it("is able to limit unique call by maxTime in ms", function () { @@ -32,7 +32,7 @@ describe("unique.js", function () { try { result = faker.unique(faker.internet.protocol, [], { maxTime: 1, maxRetries: 9999, exclude: ['https', 'http'] }); } catch (err) { - assert.equal(err.message.substr(0, 16), 'Exceeded maxTime'); + assert.strictEqual(err.message.substr(0, 16), 'Exceeded maxTime'); } }); @@ -41,7 +41,7 @@ describe("unique.js", function () { try { result = faker.unique(faker.internet.protocol, [], { maxTime: 5000, maxRetries: 5, exclude: ['https', 'http'] }); } catch (err) { - assert.equal(err.message.substr(0, 19), 'Exceeded maxRetries'); + assert.strictEqual(err.message.substr(0, 19), 'Exceeded maxRetries'); } }); diff --git a/test/vehicle.unit.js b/test/vehicle.unit.js index 1d9802e8..5500d5e6 100644 --- a/test/vehicle.unit.js +++ b/test/vehicle.unit.js @@ -10,7 +10,7 @@ describe("vehicle.js", function () { sinon.stub(faker.vehicle, 'vehicle').returns('Ford Explorer'); var vehicle = faker.vehicle.vehicle(); - assert.equal(vehicle, 'Ford Explorer'); + assert.strictEqual(vehicle, 'Ford Explorer'); faker.vehicle.vehicle.restore(); }); }); @@ -20,7 +20,7 @@ describe("vehicle.js", function () { sinon.stub(faker.vehicle, 'manufacturer').returns('Porsche'); var manufacturer = faker.vehicle.manufacturer(); - assert.equal(manufacturer, 'Porsche'); + assert.strictEqual(manufacturer, 'Porsche'); faker.vehicle.manufacturer.restore(); }); }); @@ -30,7 +30,7 @@ describe("vehicle.js", function () { sinon.stub(faker.vehicle, 'type').returns('Minivan'); var type = faker.vehicle.type(); - assert.equal(type, 'Minivan'); + assert.strictEqual(type, 'Minivan'); faker.vehicle.type.restore(); }); }); @@ -40,7 +40,7 @@ describe("vehicle.js", function () { sinon.stub(faker.vehicle, 'fuel').returns('Hybrid'); var fuel = faker.vehicle.fuel(); - assert.equal(fuel, 'Hybrid'); + assert.strictEqual(fuel, 'Hybrid'); faker.vehicle.fuel.restore(); }); }); @@ -57,8 +57,18 @@ describe("vehicle.js", function () { sinon.stub(faker.vehicle, 'color').returns('black'); var color = faker.vehicle.color(); - assert.equal(color, 'black'); + assert.strictEqual(color, 'black'); faker.vehicle.color.restore(); }); }); + + describe("vrm()", function () { + it("returns a random vrm", function () { + sinon.stub(faker.vehicle, 'vrm').returns('MF59EEW'); + var vrm = faker.vehicle.vrm(); + + assert.equal(vrm, 'MF59EEW'); + faker.vehicle.vrm.restore(); + }); + }); }); |
