aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJohnny Reina <[email protected]>2017-10-02 04:44:33 -0500
committerJohnny Reina <[email protected]>2017-10-02 04:44:33 -0500
commitf490bacd55501ce33de068e3044c1f0d8d3e149c (patch)
tree694204e726722ab170e07031bd94939f0b2fe41d /test
parentf5fc455a8fa2203970199fcc364acd32ab657106 (diff)
parentef5d2e145cac62afe61f64d6afa1ff212425f327 (diff)
downloadfaker-f490bacd55501ce33de068e3044c1f0d8d3e149c.tar.xz
faker-f490bacd55501ce33de068e3044c1f0d8d3e149c.zip
Merge branch 'master' of https://github.com/Marak/faker.js
Diffstat (limited to 'test')
-rw-r--r--test/address.unit.js26
-rw-r--r--test/commerce.unit.js2
-rw-r--r--test/date.unit.js10
-rw-r--r--test/finance.unit.js84
-rw-r--r--test/helpers.unit.js60
-rw-r--r--test/locales.unit.js13
-rw-r--r--test/phone_number.unit.js21
-rw-r--r--test/random.unit.js54
-rw-r--r--test/support/luhnCheck.js18
-rw-r--r--test/unique.unit.js54
10 files changed, 336 insertions, 6 deletions
diff --git a/test/address.unit.js b/test/address.unit.js
index b5df1d24..5aba2c36 100644
--- a/test/address.unit.js
+++ b/test/address.unit.js
@@ -265,6 +265,19 @@ describe("address.js", function () {
faker.random.number.restore();
}
});
+
+ it("returns latitude with min and max", function () {
+ for (var i = 0; i < 100; i++) {
+ sinon.spy(faker.random, 'number');
+ var latitude = faker.address.latitude(-5, 5);
+ assert.ok(typeof latitude === 'string');
+ 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();
+ }
+ });
});
describe("longitude()", function () {
@@ -280,6 +293,19 @@ describe("address.js", function () {
faker.random.number.restore();
}
});
+
+ it("returns random longitude with min and max", function () {
+ for (var i = 0; i < 100; i++) {
+ sinon.spy(faker.random, 'number');
+ var longitude = faker.address.longitude(100, -30);
+ assert.ok(typeof longitude === 'string');
+ 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();
+ }
+ });
});
});
diff --git a/test/commerce.unit.js b/test/commerce.unit.js
index 1b5c5bdd..a0336c47 100644
--- a/test/commerce.unit.js
+++ b/test/commerce.unit.js
@@ -129,4 +129,4 @@ describe("commerce.js", function() {
});
-}); \ No newline at end of file
+});
diff --git a/test/date.unit.js b/test/date.unit.js
index ffbc3201..bd283d07 100644
--- a/test/date.unit.js
+++ b/test/date.unit.js
@@ -67,6 +67,16 @@ describe("date.js", function () {
});
+ describe("soon()", function () {
+ it("returns a date N days into the future", function () {
+
+ var date = faker.date.soon(30);
+
+ assert.ok(date >= new Date());
+ });
+
+ });
+
describe("between()", function () {
it("returns a random date between the dates given", function () {
diff --git a/test/finance.unit.js b/test/finance.unit.js
index 3593ce72..a879d584 100644
--- a/test/finance.unit.js
+++ b/test/finance.unit.js
@@ -222,11 +222,91 @@ describe('finance.js', function () {
describe("bitcoinAddress()", function(){
it("returns a random bitcoin address", function(){
var bitcoinAddress = faker.finance.bitcoinAddress();
-
+
assert.ok(bitcoinAddress.match(/^[A-Z0-9.]{27,34}$/));
});
});
+ describe("ethereumAddress()", function(){
+ it("returns a random ethereum address", function(){
+ var ethereumAddress = faker.finance.ethereumAddress();
+ assert.ok(ethereumAddress.match(/^(0x)[0-9a-f]{40}$/i));
+ });
+ });
+
+ describe("creditCardNumber()", function(){
+ var luhnFormula = require("./support/luhnCheck.js");
+
+ it("returns a random credit card number", function(){
+ var number = faker.finance.creditCardNumber();
+ number = number.replace(/\D/g,""); // remove formating
+ console.log("version:", process.version, number, number.length);
+ assert.ok(number.length >= 13 && number.length <= 20);
+ assert.ok(number.match(/^[0-9]{13,20}$/));
+ assert.ok(luhnFormula(number));
+ });
+
+ it("returns a valid credit card number", function(){
+ assert.ok(luhnFormula(faker.finance.creditCardNumber("")));
+ assert.ok(luhnFormula(faker.finance.creditCardNumber()));
+ assert.ok(luhnFormula(faker.finance.creditCardNumber()));
+ assert.ok(luhnFormula(faker.finance.creditCardNumber("visa")));
+ assert.ok(luhnFormula(faker.finance.creditCardNumber("mastercard")));
+ assert.ok(luhnFormula(faker.finance.creditCardNumber("discover")));
+ assert.ok(luhnFormula(faker.finance.creditCardNumber()));
+ assert.ok(luhnFormula(faker.finance.creditCardNumber()));
+ });
+ it("returns a correct credit card number when issuer provided", function(){
+ //TODO: implement checks for each format with regexp
+ var visa = faker.finance.creditCardNumber("visa");
+ assert.ok(visa.match(/^4(([0-9]){12}|([0-9]){3}(\-([0-9]){4}){3})$/));
+ assert.ok(luhnFormula(visa));
+
+
+ var mastercard = faker.finance.creditCardNumber("mastercard");
+ assert.ok(mastercard.match(/^(5[1-5]\d{2}|6771)(\-\d{4}){3}$/));
+ assert.ok(luhnFormula(mastercard));
+
+ var discover = faker.finance.creditCardNumber("discover");
+
+ assert.ok(luhnFormula(discover));
+
+ var american_express = faker.finance.creditCardNumber("american_express");
+ assert.ok(luhnFormula(american_express));
+ var diners_club = faker.finance.creditCardNumber("diners_club");
+ assert.ok(luhnFormula(diners_club));
+ var jcb = faker.finance.creditCardNumber("jcb");
+ assert.ok(luhnFormula(jcb));
+ var switchC = faker.finance.creditCardNumber("mastercard");
+ assert.ok(luhnFormula(switchC));
+ var solo = faker.finance.creditCardNumber("solo");
+ assert.ok(luhnFormula(solo));
+ var maestro = faker.finance.creditCardNumber("maestro");
+ assert.ok(luhnFormula(maestro));
+ var laser = faker.finance.creditCardNumber("laser");
+ assert.ok(luhnFormula(laser));
+ var instapayment = faker.finance.creditCardNumber("instapayment");
+ assert.ok(luhnFormula(instapayment));
+ });
+ it("returns custom formated strings",function(){
+ var number = faker.finance.creditCardNumber("###-###-##L");
+ assert.ok(number.match(/^\d{3}\-\d{3}\-\d{3}$/));
+ assert.ok(luhnFormula(number));
+ number =faker.finance.creditCardNumber("234[5-9]#{999}L");
+ assert.ok(number.match(/^234[5-9]\d{1000}$/));
+ assert.ok(luhnFormula(number));
+ });
+ });
+
+ describe("creditCardCVV()", function(){
+ it("returns a random credit card CVV", function(){
+ var cvv = faker.finance.creditCardCVV();
+ assert.ok(cvv.length === 3);
+ assert.ok(cvv.match(/^[0-9]{3}$/));
+ });
+ });
+
+
describe("iban()", function () {
var ibanLib = require('../lib/iban');
it("returns a random yet formally correct IBAN number", function () {
@@ -246,4 +326,4 @@ describe('finance.js', function () {
assert.ok(bic.match(expr));
});
});
-}); \ No newline at end of file
+});
diff --git a/test/helpers.unit.js b/test/helpers.unit.js
index 707575ff..b9d559dd 100644
--- a/test/helpers.unit.js
+++ b/test/helpers.unit.js
@@ -21,6 +21,15 @@ describe("helpers.js", function () {
});
});
+ describe("replaceSymbols()", function () {
+ context("when '*' passed", function () {
+ it("replaces it with alphanumeric", function(){
+ var num = faker.helpers.replaceSymbols('*AB');
+ assert.ok(num.match(/\wAB/));
+ });
+ });
+ });
+
describe("shuffle()", function () {
it("the output is the same length as the input", function () {
sinon.spy(faker.random, 'number');
@@ -67,6 +76,57 @@ describe("helpers.js", function () {
});
});
+ describe("replaceCreditCardSymbols()", function () {
+ var luhnCheck = require("./support/luhnCheck.js");
+ it("returns a credit card number given a schema", function () {
+ var number = faker.helpers.replaceCreditCardSymbols("6453-####-####-####-###L");
+ assert.ok(number.match(/^6453\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}$/));
+ assert.ok(luhnCheck(number));
+ });
+ it("supports different symbols", function () {
+ var number = faker.helpers.replaceCreditCardSymbols("6453-****-****-****-***L","*");
+ assert.ok(number.match(/^6453\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}$/));
+ assert.ok(luhnCheck(number));
+ });
+ it("handles regexp style input", function () {
+ var number = faker.helpers.replaceCreditCardSymbols("6453-*{4}-*{4}-*{4}-*{3}L","*");
+ assert.ok(number.match(/^6453\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}$/));
+ assert.ok(luhnCheck(number));
+ number = faker.helpers.replaceCreditCardSymbols("645[5-9]-#{4,6}-#{1,2}-#{4,6}-#{3}L");
+ assert.ok(number.match(/^645[5-9]\-([0-9]){4,6}\-([0-9]){1,2}\-([0-9]){4,6}\-([0-9]){4}$/));
+ assert.ok(luhnCheck(number));
+ });
+ });
+
+ describe("regexpStyleStringParse()", function () {
+ it("returns an empty string when called without param", function () {
+ assert.ok(faker.helpers.regexpStyleStringParse() === "");
+ });
+ it("deals with range repeat", function () {
+ var string = faker.helpers.regexpStyleStringParse("#{5,10}");
+ assert.ok(string.length <= 10 && string.length >= 5);
+ assert.ok(string.match(/^\#{5,10}$/));
+ });
+ it("flips the range when min > max", function () {
+ var string = faker.helpers.regexpStyleStringParse("#{10,5}");
+ assert.ok(string.length <= 10 && string.length >= 5);
+ assert.ok(string.match(/^\#{5,10}$/));
+ });
+ it("repeats string {n} number of times", function () {
+ assert.ok(faker.helpers.regexpStyleStringParse("%{10}") === faker.helpers.repeatString("%",10));
+ assert.ok(faker.helpers.regexpStyleStringParse("%{30}") === faker.helpers.repeatString("%",30));
+ assert.ok(faker.helpers.regexpStyleStringParse("%{5}") === faker.helpers.repeatString("%",5));
+ });
+ it("creates a numerical range", function () {
+ var string = faker.helpers.regexpStyleStringParse("Hello[0-9]");
+ assert.ok(string.match(/^Hello[0-9]$/));
+ });
+ it("deals with multiple tokens in one string", function () {
+ var string = faker.helpers.regexpStyleStringParse("Test#{5}%{2,5}Testing**[1-5]**{10}END");
+ assert.ok(string.match(/^Test\#{5}%{2,5}Testing\*\*[1-5]\*\*{10}END$/));
+ });
+ });
+
describe("createTransaction()", function() {
it("should create a random transaction", function() {
var transaction = faker.helpers.createTransaction();
diff --git a/test/locales.unit.js b/test/locales.unit.js
index 0b2d4175..932369f4 100644
--- a/test/locales.unit.js
+++ b/test/locales.unit.js
@@ -6,4 +6,15 @@ if (typeof module !== 'undefined') {
// TODO: make some tests for getting / setting locales
-// Remark: actual use of locales functionality is currently tested in all.functional.js test \ No newline at end of file
+// Remark: actual use of locales functionality is currently tested in all.functional.js test
+
+describe("locale", function () {
+ describe("setLocale()", function () {
+ it("setLocale() changes faker.locale", function () {
+ for(var locale in faker.locales) {
+ faker.setLocale(locale)
+ assert.equal(faker.locale, locale);
+ }
+ });
+ });
+});
diff --git a/test/phone_number.unit.js b/test/phone_number.unit.js
index b49ad9cf..442a602a 100644
--- a/test/phone_number.unit.js
+++ b/test/phone_number.unit.js
@@ -18,14 +18,31 @@ describe("phone_number.js", function () {
});
describe("phoneNumberFormat()", function () {
- faker.locale = "en";
it("returns phone number with requested format (Array index)", function () {
+ faker.locale = "en";
for (var i = 0; i < 10; i++) {
var phone_number = faker.phone.phoneNumberFormat(1);
assert.ok(phone_number.match(/\(\d\d\d\) \d\d\d-\d\d\d\d/));
}
});
- });
+ it("returns phone number with proper format US (Array index)", function () {
+ faker.locale = "en";
+ for (var i = 0; i < 25; i++) {
+ var phone_number = faker.phone.phoneNumberFormat(1);
+ console.log(phone_number)
+ assert.ok(phone_number.match(/\([2-9]\d\d\) [2-9]\d\d-\d\d\d\d/));
+ }
+ });
+
+ it("returns phone number with proper format CA (Array index)", function () {
+ faker.locale = "en_CA";
+ for (var i = 0; i < 25; i++) {
+ var phone_number = faker.phone.phoneNumberFormat(1);
+ assert.ok(phone_number.match(/\([2-9]\d\d\)[2-9]\d\d-\d\d\d\d/));
+ }
+ });
+
+ });
});
diff --git a/test/random.unit.js b/test/random.unit.js
index 133fd887..e10d36be 100644
--- a/test/random.unit.js
+++ b/test/random.unit.js
@@ -97,6 +97,46 @@ describe("random.js", function () {
});
});
+ describe('arrayElements', function() {
+ it('returns a subset with random elements in the array', function() {
+ var testArray = ['hello', 'to', 'you', 'my', 'friend'];
+ var subset = faker.random.arrayElements(testArray);
+
+ // Check length
+ assert.ok(subset.length >= 1 && subset.length <= testArray.length);
+
+ // Check elements
+ subset.forEach(function(element) {
+ assert.ok(testArray.indexOf(element) > -1);
+ });
+
+ // Check uniqueness
+ subset.forEach(function(element) {
+ assert.ok(!this.hasOwnProperty(element));
+ this[element] = true;
+ }, {});
+ });
+
+ it('returns a subset of fixed length with random elements in the array', function() {
+ var testArray = ['hello', 'to', 'you', 'my', 'friend'];
+ var subset = faker.random.arrayElements(testArray, 3);
+
+ // Check length
+ assert.ok(subset.length === 3);
+
+ // Check elements
+ subset.forEach(function(element) {
+ assert.ok(testArray.indexOf(element) > -1);
+ });
+
+ // Check uniqueness
+ subset.forEach(function(element) {
+ assert.ok(!this.hasOwnProperty(element));
+ this[element] = true;
+ }, {});
+ });
+ });
+
describe('UUID', function() {
it('should generate a valid UUID', function() {
var UUID = faker.random.uuid();
@@ -135,4 +175,18 @@ describe("random.js", 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 generate a random hex string', function() {
+ var hex = hexaDecimal(5);
+ assert.ok(hex.match(/^(0x)[0-9a-f]+$/i));
+ })
+ })
});
diff --git a/test/support/luhnCheck.js b/test/support/luhnCheck.js
new file mode 100644
index 00000000..1e195544
--- /dev/null
+++ b/test/support/luhnCheck.js
@@ -0,0 +1,18 @@
+module.exports = function (number) {
+ number = number.replace(/\D/g,"");
+ var split = number.split("");
+ split = split.map(function(num){return parseInt(num);});
+ var check = split.pop();
+ split.reverse();
+ split = split.map(function(num, index){
+ if(index%2 === 0) {
+ num *= 2;
+ if(num>9) {
+ num -= 9;
+ }
+ }
+ return num;
+ });
+ var sum = split.reduce(function(prev,curr){return prev + curr;});
+ return (sum%10 === check);
+};
diff --git a/test/unique.unit.js b/test/unique.unit.js
new file mode 100644
index 00000000..4edb8220
--- /dev/null
+++ b/test/unique.unit.js
@@ -0,0 +1,54 @@
+if (typeof module !== 'undefined') {
+ var assert = require('assert');
+ var sinon = require('sinon');
+ var faker = require('../index');
+}
+
+describe("unique.js", function () {
+ describe("unique()", 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');
+ });
+
+ it("is able to call a function with arguments and return a result", function () {
+ var result = faker.unique(faker.internet.email, ['a', 'b', 'c']); // third argument is provider, or domain for email
+ assert.ok(result.match(/\@c/));
+ });
+
+ it("is able to call a function with arguments and return a result", function () {
+ var result = faker.unique(faker.internet.email, ['a', 'b', 'c']); // third argument is provider, or domain for email
+ assert.ok(result.match(/\@c/));
+ });
+
+ it("is able to exclude results as array", function () {
+ var result = faker.unique(faker.internet.protocol, [], { exclude: ['https'] });
+ assert.equal(result, 'http');
+ });
+
+ it("is able to limit unique call by maxTime in ms", function () {
+ var result;
+ 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');
+ }
+ });
+
+ it("is able to limit unique call by maxRetries", function () {
+ var result;
+ 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');
+ }
+ });
+
+ it("is able to call a function with arguments and return a result", function () {
+ var result = faker.unique(faker.internet.email, ['a', 'b', 'c']); // third argument is provider, or domain for email
+ assert.ok(result.match(/\@c/));
+ });
+
+ });
+});