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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
|
import { describe, expect, it } from 'vitest';
import { FakerError, faker } from '../../src';
import { seededTests } from '../support/seeded-runs';
import { times } from './../support/times';
const NON_SEEDED_BASED_RUN = 5;
describe('string', () => {
seededTests(faker, 'string', (t) => {
t.describe('fromCharacters', (t) => {
t.it('with string characters', 'foobar')
.it('with string[] characters', [...'foobar'])
.it('with string characters and length', 'foobar', 5)
.it('with string[] characters and length', [...'foobar'], 5)
.it('with string characters and length range', 'foobar', {
min: 10,
max: 20,
})
.it('with string[] characters and length range', [...'foobar'], {
min: 10,
max: 20,
});
});
t.describe('alpha', (t) => {
t.it('noArgs')
.itRepeated('with length parameter', 5, 5)
.it('with length', { length: 6 })
.it('with length range', { length: { min: 10, max: 20 } })
.it('with casing = lower', { casing: 'lower' })
.it('with casing = upper', { casing: 'upper' })
.it('with casing = mixed', { casing: 'mixed' })
.it('with exclude', { exclude: 'abcdefghijk' })
.it('with length, casing and exclude', {
length: 7,
casing: 'lower',
exclude: 'lmnopqrstu',
});
});
t.describe('alphanumeric', (t) => {
t.it('noArgs')
.itRepeated('with length parameter', 5, 5)
.it('with length', { length: 6 })
.it('with length range', { length: { min: 10, max: 20 } })
.it('with casing = lower', { casing: 'lower' })
.it('with casing = upper', { casing: 'upper' })
.it('with casing = mixed', { casing: 'mixed' })
.it('with exclude', { exclude: 'abcdefghijk12345' })
.it('with length, casing and exclude', {
length: 7,
casing: 'lower',
exclude: 'lmnopqrstu67890',
});
});
t.describe('binary', (t) => {
t.it('noArgs')
.it('with length', { length: 6 })
.it('with length range', { length: { min: 10, max: 20 } })
.it('with custom prefix', { prefix: 'bin_' })
.it('with length and empty prefix', {
length: 7,
prefix: '',
});
});
t.describe('octal', (t) => {
t.it('noArgs')
.it('with length', { length: 6 })
.it('with length range', { length: { min: 10, max: 20 } })
.it('with custom prefix', { prefix: 'oct_' })
.it('with length and empty prefix', {
length: 7,
prefix: '',
});
});
t.describe('hexadecimal', (t) => {
t.it('noArgs')
.it('with length', { length: 6 })
.it('with length range', { length: { min: 10, max: 20 } })
.it('with casing = lower', { casing: 'lower' })
.it('with casing = upper', { casing: 'upper' })
.it('with casing = mixed', { casing: 'mixed' })
.it('with custom prefix', { prefix: 'hex_' })
.it('with length, casing and empty prefix', {
length: 7,
casing: 'lower',
prefix: '',
});
});
t.describe('numeric', (t) => {
t.it('noArgs')
.itRepeated('with length parameter', 5, 5)
.it('with length', { length: 6 })
.it('with length range', { length: { min: 10, max: 20 } })
.it('with allowLeadingZeros', { allowLeadingZeros: false })
.it('with exclude', { exclude: '12345' })
.it('with length, allowLeadingZeros and exclude', {
length: 7,
allowLeadingZeros: true,
exclude: '12345',
});
});
t.describe('sample', (t) => {
t.it('noArgs')
.itRepeated('with length parameter', 5, 5)
.it('with length range', { min: 10, max: 20 });
});
t.itRepeated('uuid', 5);
t.describe('nanoid', (t) => {
t.itRepeated('noArgs', 5)
.it('with length parameter', 30)
.it('with length range', { min: 13, max: 37 });
});
t.describe('symbol', (t) => {
t.it('noArgs')
.itRepeated('with length parameter', 5, 5)
.it('with length range', { min: 10, max: 20 });
});
});
describe.each(times(NON_SEEDED_BASED_RUN).map(() => faker.seed()))(
'random seeded tests for seed %i',
() => {
describe('fromCharacters', () => {
it('should return single character when no length provided', () => {
const actual = faker.string.fromCharacters('foobar');
expect(actual).toHaveLength(1);
});
it('should only contain characters from provided string', () => {
const actual = faker.string.fromCharacters('foobar');
expect(actual).toMatch(/^[foobar]$/);
});
it('should generate 5 random letters', () => {
const actual = faker.string.fromCharacters('foobar', 5);
expect(actual).toHaveLength(5);
});
it.each([0, -1, -100])(
'should return empty string when length is <= 0',
(length) => {
const actual = faker.string.fromCharacters('foobar', length);
expect(actual).toBe('');
}
);
it('should return a random amount of characters', () => {
const actual = faker.string.fromCharacters('foobar', {
min: 10,
max: 20,
});
expect(actual).toBeTruthy();
expect(actual).toBeTypeOf('string');
expect(actual.length).toBeGreaterThanOrEqual(10);
expect(actual.length).toBeLessThanOrEqual(20);
});
it('should throw if no characters are passed (string)', () => {
expect(() => faker.string.fromCharacters('')).toThrow(
new FakerError(
'Unable to generate string: No characters to select from.'
)
);
});
it('should throw if no characters are passed (string[])', () => {
expect(() => faker.string.fromCharacters([])).toThrow(
new FakerError(
'Unable to generate string: No characters to select from.'
)
);
});
});
describe('alpha', () => {
it('should return single letter when no length provided', () => {
const actual = faker.string.alpha();
expect(actual).toHaveLength(1);
});
it('should return any letters when no option is provided', () => {
const actual = faker.string.alpha();
expect(actual).toMatch(/^[a-zA-Z]$/);
});
it.each([
['upper', /^[A-Z]{250}$/],
['lower', /^[a-z]{250}$/],
['mixed', /^[a-zA-Z]{250}$/],
] as const)('should return %s-case', (casing, pattern) => {
const actual = faker.string.alpha({ length: 250, casing });
expect(actual).toMatch(pattern);
});
it('should generate 5 random letters', () => {
const actual = faker.string.alpha(5);
expect(actual).toHaveLength(5);
});
it.each([0, -1, -100])(
'should return empty string when length is <= 0',
(length) => {
const actual = faker.string.alpha(length);
expect(actual).toBe('');
}
);
it('should return a random amount of characters', () => {
const actual = faker.string.alpha({ length: { min: 10, max: 20 } });
expect(actual).toBeTruthy();
expect(actual).toBeTypeOf('string');
expect(actual.length).toBeGreaterThanOrEqual(10);
expect(actual.length).toBeLessThanOrEqual(20);
});
it('should be able to ban some characters', () => {
const actual = faker.string.alpha({
length: 5,
casing: 'lower',
exclude: ['a', 'p'],
});
expect(actual).toHaveLength(5);
expect(actual).toMatch(/^[b-oq-z]{5}$/);
});
it('should be able to ban some characters via string', () => {
const actual = faker.string.alpha({
length: 5,
casing: 'lower',
exclude: 'ap',
});
expect(actual).toHaveLength(5);
expect(actual).toMatch(/^[b-oq-z]{5}$/);
});
it('should be able handle mistake in excluded characters array', () => {
const alphaText = faker.string.alpha({
length: 5,
casing: 'lower',
exclude: ['a', 'a', 'p'],
});
expect(alphaText).toHaveLength(5);
expect(alphaText).toMatch(/^[b-oq-z]{5}$/);
});
it('should throw if all possible characters being excluded (string)', () => {
const exclude = 'abcdefghijklmnopqrstuvwxyz';
expect(() =>
faker.string.alpha({
length: 5,
casing: 'lower',
exclude,
})
).toThrow(
new FakerError(
'Unable to generate string: No characters to select from.'
)
);
});
it('should throw if all possible characters being excluded (string[])', () => {
const exclude = [...'abcdefghijklmnopqrstuvwxyz'];
expect(() =>
faker.string.alpha({
length: 5,
casing: 'lower',
exclude,
})
).toThrow(
new FakerError(
'Unable to generate string: No characters to select from.'
)
);
});
it('should not mutate the input object', () => {
const input: {
length: number;
casing: 'mixed';
exclude: string[];
} = Object.freeze({
length: 5,
casing: 'mixed',
exclude: ['a', '%'],
});
expect(() => faker.string.alpha(input)).not.toThrow();
expect(input.exclude).toEqual(['a', '%']);
});
});
describe('alphaNumeric', () => {
it('should generate single character when no additional argument was provided', () => {
const actual = faker.string.alphanumeric();
expect(actual).toHaveLength(1);
});
it.each([
['upper', /^[A-Z0-9]{250}$/],
['lower', /^[a-z0-9]{250}$/],
['mixed', /^[a-zA-Z0-9]{250}$/],
] as const)('should return %s-case', (casing, pattern) => {
const actual = faker.string.alphanumeric({ length: 250, casing });
expect(actual).toMatch(pattern);
});
it('should generate 5 random characters', () => {
const actual = faker.string.alphanumeric(5);
expect(actual).toHaveLength(5);
});
it.each([0, -1, -100])(
'should return empty string when length is <= 0',
(length) => {
const actual = faker.string.alphanumeric(length);
expect(actual).toBe('');
}
);
it('should return a random amount of characters', () => {
const actual = faker.string.alphanumeric({
length: { min: 10, max: 20 },
});
expect(actual).toBeTruthy();
expect(actual).toBeTypeOf('string');
expect(actual.length).toBeGreaterThanOrEqual(10);
expect(actual.length).toBeLessThanOrEqual(20);
});
it('should be able to ban all alphabetic characters', () => {
const exclude = [...'abcdefghijklmnopqrstuvwxyz'];
const alphaText = faker.string.alphanumeric({
length: 5,
casing: 'lower',
exclude,
});
expect(alphaText).toHaveLength(5);
for (const excludedChar of exclude) {
expect(alphaText).not.includes(excludedChar);
}
});
it('should be able to ban all alphabetic characters via string', () => {
const exclude = 'abcdefghijklmnopqrstuvwxyz';
const alphaText = faker.string.alphanumeric({
length: 5,
casing: 'lower',
exclude,
});
expect(alphaText).toHaveLength(5);
for (const excludedChar of exclude) {
expect(alphaText).not.includes(excludedChar);
}
});
it('should be able to ban all numeric characters', () => {
const exclude = [...'0123456789'];
const alphaText = faker.string.alphanumeric({
length: 5,
exclude,
});
expect(alphaText).toHaveLength(5);
for (const excludedChar of exclude) {
expect(alphaText).not.includes(excludedChar);
}
});
it('should be able to ban all numeric characters via string', () => {
const exclude = '0123456789';
const alphaText = faker.string.alphanumeric({
length: 5,
exclude,
});
expect(alphaText).toHaveLength(5);
for (const excludedChar of exclude) {
expect(alphaText).not.includes(excludedChar);
}
});
it('should be able to handle mistake in excluded characters array', () => {
const alphaText = faker.string.alphanumeric({
length: 5,
casing: 'lower',
exclude: ['a', 'p', 'a'],
});
expect(alphaText).toHaveLength(5);
expect(alphaText).toMatch(/^[0-9b-oq-z]{5}$/);
});
it('should throw if all possible characters being excluded (string)', () => {
const exclude = 'abcdefghijklmnopqrstuvwxyz0123456789';
expect(() =>
faker.string.alphanumeric({
length: 5,
casing: 'lower',
exclude,
})
).toThrow(
new FakerError(
'Unable to generate string: No characters to select from.'
)
);
});
it('should throw if all possible characters being excluded (string[])', () => {
const exclude = [...'abcdefghijklmnopqrstuvwxyz0123456789'];
expect(() =>
faker.string.alphanumeric({
length: 5,
casing: 'lower',
exclude,
})
).toThrow(
new FakerError(
'Unable to generate string: No characters to select from.'
)
);
});
it('should not mutate the input object', () => {
const input: {
length: number;
exclude: string[];
} = Object.freeze({
length: 5,
exclude: ['a', '0', '%'],
});
expect(() => faker.string.alphanumeric(input)).not.toThrow();
expect(input.exclude).toEqual(['a', '0', '%']);
});
});
describe(`binary`, () => {
it('generates a single binary character when no additional argument was provided', () => {
const binary = faker.string.binary();
expect(binary).toMatch(/^0b[01]$/i);
expect(binary).toHaveLength(3);
});
it('generates a random binary string with fixed length and no prefix', () => {
const binary = faker.string.binary({
length: 5,
prefix: '',
});
expect(binary).toMatch(/^[01]*$/i);
expect(binary).toHaveLength(5);
});
it.each([0, -1, -100])(
'should return the prefix when length is <= 0',
(length) => {
const binary = faker.string.binary({ length });
expect(binary).toBe('0b');
}
);
it('should return a binary string with a random amount of characters and no prefix', () => {
const binary = faker.string.binary({
length: { min: 10, max: 20 },
prefix: '',
});
expect(binary).toBeDefined();
expect(binary).toBeTypeOf('string');
expect(binary.length).toBeGreaterThanOrEqual(10);
expect(binary.length).toBeLessThanOrEqual(20);
});
});
describe(`octal`, () => {
it('generates single octal character when no additional argument was provided', () => {
const octal = faker.string.octal();
expect(octal).toMatch(/^0o[0-7]$/i);
expect(octal).toHaveLength(3);
});
it('generates a random octal string with fixed length and no prefix', () => {
const octal = faker.string.octal({
length: 5,
prefix: '',
});
expect(octal).toMatch(/^[0-7]*$/i);
expect(octal).toHaveLength(5);
});
it.each([0, -1, -100])(
'should return the prefix when length is <= 0',
(length) => {
const octal = faker.string.octal({ length });
expect(octal).toBe('0o');
}
);
it('should return an octal string with a random amount of characters and no prefix', () => {
const octal = faker.string.octal({
length: { min: 10, max: 20 },
prefix: '',
});
expect(octal).toBeDefined();
expect(octal).toBeTypeOf('string');
expect(octal.length).toBeGreaterThanOrEqual(10);
expect(octal.length).toBeLessThanOrEqual(20);
});
});
describe(`hexadecimal`, () => {
it('generates single hex character when no additional argument was provided', () => {
const hex = faker.string.hexadecimal();
expect(hex).toMatch(/^0x[0-9a-f]*$/i);
expect(hex).toHaveLength(3);
});
it('generates a random hex string', () => {
const hex = faker.string.hexadecimal({
length: 5,
prefix: '',
});
expect(hex).toMatch(/^[0-9a-f]*$/i);
expect(hex).toHaveLength(5);
});
it.each([0, -1, -100])(
'should return the prefix when length is <= 0',
(length) => {
const actual = faker.string.hexadecimal({ length });
expect(actual).toBe('0x');
}
);
it('should return a random amount of characters', () => {
const actual = faker.string.hexadecimal({
length: { min: 10, max: 20 },
prefix: '',
});
expect(actual).toBeTruthy();
expect(actual).toBeTypeOf('string');
expect(actual.length).toBeGreaterThanOrEqual(10);
expect(actual.length).toBeLessThanOrEqual(20);
});
});
describe('numeric', () => {
it('should return single digit when no length provided', () => {
const actual = faker.string.numeric();
expect(actual).toHaveLength(1);
expect(actual).toMatch(/^[0-9]$/);
});
it.each(times(100))(
'should generate random value with a length of %s',
(length) => {
const actual = faker.string.numeric(length);
expect(actual).toHaveLength(length);
expect(actual).toMatch(/^[0-9]*$/);
}
);
it('should return a random amount of characters', () => {
const actual = faker.string.numeric({
length: { min: 10, max: 20 },
});
expect(actual).toBeTruthy();
expect(actual).toBeTypeOf('string');
expect(actual.length).toBeGreaterThanOrEqual(10);
expect(actual.length).toBeLessThanOrEqual(20);
});
it('should return empty string with a length of 0', () => {
const actual = faker.string.numeric(0);
expect(actual).toHaveLength(0);
});
it('should return empty string with a negative length', () => {
const actual = faker.string.numeric(-10);
expect(actual).toHaveLength(0);
});
it('should return a valid numeric string with provided length', () => {
const actual = faker.string.numeric(1000);
expect(actual).toBeTypeOf('string');
expect(actual).toHaveLength(1000);
expect(actual).toMatch(/^[0-9]+$/);
});
it('should allow leading zeros via option', () => {
const actual = faker.string.numeric({
length: 15,
allowLeadingZeros: true,
});
expect(actual).toMatch(/^[0-9]+$/);
});
it('should allow leading zeros via option and all other digits excluded', () => {
const actual = faker.string.numeric({
length: 4,
allowLeadingZeros: true,
exclude: [...'123456789'],
});
expect(actual).toBe('0000');
});
it('should allow leading zeros via option and all other digits excluded via string', () => {
const actual = faker.string.numeric({
length: 4,
allowLeadingZeros: true,
exclude: '123456789',
});
expect(actual).toBe('0000');
});
it('should fail on leading zeros via option and all other digits excluded', () => {
expect(() =>
faker.string.numeric({
length: 4,
allowLeadingZeros: false,
exclude: [...'123456789'],
})
).toThrow(
new FakerError(
'Unable to generate numeric string, because all possible digits are excluded.'
)
);
});
it('should fail on leading zeros via option and all other digits excluded via string', () => {
expect(() =>
faker.string.numeric({
length: 4,
allowLeadingZeros: false,
exclude: '123456789',
})
).toThrow(
new FakerError(
'Unable to generate numeric string, because all possible digits are excluded.'
)
);
});
it('should ban all digits passed via exclude', () => {
const actual = faker.string.numeric({
length: 1000,
exclude: [...'c84U1'],
});
expect(actual).toHaveLength(1000);
expect(actual).toMatch(/^[0235679]{1000}$/);
});
it('should ban all digits passed via exclude via string', () => {
const actual = faker.string.numeric({
length: 1000,
exclude: 'c84U1',
});
expect(actual).toHaveLength(1000);
expect(actual).toMatch(/^[0235679]{1000}$/);
});
});
describe('sample', () => {
it('should generate a string value', () => {
const generatedString = faker.string.sample();
expect(generatedString).toBeTypeOf('string');
expect(generatedString).toHaveLength(10);
});
it('should return empty string if negative length is passed', () => {
const negativeValue = faker.number.int({ min: -1000, max: -1 });
const generatedString = faker.string.sample(negativeValue);
expect(generatedString).toBe('');
expect(generatedString).toHaveLength(0);
});
it('should return string with a specific length', () => {
const length = 1337;
const generatedString = faker.string.sample(length);
expect(generatedString).toHaveLength(length);
});
it('should return a random amount of characters', () => {
const actual = faker.string.sample({ min: 10, max: 20 });
expect(actual).toBeTruthy();
expect(actual).toBeTypeOf('string');
expect(actual.length).toBeGreaterThanOrEqual(10);
expect(actual.length).toBeLessThanOrEqual(20);
});
});
describe(`uuid`, () => {
it('generates a valid UUID', () => {
const UUID = faker.string.uuid();
const RFC4122 =
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
expect(UUID).toMatch(RFC4122);
});
});
describe(`nanoid`, () => {
it('generates a valid Nano ID', () => {
const id = faker.string.nanoid();
const regex = /^[0-9a-zA-Z_-]+$/;
expect(id).toMatch(regex);
});
it('should have a default length of 21', () => {
const id = faker.string.nanoid();
expect(id).toHaveLength(21);
});
it('should return an empty string when length is negative', () => {
const id = faker.string.nanoid(-1);
expect(id).toBe('');
});
it('should return string with a length within a given range', () => {
const actual = faker.string.nanoid({ min: 13, max: 37 });
expect(actual.length).toBeGreaterThanOrEqual(13);
expect(actual.length).toBeLessThanOrEqual(37);
});
});
describe('special', () => {
it('should return a value of type string with default length of 1', () => {
const actual = faker.string.symbol();
expect(actual).toBeTypeOf('string');
expect(actual).toHaveLength(1);
});
it('should return an empty string when length is negative', () => {
const actual = faker.string.symbol(
faker.number.int({ min: -1000, max: -1 })
);
expect(actual).toBe('');
expect(actual).toHaveLength(0);
});
it('should return string of designated length', () => {
const length = 87;
const actual = faker.string.symbol(length);
expect(actual).toHaveLength(length);
});
it('should return string with a length within a given range', () => {
const actual = faker.string.symbol({ min: 10, max: 20 });
expect(actual.length).toBeGreaterThanOrEqual(10);
expect(actual.length).toBeLessThanOrEqual(20);
});
});
}
);
});
|