Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dummy if statements #77

Merged
merged 1 commit into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions tests/assignment.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { expectAssignable, expectNotAssignable } from 'tsd';
import { Command, CommandUnknownOpts, Option } from '..';

if ('when assign Command to CommandUnknownOpts then no error') {
// 'when assign Command to CommandUnknownOpts then no error'
{
const c = new Command();
expectAssignable<CommandUnknownOpts>(c);
}

if ('when assign CommandUnknownOpts to Command then error') {
// 'when assign CommandUnknownOpts to Command then error'
{
const custom: CommandUnknownOpts = new Command();
expectNotAssignable<Command>(custom);
}
3 changes: 2 additions & 1 deletion tests/command-unknown-opts.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { expectType } from 'tsd';

// Fallback location for CommandUnknownOpts if not better file for the test.

if ('when CommandUnknownOpts then opts().foo is allowed but unknown') {
// 'when CommandUnknownOpts then opts().foo is allowed but unknown'
{
const program: CommandUnknownOpts = new Command();
const v = program.opts()['foo'];
expectType<unknown>(v);
Expand Down
26 changes: 16 additions & 10 deletions tests/create-argument.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,56 @@ import { Command, createArgument } from '..';

// Doing end-to-end test, rather than checking created Argument directly.

if ('when cmd.createArgument with required then type is string') {
// 'when cmd.createArgument with required then type is string'
{
const program = new Command();
program.addArgument(program.createArgument('<value>')).action((arg) => {
expectType<string>(arg);
});
}

if ('when cmd.createArgument with optional then type is string|undefined') {
// ('when cmd.createArgument with optional then type is string|undefined'
{
const program = new Command();
program.addArgument(program.createArgument('[value]')).action((arg) => {
expectType<string | undefined>(arg);
});
}

if ('when cmd.createArgument with variadic then type is string[]') {
// 'when cmd.createArgument with variadic then type is string[]'
{
const program = new Command();
program.addArgument(program.createArgument('<value...>')).action((arg) => {
expectType<string[]>(arg);
});
}

if ('when global createArgument with required then type is string') {
// 'when global createArgument with required then type is string'
{
const program = new Command();
program.addArgument(createArgument('<value>')).action((arg) => {
expectType<string>(arg);
});
}

if ('when global createArgument with optional then type is string|undefined') {
// 'when global createArgument with optional then type is string|undefined'
{
const program = new Command();
program.addArgument(createArgument('[value]')).action((arg) => {
expectType<string | undefined>(arg);
});
}

if ('when global createArgument with variadic then type is string[]') {
// 'when global createArgument with variadic then type is string[]'
{
const program = new Command();
program.addArgument(createArgument('<value...>')).action((arg) => {
expectType<string[]>(arg);
});
}

if ('when global createArgument with const choices then type is string union') {
// 'when global createArgument with const choices then type is string union'
{
const program = new Command();
program
.addArgument(createArgument('<value>').choices(['A', 'B', 'C'] as const))
Expand All @@ -54,9 +61,8 @@ if ('when global createArgument with const choices then type is string union') {
});
}

if (
'when global createArgument with variadic and const choices then type is array of string union'
) {
// 'when global createArgument with variadic and const choices then type is array of string union'
{
const program = new Command();
program
.addArgument(createArgument('<value...>').choices(['A', 'B', 'C'] as const))
Expand Down
32 changes: 16 additions & 16 deletions tests/create-option.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,62 @@ import { Command, createOption } from '..';

// Doing end-to-end test, rather than checking created Option directly.

if ('when cmd.createOption with boolean then type is boolean') {
// 'when cmd.createOption with boolean then type is boolean'
{
const program = new Command();
const foo = program
.addOption(program.createOption('-f, --foo', 'decription'))
.opts().foo;
expectType<true | undefined>(foo);
}

if ('when cmd.createOption with required option-argument then type is string') {
// 'when cmd.createOption with required option-argument then type is string'
{
const program = new Command();
const foo = program
.addOption(program.createOption('-f, --foo <value>', 'decription'))
.opts().foo;
expectType<string | undefined>(foo);
}

if (
'when cmd.createOption with optional option-argument then type is string|true'
) {
// 'when cmd.createOption with optional option-argument then type is string|true'
{
const program = new Command();
const foo = program
.addOption(program.createOption('-f, --foo [value]', 'decription'))
.opts().foo;
expectType<string | true | undefined>(foo);
}

if ('when global createOption with boolean then type is boolean') {
// 'when global createOption with boolean then type is boolean'
{
const program = new Command();
const foo = program
.addOption(createOption('-f, --foo', 'decription'))
.opts().foo;
expectType<true | undefined>(foo);
}

if (
'when global createOption with required option-argument then type is string'
) {
// 'when global createOption with required option-argument then type is string'
{
const program = new Command();
const foo = program
.addOption(createOption('-f, --foo <value>', 'decription'))
.opts().foo;
expectType<string | undefined>(foo);
}

if (
'when global createOption with optional option-argument then type is string|true'
) {
// 'when global createOption with optional option-argument then type is string|true'
{
const program = new Command();
const foo = program
.addOption(createOption('-f, --foo [value]', 'decription'))
.opts().foo;
expectType<string | true | undefined>(foo);
}

if ('when global createOption with const choices then type is string union') {
// 'when global createOption with const choices then type is string union'
{
const program = new Command();
const foo = program
.addOption(
Expand All @@ -71,9 +72,8 @@ if ('when global createOption with const choices then type is string union') {
expectType<'A' | 'B' | 'C' | undefined>(foo);
}

if (
'when global createOption with variadic and const choices then type is string union array'
) {
// 'when global createOption with variadic and const choices then type is string union array'
{
const program = new Command();
const foo = program
.addOption(
Expand Down
17 changes: 10 additions & 7 deletions tests/help.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,40 @@ import { Help, Command, CommandUnknownOpts, Option } from '..';
// Reminder: we pass the command into the Help methods using JavaScript so
// no type checking there, but subclass is TypeScript.

if (
'when subclass Help method with cmd arg as CommandUnknownOpts then no error'
) {
// 'when subclass Help method with cmd arg as CommandUnknownOpts then no error'
{
class MyHelp extends Help {
subcommandTerm(cmd: CommandUnknownOpts) {
return cmd.name();
}
}
}

if ('when subclass Help method with cmd arg as Command then no error') {
// 'when subclass Help method with cmd arg as Command then no error'
{
class MyHelp extends Help {
subcommandTerm(cmd: Command) {
return cmd.name();
}
}
}

if ('when pass type Command to visibleOptions then no error') {
// 'when pass type Command to visibleOptions then no error'
{
const program = new Command().option('--foo').argument('<one>');
program.createHelp().visibleOptions(program);
}

if ('when pass type CommandUnknownOpts to visibleOptions then no error') {
// 'when pass type CommandUnknownOpts to visibleOptions then no error'
{
const program: CommandUnknownOpts = new Command()
.option('--foo')
.argument('<one>');
program.createHelp().visibleOptions(program);
}

if ('when call visibleCommands then returns CommandUnknownOpts[]') {
// 'when call visibleCommands then returns CommandUnknownOpts[]'
{
const program = new Command().option('--foo').argument('<one>');
const vo = program.createHelp().visibleCommands(program);
expectType<CommandUnknownOpts[]>(vo);
Expand Down
18 changes: 12 additions & 6 deletions tests/hook.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expectType } from 'tsd';
import { Command } from '..';

if ('when add preAction hook then thisCommand strongly typed') {
// 'when add preAction hook then thisCommand strongly typed'
{
const program = new Command()
.option('-f, --foo <value>')
.hook('preAction', (thisCommand, actionCommand) => {
Expand All @@ -10,7 +11,8 @@ if ('when add preAction hook then thisCommand strongly typed') {
});
}

if ('when add preAction hook then activeCommand strongly typed') {
// 'when add preAction hook then activeCommand strongly typed'
{
const program = new Command()
.option('-f, --foo <value>')
.hook('preAction', (thisCommand, activeCommand) => {
Expand All @@ -19,7 +21,8 @@ if ('when add preAction hook then activeCommand strongly typed') {
});
}

if ('when add postAction hook then thisCommand strongly typed') {
// 'when add postAction hook then thisCommand strongly typed'
{
const program = new Command()
.option('-f, --foo <value>')
.hook('postAction', (thisCommand, actionCommand) => {
Expand All @@ -28,7 +31,8 @@ if ('when add postAction hook then thisCommand strongly typed') {
});
}

if ('when add postAction hook then activeCommand strongly typed') {
// 'when add postAction hook then activeCommand strongly typed'
{
const program = new Command()
.option('-f, --foo <value>')
.hook('postAction', (thisCommand, activeCommand) => {
Expand All @@ -37,7 +41,8 @@ if ('when add postAction hook then activeCommand strongly typed') {
});
}

if ('when add preSubcommand hook then thisCommand strongly typed') {
// 'when add preSubcommand hook then thisCommand strongly typed'
{
const program = new Command()
.option('-f, --foo <value>')
.hook('preSubcommand', (thisCommand, actionCommand) => {
Expand All @@ -46,7 +51,8 @@ if ('when add preSubcommand hook then thisCommand strongly typed') {
});
}

if ('when add preSubcommand hook then activeCommand strongly typed') {
// 'when add preSubcommand hook then activeCommand strongly typed'
{
const program = new Command()
.option('-f, --foo <value>')
.hook('preSubcommand', (thisCommand, activeCommand) => {
Expand Down
6 changes: 4 additions & 2 deletions tests/option-value.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { expectType } from 'tsd';
import { Command } from '..';

if ('when getOptionValue is unknown then key is unknown') {
// 'when getOptionValue is unknown then key is unknown'
{
const program = new Command().option('-f, --foo');

const v = program.getOptionValue('bar');
expectType<unknown>(v);
}

if ('when getOptionValue result is typed then key is known') {
// 'when getOptionValue result is typed then key is known'
{
const program = new Command().option('-f, --foo');
const v = program.getOptionValue('foo');
expectType<true | undefined>(v);
Expand Down
18 changes: 12 additions & 6 deletions tests/processed-args.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,36 @@ import { Command } from '..';

// Doing a subset of the full tests in arguments.test-d.ts

if ('when no arguments then empty array') {
// 'when no arguments then empty array'
{
const program = new Command();
const args = program.parse().processedArgs;
expectType<[]>(args);
}

if ('when required argument then string element') {
// 'when required argument then string element'
{
const program = new Command();
const args = program.argument('<value>').parse().processedArgs;
expectType<[string]>(args);
}

if ('when optional argument then string|undefined element') {
// 'when optional argument then string|undefined element'
{
const program = new Command();
const args = program.argument('[value]').parse().processedArgs;
expectType<[string | undefined]>(args);
}

if ('when variadic argument then string[] element') {
// 'when variadic argument then string[] element'
{
const program = new Command();
const args = program.argument('<value...>').parse().processedArgs;
expectType<[string[]]>(args);
}

if ('when multiple arguments then multiple elements') {
// 'when multiple arguments then multiple elements'
{
const program = new Command();
const args = program
.argument('<value>')
Expand All @@ -36,7 +41,8 @@ if ('when multiple arguments then multiple elements') {
expectType<[string, string | undefined]>(args);
}

if ('when custom argument processing then custom type') {
// 'when custom argument processing then custom type'
{
const program = new Command();
const args = program
.argument('<value>', 'description', parseFloat)
Expand Down
Loading
Loading