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.
169 lines
8.3 KiB
Rust
169 lines
8.3 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 powershell() {
|
|
let mut app = build_app();
|
|
common::<PowerShell>(&mut app, "my_app", POWERSHELL);
|
|
}
|
|
|
|
static POWERSHELL: &str = r#"
|
|
using namespace System.Management.Automation
|
|
using namespace System.Management.Automation.Language
|
|
|
|
Register-ArgumentCompleter -Native -CommandName 'my_app' -ScriptBlock {
|
|
param($wordToComplete, $commandAst, $cursorPosition)
|
|
|
|
$commandElements = $commandAst.CommandElements
|
|
$command = @(
|
|
'my_app'
|
|
for ($i = 1; $i -lt $commandElements.Count; $i++) {
|
|
$element = $commandElements[$i]
|
|
if ($element -isnot [StringConstantExpressionAst] -or
|
|
$element.StringConstantType -ne [StringConstantType]::BareWord -or
|
|
$element.Value.StartsWith('-')) {
|
|
break
|
|
}
|
|
$element.Value
|
|
}) -join ';'
|
|
|
|
$completions = @(switch ($command) {
|
|
'my_app' {
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
|
|
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
|
|
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
|
|
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'tests things')
|
|
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Prints this message or the help of the given subcommand(s)')
|
|
break
|
|
}
|
|
'my_app;test' {
|
|
[CompletionResult]::new('--case', 'case', [CompletionResultType]::ParameterName, 'the case to test')
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
|
|
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
|
|
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
|
|
break
|
|
}
|
|
'my_app;help' {
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
|
|
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
|
|
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
|
|
break
|
|
}
|
|
})
|
|
|
|
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
|
|
Sort-Object -Property ListItemText
|
|
}
|
|
"#;
|
|
|
|
#[test]
|
|
fn powershell_with_special_commands() {
|
|
let mut app = build_app_special_commands();
|
|
common::<PowerShell>(&mut app, "my_app", POWERSHELL_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 POWERSHELL_SPECIAL_CMDS: &str = r#"
|
|
using namespace System.Management.Automation
|
|
using namespace System.Management.Automation.Language
|
|
|
|
Register-ArgumentCompleter -Native -CommandName 'my_app' -ScriptBlock {
|
|
param($wordToComplete, $commandAst, $cursorPosition)
|
|
|
|
$commandElements = $commandAst.CommandElements
|
|
$command = @(
|
|
'my_app'
|
|
for ($i = 1; $i -lt $commandElements.Count; $i++) {
|
|
$element = $commandElements[$i]
|
|
if ($element -isnot [StringConstantExpressionAst] -or
|
|
$element.StringConstantType -ne [StringConstantType]::BareWord -or
|
|
$element.Value.StartsWith('-')) {
|
|
break
|
|
}
|
|
$element.Value
|
|
}) -join ';'
|
|
|
|
$completions = @(switch ($command) {
|
|
'my_app' {
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
|
|
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
|
|
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
|
|
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'tests things')
|
|
[CompletionResult]::new('some_cmd', 'some_cmd', [CompletionResultType]::ParameterValue, 'tests other things')
|
|
[CompletionResult]::new('some-cmd-with-hypens', 'some-cmd-with-hypens', [CompletionResultType]::ParameterValue, 'some-cmd-with-hypens')
|
|
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Prints this message or the help of the given subcommand(s)')
|
|
break
|
|
}
|
|
'my_app;test' {
|
|
[CompletionResult]::new('--case', 'case', [CompletionResultType]::ParameterName, 'the case to test')
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
|
|
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
|
|
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
|
|
break
|
|
}
|
|
'my_app;some_cmd' {
|
|
[CompletionResult]::new('--config', 'config', [CompletionResultType]::ParameterName, 'the other case to test')
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
|
|
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
|
|
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
|
|
break
|
|
}
|
|
'my_app;some-cmd-with-hypens' {
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
|
|
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
|
|
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
|
|
break
|
|
}
|
|
'my_app;help' {
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
|
|
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
|
|
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
|
|
break
|
|
}
|
|
})
|
|
|
|
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
|
|
Sort-Object -Property ListItemText
|
|
}
|
|
"#;
|