Fix positive start, negative end, length 1 choice is empty

This commit is contained in:
Ryan Geary 2022-04-23 15:16:32 -04:00
parent 97b3abf520
commit dd636fa69e
7 changed files with 102 additions and 0 deletions

View file

@ -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)?;
}
}

View file

@ -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);

View file

@ -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", "");
}

19
test/choose_1x-1.txt Normal file
View file

@ -0,0 +1,19 @@
c
e f
h i
k
o
q r
t u
w

19
test/choose_1x-2.txt Normal file
View file

@ -0,0 +1,19 @@
e
h
q
t

19
test/choose_1x-3.txt Normal file
View file

@ -0,0 +1,19 @@

View file

@ -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