Skip to content

Commit

Permalink
completion tests: cover a few new scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
pacak committed Aug 11, 2024
1 parent b798f1f commit d836b8c
Showing 1 changed file with 72 additions and 7 deletions.
79 changes: 72 additions & 7 deletions tests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,15 @@ fn test_completer(input: &String) -> Vec<(&'static str, Option<&'static str>)> {
vec
}

fn test_completer2(input: &String) -> Vec<(&'static str, Option<&'static str>)> {
let items = ["auto", "mala"];
items
.iter()
.filter(|item| item.starts_with(input))
.map(|item| (*item, None))
.collect::<Vec<_>>()
}

fn test_completer_descr(input: &String) -> Vec<(&'static str, Option<&'static str>)> {
let items = ["alpha", "beta", "banana", "cat", "durian"];
items
Expand Down Expand Up @@ -1062,7 +1071,43 @@ fn suggest_double_dash_automatically_for_strictly_positional() {
}

#[test]
#[should_panic(expected = "app supports ")]
fn multiple_adjacent_single_letter_vals() {
let a = short('a').switch();
let b = short('b').switch();
let c = short('c').switch();
let parser = construct!(a, b, c).to_options();

// with no input the right behavior is to suggest all the switches
let r = parser
.run_inner(Args::from(&[""]).set_comp(0))
.unwrap_err()
.unwrap_stdout();
assert_eq!(r, "-a\t-a\t\t\n-b\t-b\t\t\n-c\t-c\t\t\n\n");

// with a single item present separately we should suggest the remaining two
let r = parser
.run_inner(Args::from(&["-a", ""]).set_comp(0))
.unwrap_err()
.unwrap_stdout();
assert_eq!(r, "-b\t-b\t\t\n-c\t-c\t\t\n\n");

// with a single item present in the same item we still should suggest the remaining two values
let r = parser
.run_inner(Args::from(&["-a"]).set_comp(0))
.unwrap_err()
.unwrap_stdout();
assert_eq!(r, "-b\t-b\t\t\n-c\t-c\t\t\n\n");

// should I change the representation for the completions? Knowing that somethnig is a
// flag with a short or long name is no longer necessary
// now I need to be able to represent adding a -v to -f to get -fv, so pretty printed value is
// -v, substitution is `-fv`...
//
//
// FWIW bash doesn't complete short flags in combination... should we behave the same way?
}

#[test]
fn ambiguity_no_resolve() {
let a0 = short('a').switch().count();
let a1 = short('a').argument::<usize>("AAAAAA");
Expand Down Expand Up @@ -1287,13 +1332,39 @@ sample\tsample\t\t\n\n"
);
}

#[test]
fn pair_of_positionals() {
// with positional items only current item should make suggestions, not both...
let a = positional::<String>("A").complete(test_completer);
let b = positional::<String>("B").complete(test_completer2);
let parser = construct!(a, b).to_options();

let r = parser
.run_inner(Args::from(&["a"]).set_comp(0))
.unwrap_err()
.unwrap_stdout();
assert_eq!(r, "alpha");

let r = parser
.run_inner(Args::from(&["alpha", "a"]).set_comp(0))
.unwrap_err()
.unwrap_stdout();
assert_eq!(r, "auto");
}

#[test]
fn double_dash_as_positional() {
let parser = positional::<String>("P")
.help("Help")
.complete(test_completer)
.to_options();

let r = parser
.run_inner(Args::from(&["--", "a"]).set_comp(0))
.unwrap_err()
.unwrap_stdout();
assert_eq!(r, "alpha");

let r = parser
.run_inner(Args::from(&["a"]).set_comp(0))
.unwrap_err()
Expand All @@ -1312,12 +1383,6 @@ fn double_dash_as_positional() {
.unwrap_stdout();
assert_eq!(r, "--\n");

let r = parser
.run_inner(Args::from(&["--", "a"]).set_comp(0))
.unwrap_err()
.unwrap_stdout();
assert_eq!(r, "alpha"); // this is not a valid positional
//
let r = parser
.run_inner(Args::from(&["--"]).set_comp(0))
.unwrap_err()
Expand Down

0 comments on commit d836b8c

Please sign in to comment.