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