[BREAKING] Default to inclusive ranges, -x for exclusive

This commit is contained in:
Ryan Geary 2019-10-13 22:00:15 -04:00
parent 0be16bf1d9
commit f662219407
6 changed files with 33 additions and 27 deletions

View file

@ -51,27 +51,27 @@ impl Choice {
.map(|x| x.1)
.collect::<Vec<&str>>(),
(None, Some(end)) => {
let e: usize = if config.opt.inclusive {
(end + 1).try_into().unwrap()
let e: usize = if config.opt.exclusive {
(end - 1).try_into().unwrap()
} else {
(*end).try_into().unwrap()
};
words
.filter(|x| x.0 < e)
.filter(|x| x.0 <= e)
.map(|x| x.1)
.collect::<Vec<&str>>()
}
(Some(start), Some(end)) => {
let e: usize = if config.opt.inclusive {
(end + 1).try_into().unwrap()
let e: usize = if config.opt.exclusive {
(end - 1).try_into().unwrap()
} else {
(*end).try_into().unwrap()
};
words
.filter(|x| {
(x.0 < e && x.0 >= (*start).try_into().unwrap())
(x.0 <= e && x.0 >= (*start).try_into().unwrap())
|| self.is_reverse_range()
&& (x.0 > e && x.0 <= (*start).try_into().unwrap())
&& (x.0 >= e && x.0 <= (*start).try_into().unwrap())
})
.map(|x| x.1)
.collect::<Vec<&str>>()
@ -144,8 +144,8 @@ mod tests {
}
#[test]
fn print_1_to_3() {
let config = Config::from_iter(vec!["choose", "1:3"]);
fn print_1_to_3_exclusive() {
let config = Config::from_iter(vec!["choose", "1:3", "-x"]);
assert_eq!(
vec!["is", "pretty"],
config.opt.choice[0]
@ -154,8 +154,8 @@ mod tests {
}
#[test]
fn print_1_to_3_inclusive() {
let config = Config::from_iter(vec!["choose", "1:3", "-n"]);
fn print_1_to_3() {
let config = Config::from_iter(vec!["choose", "1:3" ]);
assert_eq!(
vec!["is", "pretty", "cool"],
config.opt.choice[0]
@ -167,15 +167,15 @@ mod tests {
fn print_1_to_3_separated_by_hashtag() {
let config = Config::from_iter(vec!["choose", "1:3", "-f", "#"]);
assert_eq!(
vec!["is", "pretty"],
vec!["is", "pretty", "cool"],
config.opt.choice[0]
.get_choice_slice(&String::from("rust#is#pretty#cool"), &config)
);
}
#[test]
fn print_1_to_3_separated_by_varying_multiple_hashtag() {
let config = Config::from_iter(vec!["choose", "1:3", "-f", "#"]);
fn print_1_to_3_separated_by_varying_multiple_hashtag_exclusive() {
let config = Config::from_iter(vec!["choose", "1:3", "-f", "#", "-x"]);
assert_eq!(
vec!["is", "pretty"],
config.opt.choice[0]
@ -184,8 +184,8 @@ mod tests {
}
#[test]
fn print_1_to_3_separated_by_varying_multiple_hashtag_inclusive() {
let config = Config::from_iter(vec!["choose", "1:3", "-f", "#", "-n"]);
fn print_1_to_3_separated_by_varying_multiple_hashtag() {
let config = Config::from_iter(vec!["choose", "1:3", "-f", "#"]);
assert_eq!(
vec!["is", "pretty", "cool"],
config.opt.choice[0]
@ -194,8 +194,8 @@ mod tests {
}
#[test]
fn print_1_to_3_separated_by_regex_group_vowels() {
let config = Config::from_iter(vec!["choose", "1:3", "-f", "[aeiou]"]);
fn print_1_to_3_separated_by_regex_group_vowels_exclusive() {
let config = Config::from_iter(vec!["choose", "1:3", "-f", "[aeiou]", "-x"]);
assert_eq!(
vec![" q", "ck br"],
config.opt.choice[0].get_choice_slice(
@ -206,8 +206,8 @@ mod tests {
}
#[test]
fn print_1_to_3_separated_by_regex_group_vowels_inclusive() {
let config = Config::from_iter(vec!["choose", "1:3", "-f", "[aeiou]", "-n"]);
fn print_1_to_3_separated_by_regex_group_vowels() {
let config = Config::from_iter(vec!["choose", "1:3", "-f", "[aeiou]"]);
assert_eq!(
vec![" q", "ck br", "wn f"],
config.opt.choice[0].get_choice_slice(
@ -221,7 +221,7 @@ mod tests {
fn print_3_to_1() {
let config = Config::from_iter(vec!["choose", "3:1"]);
assert_eq!(
vec!["pretty", "is"],
vec!["pretty", "is", "lang"],
config.opt.choice[0].get_choice_slice(
&String::from("rust lang is pretty darn cool"),
&config

View file

@ -13,9 +13,9 @@ pub struct Opt {
#[structopt(short, long)]
pub field_separator: Option<String>,
/// Use inclusive ranges
#[structopt(short = "n", long)]
pub inclusive: bool,
/// Use exclusive ranges, similar to array slicing in many programming languages
#[structopt(short = "x", long)]
pub exclusive: bool,
/// Activate debug mode
#[structopt(short, long)]

6
test/choose_4:2.txt Normal file
View file

@ -0,0 +1,6 @@
amet, sit dolor
dolore et labore
nisi laboris ullamco
in dolor irure
sint Excepteur pariatur.
mollit deserunt officia

View file

@ -7,13 +7,13 @@ cd "$(git rev-parse --show-toplevel)"
cargo build
# basic functionality
diff -w <(cargo run -- 0:2 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_0:2.txt")
diff -w <(cargo run -- 0:1 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_0:1.txt")
diff -w <(cargo run -- 0 3 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_0_3.txt")
diff -w <(cargo run -- :2 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_:2.txt")
diff -w <(cargo run -- :1 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_:1.txt")
diff -w <(cargo run -- 9 3 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_9_3.txt")
diff -w <(cargo run -- 9 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_9.txt")
diff -w <(cargo run -- 12 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_12.txt")
diff -w <(cargo run -- 4:1 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_4:1.txt")
diff -w <(cargo run -- 4:2 -i ${test_dir}/lorem.txt) <(cat "${test_dir}/choose_4:2.txt")
# add tests for different delimiters
# add tests using piping