diff options
| author | Marak <[email protected]> | 2015-06-11 14:19:12 +0200 |
|---|---|---|
| committer | Marak <[email protected]> | 2015-06-11 14:19:12 +0200 |
| commit | 04dd183489f910648a415cd8ae03148735e2630e (patch) | |
| tree | 264901cb72c95bad6475c69e4d7bbf8fd9a5dc7a | |
| parent | 7f79b9f26ce0be14dafb0525ca258f3d958c4251 (diff) | |
| parent | c0b64f7c0f9bd921a8c719a9763d4397ecbf4749 (diff) | |
| download | faker-04dd183489f910648a415cd8ae03148735e2630e.tar.xz faker-04dd183489f910648a415cd8ae03148735e2630e.zip | |
Merge pull request #218 from philberg/master
[fix] random.number when max = 0
| -rw-r--r-- | lib/random.js | 2 | ||||
| -rw-r--r-- | test/random.unit.js | 23 |
2 files changed, 23 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..c5991d5c 100644 --- a/test/random.unit.js +++ b/test/random.unit.js @@ -14,12 +14,21 @@ 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 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++) { @@ -62,6 +71,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(); |
