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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
import test from 'ava'
import Chance from '../chance.js'
import _ from 'lodash'
const chance = new Chance()
// chance.coin()
test('coin() returns a coin', t => {
_.times(1000, () => {
t.true(/(heads|tails)/.test(chance.coin()))
})
})
// chance.d4()
test('d4() returns a properly bounded d4', t => {
_.times(1000, () => {
let die = chance.d4()
t.true(die >= 1)
t.true(die <= 4)
})
})
// chance.d6()
test('d6() returns a properly bounded d6', t => {
_.times(1000, () => {
let die = chance.d6()
t.true(die >= 1)
t.true(die <= 6)
})
})
// chance.d8()
test('d8() returns a properly bounded d8', t => {
_.times(1000, () => {
let die = chance.d8()
t.true(die >= 1)
t.true(die <= 8)
})
})
// chance.d10()
test('d10() returns a properly bounded d10', t => {
_.times(1000, () => {
let die = chance.d10()
t.true(die >= 1)
t.true(die <= 10)
})
})
// chance.d12()
test('d12() returns a properly bounded d12', t => {
_.times(1000, () => {
let die = chance.d12()
t.true(die >= 1)
t.true(die <= 12)
})
})
// chance.d20()
test('d20() returns a properly bounded d20', t => {
_.times(1000, () => {
let die = chance.d20()
t.true(die >= 1)
t.true(die <= 20)
})
})
// chance.d30()
test('d30() returns a properly bounded d30', t => {
_.times(1000, () => {
let die = chance.d30()
t.true(die >= 1)
t.true(die <= 30)
})
})
// chance.d100()
test('d100() returns a properly bounded d100', t => {
_.times(1000, () => {
let die = chance.d100()
t.true(die >= 1)
t.true(die <= 100)
})
})
// chance.emotion()
test('emotion() returns a random emotion', t => {
_.times(1000, () => {
let emotion = chance.emotion()
t.true(_.isString(emotion))
t.true(emotion.length >= 2)
t.true(emotion.length <= 30)
})
})
// chance.guid()
test('guid() returns a proper guid', t => {
_.times(1000, () => {
t.true(/([0-9a-fA-F]){8}(-([0-9a-fA-F]){4}){3}-([0-9a-fA-F]){12}/.test(chance.guid()))
})
})
test('guid() returns a proper version 1 guid', t => {
_.times(1000, () => {
t.true(/([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-1([0-9a-fA-F]){3}-([ab89])([0-9a-fA-F]){3}-([0-9a-fA-F]){12}/.test(chance.guid({ version: 1 })))
})
})
test('guid() returns a proper version 2 guid', t => {
_.times(1000, () => {
t.true(/([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-2([0-9a-fA-F]){3}-([ab89])([0-9a-fA-F]){3}-([0-9a-fA-F]){12}/.test(chance.guid({ version: 2 })))
})
})
test('guid() returns a proper version 3 guid', t => {
_.times(1000, () => {
t.true(/([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-3([0-9a-fA-F]){3}-([ab89])([0-9a-fA-F]){3}-([0-9a-fA-F]){12}/.test(chance.guid({ version: 3 })))
})
})
test('guid() returns a proper version 4 guid', t => {
_.times(1000, () => {
t.true(/([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-4([0-9a-fA-F]){3}-([ab89])([0-9a-fA-F]){3}-([0-9a-fA-F]){12}/.test(chance.guid({ version: 4 })))
})
})
test('guid() returns a proper version 5 guid', t => {
_.times(1000, () => {
t.true(/([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-5([0-9a-fA-F]){3}-([ab89])([0-9a-fA-F]){3}-([0-9a-fA-F]){12}/.test(chance.guid({ version: 5 })))
})
})
// chance.hash()
test('hash() returns a proper hash', t => {
_.times(1000, () => {
let hash = chance.hash()
t.true(/([0-9a-f]){40}/.test(hash))
t.is(hash.length, 40)
})
})
test('hash() obeys length, if supplied', t => {
_.times(1000, () => {
let length = chance.natural({ min: 1, max: 64 })
let hash = chance.hash({ length: length })
t.is(hash.length, length)
})
})
// chance.mac_address()
test('mac_address() returns a proper mac address', t => {
_.times(1000, () => {
t.true(/([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}/.test(chance.mac_address()))
})
})
test('mac_address() returns a proper colon separated mac address', t => {
_.times(1000, () => {
t.true(/([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}:([0-9a-fA-F]){2}/.test(chance.mac_address({ separator: ':' })))
})
})
test('mac_address() returns a proper hyphen separated mac address', t => {
_.times(1000, () => {
t.true(/([0-9a-fA-F]){2}-([0-9a-fA-F]){2}-([0-9a-fA-F]){2}-([0-9a-fA-F]){2}-([0-9a-fA-F]){2}-([0-9a-fA-F]){2}/.test(chance.mac_address({ separator: '-' })))
})
})
test('mac_address() returns a proper network version mac address', t => {
_.times(1000, () => {
t.true(/([0-9a-fA-F]){4}.([0-9a-fA-F]){4}.([0-9a-fA-F]){4}/.test(chance.mac_address({ networkVersion: true })))
})
})
// chance.mixin()
test('mixin() works with a simple function', t => {
chance.mixin({
user: () => {
return {
first: chance.first(),
last: chance.last(),
email: chance.email()
}
}
})
t.truthy(chance.user)
_.times(1000, () => {
let user = chance.user()
t.truthy(user)
t.truthy(user.first)
t.true(_.isString(user.last))
t.true(_.isString(user.email))
})
})
test('mixin() multiple work, we can call previously defined mixins', t => {
chance.mixin({
user: () => {
return {
first: chance.first(),
last: chance.last(),
email: chance.email()
}
},
social_user: () => {
let user = chance.user()
user.network = chance.pick(['facebook', 'twitter'])
return user
}
})
t.truthy(chance.user)
t.truthy(chance.social_user)
_.times(1000, () => {
let social_user = chance.social_user()
t.truthy(social_user)
t.truthy(social_user.first)
t.truthy(social_user.network)
t.true(social_user.network === 'facebook' ||
social_user.network === 'twitter')
})
})
// chance.radio()
test('radio() works as expected', t => {
_.times(1000, () => {
let radio = chance.radio()
t.true(_.isString(radio))
t.is(radio.length, 4)
t.true(/^[KW][A-Z][A-Z][A-Z]/.test(radio))
})
})
test('radio() accepts east', t => {
_.times(1000, () => {
let radio = chance.radio({ side: 'east' })
t.true(_.isString(radio))
t.is(radio.length, 4)
t.true(/^[W][A-Z][A-Z][A-Z]/.test(radio))
})
})
test('radio() accepts west', t => {
_.times(1000, () => {
let radio = chance.radio({ side: 'west' })
t.true(_.isString(radio))
t.is(radio.length, 4)
t.true(/^[K][A-Z][A-Z][A-Z]/.test(radio))
})
})
// chance.rpg()
test('rpg() appears to work as expected', t => {
_.times(1000, () => {
let dice = chance.rpg('5d20')
t.true(_.isArray(dice))
t.is(dice.length, 5)
dice.map((die) => {
t.true(die >= 1)
t.true(die <= 20)
})
})
})
test('rpg() without a die roll throws an error', t => {
t.throws(() => chance.rpg(), 'Chance: A type of die roll must be included')
})
test('rpg() throws errors where it should', t => {
const errorFns = [
() => chance.rpg('3'),
() => chance.rpg('hd23'),
() => chance.rpg('3d23d2'),
() => chance.rpg('d20')
]
errorFns.map((fn) => {
t.throws(fn, 'Chance: Invalid format provided. Please provide #d# where the first # is the number of dice to roll, the second # is the max of each die')
})
})
test('rpg() will take and obey a sum', t => {
_.times(1000, () => {
let rpg = chance.rpg('4d20', { sum: true })
t.true(_.isNumber(rpg))
t.true(rpg >= 4)
t.true(rpg <= 80)
})
})
// chance.tv()
test('tv() works as expected', t => {
_.times(1000, () => {
let tv = chance.tv()
t.true(_.isString(tv))
t.is(tv.length, 4)
t.true(/^[KW][A-Z][A-Z][A-Z]/.test(tv))
})
})
|