From 1153c2dc494a7676e47fa8b06b7d889a895bbeb4 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 18 Feb 2024 16:52:58 -0600 Subject: [PATCH] add history tests --- src/history.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/history.rs b/src/history.rs index bd5982a..46905e0 100644 --- a/src/history.rs +++ b/src/history.rs @@ -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); + } +}