test: add quoting test

This commit is contained in:
nibon7 2022-11-02 12:00:19 +08:00
parent 50aefdb9b3
commit da3bcd82e5
No known key found for this signature in database
GPG key ID: 281CE465D8EEC03B
3 changed files with 136 additions and 44 deletions

View file

@ -51,23 +51,72 @@ pub fn feature_sample_command(name: &'static str) -> Command {
) )
} }
pub fn special_commands_command(name: &'static str) -> clap::Command { pub fn special_commands_command(name: &'static str) -> Command {
feature_sample_command(name) feature_sample_command(name)
.subcommand( .subcommand(
clap::Command::new("some_cmd") Command::new("some_cmd")
.about("tests other things") .about("tests other things")
.arg( .arg(
clap::Arg::new("config") Arg::new("config")
.long("config") .long("config")
.hide(true) .hide(true)
.action(clap::ArgAction::Set) .action(ArgAction::Set)
.require_equals(true) .require_equals(true)
.help("the other case to test"), .help("the other case to test"),
) )
.arg(clap::Arg::new("path").num_args(1..)), .arg(Arg::new("path").num_args(1..)),
) )
.subcommand(clap::Command::new("some-cmd-with-hyphens").alias("hyphen")) .subcommand(Command::new("some-cmd-with-hyphens").alias("hyphen"))
.subcommand(clap::Command::new("some-hidden-cmd").hide(true)) .subcommand(Command::new("some-hidden-cmd").hide(true))
}
pub fn quoting_command(name: &'static str) -> Command {
Command::new(name)
.version("3.0")
.arg(
Arg::new("single-quotes")
.long("single-quotes")
.action(ArgAction::SetTrue)
.help("Can be 'always', 'auto', or 'never'"),
)
.arg(
Arg::new("double-quotes")
.long("double-quotes")
.action(ArgAction::SetTrue)
.help("Can be \"always\", \"auto\", or \"never\""),
)
.arg(
Arg::new("backticks")
.long("backticks")
.action(ArgAction::SetTrue)
.help("For more information see `echo test`"),
)
.arg(
Arg::new("backslash")
.long("backslash")
.action(ArgAction::SetTrue)
.help("Avoid '\\n'"),
)
.arg(
Arg::new("brackets")
.long("brackets")
.action(ArgAction::SetTrue)
.help("List packages [filter]"),
)
.arg(
Arg::new("expansions")
.long("expansions")
.action(ArgAction::SetTrue)
.help("Execute the shell command with $SHELL"),
)
.subcommands([
Command::new("cmd-single-quotes").about("Can be 'always', 'auto', or 'never'"),
Command::new("cmd-double-quotes").about("Can be \"always\", \"auto\", or \"never\""),
Command::new("cmd-backticks").about("For more information see `echo test`"),
Command::new("cmd-backslash").about("Avoid '\\n'"),
Command::new("cmd-brackets").about("List packages [filter]"),
Command::new("cmd-expansions").about("Execute the shell command with $SHELL"),
])
} }
pub fn aliases_command(name: &'static str) -> Command { pub fn aliases_command(name: &'static str) -> Command {
@ -116,87 +165,79 @@ pub fn sub_subcommands_command(name: &'static str) -> Command {
) )
} }
pub fn value_hint_command(name: &'static str) -> clap::Command { pub fn value_hint_command(name: &'static str) -> Command {
clap::Command::new(name) Command::new(name)
.arg( .arg(
clap::Arg::new("choice") Arg::new("choice")
.long("choice") .long("choice")
.action(clap::ArgAction::Set) .action(ArgAction::Set)
.value_parser(["bash", "fish", "zsh"]), .value_parser(["bash", "fish", "zsh"]),
) )
.arg( .arg(
clap::Arg::new("unknown") Arg::new("unknown")
.long("unknown") .long("unknown")
.value_hint(clap::ValueHint::Unknown), .value_hint(ValueHint::Unknown),
) )
.arg(Arg::new("other").long("other").value_hint(ValueHint::Other))
.arg( .arg(
clap::Arg::new("other") Arg::new("path")
.long("other")
.value_hint(clap::ValueHint::Other),
)
.arg(
clap::Arg::new("path")
.long("path") .long("path")
.short('p') .short('p')
.value_hint(clap::ValueHint::AnyPath), .value_hint(ValueHint::AnyPath),
) )
.arg( .arg(
clap::Arg::new("file") Arg::new("file")
.long("file") .long("file")
.short('f') .short('f')
.value_hint(clap::ValueHint::FilePath), .value_hint(ValueHint::FilePath),
) )
.arg( .arg(
clap::Arg::new("dir") Arg::new("dir")
.long("dir") .long("dir")
.short('d') .short('d')
.value_hint(clap::ValueHint::DirPath), .value_hint(ValueHint::DirPath),
) )
.arg( .arg(
clap::Arg::new("exe") Arg::new("exe")
.long("exe") .long("exe")
.short('e') .short('e')
.value_hint(clap::ValueHint::ExecutablePath), .value_hint(ValueHint::ExecutablePath),
) )
.arg( .arg(
clap::Arg::new("cmd_name") Arg::new("cmd_name")
.long("cmd-name") .long("cmd-name")
.value_hint(clap::ValueHint::CommandName), .value_hint(ValueHint::CommandName),
) )
.arg( .arg(
clap::Arg::new("cmd") Arg::new("cmd")
.long("cmd") .long("cmd")
.short('c') .short('c')
.value_hint(clap::ValueHint::CommandString), .value_hint(ValueHint::CommandString),
) )
.arg( .arg(
clap::Arg::new("command_with_args") Arg::new("command_with_args")
.action(clap::ArgAction::Set) .action(ArgAction::Set)
.num_args(1..) .num_args(1..)
.trailing_var_arg(true) .trailing_var_arg(true)
.value_hint(clap::ValueHint::CommandWithArguments), .value_hint(ValueHint::CommandWithArguments),
) )
.arg( .arg(
clap::Arg::new("user") Arg::new("user")
.short('u') .short('u')
.long("user") .long("user")
.value_hint(clap::ValueHint::Username), .value_hint(ValueHint::Username),
) )
.arg( .arg(
clap::Arg::new("host") Arg::new("host")
.short('H') .short('H')
.long("host") .long("host")
.value_hint(clap::ValueHint::Hostname), .value_hint(ValueHint::Hostname),
) )
.arg(Arg::new("url").long("url").value_hint(ValueHint::Url))
.arg( .arg(
clap::Arg::new("url") Arg::new("email")
.long("url")
.value_hint(clap::ValueHint::Url),
)
.arg(
clap::Arg::new("email")
.long("email") .long("email")
.value_hint(clap::ValueHint::EmailAddress), .value_hint(ValueHint::EmailAddress),
) )
} }

View file

@ -36,6 +36,18 @@ fn special_commands() {
); );
} }
#[test]
fn quoting() {
let name = "my-app";
let cmd = common::quoting_command(name);
common::assert_matches_path(
"tests/snapshots/quoting.nu",
clap_complete_nushell::Nushell,
cmd,
name,
);
}
#[test] #[test]
fn aliases() { fn aliases() {
let name = "my-app"; let name = "my-app";

View file

@ -0,0 +1,39 @@
module completions {
export extern my-app [
--single-quotes # Can be 'always', 'auto', or 'never'
--double-quotes # Can be "always", "auto", or "never"
--backticks # For more information see `echo test`
--backslash # Avoid '\n'
--brackets # List packages [filter]
--expansions # Execute the shell command with $SHELL
--version(-V) # Print version information
]
# Can be 'always', 'auto', or 'never'
export extern "my-app cmd-single-quotes" [
]
# Can be "always", "auto", or "never"
export extern "my-app cmd-double-quotes" [
]
# For more information see `echo test`
export extern "my-app cmd-backticks" [
]
# Avoid '\n'
export extern "my-app cmd-backslash" [
]
# List packages [filter]
export extern "my-app cmd-brackets" [
]
# Execute the shell command with $SHELL
export extern "my-app cmd-expansions" [
]
}
use completions *