From f0d6fa4626b2e4717844ec5f0fd0d5c6921618e2 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Sat, 25 Nov 2023 14:35:30 +0100 Subject: [PATCH 1/3] docs: add missing throw cases --- src/modules/datatype/index.ts | 6 +++++- src/modules/number/index.ts | 17 ++++++++++++----- test/modules/datatype.spec.ts | 23 +++++++++++++++++++++++ test/modules/number.spec.ts | 26 +++++++++++++++++++++++++- 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts index f46cfc0b80a..b8830610548 100644 --- a/src/modules/datatype/index.ts +++ b/src/modules/datatype/index.ts @@ -20,7 +20,8 @@ export class DatatypeModule extends SimpleModuleBase { * @param options.max Upper bound for generated number. Defaults to `min + 99999`. * @param options.precision Precision of the generated number. Defaults to `1`. * - * @throws When options define `max < min`. + * @throws When `min` is greater than `max`. + * @throws When `precision` is negative. * * @see faker.number.int(): For generating a random integer. * @see faker.number.float(): For generating a random floating-point number. @@ -85,6 +86,9 @@ export class DatatypeModule extends SimpleModuleBase { * @param options.max Upper bound for generated number. Defaults to `min + 99999`. * @param options.precision Precision of the generated number. Defaults to `0.01`. * + * @throws When `min` is greater than `max`. + * @throws When `precision` is negative. + * * @see faker.number.float(): For the replacement method. * * @example diff --git a/src/modules/number/index.ts b/src/modules/number/index.ts index a20a0040fdf..0f46da4aece 100644 --- a/src/modules/number/index.ts +++ b/src/modules/number/index.ts @@ -24,7 +24,8 @@ export class NumberModule extends SimpleModuleBase { * @param options.min Lower bound for generated number. Defaults to `0`. * @param options.max Upper bound for generated number. Defaults to `Number.MAX_SAFE_INTEGER`. * - * @throws When options define `max < min`. + * @throws When `min` is greater than `max`. + * @throws When `min` and `max` do not provide a sufficient range to generate integers in between them. * * @see faker.string.numeric(): For generating a `string` of digits with a given length (range). * @@ -93,6 +94,9 @@ export class NumberModule extends SimpleModuleBase { * @param options.precision Precision of the generated number, for example `0.01` will round to 2 decimal points. * If precision is passed, the upper bound is inclusive. * + * @throws When `min` is greater than `max`. + * @throws When `precision` is negative. + * * @example * faker.number.float() // 0.5688541042618454 * faker.number.float(3) // 2.367973240558058 @@ -168,7 +172,8 @@ export class NumberModule extends SimpleModuleBase { * @param options.min Lower bound for generated number. Defaults to `0`. * @param options.max Upper bound for generated number. Defaults to `1`. * - * @throws When options define `max < min`. + * @throws When `min` is greater than `max`. + * @throws When `min` and `max` do not provide a sufficient range to generate integers in between them. * * @see faker.string.binary(): For generating a `binary string` with a given length (range). * @@ -217,7 +222,8 @@ export class NumberModule extends SimpleModuleBase { * @param options.min Lower bound for generated number. Defaults to `0`. * @param options.max Upper bound for generated number. Defaults to `7`. * - * @throws When options define `max < min`. + * @throws When `min` is greater than `max`. + * @throws When `min` and `max` do not provide a sufficient range to generate integers in between them. * * @see faker.string.octal(): For generating an `octal string` with a given length (range). * @@ -266,7 +272,8 @@ export class NumberModule extends SimpleModuleBase { * @param options.min Lower bound for generated number. Defaults to `0`. * @param options.max Upper bound for generated number. Defaults to `15`. * - * @throws When options define `max < min`. + * @throws When `min` is greater than `max`. + * @throws When `min` and `max` do not provide a sufficient range to generate integers in between them. * * @example * faker.number.hex() // 'b' @@ -313,7 +320,7 @@ export class NumberModule extends SimpleModuleBase { * @param options.min Lower bound for generated bigint. Defaults to `0n`. * @param options.max Upper bound for generated bigint. Defaults to `min + 999999999999999n`. * - * @throws When options define `max < min`. + * @throws When `min` is greater to `max`. * * @example * faker.number.bigInt() // 55422n diff --git a/test/modules/datatype.spec.ts b/test/modules/datatype.spec.ts index b4d154f4e0b..3d65797d780 100644 --- a/test/modules/datatype.spec.ts +++ b/test/modules/datatype.spec.ts @@ -232,6 +232,12 @@ describe('datatype', () => { new FakerError(`Max ${max} should be greater than min ${min}.`) ); }); + + it('should throw when precision is negative', () => { + expect(() => { + faker.datatype.number({ precision: -0.01 }); + }).toThrow(new FakerError('Precision should be greater than 0.')); + }); }); describe('float', () => { @@ -306,6 +312,23 @@ describe('datatype', () => { expect(opts.min).toBe(min); expect(opts.max).toBe(max); }); + + it('should throw when min > max', () => { + const min = 10; + const max = 9; + + expect(() => { + faker.datatype.number({ min, max }); + }).toThrow( + new FakerError(`Max ${max} should be greater than min ${min}.`) + ); + }); + + it('should throw when precision is negative', () => { + expect(() => { + faker.datatype.float({ precision: -0.01 }); + }).toThrow(new FakerError('Precision should be greater than 0.')); + }); }); describe('datetime', () => { diff --git a/test/modules/number.spec.ts b/test/modules/number.spec.ts index 66018ee61bd..1104802cabb 100644 --- a/test/modules/number.spec.ts +++ b/test/modules/number.spec.ts @@ -304,7 +304,7 @@ describe('number', () => { return [...str].every((char) => char === '0' || char === '1'); } - it('enerates single binary character when no additional argument was provided', () => { + it('generates single binary character when no additional argument was provided', () => { const binary = faker.number.binary(); expect(binary).toBeTypeOf('string'); @@ -344,6 +344,14 @@ describe('number', () => { new FakerError(`Max ${max} should be greater than min ${min}.`) ); }); + + it('should throw when there is no integer between min and max', () => { + expect(() => { + faker.number.binary({ min: 2.1, max: 2.9 }); + }).toThrow( + new FakerError(`No integer value between 2.1 and 2.9 found.`) + ); + }); }); describe('octal', () => { @@ -387,6 +395,14 @@ describe('number', () => { new FakerError(`Max ${max} should be greater than min ${min}.`) ); }); + + it('should throw when there is no integer between min and max', () => { + expect(() => { + faker.number.octal({ min: 2.1, max: 2.9 }); + }).toThrow( + new FakerError(`No integer value between 2.1 and 2.9 found.`) + ); + }); }); describe('hex', () => { @@ -427,6 +443,14 @@ describe('number', () => { new FakerError(`Max ${max} should be greater than min ${min}.`) ); }); + + it('should throw when there is no integer between min and max', () => { + expect(() => { + faker.number.hex({ min: 2.1, max: 2.9 }); + }).toThrow( + new FakerError(`No integer value between 2.1 and 2.9 found.`) + ); + }); }); describe('bigInt', () => { From 0480a34a1577b1a61e90f3baca95f73efbec3ac9 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Sun, 3 Dec 2023 12:24:56 +0100 Subject: [PATCH 2/3] docs: rephrase throw description for insufficient int range --- src/modules/number/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/modules/number/index.ts b/src/modules/number/index.ts index 0f46da4aece..62015d9be86 100644 --- a/src/modules/number/index.ts +++ b/src/modules/number/index.ts @@ -25,7 +25,7 @@ export class NumberModule extends SimpleModuleBase { * @param options.max Upper bound for generated number. Defaults to `Number.MAX_SAFE_INTEGER`. * * @throws When `min` is greater than `max`. - * @throws When `min` and `max` do not provide a sufficient range to generate integers in between them. + * @throws When there are no integers between `min` and `max`. * * @see faker.string.numeric(): For generating a `string` of digits with a given length (range). * @@ -173,7 +173,7 @@ export class NumberModule extends SimpleModuleBase { * @param options.max Upper bound for generated number. Defaults to `1`. * * @throws When `min` is greater than `max`. - * @throws When `min` and `max` do not provide a sufficient range to generate integers in between them. + * @throws When there are no integers between `min` and `max`. * * @see faker.string.binary(): For generating a `binary string` with a given length (range). * @@ -223,7 +223,7 @@ export class NumberModule extends SimpleModuleBase { * @param options.max Upper bound for generated number. Defaults to `7`. * * @throws When `min` is greater than `max`. - * @throws When `min` and `max` do not provide a sufficient range to generate integers in between them. + * @throws When there are no integers between `min` and `max`. * * @see faker.string.octal(): For generating an `octal string` with a given length (range). * @@ -273,7 +273,7 @@ export class NumberModule extends SimpleModuleBase { * @param options.max Upper bound for generated number. Defaults to `15`. * * @throws When `min` is greater than `max`. - * @throws When `min` and `max` do not provide a sufficient range to generate integers in between them. + * @throws When there are no integers between `min` and `max`. * * @example * faker.number.hex() // 'b' From 26a7eb08d3ab83b8e102e38121bfe8ad2f188711 Mon Sep 17 00:00:00 2001 From: DivisionByZero Date: Sun, 3 Dec 2023 12:26:42 +0100 Subject: [PATCH 3/3] docs: fix grammar error in throw description Co-authored-by: Eric Cheng --- src/modules/number/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/number/index.ts b/src/modules/number/index.ts index 62015d9be86..91d987771e0 100644 --- a/src/modules/number/index.ts +++ b/src/modules/number/index.ts @@ -320,7 +320,7 @@ export class NumberModule extends SimpleModuleBase { * @param options.min Lower bound for generated bigint. Defaults to `0n`. * @param options.max Upper bound for generated bigint. Defaults to `min + 999999999999999n`. * - * @throws When `min` is greater to `max`. + * @throws When `min` is greater than `max`. * * @example * faker.number.bigInt() // 55422n