diff options
| author | Shinigami <[email protected]> | 2022-02-10 20:18:58 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-02-10 20:18:58 +0100 |
| commit | 14ecb2668602d34fe66cd775825730a345b0525e (patch) | |
| tree | 38ec7b5f508308c128f0b768ed134ccfa80bb71f /test | |
| parent | 999556bf83018daf010b05350d58934ad5bd0761 (diff) | |
| download | faker-14ecb2668602d34fe66cd775825730a345b0525e.tar.xz faker-14ecb2668602d34fe66cd775825730a345b0525e.zip | |
test: rewrite git tests (#425)
Diffstat (limited to 'test')
| -rw-r--r-- | test/git.spec.ts | 291 |
1 files changed, 159 insertions, 132 deletions
diff --git a/test/git.spec.ts b/test/git.spec.ts index bc5e3eb6..23548ef1 100644 --- a/test/git.spec.ts +++ b/test/git.spec.ts @@ -1,152 +1,179 @@ -import type { SpyInstance } from 'vitest'; -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import validator from 'validator'; +import { afterEach, describe, expect, it } from 'vitest'; import { faker } from '../src'; -describe('git.js', () => { - describe('branch()', () => { - let spy_hacker_noun: SpyInstance<[], string>; - let spy_hacker_verb: SpyInstance<[], string>; - - beforeEach(() => { - spy_hacker_noun = vi.spyOn(faker.hacker, 'noun'); - spy_hacker_verb = vi.spyOn(faker.hacker, 'verb'); - }); - - afterEach(() => { - spy_hacker_noun.mockRestore(); - spy_hacker_verb.mockRestore(); - }); - - it('returns a branch with hacker noun and verb', () => { - faker.git.branch(); - - expect(spy_hacker_noun).toHaveBeenCalledOnce(); - expect(spy_hacker_verb).toHaveBeenCalledOnce(); - }); +const seededRuns = [ + { + seed: 42, + expectations: { + branch: { + noArgs: 'array-transmit', + }, + commitEntry: { + noArgs: '', + }, + commitMessage: { + noArgs: 'navigate neural capacitor', + }, + commitSha: { + noArgs: '5cf2bc99272107d592ba00fbdf302f2949806048', + }, + shortSha: { + noArgs: '5cf2bc9', + }, + }, + }, + { + seed: 1337, + expectations: { + branch: { + noArgs: 'port-quantify', + }, + commitEntry: { + noArgs: '', + }, + commitMessage: { + noArgs: 'compress multi-byte panel', + }, + commitSha: { + noArgs: '48234870538945f4b41c61a52bf27dccc0576698', + }, + shortSha: { + noArgs: '4823487', + }, + }, + }, + { + seed: 1211, + expectations: { + branch: { + noArgs: 'capacitor-connect', + }, + commitEntry: { + noArgs: '', + }, + commitMessage: { + noArgs: 'reboot online circuit', + }, + commitSha: { + noArgs: 'e7ec32f0a2a3c652bbd0caabde64dfdf379e3259', + }, + shortSha: { + noArgs: 'e7ec32f', + }, + }, + }, +]; + +const NON_SEEDED_BASED_RUN = 5; + +const functionNames = [ + 'branch', + 'commitEntry', + 'commitMessage', + 'commitSha', + 'shortSha', +]; + +describe('git', () => { + afterEach(() => { + faker.locale = 'en'; }); - describe('commitEntry()', () => { - let spy_git_commitMessage: SpyInstance<[], string>; - let spy_git_commitSha: SpyInstance<[], string>; - let spy_internet_email: SpyInstance< - [firstName?: string, lastName?: string, provider?: string], - string - >; - let spy_name_firstName: SpyInstance<[gender?: string | number], string>; - let spy_name_lastName: SpyInstance<[gender?: string | number], string>; - let spy_datatype_number: SpyInstance< - [ - options?: - | number - | { - min?: number; - max?: number; - precision?: number; - } - ], - number - >; - - beforeEach(() => { - spy_git_commitMessage = vi.spyOn(faker.git, 'commitMessage'); - spy_git_commitSha = vi.spyOn(faker.git, 'commitSha'); - spy_internet_email = vi.spyOn(faker.internet, 'email'); - spy_name_firstName = vi.spyOn(faker.name, 'firstName'); - spy_name_lastName = vi.spyOn(faker.name, 'lastName'); - spy_datatype_number = vi.spyOn(faker.datatype, 'number'); - }); - - afterEach(() => { - spy_git_commitMessage.mockRestore(); - spy_git_commitSha.mockRestore(); - spy_internet_email.mockRestore(); - spy_name_firstName.mockRestore(); - spy_name_lastName.mockRestore(); - spy_datatype_number.mockRestore(); + for (const { seed, expectations } of seededRuns) { + describe(`seed: ${seed}`, () => { + for (const functionName of functionNames) { + if (functionName === 'commitEntry') { + it.todo(`${functionName}()`); + continue; + } + + it(`${functionName}()`, () => { + faker.seed(seed); + + const actual = faker.git[functionName](); + expect(actual).toEqual(expectations[functionName].noArgs); + }); + } }); + } - it('returns merge entry at random', () => { - faker.git.commitEntry(); + // Create and log-back the seed for debug purposes + faker.seed(Math.ceil(Math.random() * 1_000_000_000)); - expect(spy_datatype_number).toHaveBeenCalled(); - }); - - it('returns a commit entry with git commit message and sha', () => { - faker.git.commitEntry(); - - expect(spy_git_commitMessage).toHaveBeenCalledOnce(); - expect(spy_git_commitSha).toHaveBeenCalledOnce(); - }); - - it('returns a commit entry with internet email', () => { - faker.git.commitEntry(); + describe(`random seeded tests for seed ${faker.seedValue}`, () => { + for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { + describe('branch()', () => { + it('should return a random branch', () => { + const branch = faker.git.branch(); - expect(spy_internet_email).toHaveBeenCalledOnce(); - }); - - it('returns a commit entry with name first and last', () => { - faker.git.commitEntry(); - - expect(spy_name_firstName).toHaveBeenCalledTimes(2); - expect(spy_name_lastName).toHaveBeenCalledTimes(2); - }); - - describe("with options['merge'] equal to true", () => { - let spy_git_shortSha: SpyInstance<[], string>; - - beforeEach(() => { - spy_git_shortSha = vi.spyOn(faker.git, 'shortSha'); + expect(branch).toBeTruthy(); + expect(typeof branch).toBe('string'); + expect(branch).satisfy(validator.isSlug); + }); }); - afterEach(() => { - spy_git_shortSha.mockRestore(); + describe('commitEntry', () => { + it('should return a random commitEntry', () => { + const commitEntry = faker.git.commitEntry(); + + expect(commitEntry).toBeTruthy(); + expect(typeof commitEntry).toBe('string'); + + const parts = commitEntry.split(/\r?\n/); + + expect(parts.length).greaterThanOrEqual(6); + expect(parts.length).lessThanOrEqual(7); + + expect(parts[0]).match(/^commit [a-f0-9]+$/); + if (parts.length === 7) { + expect(parts[1]).match(/^Merge: [a-f0-9]+ [a-f0-9]+$/); + expect(parts[2]).match(/^Author: \w+ \w+ \<[\w\.]+@[\w\.]+\>$/); + expect(parts[3]).match(/^Date: .+$/); + expect(parts[4]).toBe(''); + expect(parts[5]).match(/^\s{4}.+$/); + } else { + expect(parts[1]).match(/^Author: \w+ \w+ \<[\w\.]+@[\w\.]+\>$/); + expect(parts[2]).match(/^Date: .+$/); + expect(parts[3]).toBe(''); + expect(parts[4]).match(/^\s{4}.+$/); + } + }); }); - it('returns a commit entry with merge details', () => { - faker.git.commitEntry({ merge: true }); + describe('commitMessage', () => { + it('should return a random commitMessage', () => { + const commitMessage = faker.git.commitMessage(); - expect(spy_git_shortSha).toHaveBeenCalledTimes(2); - }); - }); - }); + expect(commitMessage).toBeTruthy(); + expect(typeof commitMessage).toBe('string'); - describe('commitMessage()', () => { - let spy_hacker_verb: SpyInstance<[], string>; - let spy_hacker_adjective: SpyInstance<[], string>; - let spy_hacker_noun: SpyInstance<[], string>; - - beforeEach(() => { - spy_hacker_verb = vi.spyOn(faker.hacker, 'verb'); - spy_hacker_adjective = vi.spyOn(faker.hacker, 'adjective'); - spy_hacker_noun = vi.spyOn(faker.hacker, 'noun'); - }); - - afterEach(() => { - spy_hacker_verb.mockRestore(); - spy_hacker_adjective.mockRestore(); - spy_hacker_noun.mockRestore(); - }); + const parts = commitMessage.split(' '); + expect(parts.length).greaterThanOrEqual(3); + }); + }); - it('returns a commit message with hacker noun, adj and verb', () => { - faker.git.commitMessage(); + describe('commitSha', () => { + it('should return a random commitSha', () => { + const commitSha = faker.git.commitSha(); - expect(spy_hacker_verb).toHaveBeenCalledOnce(); - expect(spy_hacker_adjective).toHaveBeenCalledOnce(); - expect(spy_hacker_noun).toHaveBeenCalledOnce(); - }); - }); + expect(commitSha).toBeTruthy(); + expect(typeof commitSha).toBe('string'); + expect(commitSha).satisfy(validator.isHexadecimal); + expect(commitSha).toHaveLength(40); + }); + }); - describe('commitSha()', () => { - it('returns a random commit SHA', () => { - const commitSha = faker.git.commitSha(); - expect(commitSha).match(/^[a-f0-9]{40}$/); - }); - }); + describe('shortSha', () => { + it('should return a random shortSha', () => { + const shortSha = faker.git.shortSha(); - describe('shortSha()', () => { - it('returns a random short SHA', () => { - const shortSha = faker.git.shortSha(); - expect(shortSha).match(/^[a-f0-9]{7}$/); - }); + expect(shortSha).toBeTruthy(); + expect(typeof shortSha).toBe('string'); + expect(shortSha).satisfy(validator.isHexadecimal); + expect(shortSha).toHaveLength(7); + }); + }); + } }); }); |
