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

cli: update command_test.v #21307

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 8 additions & 30 deletions vlib/cli/command_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn test_if_command_parses_empty_args() {
execute: empty_func
}
cmd.parse(['command'])
assert cmd.name == 'command' && compare_arrays(cmd.args, [])
assert cmd.name == 'command' && cmd.args == []
}

fn test_if_command_parses_args() {
Expand All @@ -15,7 +15,7 @@ fn test_if_command_parses_args() {
execute: empty_func
}
cmd.parse(['command', 'arg0', 'arg1'])
assert cmd.name == 'command' && compare_arrays(cmd.args, ['arg0', 'arg1'])
assert cmd.name == 'command' && cmd.args == ['arg0', 'arg1']
}

fn test_if_subcommands_parse_args() {
Expand All @@ -31,24 +31,23 @@ fn test_if_subcommands_parse_args() {
}

fn if_subcommands_parse_args_func(cmd cli.Command) ! {
assert cmd.name == 'subcommand' && compare_arrays(cmd.args, ['arg0', 'arg1'])
assert cmd.name == 'subcommand' && cmd.args == ['arg0', 'arg1']
}

fn test_if_command_has_default_help_subcommand() {
fn test_default_subcommands() {
mut cmd := cli.Command{
name: 'command'
}
cmd.parse(['command'])
assert has_command(cmd, 'help')
}
assert cmd.commands.any(it.name == 'help')
assert cmd.commands.any(it.name == 'man')

fn test_if_command_has_default_version_subcommand_if_version_is_set() {
mut cmd := cli.Command{
cmd = cli.Command{
name: 'command'
version: '1.0.0'
}
cmd.parse(['command'])
assert has_command(cmd, 'version')
assert cmd.commands.any(it.name == 'version')
}

fn flag_should_be_set(cmd cli.Command) ! {
Expand Down Expand Up @@ -199,24 +198,3 @@ fn test_command_setup() {
// helper functions
fn empty_func(cmd cli.Command) ! {
}

fn has_command(cmd cli.Command, name string) bool {
for subcmd in cmd.commands {
if subcmd.name == name {
return true
}
}
return false
}

fn compare_arrays(array0 []string, array1 []string) bool {
if array0.len != array1.len {
return false
}
for i in 0 .. array0.len {
if array0[i] != array1[i] {
return false
}
}
return true
}