forked from zuriby/Faker.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.unit.js
192 lines (155 loc) · 5.6 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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(_.includes(results, 0.5));
assert.ok(_.includes(results, 1.0));
assert.equal(results[0], 0);
assert.equal(_.last(results), 1.5);
});
it("provides numbers with a with exact precision", function() {
var options = { min: 0.5, max: 0.99, precision: 0.01 };
for(var i = 0; i < 100; i++) {
var number = faker.random.number(options);
assert.equal(number, Number(number.toFixed(2)));
}
});
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);
});
it('should return deterministic results when seeded', function() {
faker.seed(100);
var name = faker.name.findName();
assert.equal(name, 'Eva Jenkins');
})
});
describe('arrayElement', function() {
it('returns a random element in the array', function() {
var testArray = ['hello', 'to', 'you', 'my', 'friend'];
assert.ok(testArray.indexOf(faker.random.arrayElement(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.arrayElement(testArray)) > -1);
});
});
describe('arrayElements', function() {
it('returns a subset with random elements in the array', function() {
var testArray = ['hello', 'to', 'you', 'my', 'friend'];
var subset = faker.random.arrayElements(testArray);
// Check length
assert.ok(subset.length >= 1 && subset.length <= testArray.length);
// Check elements
subset.forEach(function(element) {
assert.ok(testArray.indexOf(element) > -1);
});
// Check uniqueness
subset.forEach(function(element) {
assert.ok(!this.hasOwnProperty(element));
this[element] = true;
}, {});
});
it('returns a subset of fixed length with random elements in the array', function() {
var testArray = ['hello', 'to', 'you', 'my', 'friend'];
var subset = faker.random.arrayElements(testArray, 3);
// Check length
assert.ok(subset.length === 3);
// Check elements
subset.forEach(function(element) {
assert.ok(testArray.indexOf(element) > -1);
});
// Check uniqueness
subset.forEach(function(element) {
assert.ok(!this.hasOwnProperty(element));
this[element] = true;
}, {});
});
});
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');
});
});
describe('semver', function() {
var semver = faker.system.semver();
it('should generate a string', function() {
assert.ok(typeof semver === 'string');
});
it('should generate a valid semver', function() {
assert.ok(/^\d+\.\d+\.\d+$/.test(semver));
});
});
describe('alphaNumeric', function() {
var alphaNumeric = faker.random.alphaNumeric;
it('should generate single character when no additional argument was provided', function() {
assert.ok(alphaNumeric().length === 1);
})
it('should generate many random characters', function() {
assert.ok(alphaNumeric(5).length === 5);
})
})
describe('hexaDecimal', function() {
var hexaDecimal = faker.random.hexaDecimal;
it('should generate single hex character when no additional argument was provided', function() {
var hex = hexaDecimal();
assert.ok(hex.match(/^(0x)[0-9a-f]{1}$/i));
})
it('should generate a random hex string', function() {
var hex = hexaDecimal(5);
assert.ok(hex.match(/^(0x)[0-9a-f]+$/i));
})
})
});