forked from zuriby/Faker.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.unit.js
100 lines (82 loc) · 2.9 KB
/
random.unit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var _ = require('lodash');
var faker = require('../index');
}
describe("random.js", function () {
describe("number", function() {
it("returns a random number given a maximum value as Number", function() {
var max = 10;
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++) {
var randomNumber = faker.random.number(options);
assert.ok(randomNumber >= options.min);
assert.ok(randomNumber <= options.max);
}
});
it("provides numbers with a given precision", function() {
var options = { min: 0, max: 1.5, precision: 0.5 };
var results = _.chain(_.range(50))
.map(function() {
return faker.random.number(options);
})
.uniq()
.value()
.sort();
assert.ok(_.contains(results, 0.5));
assert.ok(_.contains(results, 1.0));
assert.equal(results[0], 0);
assert.equal(_.last(results), 1.5);
});
it("should not modify the input object", function() {
var min = 1;
var max = 2;
var opts = {
min: min,
max: max
};
faker.random.number(opts);
assert.equal(opts.min, min);
assert.equal(opts.max, max);
});
});
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();
var RFC4122 = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
assert.ok(RFC4122.test(UUID));
})
})
describe('boolean', function() {
it('should generate a boolean value', function() {
var bool = faker.random.boolean();
assert.ok(typeof bool == 'boolean');
});
});
});