mirror of
https://github.com/clap-rs/clap
synced 2024-12-15 15:22:30 +00:00
84d99d6bb8
Right now, adjusting test cases to cover some special edge-cases for a specific shell is a little problematic. For instance, if you add a special character that needs escaping to one of the help strings, not only the tests for the shell you're currently working on need adjustments, but rather all shell tests! Contributors can often only work on one specific shell. The need to adjust shells they don't know anything about discourages testing or makes it simply impossible for a single contributor. Arguably, this commit introduces a lot of code duplication, but on the other hand it also makes testing a lot easier.
147 lines
4.5 KiB
Rust
147 lines
4.5 KiB
Rust
use super::*;
|
|
|
|
fn build_app() -> App<'static> {
|
|
build_app_with_name("myapp")
|
|
}
|
|
|
|
fn build_app_with_name(s: &'static str) -> App<'static> {
|
|
App::new(s)
|
|
.about("Tests completions")
|
|
.arg(
|
|
Arg::new("file")
|
|
.value_hint(ValueHint::FilePath)
|
|
.about("some input file"),
|
|
)
|
|
.subcommand(
|
|
App::new("test").about("tests things").arg(
|
|
Arg::new("case")
|
|
.long("case")
|
|
.takes_value(true)
|
|
.about("the case to test"),
|
|
),
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn elvish() {
|
|
let mut app = build_app();
|
|
common::<Elvish>(&mut app, "my_app", ELVISH);
|
|
}
|
|
|
|
static ELVISH: &str = r#"
|
|
edit:completion:arg-completer[my_app] = [@words]{
|
|
fn spaces [n]{
|
|
repeat $n ' ' | joins ''
|
|
}
|
|
fn cand [text desc]{
|
|
edit:complex-candidate $text &display-suffix=' '(spaces (- 14 (wcswidth $text)))$desc
|
|
}
|
|
command = 'my_app'
|
|
for word $words[1:-1] {
|
|
if (has-prefix $word '-') {
|
|
break
|
|
}
|
|
command = $command';'$word
|
|
}
|
|
completions = [
|
|
&'my_app'= {
|
|
cand -h 'Prints help information'
|
|
cand --help 'Prints help information'
|
|
cand -V 'Prints version information'
|
|
cand --version 'Prints version information'
|
|
cand test 'tests things'
|
|
cand help 'Prints this message or the help of the given subcommand(s)'
|
|
}
|
|
&'my_app;test'= {
|
|
cand --case 'the case to test'
|
|
cand -h 'Prints help information'
|
|
cand --help 'Prints help information'
|
|
cand -V 'Prints version information'
|
|
cand --version 'Prints version information'
|
|
}
|
|
&'my_app;help'= {
|
|
cand -h 'Prints help information'
|
|
cand --help 'Prints help information'
|
|
cand -V 'Prints version information'
|
|
cand --version 'Prints version information'
|
|
}
|
|
]
|
|
$completions[$command]
|
|
}
|
|
"#;
|
|
|
|
#[test]
|
|
fn elvish_with_special_commands() {
|
|
let mut app = build_app_special_commands();
|
|
common::<Elvish>(&mut app, "my_app", ELVISH_SPECIAL_CMDS);
|
|
}
|
|
|
|
fn build_app_special_commands() -> App<'static> {
|
|
build_app_with_name("my_app")
|
|
.subcommand(
|
|
App::new("some_cmd").about("tests other things").arg(
|
|
Arg::new("config")
|
|
.long("--config")
|
|
.takes_value(true)
|
|
.about("the other case to test"),
|
|
),
|
|
)
|
|
.subcommand(App::new("some-cmd-with-hypens").alias("hyphen"))
|
|
}
|
|
|
|
static ELVISH_SPECIAL_CMDS: &str = r#"
|
|
edit:completion:arg-completer[my_app] = [@words]{
|
|
fn spaces [n]{
|
|
repeat $n ' ' | joins ''
|
|
}
|
|
fn cand [text desc]{
|
|
edit:complex-candidate $text &display-suffix=' '(spaces (- 14 (wcswidth $text)))$desc
|
|
}
|
|
command = 'my_app'
|
|
for word $words[1:-1] {
|
|
if (has-prefix $word '-') {
|
|
break
|
|
}
|
|
command = $command';'$word
|
|
}
|
|
completions = [
|
|
&'my_app'= {
|
|
cand -h 'Prints help information'
|
|
cand --help 'Prints help information'
|
|
cand -V 'Prints version information'
|
|
cand --version 'Prints version information'
|
|
cand test 'tests things'
|
|
cand some_cmd 'tests other things'
|
|
cand some-cmd-with-hypens 'some-cmd-with-hypens'
|
|
cand help 'Prints this message or the help of the given subcommand(s)'
|
|
}
|
|
&'my_app;test'= {
|
|
cand --case 'the case to test'
|
|
cand -h 'Prints help information'
|
|
cand --help 'Prints help information'
|
|
cand -V 'Prints version information'
|
|
cand --version 'Prints version information'
|
|
}
|
|
&'my_app;some_cmd'= {
|
|
cand --config 'the other case to test'
|
|
cand -h 'Prints help information'
|
|
cand --help 'Prints help information'
|
|
cand -V 'Prints version information'
|
|
cand --version 'Prints version information'
|
|
}
|
|
&'my_app;some-cmd-with-hypens'= {
|
|
cand -h 'Prints help information'
|
|
cand --help 'Prints help information'
|
|
cand -V 'Prints version information'
|
|
cand --version 'Prints version information'
|
|
}
|
|
&'my_app;help'= {
|
|
cand -h 'Prints help information'
|
|
cand --help 'Prints help information'
|
|
cand -V 'Prints version information'
|
|
cand --version 'Prints version information'
|
|
}
|
|
]
|
|
$completions[$command]
|
|
}
|
|
"#;
|