aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarak <[email protected]>2015-06-11 14:19:12 +0200
committerMarak <[email protected]>2015-06-11 14:19:12 +0200
commit04dd183489f910648a415cd8ae03148735e2630e (patch)
tree264901cb72c95bad6475c69e4d7bbf8fd9a5dc7a
parent7f79b9f26ce0be14dafb0525ca258f3d958c4251 (diff)
parentc0b64f7c0f9bd921a8c719a9763d4397ecbf4749 (diff)
downloadfaker-04dd183489f910648a415cd8ae03148735e2630e.tar.xz
faker-04dd183489f910648a415cd8ae03148735e2630e.zip
Merge pull request #218 from philberg/master
[fix] random.number when max = 0
-rw-r--r--lib/random.js2
-rw-r--r--test/random.unit.js23
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();