aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosef Salyer <[email protected]>2013-08-03 21:11:49 -0400
committerJosef Salyer <[email protected]>2013-08-03 21:11:49 -0400
commita5da426f33aab13d0946be2c14ae0dae7c302500 (patch)
treee70e87d268d14a5d1caacc84e165f8324d1e81a1
parenta7b1cedc260a89c52f6a304230b939c2ba9d51a0 (diff)
downloadfaker-a5da426f33aab13d0946be2c14ae0dae7c302500.tar.xz
faker-a5da426f33aab13d0946be2c14ae0dae7c302500.zip
I forgot how much of stinker zero is in Javascript. Looping tests on finance should no longer fail
-rw-r--r--lib/finance.js9
-rw-r--r--test/finance.unit.js361
2 files changed, 193 insertions, 177 deletions
diff --git a/lib/finance.js b/lib/finance.js
index 44990aad..4bf50bff 100644
--- a/lib/finance.js
+++ b/lib/finance.js
@@ -23,11 +23,12 @@ var finance = {
mask: function (length, parens, elipsis) {
+
//set defaults
- length = length || 4;
+ length = (length == 0 || !length || typeof length == 'undefined') ? 4 : length;
parens = (parens === null) ? true : parens;
elipsis = (elipsis === null) ? true : elipsis;
-
+
//create a template for length
var template = '';
@@ -52,12 +53,12 @@ var finance = {
amount: function (min, max, dec, symbol) {
- min = min || 1;
+ min = min || 0;
max = max || 1000;
dec = dec || 2;
symbol = symbol || '';
- return symbol + Math.round((Math.random() * (max - min) + min) * Math.pow(10, dec)) / Math.pow(10, dec);
+ return symbol + (Math.round((Math.random() * (max - min) + min) * Math.pow(10, dec)) / Math.pow(10, dec)).toFixed(dec);
},
diff --git a/test/finance.unit.js b/test/finance.unit.js
index 3b33a991..243b6a01 100644
--- a/test/finance.unit.js
+++ b/test/finance.unit.js
@@ -6,177 +6,192 @@ if (typeof module !== 'undefined') {
describe('finance.js', function () {
- describe('account( length )', function(){
-
- it('should supply a default length if no length is passed', function(){
-
- var account = Faker.Finance.account();
-
- var expected = 8;
- var actual = account.length;
-
- assert.equal(actual, expected, 'The expected default account length is ' + expected + ' but it was ' + actual);
-
- });
-
- it('should supply a length if a length is passed', function(){
-
- var expected = 9;
-
- var account = Faker.Finance.account(expected);
-
- var actual = account.length;
-
- assert.equal(actual, expected, 'The expected default account length is ' + expected + ' but it was ' + actual);
-
- });
-
- it('should supply a default length if a zero is passed', function(){
-
- var expected = 8;
-
- var account = Faker.Finance.account(0);
-
- var actual = account.length;
-
- assert.equal(actual, expected, 'The expected default account length is ' + expected + ' but it was ' + actual);
-
- });
-
- });
-
- describe('accountName()', function(){
-
- it("should return an account name", function() {
-
- var actual = Faker.Finance.accountName();
-
- assert.ok(actual);
-
- });
-
- });
-
-
- describe('mask( length, parens, elipsis )', function(){
- it("should set a default length", function() {
-
- var expected = 4; //default account mask length
-
- var mask = Faker.Finance.mask(null, false, false);
-
- var actual = mask.length;
-
- assert.equal(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 mask = Faker.Finance.mask(expected, false, false);
-
- var actual = mask.length || 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);
-
- });
-
- it("should by default include parentheses around a partial account number", function() {
-
- var expected = true;
-
- var mask = Faker.Finance.mask(null, null, false);
-
- 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);
-
- });
-
- it("should by default include an elipsis", function() {
-
- var expected = true;
-
- var mask = Faker.Finance.mask(null, false, null);
-
- 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);
-
- });
-
- it("should work when random variables are passed into the arguments", function() {
-
- var length = Faker.random.number(20);
- var elipsis = (length % 2 === 0) ? true : false;
- var parens = !elipsis;
-
- var mask = Faker.Finance.mask(length, elipsis, parens);
- assert.ok(mask);
-
- });
-
-
- });
-
- describe('amount(min, max, dec, symbol)', function(){
-
- it("should use the default amounts when not passing arguments", 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");
-
- });
-
- it("should use the defaul decimal location when not passing arguments", function() {
-
- var amount = Faker.Finance.amount();
-
- var decimal = '.';
- var expected = amount.length - 3;
- var actual = amount.indexOf(decimal);
-
- assert.equal(actual, expected, 'The expected location of the decimal is ' + expected + ' but it was ' + actual + ' amount ' + amount);
- });
-
- //TODO: add support for more currency and decimal options
- it("should not include a currency symbol by default", function() {
-
- var amount = Faker.Finance.amount();
-
- var regexp = new RegExp(/[0-9.]/);
-
- var expected = true;
- var actual = regexp.test(amount);
-
- assert.equal(actual, expected, 'The expected match should not include a currency symbol');
- });
-
-
- it("it should handle negative amounts", 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");
- });
-
-
- });
-
- describe('transactionType()', function(){
-
- it("should return a random transaction type", function() {
- var transactionType = Faker.Finance.transactionType();
-
- assert.ok(transactionType);
- });
- });
+ describe('account( length )', function () {
+
+ it('should supply a default length if no length is passed', function () {
+
+ var account = Faker.Finance.account();
+
+ var expected = 8;
+ var actual = account.length;
+
+ assert.equal(actual, expected, 'The expected default account length is ' + expected + ' but it was ' + actual);
+
+ });
+
+ it('should supply a length if a length is passed', function () {
+
+ var expected = 9;
+
+ var account = Faker.Finance.account(expected);
+
+ var actual = account.length;
+
+ assert.equal(actual, expected, 'The expected default account length is ' + expected + ' but it was ' + actual);
+
+ });
+
+ it('should supply a default length if a zero is passed', function () {
+
+ var expected = 8;
+
+ var account = Faker.Finance.account(0);
+
+ var actual = account.length;
+
+ assert.equal(actual, expected, 'The expected default account length is ' + expected + ' but it was ' + actual);
+
+ });
+
+ });
+
+ describe('accountName()', function () {
+
+ it("should return an account name", function () {
+
+ var actual = Faker.Finance.accountName();
+
+ assert.ok(actual);
+
+ });
+
+ });
+
+
+ describe('mask( length, parens, elipsis )', function () {
+ it("should set a default length", function () {
+
+ var expected = 4; //default account mask length
+
+ var mask = Faker.Finance.mask(null, false, false);
+
+ var actual = mask.length;
+
+ assert.equal(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);
+
+ expected = (expected == 0 || !expected || typeof expected == 'undefined') ? 4 : expected;
+
+ var mask = Faker.Finance.mask(expected, false, false);
+
+ 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);
+
+ });
+
+ it("should set a default length of 4 for a zero value", function () {
+
+ var expected = 4;
+
+ var mask = 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);
+
+ });
+
+
+ it("should by default include parentheses around a partial account number", function () {
+
+ var expected = true;
+
+ var mask = Faker.Finance.mask(null, null, false);
+
+ 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);
+
+ });
+
+ it("should by default include an elipsis", function () {
+
+ var expected = true;
+
+ var mask = Faker.Finance.mask(null, false, null);
+
+ 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);
+
+ });
+
+ it("should work when random variables are passed into the arguments", function () {
+
+ var length = Faker.random.number(20);
+ var elipsis = (length % 2 === 0) ? true : false;
+ var parens = !elipsis;
+
+ var mask = Faker.Finance.mask(length, elipsis, parens);
+ assert.ok(mask);
+
+ });
+
+
+ });
+
+ describe('amount(min, max, dec, symbol)', function () {
+
+ it("should use the default amounts when not passing arguments", 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");
+
+ });
+
+ it("should use the defaul decimal location when not passing arguments", function () {
+
+ var amount = Faker.Finance.amount();
+
+ var decimal = '.';
+ var expected = amount.length - 3;
+ var actual = amount.indexOf(decimal);
+
+ assert.equal(actual, expected, 'The expected location of the decimal is ' + expected + ' but it was ' + actual + ' amount ' + amount);
+ });
+
+ //TODO: add support for more currency and decimal options
+ it("should not include a currency symbol by default", function () {
+
+ var amount = Faker.Finance.amount();
+
+ var regexp = new RegExp(/[0-9.]/);
+
+ var expected = true;
+ var actual = regexp.test(amount);
+
+ assert.equal(actual, expected, 'The expected match should not include a currency symbol');
+ });
+
+
+ it("it should handle negative amounts", 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");
+ });
+
+
+ });
+
+ describe('transactionType()', function () {
+
+ it("should return a random transaction type", function () {
+ var transactionType = Faker.Finance.transactionType();
+
+ assert.ok(transactionType);
+ });
+ });
}); \ No newline at end of file