diff options
author | Ryan Geary <rtgnj42@gmail.com> | 2022-04-23 15:16:32 -0400 |
---|---|---|
committer | Ryan Geary <rtgnj42@gmail.com> | 2022-04-23 17:24:25 -0400 |
commit | dd636fa69ee0cb1f292a0c76605617a461b36e47 (patch) | |
tree | 47828e6a78305dbdfba959a1e15697f15eaa3454 | |
parent | 97b3abf5204badf60eb79be6be07285bdef83049 (diff) |
Fix positive start, negative end, length 1 choice is empty42-unexpected-result-in-x_-y-case
-rw-r--r-- | src/choice/mod.rs | 2 | ||||
-rw-r--r-- | src/choice/test/get_negative_start_end.rs | 10 | ||||
-rw-r--r-- | src/choice/test/print_choice.rs | 30 | ||||
-rw-r--r-- | test/choose_1x-1.txt | 19 | ||||
-rw-r--r-- | test/choose_1x-2.txt | 19 | ||||
-rw-r--r-- | test/choose_1x-3.txt | 19 | ||||
-rwxr-xr-x | test/e2e_test.sh | 3 |
7 files changed, 102 insertions, 0 deletions
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 @@ -132,6 +132,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); let config = Config::from_iter(vec!["choose", &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 |