Skip to content

Commit

Permalink
Ensure that a single bound cannot go backward
Browse files Browse the repository at this point in the history
  • Loading branch information
riquito committed May 21, 2022
1 parent 9c2f16f commit c92c91b
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/bin/tuc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ impl FromStr for UserBounds {
(_, Side::Some(0)) => {
return Err("Field value 0 is not allowed (fields are 1-indexed)".into());
}
(Side::Some(left), Side::Some(right)) if right < left => {
return Err("Field left value cannot be greater than right value".into());
}
_ => (),
}

Expand Down Expand Up @@ -815,6 +818,62 @@ mod tests {
);
}

#[test]
fn test_user_bounds_from_str() {
assert_eq!(
UserBounds::from_str("1").ok(),
Some(UserBounds::new(Side::Some(1), Side::Some(1))),
);
assert_eq!(
UserBounds::from_str("-1").ok(),
Some(UserBounds::new(Side::Some(-1), Side::Some(-1))),
);
assert_eq!(
UserBounds::from_str("1:2").ok(),
Some(UserBounds::new(Side::Some(1), Side::Some(2))),
);
assert_eq!(
UserBounds::from_str("-2:-1").ok(),
Some(UserBounds::new(Side::Some(-2), Side::Some(-1))),
);
assert_eq!(
UserBounds::from_str("1:").ok(),
Some(UserBounds::new(Side::Some(1), Side::Continue)),
);
assert_eq!(
UserBounds::from_str("-1:").ok(),
Some(UserBounds::new(Side::Some(-1), Side::Continue)),
);
assert_eq!(
UserBounds::from_str(":1").ok(),
Some(UserBounds::new(Side::Continue, Side::Some(1))),
);
assert_eq!(
UserBounds::from_str(":-1").ok(),
Some(UserBounds::new(Side::Continue, Side::Some(-1))),
);

{
#![allow(clippy::bind_instead_of_map)]
assert_eq!(
UserBounds::from_str("2:1")
.err()
.and_then(|x| Some(x.to_string())),
Some(String::from(
"Field left value cannot be greater than right value"
))
);
assert_eq!(
UserBounds::from_str("-1:-2")
.err()
.and_then(|x| Some(x.to_string())),
Some(String::from(
"Field left value cannot be greater than right value"
))
);
}
}

#[test]
fn test_user_bounds_is_sortable() {
assert!(UserBoundsList(Vec::new()).is_sortable());
Expand Down

0 comments on commit c92c91b

Please sign in to comment.