Skip to content

Commit

Permalink
review(ap): generalize BOOLEAN_STRING tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaskiewicz committed May 9, 2023
1 parent 828cb84 commit 77d7e51
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/cli/config-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export type StringCLIFlag = ArrayValuesAsUnion<typeof STRING_CLI_FLAGS>;
export type StringArrayCLIFlag = ArrayValuesAsUnion<typeof STRING_ARRAY_CLI_FLAGS>;
export type NumberCLIFlag = ArrayValuesAsUnion<typeof NUMBER_CLI_FLAGS>;
export type StringNumberCLIFlag = ArrayValuesAsUnion<typeof STRING_NUMBER_CLI_FLAGS>;
type BooleanStringCLIFlag = ArrayValuesAsUnion<typeof BOOLEAN_STRING_CLI_FLAGS>;
export type BooleanStringCLIFlag = ArrayValuesAsUnion<typeof BOOLEAN_STRING_CLI_FLAGS>;
export type LogCLIFlag = ArrayValuesAsUnion<typeof LOG_LEVEL_CLI_FLAGS>;

export type KnownCLIFlag =
Expand Down
42 changes: 25 additions & 17 deletions src/cli/test/parse-flags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { toDashCase } from '@utils';
import { LogLevel } from '../../declarations';
import {
BOOLEAN_CLI_FLAGS,
BOOLEAN_STRING_CLI_FLAGS,
BooleanStringCLIFlag,
ConfigFlags,
NUMBER_CLI_FLAGS,
STRING_ARRAY_CLI_FLAGS,
Expand Down Expand Up @@ -132,36 +134,42 @@ describe('parseFlags', () => {
expect(flags.config).toBe('/config-2.js');
});

describe('boolean-string flag', () => {
describe.each(BOOLEAN_STRING_CLI_FLAGS)('boolean-string flag - %s', (cliArg: BooleanStringCLIFlag) => {
it('parses a boolean-string flag as a boolean with no arg', () => {
const args = ['--headless'];
const args = [`--${cliArg}`];
const flags = parseFlags(args);
expect(flags.headless).toBe(true);
expect(flags.knownArgs).toEqual(['--headless']);
expect(flags.knownArgs).toEqual([`--${cliArg}`]);
});

it.each([['--noHeadless'], ['--no-headless']])(
'parses a boolean-string flag as a falsy boolean with "no" arg - \'%s\'',
(noVariant) => {
const args = [noVariant];
const flags = parseFlags(args);
expect(flags.headless).toBe(false);
expect(flags.knownArgs).toEqual([noVariant]);
}
);
it(`parses a boolean-string flag as a falsy boolean with "no" arg - --no-${cliArg}`, () => {
const args = [`--no-${cliArg}`];
const flags = parseFlags(args);
expect(flags.headless).toBe(false);
expect(flags.knownArgs).toEqual([`--no-${cliArg}`]);
});

it("parses a boolean-string flag as a string with 'new' arg", () => {
const args = ['--headless', 'new'];
it(`parses a boolean-string flag as a falsy boolean with "no" arg - --no${
cliArg.charAt(0).toUpperCase() + cliArg.slice(1)
}`, () => {
const negativeFlag = '--no' + cliArg.charAt(0).toUpperCase() + cliArg.slice(1);
const flags = parseFlags([negativeFlag]);
expect(flags.headless).toBe(false);
expect(flags.knownArgs).toEqual([negativeFlag]);
});

it('parses a boolean-string flag as a string with a string arg', () => {
const args = [`--${cliArg}`, 'new'];
const flags = parseFlags(args);
expect(flags.headless).toBe('new');
expect(flags.knownArgs).toEqual(['--headless', 'new']);
});

it("parses a boolean-string flag as a string with 'new' arg using equality", () => {
const args = ['--headless=new'];
it('parses a boolean-string flag as a string with a string arg using equality', () => {
const args = [`--${cliArg}=new`];
const flags = parseFlags(args);
expect(flags.headless).toBe('new');
expect(flags.knownArgs).toEqual(['--headless', 'new']);
expect(flags.knownArgs).toEqual([`--${cliArg}`, 'new']);
});
});

Expand Down

0 comments on commit 77d7e51

Please sign in to comment.