diff options
| author | David Kossoglyad <[email protected]> | 2021-03-18 02:42:43 +0200 |
|---|---|---|
| committer | David Kossoglyad <[email protected]> | 2021-03-18 02:42:43 +0200 |
| commit | 20e665cae7d7a516362d12a18ed14656ee52f0aa (patch) | |
| tree | 866ab2050671ce7f6aa4139c37f0d8634f2a0382 /test | |
| parent | f4b0a92e1743b2a894811ae7b189592b2b6c1979 (diff) | |
| parent | 81f27065297c3e29eb552911b9a5dc8755600efb (diff) | |
| download | faker-20e665cae7d7a516362d12a18ed14656ee52f0aa.tar.xz faker-20e665cae7d7a516362d12a18ed14656ee52f0aa.zip | |
Merge branch 'master' into feature/add-hebrew-localization
Diffstat (limited to 'test')
| -rw-r--r-- | test/address.unit.js | 143 | ||||
| -rw-r--r-- | test/animal.unit.js | 15 | ||||
| -rw-r--r-- | test/browser.unit.html | 1 | ||||
| -rw-r--r-- | test/commerce.unit.js | 10 | ||||
| -rw-r--r-- | test/company.unit.js | 12 | ||||
| -rw-r--r-- | test/database.unit.js | 12 | ||||
| -rw-r--r-- | test/datatype.unit.js | 289 | ||||
| -rw-r--r-- | test/date.unit.js | 13 | ||||
| -rw-r--r-- | test/finance.unit.js | 86 | ||||
| -rw-r--r-- | test/finance_iban.unit.js | 167 | ||||
| -rw-r--r-- | test/git.unit.js | 6 | ||||
| -rw-r--r-- | test/helpers.unit.js | 20 | ||||
| -rw-r--r-- | test/image.unit.js | 86 | ||||
| -rw-r--r-- | test/internet.unit.js | 53 | ||||
| -rw-r--r-- | test/locales.unit.js | 2 | ||||
| -rw-r--r-- | test/lorem.unit.js | 28 | ||||
| -rw-r--r-- | test/music.unit.js | 2 | ||||
| -rw-r--r-- | test/name.unit.js | 44 | ||||
| -rw-r--r-- | test/random.unit.js | 265 | ||||
| -rw-r--r-- | test/system.unit.js | 8 | ||||
| -rw-r--r-- | test/unique.unit.js | 8 | ||||
| -rw-r--r-- | test/vehicle.unit.js | 32 |
22 files changed, 905 insertions, 397 deletions
diff --git a/test/address.unit.js b/test/address.unit.js index dbd6cca6..0306f4f1 100644 --- a/test/address.unit.js +++ b/test/address.unit.js @@ -14,7 +14,7 @@ describe("address.js", function () { }); afterEach(function () { - faker.random.number.restore(); + faker.datatype.number.restore(); faker.address.cityPrefix.restore(); faker.name.firstName.restore(); faker.name.lastName.restore(); @@ -22,7 +22,7 @@ describe("address.js", function () { }); it("occasionally returns prefix + first name + suffix", function () { - sinon.stub(faker.random, 'number').returns(0); + sinon.stub(faker.datatype, 'number').returns(0); var city = faker.address.city(); assert.ok(city); @@ -33,7 +33,7 @@ describe("address.js", function () { }); it("occasionally returns prefix + first name", function () { - sinon.stub(faker.random, 'number').returns(1); + sinon.stub(faker.datatype, 'number').returns(1); var city = faker.address.city(); assert.ok(city); @@ -43,7 +43,7 @@ describe("address.js", function () { }); it("occasionally returns first name + suffix", function () { - sinon.stub(faker.random, 'number').returns(2); + sinon.stub(faker.datatype, 'number').returns(2); var city = faker.address.city(); assert.ok(city); @@ -52,7 +52,7 @@ describe("address.js", function () { }); it("occasionally returns last name + suffix", function () { - sinon.stub(faker.random, 'number').returns(3); + sinon.stub(faker.datatype, 'number').returns(3); var city = faker.address.city(); assert.ok(city); @@ -79,7 +79,7 @@ describe("address.js", function () { }); it("occasionally returns last name + suffix", function () { - sinon.stub(faker.random, 'number').returns(0); + sinon.stub(faker.datatype, 'number').returns(0); var street_name = faker.address.streetName(); assert.ok(street_name); @@ -87,11 +87,11 @@ describe("address.js", function () { assert.ok(faker.name.lastName.calledOnce); assert.ok(faker.address.streetSuffix.calledOnce); - faker.random.number.restore(); + faker.datatype.number.restore(); }); it("occasionally returns first name + suffix", function () { - sinon.stub(faker.random, 'number').returns(1); + sinon.stub(faker.datatype, 'number').returns(1); var street_name = faker.address.streetName(); assert.ok(street_name); @@ -100,7 +100,7 @@ describe("address.js", function () { assert.ok(!faker.name.lastName.called); assert.ok(faker.address.streetSuffix.calledOnce); - faker.random.number.restore(); + faker.datatype.number.restore(); }); it("trims trailing whitespace from the name", function() { @@ -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'); @@ -126,44 +131,46 @@ describe("address.js", function () { }); it("occasionally returns a 5-digit street number", function () { - sinon.stub(faker.random, 'number').returns(0); + sinon.stub(faker.datatype, '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(); + faker.datatype.number.restore(); }); it("occasionally returns a 4-digit street number", function () { - sinon.stub(faker.random, 'number').returns(1); + sinon.stub(faker.datatype, '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(); + faker.datatype.number.restore(); }); it("occasionally returns a 3-digit street number", function () { - sinon.stub(faker.random, 'number').returns(2); + sinon.stub(faker.datatype, '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); - faker.random.number.restore(); + faker.datatype.number.restore(); }); 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); }); }); @@ -222,7 +229,7 @@ describe("address.js", function () { var countryCode = faker.address.countryCode("alpha-3"); assert.ok(countryCode); assert.ok(faker.address.countryCode.called); - assert.equal(countryCode.length, 3); + assert.strictEqual(countryCode.length, 3, "The countryCode should be had 3 characters"); faker.address.countryCode.restore(); }); @@ -282,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(); }); @@ -291,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(); }); @@ -300,42 +307,42 @@ describe("address.js", function () { describe("latitude()", function () { it("returns random latitude", function () { for (var i = 0; i < 100; i++) { - sinon.spy(faker.random, 'number'); + sinon.spy(faker.datatype, 'number'); var latitude = faker.address.latitude(); assert.ok(typeof latitude === 'string'); var latitude_float = parseFloat(latitude); assert.ok(latitude_float >= -90.0); assert.ok(latitude_float <= 90.0); - assert.ok(faker.random.number.called); - faker.random.number.restore(); + assert.ok(faker.datatype.number.called); + faker.datatype.number.restore(); } }); it("returns latitude with min and max and default precision", function () { for (var i = 0; i < 100; i++) { - sinon.spy(faker.random, 'number'); + sinon.spy(faker.datatype, '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); - assert.ok(faker.random.number.called); - faker.random.number.restore(); + assert.ok(faker.datatype.number.called); + faker.datatype.number.restore(); } }); it("returns random latitude with custom precision", function () { for (var i = 0; i < 100; i++) { - sinon.spy(faker.random, 'number'); + sinon.spy(faker.datatype, '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); - assert.ok(faker.random.number.called); - faker.random.number.restore(); + assert.ok(faker.datatype.number.called); + faker.datatype.number.restore(); } }); }); @@ -343,42 +350,42 @@ describe("address.js", function () { describe("longitude()", function () { it("returns random longitude", function () { for (var i = 0; i < 100; i++) { - sinon.spy(faker.random, 'number'); + sinon.spy(faker.datatype, 'number'); var longitude = faker.address.longitude(); assert.ok(typeof longitude === 'string'); var longitude_float = parseFloat(longitude); assert.ok(longitude_float >= -180.0); assert.ok(longitude_float <= 180.0); - assert.ok(faker.random.number.called); - faker.random.number.restore(); + assert.ok(faker.datatype.number.called); + faker.datatype.number.restore(); } }); it("returns random longitude with min and max and default precision", function () { for (var i = 0; i < 100; i++) { - sinon.spy(faker.random, 'number'); + sinon.spy(faker.datatype, '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); - assert.ok(faker.random.number.called); - faker.random.number.restore(); + assert.ok(faker.datatype.number.called); + faker.datatype.number.restore(); } }); it("returns random longitude with custom precision", function () { for (var i = 0; i < 100; i++) { - sinon.spy(faker.random, 'number'); + sinon.spy(faker.datatype, '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); - assert.ok(faker.random.number.called); - faker.random.number.restore(); + assert.ok(faker.datatype.number.called); + faker.datatype.number.restore(); } }); }); @@ -387,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(); }) @@ -418,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); }) @@ -444,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); }) }) diff --git a/test/animal.unit.js b/test/animal.unit.js new file mode 100644 index 00000000..5f0e7506 --- /dev/null +++ b/test/animal.unit.js @@ -0,0 +1,15 @@ +if (typeof module !== 'undefined') { + var assert = require('assert'); + var sinon = require('sinon'); + var faker = require('../index'); +} + +describe("animal.js", function() { + + describe("dog()", function() { + it("returns random value from dog array", function() { + var dog = faker.animal.dog(); + assert.ok(faker.definitions.animal.dog.indexOf(dog) !== -1); + }); + }); +});
\ No newline at end of file diff --git a/test/browser.unit.html b/test/browser.unit.html index 467f35ce..d01126d0 100644 --- a/test/browser.unit.html +++ b/test/browser.unit.html @@ -18,6 +18,7 @@ <script src="helpers.unit.js"></script> <script src="internet.unit.js"></script> <script src="database.unit.js"></script> + <script src="datatype.unit.js"></script> <script src="lorem.unit.js"></script> <script src="name.unit.js"></script> <script src="phone_number.unit.js"></script> diff --git a/test/commerce.unit.js b/test/commerce.unit.js index 04d4277e..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 () { diff --git a/test/company.unit.js b/test/company.unit.js index 06292b35..df8b5a2c 100644 --- a/test/company.unit.js +++ b/test/company.unit.js @@ -9,34 +9,34 @@ describe("company.js", function () { it("sometimes returns three last names", function () { sinon.spy(faker.name, 'lastName'); - sinon.stub(faker.random, 'number').returns(2); + sinon.stub(faker.datatype, 'number').returns(2); var name = faker.company.companyName(); var parts = name.split(' '); assert.strictEqual(parts.length, 4); // account for word 'and' assert.ok(faker.name.lastName.calledThrice); - faker.random.number.restore(); + faker.datatype.number.restore(); faker.name.lastName.restore(); }); it("sometimes returns two last names separated by a hyphen", function () { sinon.spy(faker.name, 'lastName'); - sinon.stub(faker.random, 'number').returns(1); + sinon.stub(faker.datatype, 'number').returns(1); var name = faker.company.companyName(); var parts = name.split('-'); assert.ok(parts.length >= 2); assert.ok(faker.name.lastName.calledTwice); - faker.random.number.restore(); + faker.datatype.number.restore(); faker.name.lastName.restore(); }); it("sometimes returns a last name with a company suffix", function () { sinon.spy(faker.company, 'companySuffix'); sinon.spy(faker.name, 'lastName'); - sinon.stub(faker.random, 'number').returns(0); + sinon.stub(faker.datatype, 'number').returns(0); var name = faker.company.companyName(); var parts = name.split(' '); @@ -44,7 +44,7 @@ describe("company.js", function () { assert.ok(faker.name.lastName.calledOnce); assert.ok(faker.company.companySuffix.calledOnce); - faker.random.number.restore(); + faker.datatype.number.restore(); faker.name.lastName.restore(); faker.company.companySuffix.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/datatype.unit.js b/test/datatype.unit.js new file mode 100644 index 00000000..90c89391 --- /dev/null +++ b/test/datatype.unit.js @@ -0,0 +1,289 @@ +if (typeof module !== 'undefined') {
+ var assert = require('assert');
+ var sinon = require('sinon');
+ var _ = require('lodash');
+ var faker = require('../index');
+ var mersenne = require('../vendor/mersenne');
+}
+
+
+describe("datatype.js", function () {
+
+ describe("number", function () {
+
+ it("returns a random number given a maximum value as Number", function () {
+ var max = 10;
+ assert.ok(faker.datatype.number(max) <= max);
+ });
+
+ it("returns a random number given a maximum value as Object", function () {
+ var options = {max: 10};
+ assert.ok(faker.datatype.number(options) <= options.max);
+ });
+
+ it("returns a random number given a maximum value of 0", function () {
+ var options = {max: 0};
+ assert.ok(faker.datatype.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.datatype.number(options) <= options.max);
+ });
+
+ it("returns a random number between a range", function () {
+ var options = {min: 22, max: 33};
+ for (var i = 0; i < 100; i++) {
+ var randomNumber = faker.datatype.number(options);
+ assert.ok(randomNumber >= options.min);
+ assert.ok(randomNumber <= options.max);
+ }
+ });
+
+ it("provides numbers with a given precision", function () {
+ var options = {min: 0, max: 1.5, precision: 0.5};
+ var results = _.chain(_.range(50))
+ .map(function () {
+ return faker.datatype.number(options);
+ })
+ .uniq()
+ .value()
+ .sort();
+
+ assert.ok(_.includes(results, 0.5));
+ assert.ok(_.includes(results, 1.0));
+
+ assert.strictEqual(results[0], 0);
+ assert.strictEqual(_.last(results), 1.5);
+
+ });
+
+ it("provides numbers with a with exact precision", function () {
+ var options = {min: 0.5, max: 0.99, precision: 0.01};
+ for (var i = 0; i < 100; i++) {
+ var number = faker.datatype.number(options);
+ assert.strictEqual(number, Number(number.toFixed(2)));
+ }
+ });
+
+ it("should not modify the input object", function () {
+ var min = 1;
+ var max = 2;
+ var opts = {
+ min: min,
+ max: max
+ };
+
+ faker.datatype.number(opts);
+
+ assert.strictEqual(opts.min, min);
+ assert.strictEqual(opts.max, max);
+ });
+
+ });
+
+ describe("float", function () {
+
+ it("returns a random float with a default precision value (0.01)", function () {
+ var number = faker.datatype.float();
+ assert.strictEqual(number, Number(number.toFixed(2)));
+ });
+
+ it("returns a random float given a precision value", function () {
+ var number = faker.datatype.float(0.001);
+ assert.strictEqual(number, Number(number.toFixed(3)));
+ });
+
+ it("returns a random number given a maximum value as Object", function () {
+ var options = {max: 10};
+ assert.ok(faker.datatype.float(options) <= options.max);
+ });
+
+ it("returns a random number given a maximum value of 0", function () {
+ var options = {max: 0};
+ assert.ok(faker.datatype.float(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.datatype.float(options) <= options.max);
+ });
+
+ it("returns a random number between a range", function () {
+ var options = {min: 22, max: 33};
+ for (var i = 0; i < 5; i++) {
+ var randomNumber = faker.datatype.float(options);
+ assert.ok(randomNumber >= options.min);
+ assert.ok(randomNumber <= options.max);
+ }
+ });
+
+ it("provides numbers with a given precision", function () {
+ var options = {min: 0, max: 1.5, precision: 0.5};
+ var results = _.chain(_.range(50))
+ .map(function () {
+ return faker.datatype.float(options);
+ })
+ .uniq()
+ .value()
+ .sort();
+
+ assert.ok(_.includes(results, 0.5));
+ assert.ok(_.includes(results, 1.0));
+
+ assert.strictEqual(results[0], 0);
+ assert.strictEqual(_.last(results), 1.5);
+
+ });
+
+ it("provides numbers with a with exact precision", function () {
+ var options = {min: 0.5, max: 0.99, precision: 0.01};
+ for (var i = 0; i < 100; i++) {
+ var number = faker.datatype.float(options);
+ assert.strictEqual(number, Number(number.toFixed(2)));
+ }
+ });
+
+ it("should not modify the input object", function () {
+ var min = 1;
+ var max = 2;
+ var opts = {
+ min: min,
+ max: max
+ };
+
+ faker.datatype.float(opts);
+
+ assert.strictEqual(opts.min, min);
+ assert.strictEqual(opts.max, max);
+ });
+ });
+
+ describe('datetime', function () {
+ it('check validity of date and if returned value is created by Date()', function () {
+ var date = faker.datatype.datetime();
+ assert.strictEqual(typeof date, 'object');
+ assert.ok(!isNaN(date.getTime()));
+ assert.strictEqual(Object.prototype.toString.call(date), "[object Date]");
+ });
+ it('basic test with stubed value', function () {
+ var today = new Date();
+ sinon.stub(faker.datatype, 'number').returns(today);
+ var date = faker.datatype.datetime();
+ assert.strictEqual(today.valueOf(), date.valueOf());
+ faker.datatype.number.restore();
+ });
+
+ //generating a datetime with seeding is currently not working
+ });
+
+ describe('string', function () {
+ it('should generate a string value', function () {
+ var generateString = faker.datatype.string();
+ assert.strictEqual(typeof generateString, 'string');
+ assert.strictEqual(generateString.length, 10);
+ });
+
+ it('should generate a string value, checks seeding', function () {
+ faker.seed(100);
+ var generateString = faker.datatype.string();
+ assert.strictEqual(generateString, 'S_:GHQo.!/');
+ });
+
+ it('returns empty string if negative length is passed', function () {
+ var negativeValue = faker.datatype.number({min: -1000, max: -1});
+ var generateString = faker.datatype.string(negativeValue);
+ assert.strictEqual(generateString, '');
+ assert.strictEqual(generateString.length, 0);
+ });
+
+ it('returns string with length of 2^20 if bigger length value is passed', function () {
+ var overMaxValue = Math.pow(2, 28);
+ var generateString = faker.datatype.string(overMaxValue);
+ assert.strictEqual(generateString.length, (Math.pow(2, 20)));
+ });
+
+
+ });
+
+ describe('boolean', function () {
+ it('generates a boolean value', function () {
+ var bool = faker.datatype.boolean();
+ assert.strictEqual(typeof bool, 'boolean');
+ });
+ it('generates a boolean value, checks seeding', function (){
+ faker.seed(1);
+ var bool = faker.datatype.boolean();
+ assert.strictEqual(bool, false);
+ });
+ });
+
+ describe('UUID', function () {
+ it('generates a valid UUID', function () {
+ var UUID = faker.datatype.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('hexaDecimal', function () {
+ var hexaDecimal = faker.datatype.hexaDecimal;
+
+ it('generates single hex character when no additional argument was provided', function () {
+ var hex = hexaDecimal();
+ assert.ok(hex.match(/^(0x)[0-9a-f]{1}$/i));
+ });
+
+ it('generates a random hex string', function () {
+ var hex = hexaDecimal(5);
+ assert.ok(hex.match(/^(0x)[0-9a-f]+$/i));
+ });
+ });
+
+ describe('json', function () {
+ it('generates a valid json object', function () {
+ var jsonObject = faker.datatype.json();
+ assert.strictEqual(typeof jsonObject, 'string');
+ assert.ok(JSON.parse(jsonObject));
+ });
+
+ it('generates a valid json object, with seeding', function () {
+ faker.seed(10);
+ var jsonObject = faker.datatype.json();
+ var parsedObject = JSON.parse(jsonObject);
+ assert.strictEqual(typeof jsonObject, 'string');
+ assert.strictEqual(parsedObject.foo, '<\"N[JfnOW5');
+ assert.strictEqual(parsedObject.bar, 19806);
+ assert.strictEqual(parsedObject.bike, 'g909).``yl');
+ assert.strictEqual(parsedObject.a, 33607);
+ assert.strictEqual(parsedObject.b, 'sl3Y#dr<dv');
+ assert.strictEqual(parsedObject.name, 'c-SG.iCW_1');
+ assert.strictEqual(parsedObject.prop, 82608);
+ });
+ });
+
+ describe('array', function () {
+ it('generates an array', function () {
+ var stubArray = [0, 1, 3, 4, 5, 6, 1, 'a', 'b', 'c'];
+ sinon.stub(faker.datatype, 'array').returns(stubArray);
+ var generatedArray = faker.datatype.array();
+ assert.strictEqual(generatedArray.length, stubArray.length);
+ assert.strictEqual(stubArray, generatedArray);
+ faker.datatype.array.restore();
+
+ });
+
+ it('generates an array with passed size', function () {
+ var randomSize = faker.datatype.number();
+ var generatedArray = faker.datatype.array(randomSize);
+ assert.strictEqual(generatedArray.length, randomSize);
+ });
+
+ it('generates an array with 1 element, with seeding', function () {
+ faker.seed(10);
+ var generatedArray = faker.datatype.array(1);
+ assert.strictEqual(generatedArray[0], '<"N[JfnOW5');
+ });
+ });
+
+});
\ No newline at end of file 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 8935eaaa..aa617d0a 100644 --- a/test/finance.unit.js +++ b/test/finance.unit.js @@ -16,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); }); @@ -28,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); }); @@ -40,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); }); @@ -79,13 +79,13 @@ 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); }); it("should set a specified length", function () { - var expected = faker.random.number(20); + var expected = faker.datatype.number(20); expected = (expected == 0 || !expected || typeof expected == 'undefined') ? 4 : expected; @@ -93,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); }); @@ -101,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); }); @@ -119,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); }); @@ -132,13 +132,13 @@ 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); }); it("should work when random variables are passed into the arguments", function () { - var length = faker.random.number(20); + var length = faker.datatype.number(20); var ellipsis = (length % 2 === 0) ? true : false; var parens = !ellipsis; @@ -156,8 +156,8 @@ 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"); }); @@ -172,7 +172,7 @@ describe('finance.js', function () { 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 () { @@ -183,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'); }); @@ -192,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"); }); @@ -223,6 +223,26 @@ describe('finance.js', function () { assert.strictEqual(typeOfAmount , "string", "the amount type should be number"); }); + [false, undefined].forEach(function (autoFormat){ + it(`should return unformatted if autoformat is ${autoFormat}`, function() { + + const number = 6000; + const amount = faker.finance.amount(number, number, 0, undefined, autoFormat); + + assert.strictEqual(amount, number.toString()); + }); + }); + + it("should return the number formatted on the current locale", function() { + + const number = 6000, decimalPlaces = 2; + const expected = number.toLocaleString(undefined, {minimumFractionDigits: decimalPlaces}); + + const amount = faker.finance.amount(number, number, decimalPlaces, undefined, true); + + assert.strictEqual(amount, expected); + }); + }); describe('transactionType()', function () { @@ -259,7 +279,7 @@ describe('finance.js', function () { it("returns a random litecoin address", function(){ var litecoinAddress = faker.finance.litecoinAddress(); - assert.ok(litecoinAddress.match(/^[LM3][a-km-zA-HJ-NP-Z1-9]{26,33}$/)); + assert.ok(litecoinAddress.match(/^[LM3][1-9a-km-zA-HJ-NP-Z]{25,32}$/)); }); }); @@ -349,8 +369,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 () { @@ -364,10 +395,19 @@ describe('finance.js', function () { }); describe("transactionDescription()", function() { - it("returns a random transaction description", function() { - var transactionDescription = faker.finance.transactionDescription(); + beforeEach(function () { + sinon.spy(faker.helpers, 'createTransaction'); + }); - assert.ok(transactionDescription); - }) - }) + afterEach(function () { + faker.helpers.createTransaction.restore(); + }); + + it("returns a random transaction description", function() { + var transactionDescription = faker.finance.transactionDescription(); + + assert.ok(transactionDescription); + assert.ok(faker.helpers.createTransaction.calledOnce); + }); + }); }); diff --git a/test/finance_iban.unit.js b/test/finance_iban.unit.js new file mode 100644 index 00000000..9ca3c465 --- /dev/null +++ b/test/finance_iban.unit.js @@ -0,0 +1,167 @@ +if (typeof module !== 'undefined') { + var assert = require('assert'); + var faker = require('../index'); +} + +function getAnIbanByCountry(countryCode) { + var iban = faker.finance.iban(); + var maxTry = 100000; + var countTry = maxTry; + while (countTry && iban.substring(0, 2) != countryCode) { + faker.seed(100000- countTry); + iban = faker.finance.iban(); + countTry--; + } + + if (countTry === 0) { + console.log('Not found with 10000 seed, vraiment pas de bol'); + } else if (countTry < maxTry) { + console.log('you can optimize this helper by add faker.seed(' + (100000 - 1 - countTry) + ') before the call of getAnIbanByCountry()'); + } + // console.log(iban); + + return iban; +} + +describe('finance_iban.js', function () { + + describe("issue_944 IBAN Georgia", function () { + // Georgia + // https://transferwise.com/fr/iban/georgia + // Length 22 + // BBAN 2c,16n + // GEkk bbcc cccc cccc cccc cc + // b = National bank code (alpha) + // c = Account number + + // example IBAN GE29 NB00 0000 0101 9049 17 + + var ibanLib = require('../lib/iban'); + + it("IBAN for Georgia is correct", function () { + + faker.seed(17); + var iban = getAnIbanByCountry('GE'); + var ibanFormated = iban.match(/.{1,4}/g).join(" "); + var bban = iban.substring(4) + iban.substring(0, 4); + + assert.equal(22, iban.length, 'GE IBAN would be 22 chars length, given is ' + iban.length); + + assert.ok(iban.substring(0, 2).match(/^[A-Z]{2}$/), iban.substring(0, 2) + ' must contains only characters in GE IBAN ' + ibanFormated); + assert.ok(iban.substring(2, 4).match(/^\d{2}$/), iban.substring(2, 4) + ' must contains only digit in GE IBAN ' + ibanFormated); + assert.ok(iban.substring(4, 6).match(/^[A-Z]{2}$/), iban.substring(4, 6) + ' must contains only characters in GE IBAN ' + ibanFormated); + assert.ok(iban.substring(6, 24).match(/^\d{16}$/), iban.substring(6, 24) + ' must contains only characters in GE IBAN ' + ibanFormated); + + assert.equal(ibanLib.mod97(ibanLib.toDigitString(bban)), 1, "the result should be equal to 1"); + }); + }); + + describe("issue_945 IBAN Pakistan", function () { + + // https://transferwise.com/fr/iban/pakistan + // Example IBAN Pakistan + // PK36SCBL0000001123456702 + // IBAN en format imprimé + // PK36 SCBL 0000 0011 2345 6702 + // Code pays 2 alpha + // PK + // Key 2 digits + // Bank Code 4 alpha + // Account Code 16 digits + // Total Length 24 chars + + var ibanLib = require('../lib/iban'); + + it("IBAN for Pakistan is correct", function () { + + faker.seed(28); + var iban = getAnIbanByCountry('PK'); + var ibanFormated = iban.match(/.{1,4}/g).join(" "); + var bban = iban.substring(4) + iban.substring(0, 4); + + assert.equal(24, iban.length, 'PK IBAN would be 24 chars length, given is ' + iban.length); + + assert.ok(iban.substring(0, 2).match(/^[A-Z]{2}$/), iban.substring(0, 2) + ' must contains only characters in PK IBAN ' + ibanFormated); + assert.ok(iban.substring(2, 4).match(/^\d{2}$/), iban.substring(2, 4) + ' must contains only digit in PK IBAN ' + ibanFormated); + assert.ok(iban.substring(4, 8).match(/^[A-Z]{4}$/), iban.substring(4, 8) + ' must contains only characters in PK IBAN ' + ibanFormated); + assert.ok(iban.substring(8, 24).match(/^\d{16}$/), iban.substring(8, 24) + ' must contains only digits in PK IBAN ' + ibanFormated); + + assert.equal(ibanLib.mod97(ibanLib.toDigitString(bban)), 1, "the result should be equal to 1"); + }); + }); + + describe("issue_946 IBAN Turkish", function () { + + // https://transferwise.com/fr/iban/turkey + // Un IBAN en Turquie est constitué de 26 caractères : + // + // Code pays à 2 lettres + // Clé de contrôle à 2 chiffres + // 5 caractères du SWIFT/BIC de la banque + // Code à 1 chiffres pour le code national + // Code à 16 chiffres pour le numéro de compte bancaire + // Vous avez déjà un code IBAN ? + // + // Exemple d'IBAN en Turquie TR330006100519786457841326 + // IBAN en format imprimé TR33 0006 1005 1978 6457 8413 26 + // Code pays TR + // Clé de contrôle 33 + // Code banque 00061 + // Chiffre d'indicatif national 0 + // Numéro de compte bancaire 0519786457841326 + + var ibanLib = require('../lib/iban'); + + it("IBAN for Turkish is correct", function () { + + faker.seed(37); + + var iban = getAnIbanByCountry('TR'); + var ibanFormated = iban.match(/.{1,4}/g).join(" "); + var bban = iban.substring(4) + iban.substring(0, 4); + + assert.equal(26, iban.length, 'PK IBAN would be 26 chars length, given is ' + iban.length); + + assert.ok(iban.substring(0, 2).match(/^[A-Z]{2}$/), 'Country Code:' + iban.substring(0, 2) + ' must contains only characters in PK IBAN ' + ibanFormated); + assert.ok(iban.substring(2, 4).match(/^\d{2}$/), 'Control key:' + iban.substring(2, 4) + ' must contains only digit in PK IBAN ' + ibanFormated); + assert.ok(iban.substring(4, 9).match(/^\d{5}$/), 'Swift Bank Code:' + iban.substring(4, 9) + ' must contains only digits in PK IBAN ' + ibanFormated); + assert.ok(iban.substring(9, 10).match(/^\d{1}$/), 'National Digit:' + iban.substring(9, 10) + ' must contains only digits in PK IBAN ' + ibanFormated); + assert.ok(iban.substring(10, 26).match(/^\d{16}$/), 'Account Code:' + iban.substring(10, 26) + ' must contains only digits in PK IBAN ' + ibanFormated); + + assert.ok(iban.substring(2, 26).match(/^\d{24}$/), 'No character after TR ' + ibanFormated); + + assert.equal(ibanLib.mod97(ibanLib.toDigitString(bban)), 1, "the result should be equal to 1"); + }); + }); + + describe("issue_846 IBAN Azerbaijan", function () { + // Azerbaijan + // https://transferwise.com/fr/iban/azerbaijan + // Length 28 + // BBAN 4c,20n + // GEkk bbbb cccc cccc cccc cccc cccc + // b = National bank code (alpha) + // c = Account number + + // example IBAN AZ21 NABZ 0000 0000 1370 1000 1944 + + var ibanLib = require('../lib/iban'); + + it("IBAN for Azerbaijan is correct", function () { + + faker.seed(21); + var iban = getAnIbanByCountry('AZ'); + var ibanFormated = iban.match(/.{1,4}/g).join(" "); + var bban = iban.substring(4) + iban.substring(0, 4); + + assert.equal(28, iban.length, 'AZ IBAN would be 28 chars length, given is ' + iban.length); + + assert.ok(iban.substring(0, 2).match(/^[A-Z]{2}$/), iban.substring(0, 2) + ' must contains only characters in AZ IBAN ' + ibanFormated); + assert.ok(iban.substring(2, 4).match(/^\d{2}$/), iban.substring(2, 4) + ' must contains only digit in AZ IBAN ' + ibanFormated); + assert.ok(iban.substring(4, 8).match(/^[A-Z]{4}$/), iban.substring(4, 8) + ' must contains only characters in AZ IBAN ' + ibanFormated); + assert.ok(iban.substring(8, 28).match(/^\d{20}$/), iban.substring(8, 28) + ' must contains 20 characters in AZ IBAN ' + ibanFormated); + + assert.equal(ibanLib.mod97(ibanLib.toDigitString(bban)), 1, "the result should be equal to 1"); + }); + }); + }); diff --git a/test/git.unit.js b/test/git.unit.js index df86be0b..46e25724 100644 --- a/test/git.unit.js +++ b/test/git.unit.js @@ -31,7 +31,7 @@ describe("git.js", function() { sinon.spy(faker.internet, 'email'); sinon.spy(faker.name, 'firstName'); sinon.spy(faker.name, 'lastName'); - sinon.spy(faker.random, 'number'); + sinon.spy(faker.datatype, 'number'); }); afterEach(function() { @@ -40,13 +40,13 @@ describe("git.js", function() { faker.internet.email.restore(); faker.name.firstName.restore(); faker.name.lastName.restore(); - faker.random.number.restore(); + faker.datatype.number.restore(); }); it("returns merge entry at random", function() { faker.git.commitEntry(); - assert.ok(faker.random.number.called); + assert.ok(faker.datatype.number.called); }); it("returns a commit entry with git commit message and sha", function() { diff --git a/test/helpers.unit.js b/test/helpers.unit.js index f08be413..7436da03 100644 --- a/test/helpers.unit.js +++ b/test/helpers.unit.js @@ -32,11 +32,11 @@ describe("helpers.js", function () { describe("shuffle()", function () { it("the output is the same length as the input", function () { - sinon.spy(faker.random, 'number'); + sinon.spy(faker.datatype, 'number'); var shuffled = faker.helpers.shuffle(["a", "b"]); assert.ok(shuffled.length === 2); - assert.ok(faker.random.number.calledWith(1)); - faker.random.number.restore(); + assert.ok(faker.datatype.number.calledWith(1)); + faker.datatype.number.restore(); }); it("empty array returns empty array", function () { @@ -47,39 +47,39 @@ describe("helpers.js", function () { 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.deepEqual(shuffled, 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.deepEqual(shuffled, ["b", "e", "a", "d", "j", "i", "h", "c", "g", "f"]); + 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 ca4c6c29..66664b9e 100644 --- a/test/image.unit.js +++ b/test/image.unit.js @@ -10,40 +10,40 @@ describe("image.js", function () { it("returns a random image url from lorempixel", function () { var imageUrl = faker.image.lorempicsum.imageUrl(); - assert.equal(imageUrl, 'https://picsum.photos/640/480'); + 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.equal(imageUrl, 'https://picsum.photos/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.equal(imageUrl, 'https://picsum.photos/100/100?grayscale'); + 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.equal(imageUrl, 'https://picsum.photos/100/100?grayscale&blur=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.equal(imageUrl, 'https://picsum.photos/100/100?blur=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.equal(imageUrl, 'https://picsum.photos/seed/picsum/100/100'); + assert.strictEqual(imageUrl, 'https://picsum.photos/seed/picsum/100/100'); }); }); describe("avatar()", function () { - it("return a random avatar from UIFaces", function () { - assert.notEqual(-1, faker.image.lorempicsum.avatar().indexOf('s3.amazonaws.com/uifaces/faces')); + it("return a random avatar from FakerCloud", function () { + assert.notStrictEqual(-1, faker.image.lorempicsum.avatar().indexOf('cdn.fakercloud.com/avatars')); }) }); @@ -51,21 +51,21 @@ describe("image.js", function () { it("returns a random URL with grayscale image", function () { var imageUrl = faker.image.lorempicsum.imageGrayscale(100, 100, true); - assert.equal(imageUrl, 'https://picsum.photos/100/100?grayscale'); + 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.equal(imageUrl, 'https://picsum.photos/100/100?blur=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.equal(imageUrl, 'https://picsum.photos/seed/picsum/100/100'); + assert.strictEqual(imageUrl, 'https://picsum.photos/seed/picsum/100/100'); }); }); }); @@ -75,100 +75,100 @@ describe("image.js", function () { it("returns a random image url from lorempixel", function () { var imageUrl = faker.image.lorempixel.imageUrl(); - assert.equal(imageUrl, 'https://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, 'https://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, 'https://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')); + it("return a random avatar from FakerCloud", function () { + assert.notStrictEqual(-1, faker.image.lorempixel.avatar().indexOf('cdn.fakercloud.com/avatars')); }) }); describe("abstract()", function () { it("returns a random abstract image url", function () { var abstract = faker.image.lorempixel.abstract(); - assert.equal(abstract, 'https://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, 'https://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, 'https://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, 'https://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, 'https://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, 'https://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, 'https://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, 'https://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, 'https://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, 'https://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, 'https://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, 'https://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, 'https://lorempixel.com/640/480/transport'); + assert.strictEqual(transport, 'https://lorempixel.com/640/480/transport'); }); }); }); @@ -178,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 a554cea0..48006472 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(); }); @@ -34,19 +43,19 @@ describe("internet.js", function () { describe("userName()", function () { it("occasionally returns a single firstName", function () { - sinon.stub(faker.random, 'number').returns(0); + sinon.stub(faker.datatype, 'number').returns(0); sinon.spy(faker.name, 'firstName'); var username = faker.internet.userName(); assert.ok(username); assert.ok(faker.name.firstName.called); - faker.random.number.restore(); + faker.datatype.number.restore(); faker.name.firstName.restore(); }); it("occasionally returns a firstName with a period or hyphen and a lastName", function () { - sinon.stub(faker.random, 'number').returns(1); + sinon.stub(faker.datatype, 'number').returns(1); sinon.spy(faker.name, 'firstName'); sinon.spy(faker.name, 'lastName'); sinon.spy(faker.random, 'arrayElement'); @@ -57,7 +66,7 @@ describe("internet.js", function () { assert.ok(faker.name.lastName.called); assert.ok(faker.random.arrayElement.calledWith(['.', '_'])); - faker.random.number.restore(); + faker.datatype.number.restore(); faker.name.firstName.restore(); faker.name.lastName.restore(); faker.random.arrayElement.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(); @@ -107,21 +116,29 @@ describe("internet.js", function () { }); it('should occasionally return http', function () { - sinon.stub(faker.random, 'number').returns(0); + sinon.stub(faker.datatype, 'number').returns(0); var protocol = faker.internet.protocol(); assert.ok(protocol); assert.strictEqual(protocol, 'http'); - faker.random.number.restore(); + faker.datatype.number.restore(); }); it('should occasionally return https', function () { - sinon.stub(faker.random, 'number').returns(1); + sinon.stub(faker.datatype, 'number').returns(1); var protocol = faker.internet.protocol(); assert.ok(protocol); assert.strictEqual(protocol, 'https'); - faker.random.number.restore(); + faker.datatype.number.restore(); + }); + }); + + 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)); }); }); @@ -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); }); }); @@ -165,7 +190,7 @@ describe("internet.js", function () { var ua1 = faker.internet.userAgent(); faker.seed(1); var ua2 = faker.internet.userAgent(); - assert.equal(ua1, ua2); + assert.strictEqual(ua1, ua2); }); }); 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 04c03a55..d7853a4b 100644 --- a/test/lorem.unit.js +++ b/test/lorem.unit.js @@ -18,7 +18,7 @@ describe("lorem.js", function () { it("returns a word with the requested length", function () { var str = faker.lorem.word(5); assert.ok(typeof str === 'string'); - assert.equal(str.length, 5); + assert.strictEqual(str.length, 5); }); }); }); @@ -37,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); }); }); @@ -47,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); }); }); }); @@ -62,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 () { @@ -91,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(); @@ -107,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(); @@ -124,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)); @@ -145,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(); @@ -159,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(); }); @@ -176,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(); @@ -192,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(); @@ -212,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(); @@ -226,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 index df679333..3abc0787 100644 --- a/test/music.unit.js +++ b/test/music.unit.js @@ -10,7 +10,7 @@ describe("music.js", function () { sinon.stub(faker.music, 'genre').returns('Rock'); var genre = faker.music.genre(); - assert.equal(genre, 'Rock'); + assert.strictEqual(genre, 'Rock'); faker.music.genre.restore(); }); }); diff --git a/test/name.unit.js b/test/name.unit.js index 361fde4e..89d7effa 100644 --- a/test/name.unit.js +++ b/test/name.unit.js @@ -16,7 +16,7 @@ describe("name.js", 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(); }); @@ -46,7 +46,7 @@ describe("name.js", function () { var last_name = faker.name.lastName(); - assert.equal(last_name, 'foo'); + assert.strictEqual(last_name, 'foo'); faker.name.lastName.restore(); }); @@ -59,7 +59,7 @@ describe("name.js", function () { var middle_name = faker.name.middleName(); - assert.equal(middle_name, 'foo'); + assert.strictEqual(middle_name, 'foo'); faker.name.middleName.restore(); }); @@ -85,13 +85,13 @@ describe("name.js", function () { it("returns male prefix", function () { var middle_name = faker.name.middleName(0); - assert.equal(middle_name, 'Genaddiesvich') + assert.strictEqual(middle_name, 'Genaddiesvich') }); it("returns female prefix", function () { var middle_name = faker.name.middleName(1); - assert.equal(middle_name, 'Genaddievna'); + assert.strictEqual(middle_name, 'Genaddievna'); }); }); }); @@ -99,28 +99,28 @@ describe("name.js", function () { describe("findName()", function () { it("usually returns a first name and last name", function () { - sinon.stub(faker.random, 'number').returns(5); + sinon.stub(faker.datatype, 'number').returns(5); var name = faker.name.findName(); assert.ok(name); var parts = name.split(' '); assert.strictEqual(parts.length, 2); - faker.random.number.restore(); + faker.datatype.number.restore(); }); it("occasionally returns a first name and last name with a prefix", function () { - sinon.stub(faker.random, 'number').returns(0); + sinon.stub(faker.datatype, 'number').returns(0); var name = faker.name.findName(); var parts = name.split(' '); assert.ok(parts.length >= 3); - faker.random.number.restore(); + faker.datatype.number.restore(); }); it("occasionally returns a male full name with a prefix", function () { - sinon.stub(faker.random, 'number') + sinon.stub(faker.datatype, 'number') .withArgs(8).returns(0) // with prefix .withArgs(1).returns(0); // gender male @@ -130,16 +130,16 @@ 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.datatype.number.restore(); faker.name.prefix.restore(); faker.name.firstName.restore(); faker.name.lastName.restore(); }); it("occasionally returns a female full name with a prefix", function () { - sinon.stub(faker.random, 'number') + sinon.stub(faker.datatype, 'number') .withArgs(8).returns(0) // with prefix .withArgs(1).returns(1); // gender female @@ -149,25 +149,25 @@ 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.datatype.number.restore(); faker.name.prefix.restore(); faker.name.firstName.restore(); faker.name.lastName.restore(); }); it("occasionally returns a first name and last name with a suffix", function () { - sinon.stub(faker.random, 'number').returns(1); + sinon.stub(faker.datatype, 'number').returns(1); sinon.stub(faker.name, 'suffix').returns('Jr.'); var name = faker.name.findName(); 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(); + faker.datatype.number.restore(); }); it("needs to work with specific locales and respect the fallbacks", function () { @@ -183,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(); }); @@ -231,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 () { @@ -267,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..a596d75b 100644 --- a/test/random.unit.js +++ b/test/random.unit.js @@ -9,170 +9,50 @@ if (typeof module !== 'undefined') { describe("random.js", function () { describe("number", function() { - - it("returns a random number given a maximum value as Number", function() { - var max = 10; - 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); - }); - - 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() { - var options = { min: 22, max: 33 }; - for(var i = 0; i < 100; i++) { - var randomNumber = faker.random.number(options); - assert.ok(randomNumber >= options.min); - assert.ok(randomNumber <= options.max); - } - }); - - it("provides numbers with a given precision", function() { - var options = { min: 0, max: 1.5, precision: 0.5 }; - var results = _.chain(_.range(50)) - .map(function() { - return faker.random.number(options); - }) - .uniq() - .value() - .sort(); - - assert.ok(_.includes(results, 0.5)); - assert.ok(_.includes(results, 1.0)); - - assert.equal(results[0], 0); - assert.equal(_.last(results), 1.5); - - }); - - it("provides numbers with a with exact precision", 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))); - } - }); - - it("should not modify the input object", function() { - var min = 1; - var max = 2; - var opts = { - min: min, - max: max - }; - - faker.random.number(opts); - - assert.equal(opts.min, min); - assert.equal(opts.max, max); + it("random.number() uses datatype module and prints deprecation warning", function() { + sinon.spy(console, 'log'); + sinon.spy(faker.datatype, 'number'); + faker.random.number(); + assert.ok(faker.datatype.number.called); + assert.ok(console.log.calledWith('Deprecation Warning: faker.random.number is now located in faker.datatype.number')); + faker.datatype.number.restore(); + console.log.restore(); }); 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'); + }); }); describe("float", 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))); - }); - - it("returns a random float given a precision value", function() { - var number = faker.random.float(0.001); - assert.equal(number, Number(number.toFixed(3))); - }); - - it("returns a random number given a maximum value as Object", function() { - var options = { max: 10 }; - assert.ok(faker.random.float(options) <= options.max); - }); - - it("returns a random number given a maximum value of 0", function() { - var options = { max: 0 }; - assert.ok(faker.random.float(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.float(options) <= options.max); - }); - - it("returns a random number between a range", function() { - var options = { min: 22, max: 33 }; - for(var i = 0; i < 5; i++) { - var randomNumber = faker.random.float(options); - assert.ok(randomNumber >= options.min); - assert.ok(randomNumber <= options.max); - } - }); - - it("provides numbers with a given precision", function() { - var options = { min: 0, max: 1.5, precision: 0.5 }; - var results = _.chain(_.range(50)) - .map(function() { - return faker.random.float(options); - }) - .uniq() - .value() - .sort(); - - assert.ok(_.includes(results, 0.5)); - assert.ok(_.includes(results, 1.0)); - - assert.equal(results[0], 0); - assert.equal(_.last(results), 1.5); - - }); - - it("provides numbers with a with exact precision", 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))); - } - }); - - it("should not modify the input object", function() { - var min = 1; - var max = 2; - var opts = { - min: min, - max: max - }; - - faker.random.float(opts); - - assert.equal(opts.min, min); - assert.equal(opts.max, max); + it("random.float() uses datatype module and prints deprecation warning", function() { + sinon.spy(console, 'log'); + sinon.spy(faker.datatype, 'float'); + faker.random.float(); + assert.ok(faker.datatype.float.called); + assert.ok(console.log.calledWith('Deprecation Warning: faker.random.float is now located in faker.datatype.float')); + faker.datatype.float.restore(); + console.log.restore(); }); }); @@ -229,17 +109,26 @@ describe("random.js", function () { }); 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)); - }) - }) + it("random.uuid() uses datatype module and prints deprecation warning", function() { + sinon.spy(console, 'log'); + sinon.spy(faker.datatype, 'uuid'); + faker.random.uuid(); + assert.ok(faker.datatype.uuid.called); + assert.ok(console.log.calledWith('Deprecation Warning: faker.random.uuid is now located in faker.datatype.uuid')); + faker.datatype.uuid.restore(); + console.log.restore(); + }); + }); describe('boolean', function() { - it('should generate a boolean value', function() { - var bool = faker.random.boolean(); - assert.ok(typeof bool == 'boolean'); + it("random.boolean() uses datatype module and prints deprecation warning", function() { + sinon.spy(console, 'log'); + sinon.spy(faker.datatype, 'boolean'); + faker.random.boolean(); + assert.ok(faker.datatype.boolean.called); + assert.ok(console.log.calledWith('Deprecation Warning: faker.random.boolean is now located in faker.datatype.boolean')); + faker.datatype.boolean.restore(); + console.log.restore(); }); }); @@ -260,46 +149,66 @@ describe("random.js", function () { it('should return single letter when no count provided', function() { assert.ok(alpha().length === 1); - }) + }); it('should return lowercase letter when no upcase option provided', function() { assert.ok(alpha().match(/[a-z]/)); - }) + }); it('should return uppercase when upcase option is true', function() { assert.ok(alpha({ upcase: true }).match(/[A-Z]/)); - }) + }); it('should generate many random letters', function() { assert.ok(alpha(5).length === 5); - }) - }) + }); + + it('should be able to ban some characters', function() { + var alphaText = alpha(5,{bannedChars:['a', 'p']}); + assert.ok(alphaText.length === 5); + assert.ok(alphaText.match(/[b-oq-z]/)); + }); + it('should be able handle mistake in banned characters array', function() { + var alphaText = alpha(5,{bannedChars:['a', 'a', 'p']}); + assert.ok(alphaText.length === 5); + assert.ok(alphaText.match(/[b-oq-z]/)); + }); + }); describe('alphaNumeric', function() { var alphaNumeric = faker.random.alphaNumeric; it('should generate single character when no additional argument was provided', function() { assert.ok(alphaNumeric().length === 1); - }) + }); it('should generate many random characters', function() { assert.ok(alphaNumeric(5).length === 5); - }) - }) - - describe('hexaDecimal', function() { - var hexaDecimal = faker.random.hexaDecimal; + }); - it('should generate single hex character when no additional argument was provided', function() { - var hex = hexaDecimal(); - assert.ok(hex.match(/^(0x)[0-9a-f]{1}$/i)); - }) + it('should be able to ban some characters', function() { + var alphaText = alphaNumeric(5,{bannedChars:['a','p']}); + assert.ok(alphaText.length === 5); + assert.ok(alphaText.match(/[b-oq-z]/)); + }); + it('should be able handle mistake in banned characters array', function() { + var alphaText = alphaNumeric(5,{bannedChars:['a','p','a']}); + assert.ok(alphaText.length === 5); + assert.ok(alphaText.match(/[b-oq-z]/)); + }); + }); - it('should generate a random hex string', function() { - var hex = hexaDecimal(5); - assert.ok(hex.match(/^(0x)[0-9a-f]+$/i)); - }) - }) + describe('hexaDecimal', function() { + it("random.hexaDecimal() uses datatype module and prints deprecation warning", function() { + sinon.spy(console, 'log'); + sinon.spy(faker.datatype, 'hexaDecimal'); + faker.random.hexaDecimal(); + assert.ok(faker.datatype.hexaDecimal.called); + assert.ok(console.log.calledWith('Deprecation Warning: faker.random.hexaDecimal is now located in faker.datatype.hexaDecimal')); + faker.datatype.hexaDecimal.restore(); + console.log.restore(); + }); + }); describe("mersenne twister", function() { it("returns a random number without given min / max arguments", function() { @@ -319,6 +228,6 @@ describe("random.js", function () { mersenne.seed_array('abc'); }, Error); }); - }) + }); }); 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/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..80504581 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(); }); }); @@ -48,7 +48,7 @@ describe("vehicle.js", function () { describe("vin()", function () { it("returns valid vin number", function () { var vin = faker.vehicle.vin(); - assert.ok(vin.match(/^[A-Z0-9]{10}[A-Z]{1}[A-Z0-9]{1}\d{5}$/)); + assert.ok(vin.match(/^([A-HJ-NPR-Z0-9]{10}[A-HJ-NPR-Z0-9]{1}[A-HJ-NPR-Z0-9]{1}\d{5})$/)); }); }); @@ -57,8 +57,28 @@ 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(); + }); + }); + + describe("bicycle()", function () { + it("returns a random type of bicycle", function () { + sinon.stub(faker.vehicle, 'bicycle').returns('Adventure Road Bicycle'); + var bicycle = faker.vehicle.bicycle(); + + assert.equal(bicycle, 'Adventure Road Bicycle'); + faker.vehicle.bicycle.restore(); + }); + }); }); |
