From 7264ff03f3374b32e229c8de827a746762a493c8 Mon Sep 17 00:00:00 2001 From: itsscb Date: Mon, 9 Sep 2024 13:13:55 +0200 Subject: [PATCH] feat: add tests --- Cargo.toml | 3 ++ src/model/game.rs | 131 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 128 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e91ef38..390eee8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,6 @@ tracing = "0.1.40" [workspace] members = [".", "frontend"] + +[dev-dependencies] +rand = "0.8.5" diff --git a/src/model/game.rs b/src/model/game.rs index ac081e8..cb93f00 100644 --- a/src/model/game.rs +++ b/src/model/game.rs @@ -5,11 +5,11 @@ type Tries = usize; const MAX_TRIES: Tries = 5; -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] pub struct Game { pub word: Option, pub submitted_words: Vec>>, - tries: usize, + tries: Tries, status: Status, } @@ -24,16 +24,16 @@ impl Game { } } - pub fn start(&mut self, word: String) { + pub fn start(&mut self, word: &str) { if self.word.is_none() && self.status == Status::New { self.status = Status::InProgress; - self.word = Some(word); + self.word = Some(word.to_uppercase()); } } - pub fn submit_answer(&mut self, answer: &[String]) { + pub fn submit_answer(&mut self, answer: &str) { if let Some(ref word) = self.word { - let res = compare_strings(word, &answer.join("")); + let res = compare_strings(word, &answer.to_uppercase()); self.submitted_words.push(res); self.tries += 1; self.status = self.current_status(); @@ -85,3 +85,122 @@ pub enum Status { Lose(Tries), InProgress, } + +#[cfg(test)] +mod test { + use super::*; + use rand::Rng; + use std::iter; + #[test] + fn new() { + assert_eq!( + Game { + word: None, + tries: 0, + submitted_words: Vec::new(), + status: Status::New, + }, + Game::new() + ); + } + + #[test] + fn start() { + let word: String = random_word(5); + + let want = Game { + word: Some(word.to_uppercase()), + submitted_words: Vec::new(), + tries: 0, + status: Status::InProgress, + }; + + let mut got = Game::new(); + got.start(&word); + + assert_eq!(got, want); + } + + #[test] + fn submit_answer() { + let word = "hallo"; + let answer = "hello"; + + let want = Game { + word: Some(word.to_uppercase()), + submitted_words: vec![compare_strings( + &word.to_uppercase(), + &answer.to_uppercase(), + )], + tries: 1, + status: Status::InProgress, + }; + + let mut got = Game::new(); + got.start(word); + got.submit_answer(answer); + + assert_eq!(got, want); + } + + #[test] + fn current_status() { + let mut got = Game::new(); + + assert_eq!(got.current_status(), Status::New); + + let word = "hallo"; + + let want = Game { + word: Some(word.to_uppercase()), + submitted_words: Vec::new(), + tries: 0, + status: Status::InProgress, + }; + got.start(word); + + assert_eq!(got, want); + + let answer = "hello"; + let want = Game { + word: Some(word.to_uppercase()), + submitted_words: vec![compare_strings( + &word.to_uppercase(), + &answer.to_uppercase(), + )], + tries: 1, + status: Status::InProgress, + }; + got.submit_answer(answer); + assert_eq!(got, want); + + got.submit_answer(answer); + got.submit_answer(answer); + got.submit_answer(answer); + got.submit_answer(answer); + assert_eq!(got.current_status(), Status::Lose(5)); + + let mut got = Game::new(); + got.start(word); + got.submit_answer(word); + assert_eq!(got.current_status(), Status::Win(1)); + + let mut got = Game::new(); + got.start(word); + got.submit_answer(answer); + got.submit_answer(answer); + got.submit_answer(word); + assert_eq!(got.current_status(), Status::Win(3)); + } + + fn random_word(len: usize) -> String { + let mut rng = rand::thread_rng(); + let word: String = iter::repeat(()) + .map(|()| rng.sample(rand::distributions::Alphanumeric)) + .map(char::from) + .filter(char::is_ascii_lowercase) + .take(len) + .collect(); + word + } +}