add history tests

main
Jordan Orelli 10 months ago
parent 437afd9d76
commit 1153c2dc49

@ -12,9 +12,30 @@ impl History {
}
pub fn look_back(&self, idx: usize) -> Option<&str> {
if idx > self.events.len() {
if idx == 0 || idx > self.events.len() {
return None;
}
Some(&self.events[self.events.len() - idx])
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_history() {
let mut history = History::new();
assert_eq!(history.look_back(1), None);
assert_eq!(history.look_back(0), None);
history.add("Hello");
assert_eq!(history.look_back(1), Some("Hello"));
assert_eq!(history.look_back(0), None);
history.add("Hello again");
assert_eq!(history.look_back(2), Some("Hello"));
assert_eq!(history.look_back(1), Some("Hello again"));
assert_eq!(history.look_back(0), None);
}
}

Loading…
Cancel
Save