diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/modules/date.spec.ts | 26 | ||||
| -rw-r--r-- | test/modules/git.spec.ts | 23 | ||||
| -rw-r--r-- | test/modules/number.spec.ts | 16 | ||||
| -rw-r--r-- | test/utils/mersenne.spec.ts | 8 |
4 files changed, 37 insertions, 36 deletions
diff --git a/test/modules/date.spec.ts b/test/modules/date.spec.ts index e30da8d9..19a2d0a7 100644 --- a/test/modules/date.spec.ts +++ b/test/modules/date.spec.ts @@ -12,6 +12,19 @@ const converterMap = [ const NON_SEEDED_BASED_RUN = 5; const refDate = '2021-02-21T17:09:15.711Z'; +function calculateAge(birthdate: Date, refDate: Date): number { + let age = refDate.getFullYear() - birthdate.getFullYear(); + if ( + refDate.getMonth() < birthdate.getMonth() || + (refDate.getMonth() === birthdate.getMonth() && + refDate.getDate() < birthdate.getDate()) + ) { + age--; + } + + return age; +} + describe('date', () => { seededTests(faker, 'date', (t) => { t.describe('anytime', (t) => { @@ -530,19 +543,6 @@ describe('date', () => { }); describe('birthdate', () => { - function calculateAge(birthdate: Date, refDate: Date): number { - let age = refDate.getFullYear() - birthdate.getFullYear(); - if ( - refDate.getMonth() < birthdate.getMonth() || - (refDate.getMonth() === birthdate.getMonth() && - refDate.getDate() < birthdate.getDate()) - ) { - age--; - } - - return age; - } - it('returns a random birthdate', () => { const birthdate = faker.date.birthdate(); expect(birthdate).toBeInstanceOf(Date); diff --git a/test/modules/git.spec.ts b/test/modules/git.spec.ts index e4692b20..164b19c6 100644 --- a/test/modules/git.spec.ts +++ b/test/modules/git.spec.ts @@ -8,6 +8,16 @@ const NON_SEEDED_BASED_RUN = 5; const refDate = '2020-01-01T00:00:00.000Z'; +function isValidCommitAuthor(email: string): boolean { + // `validator.isEmail()` does not support display names + // that contain unquoted characters like . output by Git so we need + // to quote the display name + const quotedEmail = email.replace(/^(.*) </, '"$1" <'); + return validator.isEmail(quotedEmail, { + require_display_name: true, + }); +} + describe('git', () => { seededTests(faker, 'git', (t) => { t.itEach('branch', 'commitMessage'); @@ -56,27 +66,18 @@ describe('git', () => { expect(parts.length).toBeLessThanOrEqual(7); expect(parts[0]).toMatch(/^commit [a-f0-9]+$/); - const isValidAuthor = (email: string) => { - // `validator.isEmail()` does not support display names - // that contain unquoted characters like . output by Git so we need - // to quote the display name - const quotedEmail = email.replace(/^(.*) </, '"$1" <'); - return validator.isEmail(quotedEmail, { - require_display_name: true, - }); - }; const authorRegex = /^Author: .*$/; if (parts.length === 7) { expect(parts[1]).toMatch(/^Merge: [a-f0-9]+ [a-f0-9]+$/); expect(parts[2]).toMatch(authorRegex); - expect(parts[2].substring(8)).toSatisfy(isValidAuthor); + expect(parts[2].substring(8)).toSatisfy(isValidCommitAuthor); expect(parts[3]).toMatch(/^Date: .+$/); expect(parts[4]).toBe(''); expect(parts[5]).toMatch(/^\s{4}.+$/); } else { expect(parts[1]).toMatch(authorRegex); - expect(parts[1].substring(8)).toSatisfy(isValidAuthor); + expect(parts[1].substring(8)).toSatisfy(isValidCommitAuthor); expect(parts[2]).toMatch(/^Date: .+$/); expect(parts[3]).toBe(''); expect(parts[4]).toMatch(/^\s{4}.+$/); diff --git a/test/modules/number.spec.ts b/test/modules/number.spec.ts index cfd50b20..3b21f64c 100644 --- a/test/modules/number.spec.ts +++ b/test/modules/number.spec.ts @@ -5,6 +5,14 @@ import { seededTests } from '../support/seeded-runs'; import { MERSENNE_MAX_VALUE } from '../utils/mersenne-test-utils'; import { times } from './../support/times'; +function isFloat(value: number): boolean { + return value % 1 !== 0; +} + +function isBinary(str: string): boolean { + return [...str].every((char) => char === '0' || char === '1'); +} + describe('number', () => { seededTests(faker, 'number', (t) => { t.describeEach( @@ -259,10 +267,6 @@ describe('number', () => { }); describe('float', () => { - function isFloat(value: number) { - return value % 1 !== 0; - } - it('should return a float between 0 and 1 (inclusive) by default', () => { const actual = faker.number.float(); @@ -405,10 +409,6 @@ describe('number', () => { }); describe('binary', () => { - function isBinary(str: string) { - return [...str].every((char) => char === '0' || char === '1'); - } - it('generates single binary character when no additional argument was provided', () => { const binary = faker.number.binary(); diff --git a/test/utils/mersenne.spec.ts b/test/utils/mersenne.spec.ts index 5e9e4c27..4699b9b0 100644 --- a/test/utils/mersenne.spec.ts +++ b/test/utils/mersenne.spec.ts @@ -23,6 +23,10 @@ function newTwister( return twister; } +function randomSeed(): number { + return Math.ceil(Math.random() * 1_000_000_000); +} + describe('MersenneTwister19937', () => { describe('genrandInt32()', () => { it('should be able to return 0', () => { @@ -112,10 +116,6 @@ describe.each([ }); }); - function randomSeed(): number { - return Math.ceil(Math.random() * 1_000_000_000); - } - // Create and log-back the seed for debug purposes describe.each( times(NON_SEEDED_BASED_RUN).flatMap(() => [ |
