1#![cfg(feature = "unstable-dynamic")]
2
3#[test]
4fn suggest_subcommand_subset() {
5    let name = "test";
6    let mut cmd = clap::Command::new(name)
7        .subcommand(clap::Command::new("hello-world"))
8        .subcommand(clap::Command::new("hello-moon"))
9        .subcommand(clap::Command::new("goodbye-world"));
10
11    let args = [name, "he"];
12    let arg_index = 1;
13    let args = IntoIterator::into_iter(args)
14        .map(std::ffi::OsString::from)
15        .collect::<Vec<_>>();
16    let comp_type = clap_complete::dynamic::bash::CompType::default();
17    let trailing_space = true;
18    let current_dir = None;
19
20    let completions = clap_complete::dynamic::bash::complete(
21        &mut cmd,
22        args,
23        arg_index,
24        comp_type,
25        trailing_space,
26        current_dir,
27    )
28    .unwrap();
29    let completions = completions
30        .into_iter()
31        .map(|s| s.to_string_lossy().into_owned())
32        .collect::<Vec<_>>();
33
34    assert_eq!(completions, ["hello-moon", "hello-world", "help"]);
35}
36
37#[test]
38fn suggest_long_flag_subset() {
39    let name = "test";
40    let mut cmd = clap::Command::new(name)
41        .arg(
42            clap::Arg::new("hello-world")
43                .long("hello-world")
44                .action(clap::ArgAction::Count),
45        )
46        .arg(
47            clap::Arg::new("hello-moon")
48                .long("hello-moon")
49                .action(clap::ArgAction::Count),
50        )
51        .arg(
52            clap::Arg::new("goodbye-world")
53                .long("goodbye-world")
54                .action(clap::ArgAction::Count),
55        );
56
57    let args = [name, "--he"];
58    let arg_index = 1;
59    let args = IntoIterator::into_iter(args)
60        .map(std::ffi::OsString::from)
61        .collect::<Vec<_>>();
62    let comp_type = clap_complete::dynamic::bash::CompType::default();
63    let trailing_space = true;
64    let current_dir = None;
65
66    let completions = clap_complete::dynamic::bash::complete(
67        &mut cmd,
68        args,
69        arg_index,
70        comp_type,
71        trailing_space,
72        current_dir,
73    )
74    .unwrap();
75    let completions = completions
76        .into_iter()
77        .map(|s| s.to_string_lossy().into_owned())
78        .collect::<Vec<_>>();
79
80    assert_eq!(completions, ["--hello-world", "--hello-moon", "--help"]);
81}
82
83#[test]
84fn suggest_possible_value_subset() {
85    let name = "test";
86    let mut cmd = clap::Command::new(name).arg(clap::Arg::new("hello-world").value_parser([
87        "hello-world",
88        "hello-moon",
89        "goodbye-world",
90    ]));
91
92    let args = [name, "hello"];
93    let arg_index = 1;
94    let args = IntoIterator::into_iter(args)
95        .map(std::ffi::OsString::from)
96        .collect::<Vec<_>>();
97    let comp_type = clap_complete::dynamic::bash::CompType::default();
98    let trailing_space = true;
99    let current_dir = None;
100
101    let completions = clap_complete::dynamic::bash::complete(
102        &mut cmd,
103        args,
104        arg_index,
105        comp_type,
106        trailing_space,
107        current_dir,
108    )
109    .unwrap();
110    let completions = completions
111        .into_iter()
112        .map(|s| s.to_string_lossy().into_owned())
113        .collect::<Vec<_>>();
114
115    assert_eq!(completions, ["hello-world", "hello-moon"]);
116}
117
118#[test]
119fn suggest_additional_short_flags() {
120    let name = "test";
121    let mut cmd = clap::Command::new(name)
122        .arg(
123            clap::Arg::new("a")
124                .short('a')
125                .action(clap::ArgAction::Count),
126        )
127        .arg(
128            clap::Arg::new("b")
129                .short('b')
130                .action(clap::ArgAction::Count),
131        )
132        .arg(
133            clap::Arg::new("c")
134                .short('c')
135                .action(clap::ArgAction::Count),
136        );
137
138    let args = [name, "-a"];
139    let arg_index = 1;
140    let args = IntoIterator::into_iter(args)
141        .map(std::ffi::OsString::from)
142        .collect::<Vec<_>>();
143    let comp_type = clap_complete::dynamic::bash::CompType::default();
144    let trailing_space = true;
145    let current_dir = None;
146
147    let completions = clap_complete::dynamic::bash::complete(
148        &mut cmd,
149        args,
150        arg_index,
151        comp_type,
152        trailing_space,
153        current_dir,
154    )
155    .unwrap();
156    let completions = completions
157        .into_iter()
158        .map(|s| s.to_string_lossy().into_owned())
159        .collect::<Vec<_>>();
160
161    assert_eq!(completions, ["-aa", "-ab", "-ac", "-ah"]);
162}
163