feat: add tests
This commit is contained in:
parent
943c39ea96
commit
7264ff03f3
@ -16,3 +16,6 @@ tracing = "0.1.40"
|
||||
|
||||
[workspace]
|
||||
members = [".", "frontend"]
|
||||
|
||||
[dev-dependencies]
|
||||
rand = "0.8.5"
|
||||
|
@ -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<String>,
|
||||
pub submitted_words: Vec<Vec<CharStatus<String>>>,
|
||||
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
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user