From dd636fa69ee0cb1f292a0c76605617a461b36e47 Mon Sep 17 00:00:00 2001 From: Ryan Geary Date: Sat, 23 Apr 2022 15:16:32 -0400 Subject: Fix positive start, negative end, length 1 choice is empty --- src/choice/mod.rs | 2 ++ src/choice/test/get_negative_start_end.rs | 10 ++++++++++ src/choice/test/print_choice.rs | 30 ++++++++++++++++++++++++++++++ test/choose_1x-1.txt | 19 +++++++++++++++++++ test/choose_1x-2.txt | 19 +++++++++++++++++++ test/choose_1x-3.txt | 19 +++++++++++++++++++ test/e2e_test.sh | 3 +++ 7 files changed, 102 insertions(+) create mode 100644 test/choose_1x-1.txt create mode 100644 test/choose_1x-2.txt create mode 100644 test/choose_1x-3.txt diff --git a/src/choice/mod.rs b/src/choice/mod.rs index 4a4e88f..b835eb8 100644 --- a/src/choice/mod.rs +++ b/src/choice/mod.rs @@ -140,6 +140,8 @@ impl Choice { handle.write_choice(*word, config, true)?; } handle.write_choice(vec[end], config, false)?; + } else if start == end && self.start < vec.len().try_into()? { + handle.write_choice(vec[start], config, false)?; } } diff --git a/src/choice/test/get_negative_start_end.rs b/src/choice/test/get_negative_start_end.rs index deef6ea..edbc5bb 100644 --- a/src/choice/test/get_negative_start_end.rs +++ b/src/choice/test/get_negative_start_end.rs @@ -131,6 +131,16 @@ fn positive_negative_some() { ); } +#[test] +fn positive_negative_same() { + let config = Config::from_iter(vec!["choose", "1:-3"]); + let slice = &[0, 1, 2, 3]; + assert_eq!( + Some((1, 1)), + config.opt.choices[0].get_negative_start_end(slice).unwrap() + ); +} + #[test] fn error_when_choice_is_isize_min() { let isize_min = format!("{}", isize::MIN); diff --git a/src/choice/test/print_choice.rs b/src/choice/test/print_choice.rs index 31fa941..63a63b5 100644 --- a/src/choice/test/print_choice.rs +++ b/src/choice/test/print_choice.rs @@ -1000,3 +1000,33 @@ fn print_after_to_after_empty() { fn print_negative_end_to_negative_end_empty() { test_fn(vec!["choose", "-1:-1"], "", ""); } + +#[test] +fn print_positive_to_following_negative() { + test_fn(vec!["choose", "1:-3"], "a b c d e", "b c"); +} + +#[test] +fn print_positive_to_same_as_negative() { + test_fn(vec!["choose", "1:-4"], "a b c d e", "b"); +} + +#[test] +fn print_positive_to_preceding_negative() { + test_fn(vec!["choose", "1:-5"], "a b c d e", ""); +} + +#[test] +fn print_end_to_last_negative_is_last() { + test_fn(vec!["choose", "4:-1"], "a b c d e", "e"); +} + +#[test] +fn print_after_end_to_last_negative_is_empty() { + test_fn(vec!["choose", "5:-1"], "a b c d e", ""); +} + +#[test] +fn print_after_end_to_second_to_last_negative_is_empty() { + test_fn(vec!["choose", "5:-2"], "a b c d e", ""); +} diff --git a/test/choose_1x-1.txt b/test/choose_1x-1.txt new file mode 100644 index 0000000..33a230e --- /dev/null +++ b/test/choose_1x-1.txt @@ -0,0 +1,19 @@ + +c +e f + +h i +k + + + +o +q r + +t u +w + + + + + diff --git a/test/choose_1x-2.txt b/test/choose_1x-2.txt new file mode 100644 index 0000000..0544919 --- /dev/null +++ b/test/choose_1x-2.txt @@ -0,0 +1,19 @@ + + +e + +h + + + + + +q + +t + + + + + + diff --git a/test/choose_1x-3.txt b/test/choose_1x-3.txt new file mode 100644 index 0000000..a60f303 --- /dev/null +++ b/test/choose_1x-3.txt @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/test/e2e_test.sh b/test/e2e_test.sh index 320521b..6fe58ff 100755 --- a/test/e2e_test.sh +++ b/test/e2e_test.sh @@ -21,6 +21,9 @@ diff -w <(cargo run -- 3:6 -c -i ${test_dir}/lorem.txt 2>/dev/null) <(cat "${tes diff -w <(cargo run -- 2 -x -i ${test_dir}/lorem.txt 2>/dev/null) <(cat "${test_dir}/choose_2_x.txt") diff -w <(cargo run -- -1 -i ${test_dir}/alphabet.txt 2>/dev/null) <(cat "${test_dir}/choose_-1.txt") diff -w <(cargo run -- -2 -i ${test_dir}/alphabet.txt 2>/dev/null) <(cat "${test_dir}/choose_-2.txt") +diff -w <(cargo run -- 1:-1 -i ${test_dir}/alphabet.txt 2>/dev/null) <(cat "${test_dir}/choose_1x-1.txt") +diff -w <(cargo run -- 1:-2 -i ${test_dir}/alphabet.txt 2>/dev/null) <(cat "${test_dir}/choose_1x-2.txt") +diff -w <(cargo run -- 1:-3 -i ${test_dir}/alphabet.txt 2>/dev/null) <(cat "${test_dir}/choose_1x-3.txt") # add tests for different delimiters # add tests using piping -- cgit v1.2.3