mirror of
https://github.com/nushell/nushell.git
synced 2025-01-05 05:50:14 +01:00
fix(explore): handle cursor size zero using saturating_sub
This commit is contained in:
parent
baf86dfb0e
commit
eb86d549a1
@ -36,7 +36,7 @@ impl Cursor {
|
|||||||
|
|
||||||
/// The max position the cursor can be at
|
/// The max position the cursor can be at
|
||||||
pub fn end(&self) -> usize {
|
pub fn end(&self) -> usize {
|
||||||
self.size - 1
|
self.size.saturating_sub(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the position to a specific value within the bounds [0, end]
|
/// Set the position to a specific value within the bounds [0, end]
|
||||||
@ -121,4 +121,28 @@ mod tests {
|
|||||||
cursor.move_backward(3);
|
cursor.move_backward(3);
|
||||||
assert_eq!(cursor.position, 0);
|
assert_eq!(cursor.position, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cursor_size_zero_handling() {
|
||||||
|
|
||||||
|
let cursor = Cursor::new(0);
|
||||||
|
assert_eq!(cursor.end(), 0);
|
||||||
|
|
||||||
|
let mut cursor = Cursor::new(0);
|
||||||
|
cursor.move_forward(1);
|
||||||
|
assert_eq!(cursor.position, 0);
|
||||||
|
|
||||||
|
cursor.move_backward(1);
|
||||||
|
assert_eq!(cursor.position, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cursor_size_one() {
|
||||||
|
let mut cursor = Cursor::new(1);
|
||||||
|
assert_eq!(cursor.end(), 0);
|
||||||
|
|
||||||
|
cursor.move_forward(1);
|
||||||
|
assert_eq!(cursor.position, 0);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user