UNPKG

8.18 kBJavaScriptView Raw
1import test from 'ava'
2import Chance from '../chance.js'
3import _ from 'lodash'
4
5const chance = new Chance()
6
7// chance.coin()
8test('coin() returns a coin', t => {
9 _.times(1000, () => {
10 t.true(/(heads|tails)/.test(chance.coin()))
11 })
12})
13
14// chance.d4()
15test('d4() returns a properly bounded d4', t => {
16 _.times(1000, () => {
17 let die = chance.d4()
18 t.true(die >= 1)
19 t.true(die <= 4)
20 })
21})
22
23// chance.d6()
24test('d6() returns a properly bounded d6', t => {
25 _.times(1000, () => {
26 let die = chance.d6()
27 t.true(die >= 1)
28 t.true(die <= 6)
29 })
30})
31
32// chance.d8()
33test('d8() returns a properly bounded d8', t => {
34 _.times(1000, () => {
35 let die = chance.d8()
36 t.true(die >= 1)
37 t.true(die <= 8)
38 })
39})
40
41// chance.d10()
42test('d10() returns a properly bounded d10', t => {
43 _.times(1000, () => {
44 let die = chance.d10()
45 t.true(die >= 1)
46 t.true(die <= 10)
47 })
48})
49
50// chance.d12()
51test('d12() returns a properly bounded d12', t => {
52 _.times(1000, () => {
53 let die = chance.d12()
54 t.true(die >= 1)
55 t.true(die <= 12)
56 })
57})
58
59// chance.d20()
60test('d20() returns a properly bounded d20', t => {
61 _.times(1000, () => {
62 let die = chance.d20()
63 t.true(die >= 1)
64 t.true(die <= 20)
65 })
66})
67
68// chance.d30()
69test('d30() returns a properly bounded d30', t => {
70 _.times(1000, () => {
71 let die = chance.d30()
72 t.true(die >= 1)
73 t.true(die <= 30)
74 })
75})
76
77// chance.d100()
78test('d100() returns a properly bounded d100', t => {
79 _.times(1000, () => {
80 let die = chance.d100()
81 t.true(die >= 1)
82 t.true(die <= 100)
83 })
84})
85
86
87// chance.emotion()
88test('emotion() returns a random emotion', t => {
89 _.times(1000, () => {
90 let emotion = chance.emotion()
91 t.true(_.isString(emotion))
92 t.true(emotion.length >= 2)
93 t.true(emotion.length <= 30)
94 })
95})
96
97// chance.guid()
98test('guid() returns a proper guid', t => {
99 _.times(1000, () => {
100 t.true(/([0-9a-fA-F]){8}(-([0-9a-fA-F]){4}){3}-([0-9a-fA-F]){12}/.test(chance.guid()))
101 })
102})
103
104test('guid() returns a proper version 1 guid', t => {
105 _.times(1000, () => {
106 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 })))
107 })
108})
109
110test('guid() returns a proper version 2 guid', t => {
111 _.times(1000, () => {
112 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 })))
113 })
114})
115
116test('guid() returns a proper version 3 guid', t => {
117 _.times(1000, () => {
118 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 })))
119 })
120})
121
122test('guid() returns a proper version 4 guid', t => {
123 _.times(1000, () => {
124 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 })))
125 })
126})
127
128test('guid() returns a proper version 5 guid', t => {
129 _.times(1000, () => {
130 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 })))
131 })
132})
133
134// chance.hash()
135test('hash() returns a proper hash', t => {
136 _.times(1000, () => {
137 let hash = chance.hash()
138 t.true(/([0-9a-f]){40}/.test(hash))
139 t.is(hash.length, 40)
140 })
141})
142
143test('hash() obeys length, if supplied', t => {
144 _.times(1000, () => {
145 let length = chance.natural({ min: 1, max: 64 })
146 let hash = chance.hash({ length: length })
147 t.is(hash.length, length)
148 })
149})
150
151// chance.mac_address()
152test('mac_address() returns a proper mac address', t => {
153 _.times(1000, () => {
154 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()))
155 })
156})
157
158test('mac_address() returns a proper colon separated mac address', t => {
159 _.times(1000, () => {
160 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: ':' })))
161 })
162})
163
164test('mac_address() returns a proper hyphen separated mac address', t => {
165 _.times(1000, () => {
166 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: '-' })))
167 })
168})
169
170test('mac_address() returns a proper network version mac address', t => {
171 _.times(1000, () => {
172 t.true(/([0-9a-fA-F]){4}.([0-9a-fA-F]){4}.([0-9a-fA-F]){4}/.test(chance.mac_address({ networkVersion: true })))
173 })
174})
175
176// chance.mixin()
177test('mixin() works with a simple function', t => {
178 chance.mixin({
179 user: () => {
180 return {
181 first: chance.first(),
182 last: chance.last(),
183 email: chance.email()
184 }
185 }
186 })
187 t.truthy(chance.user)
188 _.times(1000, () => {
189 let user = chance.user()
190 t.truthy(user)
191 t.truthy(user.first)
192 t.true(_.isString(user.last))
193 t.true(_.isString(user.email))
194 })
195})
196
197test('mixin() multiple work, we can call previously defined mixins', t => {
198 chance.mixin({
199 user: () => {
200 return {
201 first: chance.first(),
202 last: chance.last(),
203 email: chance.email()
204 }
205 },
206 social_user: () => {
207 let user = chance.user()
208 user.network = chance.pick(['facebook', 'twitter'])
209 return user
210 }
211 })
212 t.truthy(chance.user)
213 t.truthy(chance.social_user)
214 _.times(1000, () => {
215 let social_user = chance.social_user()
216 t.truthy(social_user)
217 t.truthy(social_user.first)
218 t.truthy(social_user.network)
219 t.true(social_user.network === 'facebook' ||
220 social_user.network === 'twitter')
221 })
222})
223
224// chance.radio()
225test('radio() works as expected', t => {
226 _.times(1000, () => {
227 let radio = chance.radio()
228 t.true(_.isString(radio))
229 t.is(radio.length, 4)
230 t.true(/^[KW][A-Z][A-Z][A-Z]/.test(radio))
231 })
232})
233
234test('radio() accepts east', t => {
235 _.times(1000, () => {
236 let radio = chance.radio({ side: 'east' })
237 t.true(_.isString(radio))
238 t.is(radio.length, 4)
239 t.true(/^[W][A-Z][A-Z][A-Z]/.test(radio))
240 })
241})
242
243test('radio() accepts west', t => {
244 _.times(1000, () => {
245 let radio = chance.radio({ side: 'west' })
246 t.true(_.isString(radio))
247 t.is(radio.length, 4)
248 t.true(/^[K][A-Z][A-Z][A-Z]/.test(radio))
249 })
250})
251
252// chance.rpg()
253test('rpg() appears to work as expected', t => {
254 _.times(1000, () => {
255 let dice = chance.rpg('5d20')
256 t.true(_.isArray(dice))
257 t.is(dice.length, 5)
258 dice.map((die) => {
259 t.true(die >= 1)
260 t.true(die <= 20)
261 })
262 })
263})
264
265test('rpg() without a die roll throws an error', t => {
266 t.throws(() => chance.rpg(), 'Chance: A type of die roll must be included')
267})
268
269test('rpg() throws errors where it should', t => {
270 const errorFns = [
271 () => chance.rpg('3'),
272 () => chance.rpg('hd23'),
273 () => chance.rpg('3d23d2'),
274 () => chance.rpg('d20')
275 ]
276 errorFns.map((fn) => {
277 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')
278 })
279})
280
281test('rpg() will take and obey a sum', t => {
282 _.times(1000, () => {
283 let rpg = chance.rpg('4d20', { sum: true })
284 t.true(_.isNumber(rpg))
285 t.true(rpg >= 4)
286 t.true(rpg <= 80)
287 })
288})
289
290// chance.tv()
291test('tv() works as expected', t => {
292 _.times(1000, () => {
293 let tv = chance.tv()
294 t.true(_.isString(tv))
295 t.is(tv.length, 4)
296 t.true(/^[KW][A-Z][A-Z][A-Z]/.test(tv))
297 })
298})