UNPKG

7.92 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// chance.guid()
87test('guid() returns a proper guid', t => {
88 _.times(1000, () => {
89 t.true(/([0-9a-fA-F]){8}(-([0-9a-fA-F]){4}){3}-([0-9a-fA-F]){12}/.test(chance.guid()))
90 })
91})
92
93test('guid() returns a proper version 1 guid', t => {
94 _.times(1000, () => {
95 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 })))
96 })
97})
98
99test('guid() returns a proper version 2 guid', t => {
100 _.times(1000, () => {
101 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 })))
102 })
103})
104
105test('guid() returns a proper version 3 guid', t => {
106 _.times(1000, () => {
107 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 })))
108 })
109})
110
111test('guid() returns a proper version 4 guid', t => {
112 _.times(1000, () => {
113 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 })))
114 })
115})
116
117test('guid() returns a proper version 5 guid', t => {
118 _.times(1000, () => {
119 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 })))
120 })
121})
122
123// chance.hash()
124test('hash() returns a proper hash', t => {
125 _.times(1000, () => {
126 let hash = chance.hash()
127 t.true(/([0-9a-f]){40}/.test(hash))
128 t.is(hash.length, 40)
129 })
130})
131
132test('hash() obeys length, if supplied', t => {
133 _.times(1000, () => {
134 let length = chance.natural({ min: 1, max: 64 })
135 let hash = chance.hash({ length: length })
136 t.is(hash.length, length)
137 })
138})
139
140// chance.mac_address()
141test('mac_address() returns a proper mac address', t => {
142 _.times(1000, () => {
143 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()))
144 })
145})
146
147test('mac_address() returns a proper colon separated mac address', t => {
148 _.times(1000, () => {
149 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: ':' })))
150 })
151})
152
153test('mac_address() returns a proper hyphen separated mac address', t => {
154 _.times(1000, () => {
155 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: '-' })))
156 })
157})
158
159test('mac_address() returns a proper network version mac address', t => {
160 _.times(1000, () => {
161 t.true(/([0-9a-fA-F]){4}.([0-9a-fA-F]){4}.([0-9a-fA-F]){4}/.test(chance.mac_address({ networkVersion: true })))
162 })
163})
164
165// chance.mixin()
166test('mixin() works with a simple function', t => {
167 chance.mixin({
168 user: () => {
169 return {
170 first: chance.first(),
171 last: chance.last(),
172 email: chance.email()
173 }
174 }
175 })
176 t.truthy(chance.user)
177 _.times(1000, () => {
178 let user = chance.user()
179 t.truthy(user)
180 t.truthy(user.first)
181 t.true(_.isString(user.last))
182 t.true(_.isString(user.email))
183 })
184})
185
186test('mixin() multiple work, we can call previously defined mixins', t => {
187 chance.mixin({
188 user: () => {
189 return {
190 first: chance.first(),
191 last: chance.last(),
192 email: chance.email()
193 }
194 },
195 social_user: () => {
196 let user = chance.user()
197 user.network = chance.pick(['facebook', 'twitter'])
198 return user
199 }
200 })
201 t.truthy(chance.user)
202 t.truthy(chance.social_user)
203 _.times(1000, () => {
204 let social_user = chance.social_user()
205 t.truthy(social_user)
206 t.truthy(social_user.first)
207 t.truthy(social_user.network)
208 t.true(social_user.network === 'facebook' ||
209 social_user.network === 'twitter')
210 })
211})
212
213// chance.radio()
214test('radio() works as expected', t => {
215 _.times(1000, () => {
216 let radio = chance.radio()
217 t.true(_.isString(radio))
218 t.is(radio.length, 4)
219 t.true(/^[KW][A-Z][A-Z][A-Z]/.test(radio))
220 })
221})
222
223test('radio() accepts east', t => {
224 _.times(1000, () => {
225 let radio = chance.radio({ side: 'east' })
226 t.true(_.isString(radio))
227 t.is(radio.length, 4)
228 t.true(/^[W][A-Z][A-Z][A-Z]/.test(radio))
229 })
230})
231
232test('radio() accepts west', t => {
233 _.times(1000, () => {
234 let radio = chance.radio({ side: 'west' })
235 t.true(_.isString(radio))
236 t.is(radio.length, 4)
237 t.true(/^[K][A-Z][A-Z][A-Z]/.test(radio))
238 })
239})
240
241// chance.rpg()
242test('rpg() appears to work as expected', t => {
243 _.times(1000, () => {
244 let dice = chance.rpg('5d20')
245 t.true(_.isArray(dice))
246 t.is(dice.length, 5)
247 dice.map((die) => {
248 t.true(die >= 1)
249 t.true(die <= 20)
250 })
251 })
252})
253
254test('rpg() without a die roll throws an error', t => {
255 t.throws(() => chance.rpg(), 'Chance: A type of die roll must be included')
256})
257
258test('rpg() throws errors where it should', t => {
259 const errorFns = [
260 () => chance.rpg('3'),
261 () => chance.rpg('hd23'),
262 () => chance.rpg('3d23d2'),
263 () => chance.rpg('d20')
264 ]
265 errorFns.map((fn) => {
266 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')
267 })
268})
269
270test('rpg() will take and obey a sum', t => {
271 _.times(1000, () => {
272 let rpg = chance.rpg('4d20', { sum: true })
273 t.true(_.isNumber(rpg))
274 t.true(rpg >= 4)
275 t.true(rpg <= 80)
276 })
277})
278
279// chance.tv()
280test('tv() works as expected', t => {
281 _.times(1000, () => {
282 let tv = chance.tv()
283 t.true(_.isString(tv))
284 t.is(tv.length, 4)
285 t.true(/^[KW][A-Z][A-Z][A-Z]/.test(tv))
286 })
287})