|
1 | 1 | import { beforeEach, describe, expect, it, vi } from 'vitest'; |
2 | | -import { faker } from '../src'; |
| 2 | +import { faker, FakerError } from '../src'; |
3 | 3 | import { times } from './support/times'; |
4 | 4 |
|
5 | 5 | describe('random', () => { |
@@ -304,6 +304,82 @@ describe('random', () => { |
304 | 304 | }); |
305 | 305 | }); |
306 | 306 |
|
| 307 | + describe('numeric', () => { |
| 308 | + it('should return single digit when no length provided', () => { |
| 309 | + const actual = faker.random.numeric(); |
| 310 | + |
| 311 | + expect(actual).toHaveLength(1); |
| 312 | + expect(actual).toMatch(/^[1-9]$/); |
| 313 | + }); |
| 314 | + |
| 315 | + it.each(times(100))( |
| 316 | + 'should generate random value with a length of %s', |
| 317 | + (length) => { |
| 318 | + const actual = faker.random.numeric(length); |
| 319 | + |
| 320 | + expect(actual).toHaveLength(length); |
| 321 | + expect(actual).toMatch(/^[1-9][0-9]*$/); |
| 322 | + } |
| 323 | + ); |
| 324 | + |
| 325 | + it('should return empty string with a length of 0', () => { |
| 326 | + const actual = faker.random.numeric(0); |
| 327 | + |
| 328 | + expect(actual).toHaveLength(0); |
| 329 | + }); |
| 330 | + |
| 331 | + it('should return empty string with a negative length', () => { |
| 332 | + const actual = faker.random.numeric(-10); |
| 333 | + |
| 334 | + expect(actual).toHaveLength(0); |
| 335 | + }); |
| 336 | + |
| 337 | + it('should return a valid numeric string with provided length', () => { |
| 338 | + const actual = faker.random.numeric(1000); |
| 339 | + |
| 340 | + expect(actual).toBeTypeOf('string'); |
| 341 | + expect(actual).toHaveLength(1000); |
| 342 | + expect(actual).toMatch(/^[1-9][0-9]+$/); |
| 343 | + }); |
| 344 | + |
| 345 | + it('should allow leading zeros via option', () => { |
| 346 | + const actual = faker.random.numeric(15, { allowLeadingZeros: true }); |
| 347 | + |
| 348 | + expect(actual).toMatch(/^[0-9]+$/); |
| 349 | + }); |
| 350 | + |
| 351 | + it('should allow leading zeros via option and all other digits banned', () => { |
| 352 | + const actual = faker.random.numeric(4, { |
| 353 | + allowLeadingZeros: true, |
| 354 | + bannedDigits: '123456789'.split(''), |
| 355 | + }); |
| 356 | + |
| 357 | + expect(actual).toBe('0000'); |
| 358 | + }); |
| 359 | + |
| 360 | + it('should fail on leading zeros via option and all other digits banned', () => { |
| 361 | + expect(() => |
| 362 | + faker.random.numeric(4, { |
| 363 | + allowLeadingZeros: false, |
| 364 | + bannedDigits: '123456789'.split(''), |
| 365 | + }) |
| 366 | + ).toThrowError( |
| 367 | + new FakerError( |
| 368 | + 'Unable to generate numeric string, because all possible digits are banned.' |
| 369 | + ) |
| 370 | + ); |
| 371 | + }); |
| 372 | + |
| 373 | + it('should ban all digits passed via bannedDigits', () => { |
| 374 | + const actual = faker.random.numeric(1000, { |
| 375 | + bannedDigits: 'c84U1'.split(''), |
| 376 | + }); |
| 377 | + |
| 378 | + expect(actual).toHaveLength(1000); |
| 379 | + expect(actual).toMatch(/^[0235679]{1000}$/); |
| 380 | + }); |
| 381 | + }); |
| 382 | + |
307 | 383 | describe('deprecation warnings', () => { |
308 | 384 | it.each([ |
309 | 385 | ['number', 'datatype.number'], |
|
0 commit comments