diff options
| author | Phil Greenberg <[email protected]> | 2015-06-10 22:49:12 -0700 |
|---|---|---|
| committer | Phil Greenberg <[email protected]> | 2015-06-10 22:49:12 -0700 |
| commit | cb82376c37716f765aee2a856567a02d1d1233ff (patch) | |
| tree | 8c63227c9b7e7454385929b78864a09d7c7f64cd | |
| parent | 7f79b9f26ce0be14dafb0525ca258f3d958c4251 (diff) | |
| download | faker-cb82376c37716f765aee2a856567a02d1d1233ff.tar.xz faker-cb82376c37716f765aee2a856567a02d1d1233ff.zip | |
Fixing bug in random.number when max = 0
Also added tests for random.array_element
| -rw-r--r-- | lib/random.js | 2 | ||||
| -rw-r--r-- | test/random.unit.js | 18 |
2 files changed, 18 insertions, 2 deletions
diff --git a/lib/random.js b/lib/random.js index 3205d8c4..0fc33451 100644 --- a/lib/random.js +++ b/lib/random.js @@ -26,7 +26,7 @@ var random = { // Make the range inclusive of the max value var max = options.max; - if (max > 0) { + if (max >= 0) { max += options.precision; } diff --git a/test/random.unit.js b/test/random.unit.js index a67e42fd..97612c47 100644 --- a/test/random.unit.js +++ b/test/random.unit.js @@ -14,12 +14,16 @@ describe("random.js", function () { assert.ok(faker.random.number(max) <= max); }); - it("returns a random number given a maximum value as Object", function() { var options = { max: 10 }; assert.ok(faker.random.number(options) <= options.max); }); + 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 between a range", function() { var options = { min: 22, max: 33 }; for(var i = 0; i < 100; i++) { @@ -62,6 +66,18 @@ describe("random.js", function () { }); }); + describe('array_element', function() { + it('returns a random element in the array', function() { + var testArray = ['hello', 'to', 'you', 'my', 'friend']; + assert.ok(testArray.indexOf(faker.random.array_element(testArray)) > -1); + }); + + it('returns a random element in the array when there is only 1', function() { + var testArray = ['hello']; + assert.ok(testArray.indexOf(faker.random.array_element(testArray)) > -1); + }); + }); + describe('UUID', function() { it('should generate a valid UUID', function() { var UUID = faker.random.uuid(); |
