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
299
|
import test from 'ava'
import Chance from '../chance.js'
import _ from 'lodash'
const chance = new Chance()
// chance.ampm()
test('ampm() returns am or pm', t => {
_.times(1000, () => {
let ampm = chance.ampm()
t.true(_.isString(ampm))
t.true(/^([ap]m)$/m.test(ampm))
})
})
// chance.date()
test('date() returns a random date', t => {
_.times(1000, () => {
let date = chance.date()
t.true(_.isDate(date))
t.truthy(date.getTime())
})
})
test('date() accepts an american option', t => {
_.times(1000, () => {
let date = chance.date({ american: chance.bool() })
t.true(_.isDate(date))
t.truthy(date.getTime())
})
})
test('date() can have some default provided and obey them', t => {
_.times(1000, () => {
// One of each month type in terms of number of days.
let month = [0, 1, 3][Math.floor(Math.random() * 3)]
let date = chance.date({ year: 1983 })
t.true(_.isDate(date))
t.is(date.getFullYear(), 1983)
date = chance.date({ month: month })
t.true(_.isDate(date))
t.is(date.getMonth(), month)
date = chance.date({ day: 21 })
t.true(_.isDate(date))
t.is(date.getDate(), 21)
})
})
test('date() can specify min and max', t => {
_.times(1000, () => {
let bounds = {
min: new Date(),
max: new Date(new Date().getTime() + 1234567890123),
}
let date = chance.date(bounds)
t.true(_.isDate(date))
t.true(date >= bounds.min)
t.true(date <= bounds.max)
})
})
test('date() returns a date, can specify just min', t => {
_.times(1000, () => {
let bounds = { min: new Date() }
let date = chance.date(bounds)
t.true(_.isDate(date))
t.true(date >= bounds.min)
})
})
test('date() returns a date, can specify just max', t => {
_.times(1000, () => {
let bounds = { max: new Date() }
let date = chance.date(bounds)
t.true(_.isDate(date))
t.true(date <= bounds.max)
})
})
test('date() can return a string date', t => {
_.times(1000, () => {
let date = chance.date({ string: true })
t.true(_.isString(date))
t.true(/^[0-9][0-9]?\/[0-9][0-9]?\/[0-9]{4}/m.test(date))
})
})
// chance.hammertime()
test('hammertime() works', t => {
_.times(1000, () => {
let hammertime = chance.hammertime()
t.true(_.isNumber(hammertime))
t.true(hammertime > 0)
t.true(hammertime < 8640000000000000)
})
})
// chance.hour()
test('hour() returns an hour', t => {
_.times(1000, () => {
let hour = chance.hour()
t.true(_.isNumber(hour))
t.true(hour >= 1)
t.true(hour <= 12)
})
})
test('hour() returns an hour in 24 hour format', t => {
_.times(1000, () => {
let hour = chance.hour({ twentyfour: true })
t.true(_.isNumber(hour))
t.true(hour >= 0)
t.true(hour <= 23)
})
})
test('hour() returns an hour, can specify min and max', t => {
_.times(1000, () => {
let hour = chance.hour({ min: 7, max: 10 })
t.true(_.isNumber(hour))
t.true(hour >= 7)
t.true(hour <= 10)
})
})
test('hour() returns an hour, can specify just min', t => {
_.times(1000, () => {
let hour = chance.hour({ min: 7 })
t.true(_.isNumber(hour))
t.true(hour >= 7)
t.true(hour <= 12)
})
})
test('hour() returns an hour, can specify just max', t => {
_.times(1000, () => {
let hour = chance.hour({ max: 10 })
t.true(_.isNumber(hour))
t.true(hour >= 1)
t.true(hour <= 10)
})
})
// chance.minute()
test('minute() returns a minute', t => {
_.times(1000, () => {
let minute = chance.minute()
t.true(_.isNumber(minute))
t.true(minute >= 0)
t.true(minute <= 59)
})
})
test('minute() returns an minute, can specify min and max', t => {
_.times(1000, () => {
let minute = chance.minute({ min: 18, max: 35 })
t.true(_.isNumber(minute))
t.true(minute >= 18)
t.true(minute <= 35)
})
})
test('minute() returns an minute, can specify just min', t => {
_.times(1000, () => {
let minute = chance.minute({ min: 5 })
t.true(_.isNumber(minute))
t.true(minute >= 5)
t.true(minute <= 59)
})
})
test('minute() returns an minute, can specify just max', t => {
_.times(1000, () => {
let minute = chance.minute({ max: 32 })
t.true(_.isNumber(minute))
t.true(minute >= 0)
t.true(minute <= 32)
})
})
test('month() returns a month', t => {
_.times(1000, () => {
let month = chance.month()
t.true(_.isString(month))
})
})
test('month() will return a raw month', t => {
_.times(1000, () => {
let month = chance.month({ raw: true })
t.false(_.isString(month))
t.true(_.isObject(month))
})
})
test('month() returns a month, can specify min and max', t => {
_.times(1000, () => {
let month = chance.month({raw: true, min: 5, max: 10})
t.false(_.isString(month))
t.true(month.numeric >= 5)
t.true(month.numeric <= 10)
})
})
test('month() returns a month, can specify just min', t => {
_.times(1000, () => {
let month = chance.month({raw: true, min: 5})
t.false(_.isString(month))
t.true(month.numeric >= 5)
t.true(month.numeric <= 12)
})
})
test('month() returns a month, can specify just max', t => {
_.times(1000, () => {
let month = chance.month({raw: true, max: 7})
t.false(_.isString(month))
t.true(month.numeric >= 1)
t.true(month.numeric <= 7)
})
})
// chance.timestamp()
test('timestamp() returns a timestamp', t => {
_.times(1000, () => {
let timestamp = chance.timestamp()
t.true(_.isNumber(timestamp))
t.true(timestamp > 0)
t.true(timestamp <= parseInt(new Date().getTime() / 1000, 10))
})
})
// chance.timezone()
test('timezone() returns a timezone', t => {
_.times(1000, () => {
let timezone = chance.timezone()
t.true(_.isString(timezone.name))
t.true(timezone.abbr.length < 6)
t.true(_.isNumber(timezone.offset))
})
})
// chance.weekday()
test('weekday() will return a weekday as a string', t => {
_.times(1000, () => {
let weekday = chance.weekday()
t.true(_.isString(weekday))
})
})
test('weekday() can take work: true and obey it', t => {
_.times(1000, () => {
let weekday = chance.weekday({ weekday_only: true })
t.true(_.isString(weekday))
t.not(weekday, 'Saturday')
t.not(weekday, 'Sunday')
})
})
// chance.year()
test('year() returns a year, default between today and a century after', t => {
_.times(1000, () => {
let year = chance.year()
t.true(_.isString(year))
t.true(year >= new Date().getFullYear())
t.true(year <= new Date().getFullYear() + 100)
})
})
test('year() returns a year, can specify min and max', t => {
_.times(1000, () => {
let year = chance.year({ min: 2500, max: 2600 })
t.true(_.isString(year))
t.true(year >= 2500)
t.true(year <= 2600)
})
})
test('year() returns a year, can specify just min', t => {
_.times(1000, () => {
let year = chance.year({ min: 2500 })
t.true(_.isString(year))
t.true(year >= 2500)
})
})
test('year() returns a year, can specify just max', t => {
_.times(1000, () => {
let year = chance.year({ max: 2500 })
t.true(_.isString(year))
t.true(year <= 2501)
// Ensure year not negative. Perhaps add BCE/AD and such later,
// but for now just positive is good enough.
t.true(year >= 0)
})
})
|