UNPKG

7.94 kBJavaScriptView Raw
1import test from 'ava'
2import Chance from '../chance.js'
3import _ from 'lodash'
4
5const chance = new Chance()
6
7// chance.ampm()
8test('ampm() returns am or pm', t => {
9 _.times(1000, () => {
10 let ampm = chance.ampm()
11 t.true(_.isString(ampm))
12 t.true(/^([ap]m)$/m.test(ampm))
13 })
14})
15
16// chance.date()
17test('date() returns a random date', t => {
18 _.times(1000, () => {
19 let date = chance.date()
20 t.true(_.isDate(date))
21 t.truthy(date.getTime())
22 })
23})
24
25test('date() accepts an american option', t => {
26 _.times(1000, () => {
27 let date = chance.date({ american: chance.bool() })
28 t.true(_.isDate(date))
29 t.truthy(date.getTime())
30 })
31})
32
33test('date() can have some default provided and obey them', t => {
34 _.times(1000, () => {
35 // One of each month type in terms of number of days.
36 let month = [0, 1, 3][Math.floor(Math.random() * 3)]
37
38 let date = chance.date({ year: 1983 })
39 t.true(_.isDate(date))
40 t.is(date.getFullYear(), 1983)
41
42 date = chance.date({ month: month })
43 t.true(_.isDate(date))
44 t.is(date.getMonth(), month)
45
46 date = chance.date({ day: 21 })
47 t.true(_.isDate(date))
48 t.is(date.getDate(), 21)
49 })
50})
51
52test('date() can specify min and max', t => {
53 _.times(1000, () => {
54 let bounds = {
55 min: new Date(),
56 max: new Date(new Date().getTime() + 1234567890123),
57 }
58 let date = chance.date(bounds)
59 t.true(_.isDate(date))
60 t.true(date >= bounds.min)
61 t.true(date <= bounds.max)
62 })
63})
64
65test('date() returns a date, can specify just min', t => {
66 _.times(1000, () => {
67 let bounds = { min: new Date() }
68 let date = chance.date(bounds)
69 t.true(_.isDate(date))
70 t.true(date >= bounds.min)
71 })
72})
73
74test('date() returns a date, can specify just max', t => {
75 _.times(1000, () => {
76 let bounds = { max: new Date() }
77 let date = chance.date(bounds)
78 t.true(_.isDate(date))
79 t.true(date <= bounds.max)
80 })
81})
82
83test('date() can return a string date', t => {
84 _.times(1000, () => {
85 let date = chance.date({ string: true })
86 t.true(_.isString(date))
87 t.true(/^[0-9][0-9]?\/[0-9][0-9]?\/[0-9]{4}/m.test(date))
88 })
89})
90
91// chance.hammertime()
92test('hammertime() works', t => {
93 _.times(1000, () => {
94 let hammertime = chance.hammertime()
95 t.true(_.isNumber(hammertime))
96 t.true(hammertime > 0)
97 t.true(hammertime < 8640000000000000)
98 })
99})
100
101// chance.hour()
102test('hour() returns an hour', t => {
103 _.times(1000, () => {
104 let hour = chance.hour()
105 t.true(_.isNumber(hour))
106 t.true(hour >= 1)
107 t.true(hour <= 12)
108 })
109})
110
111test('hour() returns an hour in 24 hour format', t => {
112 _.times(1000, () => {
113 let hour = chance.hour({ twentyfour: true })
114 t.true(_.isNumber(hour))
115 t.true(hour >= 0)
116 t.true(hour <= 23)
117 })
118})
119
120test('hour() returns an hour, can specify min and max', t => {
121 _.times(1000, () => {
122 let hour = chance.hour({ min: 7, max: 10 })
123 t.true(_.isNumber(hour))
124 t.true(hour >= 7)
125 t.true(hour <= 10)
126 })
127})
128
129test('hour() returns an hour, can specify just min', t => {
130 _.times(1000, () => {
131 let hour = chance.hour({ min: 7 })
132 t.true(_.isNumber(hour))
133 t.true(hour >= 7)
134 t.true(hour <= 12)
135 })
136})
137
138test('hour() returns an hour, can specify just max', t => {
139 _.times(1000, () => {
140 let hour = chance.hour({ max: 10 })
141 t.true(_.isNumber(hour))
142 t.true(hour >= 1)
143 t.true(hour <= 10)
144 })
145})
146
147// chance.minute()
148test('minute() returns a minute', t => {
149 _.times(1000, () => {
150 let minute = chance.minute()
151 t.true(_.isNumber(minute))
152 t.true(minute >= 0)
153 t.true(minute <= 59)
154 })
155})
156
157test('minute() returns an minute, can specify min and max', t => {
158 _.times(1000, () => {
159 let minute = chance.minute({ min: 18, max: 35 })
160 t.true(_.isNumber(minute))
161 t.true(minute >= 18)
162 t.true(minute <= 35)
163 })
164})
165
166test('minute() returns an minute, can specify just min', t => {
167 _.times(1000, () => {
168 let minute = chance.minute({ min: 5 })
169 t.true(_.isNumber(minute))
170 t.true(minute >= 5)
171 t.true(minute <= 59)
172 })
173})
174
175test('minute() returns an minute, can specify just max', t => {
176 _.times(1000, () => {
177 let minute = chance.minute({ max: 32 })
178 t.true(_.isNumber(minute))
179 t.true(minute >= 0)
180 t.true(minute <= 32)
181 })
182})
183
184test('month() returns a month', t => {
185 _.times(1000, () => {
186 let month = chance.month()
187 t.true(_.isString(month))
188 })
189})
190
191test('month() will return a raw month', t => {
192 _.times(1000, () => {
193 let month = chance.month({ raw: true })
194 t.false(_.isString(month))
195 t.true(_.isObject(month))
196 })
197})
198
199test('month() returns a month, can specify min and max', t => {
200 _.times(1000, () => {
201 let month = chance.month({raw: true, min: 5, max: 10})
202 t.false(_.isString(month))
203 t.true(month.numeric >= 5)
204 t.true(month.numeric <= 10)
205 })
206})
207
208test('month() returns a month, can specify just min', t => {
209 _.times(1000, () => {
210 let month = chance.month({raw: true, min: 5})
211 t.false(_.isString(month))
212 t.true(month.numeric >= 5)
213 t.true(month.numeric <= 12)
214 })
215})
216
217test('month() returns a month, can specify just max', t => {
218 _.times(1000, () => {
219 let month = chance.month({raw: true, max: 7})
220 t.false(_.isString(month))
221 t.true(month.numeric >= 1)
222 t.true(month.numeric <= 7)
223 })
224})
225
226// chance.timestamp()
227test('timestamp() returns a timestamp', t => {
228 _.times(1000, () => {
229 let timestamp = chance.timestamp()
230 t.true(_.isNumber(timestamp))
231 t.true(timestamp > 0)
232 t.true(timestamp <= parseInt(new Date().getTime() / 1000, 10))
233 })
234})
235
236// chance.timezone()
237test('timezone() returns a timezone', t => {
238 _.times(1000, () => {
239 let timezone = chance.timezone()
240 t.true(_.isString(timezone.name))
241 t.true(timezone.abbr.length < 6)
242 t.true(_.isNumber(timezone.offset))
243 })
244})
245
246// chance.weekday()
247test('weekday() will return a weekday as a string', t => {
248 _.times(1000, () => {
249 let weekday = chance.weekday()
250 t.true(_.isString(weekday))
251 })
252})
253
254test('weekday() can take work: true and obey it', t => {
255 _.times(1000, () => {
256 let weekday = chance.weekday({ weekday_only: true })
257 t.true(_.isString(weekday))
258 t.not(weekday, 'Saturday')
259 t.not(weekday, 'Sunday')
260 })
261})
262
263// chance.year()
264test('year() returns a year, default between today and a century after', t => {
265 _.times(1000, () => {
266 let year = chance.year()
267 t.true(_.isString(year))
268 t.true(year >= new Date().getFullYear())
269 t.true(year <= new Date().getFullYear() + 100)
270 })
271})
272
273test('year() returns a year, can specify min and max', t => {
274 _.times(1000, () => {
275 let year = chance.year({ min: 2500, max: 2600 })
276 t.true(_.isString(year))
277 t.true(year >= 2500)
278 t.true(year <= 2600)
279 })
280})
281
282test('year() returns a year, can specify just min', t => {
283 _.times(1000, () => {
284 let year = chance.year({ min: 2500 })
285 t.true(_.isString(year))
286 t.true(year >= 2500)
287 })
288})
289
290test('year() returns a year, can specify just max', t => {
291 _.times(1000, () => {
292 let year = chance.year({ max: 2500 })
293 t.true(_.isString(year))
294 t.true(year <= 2501)
295 // Ensure year not negative. Perhaps add BCE/AD and such later,
296 // but for now just positive is good enough.
297 t.true(year >= 0)
298 })
299})