1 | var casual = require('../');
|
2 |
|
3 | describe('API', function() {
|
4 | var max_times = 10;
|
5 | var test = function(name) {
|
6 | it('casual.' + name + ' should be ok', function(done) {
|
7 | if (typeof casual[name] === 'function') {
|
8 | var pivot = casual[name]();
|
9 | } else {
|
10 | var pivot = casual[name];
|
11 | }
|
12 |
|
13 | for (var i = 0; i < max_times; i++) {
|
14 | if (typeof casual[name] === 'function') {
|
15 | var result = casual[name]();
|
16 | } else {
|
17 | var result = casual[name];
|
18 | }
|
19 |
|
20 | if (result != pivot) {
|
21 | return done();
|
22 | }
|
23 | }
|
24 |
|
25 | done(new Error('Fail'));
|
26 | });
|
27 | };
|
28 |
|
29 | var address = [
|
30 | 'zip',
|
31 | 'city',
|
32 | 'street',
|
33 | 'address',
|
34 | 'address1',
|
35 | 'address2',
|
36 | 'state',
|
37 | 'state_abbr',
|
38 | 'latitude',
|
39 | 'longitude',
|
40 | 'country',
|
41 | 'building_number'
|
42 | ];
|
43 |
|
44 | var text = [
|
45 | 'sentence',
|
46 | 'sentences',
|
47 | 'title',
|
48 | 'text',
|
49 | 'description',
|
50 | 'short_description',
|
51 | 'string',
|
52 | 'word',
|
53 | 'words',
|
54 | 'array_of_words',
|
55 | 'letter',
|
56 | 'letter_phonetic'
|
57 | ];
|
58 |
|
59 | var internet = [
|
60 | 'ip',
|
61 | 'domain',
|
62 | 'url',
|
63 | 'email'
|
64 | ];
|
65 |
|
66 | var person = [
|
67 | 'name',
|
68 | 'username',
|
69 | 'first_name',
|
70 | 'last_name',
|
71 | 'full_name',
|
72 | 'password',
|
73 | 'name_prefix',
|
74 | 'name_suffix',
|
75 | 'company_name',
|
76 | 'catch_phrase',
|
77 | 'phone'
|
78 | ];
|
79 |
|
80 | var number = [
|
81 | 'integer',
|
82 | 'double',
|
83 | 'digit',
|
84 | 'array_of_digits',
|
85 | 'array_of_integers',
|
86 | 'array_of_doubles'
|
87 | ];
|
88 |
|
89 | var date = [
|
90 | 'unix_time',
|
91 | 'moment',
|
92 | 'date',
|
93 | 'time',
|
94 | 'century',
|
95 | 'am_pm',
|
96 | 'day_of_year',
|
97 | 'day_of_month',
|
98 | 'day_of_week',
|
99 | 'month_number',
|
100 | 'month_name',
|
101 | 'year',
|
102 | 'timezone'
|
103 | ];
|
104 |
|
105 | var payment = [
|
106 | 'card_type',
|
107 | 'card_number',
|
108 | 'card_exp',
|
109 | 'card_data'
|
110 | ];
|
111 |
|
112 | var misc = [
|
113 | 'country_code',
|
114 | 'language_code',
|
115 | 'locale',
|
116 | 'currency',
|
117 | 'currency_code',
|
118 | 'currency_symbol',
|
119 | 'currency_name',
|
120 | 'mime_type',
|
121 | 'file_extension',
|
122 | 'boolean',
|
123 | 'uuid'
|
124 | ];
|
125 |
|
126 | var color = [
|
127 | 'color_name',
|
128 | 'safe_color_name',
|
129 | 'rgb_hex',
|
130 | 'rgb_array'
|
131 | ];
|
132 |
|
133 | var providers = [
|
134 | address,
|
135 | text,
|
136 | internet,
|
137 | person,
|
138 | number,
|
139 | date,
|
140 | payment,
|
141 | misc,
|
142 | color
|
143 | ];
|
144 |
|
145 | describe('Embedded generators', function() {
|
146 | describe('Address address provider', function() {
|
147 | address.forEach(test);
|
148 | });
|
149 |
|
150 | describe('Text provider', function() {
|
151 | text.forEach(test);
|
152 | });
|
153 |
|
154 | describe('Internet provider', function() {
|
155 | internet.forEach(test);
|
156 | });
|
157 |
|
158 | describe('Person provider', function() {
|
159 | person.forEach(test);
|
160 | });
|
161 |
|
162 | describe('Number generator', function() {
|
163 | number.forEach(test);
|
164 | });
|
165 |
|
166 | describe('Date provider', function() {
|
167 | date.forEach(test);
|
168 | });
|
169 |
|
170 | describe('Payment provider', function() {
|
171 | payment.forEach(test);
|
172 | });
|
173 |
|
174 | describe('Misc provider', function() {
|
175 | misc.forEach(test);
|
176 | });
|
177 |
|
178 | describe('Color provider', function() {
|
179 | color.forEach(test);
|
180 | });
|
181 | });
|
182 |
|
183 | describe('Casual helpers', function() {
|
184 | describe('define', function() {
|
185 | it('Should create new casual property if getter doesn\'t have arguments', function() {
|
186 | casual.define('wow', function() {
|
187 | return 'wow wow';
|
188 | });
|
189 |
|
190 | casual.wow.should.be.equal('wow wow');
|
191 | });
|
192 |
|
193 | it('Should create new casual method if getter does have arguments', function() {
|
194 | casual.define('x2', function(x) {
|
195 | x = x || 2;
|
196 | return x * 2;
|
197 | });
|
198 |
|
199 | casual.x2(3).should.be.equal(6);
|
200 | });
|
201 | });
|
202 |
|
203 | describe('random_element', function() {
|
204 | it('Should pick random element from array', function(done) {
|
205 | var array = [1,2,3,4,5,23,6,7,8,95,43];
|
206 | var pivot = casual.random_element(array);
|
207 |
|
208 | for (var i = 0; i < max_times; ++i) {
|
209 | if (pivot != casual.random_element(array)) {
|
210 | return done();
|
211 | }
|
212 | }
|
213 |
|
214 | done(new Error('Fail'));
|
215 | });
|
216 | });
|
217 |
|
218 | describe('random_key', function() {
|
219 | it('Should return random object key', function(done) {
|
220 | var key = casual.random_key({ a: 1, b: 2});
|
221 | if (key === 'a' || key === 'b') {
|
222 | return done();
|
223 | }
|
224 |
|
225 | done(new Error('Fail'));
|
226 | });
|
227 | });
|
228 |
|
229 | describe('random_value', function() {
|
230 | it('Should return random object value', function(done) {
|
231 | var key = casual.random_value({ a: 1, b: 2});
|
232 | if (key === 1 || key === 2) {
|
233 | return done();
|
234 | }
|
235 |
|
236 | done(new Error('Fail'));
|
237 | });
|
238 | });
|
239 |
|
240 | describe('extend', function() {
|
241 | it('Should extend object', function() {
|
242 | var result = casual.extend({}, {a: 42});
|
243 | result.should.have.property('a', 42);
|
244 | });
|
245 | });
|
246 |
|
247 | describe('numerify', function() {
|
248 | it('Should replace every # in string with digit', function() {
|
249 | var format = '####';
|
250 | var numbers = casual.numerify(format);
|
251 | parseInt(numbers).should.be.within(0, 9999);
|
252 | });
|
253 | });
|
254 |
|
255 | describe('letterify',function(){
|
256 | it('should replace every X in a string with a letter',function() {
|
257 | var re = /^[a-zA-Z]+$/;
|
258 |
|
259 | var result = casual.letterify('XXXX');
|
260 | re.test(result).should.be.true;
|
261 |
|
262 | result = casual.letterify('1234');
|
263 | re.test(result).should.be.false;
|
264 |
|
265 | result = casual.letterify('X123X');
|
266 | re.test(result).should.be.false;
|
267 | });
|
268 | })
|
269 |
|
270 | describe('register_provider', function() {
|
271 | it('Should define generators', function() {
|
272 | casual.register_provider({
|
273 | really_custom_generator: function() { return 'custom' }
|
274 | });
|
275 | casual.really_custom_generator.should.be.equal('custom');
|
276 | });
|
277 | });
|
278 |
|
279 | describe('join', function() {
|
280 | it('Should join strings with spaces like a boss', function() {
|
281 | casual.join('wow', 'such', 'sentence').should.be.equal('wow such sentence');
|
282 | })
|
283 | });
|
284 | });
|
285 |
|
286 | describe('Generator seeding', function() {
|
287 | var create_data_set = function() {
|
288 | return {
|
289 | description: casual.description,
|
290 | text: casual.text,
|
291 | random: casual.random,
|
292 | integer: casual.integer,
|
293 | card_number: casual.card_number,
|
294 | phone: casual.phone,
|
295 | unix_time: casual.unix_time,
|
296 | day_of_year: casual.day_of_year,
|
297 | date: casual.date,
|
298 | time: casual.time,
|
299 | moment_string: casual.moment.toISOString(),
|
300 | };
|
301 | };
|
302 |
|
303 | it('Should repeat random sequence on same seed', function(done) {
|
304 | this.timeout(3000);
|
305 |
|
306 | var seed = 123;
|
307 |
|
308 | casual.seed(seed);
|
309 | var set1 = create_data_set();
|
310 |
|
311 | setTimeout(function () {
|
312 | casual.seed(seed);
|
313 | var set2 = create_data_set();
|
314 |
|
315 | for (var i in set1) {
|
316 | set1[i].should.be.equal(set2[i], i);
|
317 | }
|
318 |
|
319 | done();
|
320 | }, 2001);
|
321 | })
|
322 | });
|
323 |
|
324 | var verify_generator = function(generator) {
|
325 | var function_name = '_' + generator;
|
326 | casual[function_name].should.be.a.Function;
|
327 |
|
328 | var seed = 123;
|
329 |
|
330 | casual.seed(seed);
|
331 | var val = casual[generator];
|
332 | val = typeof val === 'function' ? val() : val;
|
333 |
|
334 | casual.seed(seed);
|
335 | var function_val = casual[function_name]();
|
336 | val.should.be.eql(function_val);
|
337 | };
|
338 |
|
339 | var check_getters = function(generators) {
|
340 | generators.forEach(verify_generator);
|
341 | };
|
342 |
|
343 | describe('Pure getters', function() {
|
344 | it('Should have getter function at _{name}', function() {
|
345 | providers.forEach(check_getters);
|
346 | });
|
347 |
|
348 | it('Should return only funtions interface', function() {
|
349 | var functions = casual.functions();
|
350 | for (var name in functions) {
|
351 | if (name === 'seed' || casual.locales.indexOf(name) !== -1) {
|
352 | continue;
|
353 | }
|
354 |
|
355 | var generator = functions[name];
|
356 | var seed = 546;
|
357 |
|
358 | casual.seed(seed);
|
359 | var first = casual['_' + name]();
|
360 |
|
361 | casual.seed(seed);
|
362 | var second = generator();
|
363 |
|
364 | first.should.be.eql(second);
|
365 | }
|
366 | });
|
367 | });
|
368 | });
|