Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
a-barwick committed May 21, 2023
1 parent 5a72087 commit b4e27cf
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 3 deletions.
80 changes: 77 additions & 3 deletions src/game/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ impl<'a> Game<'a> {
}
}

#[derive(PartialEq, Debug)]
enum GameState {
InProgress,
Win,
Expand Down Expand Up @@ -189,7 +190,79 @@ mod tests {
}

#[test]
fn test_run() {
fn test_draw_board() {
let player_one = Player {
symbol: PlayerSymbol::X,
};
let mock_input = MockInput {
input: "1".to_string(),
};
let game = Game::new(&player_one, &mock_input);
game.draw_board();
}

#[test]
fn test_get_state_in_progress() {
let player_one = Player {
symbol: PlayerSymbol::X,
};
let mock_input = MockInput {
input: "1".to_string(),
};
let mut game = Game::new(&player_one, &mock_input);
assert_eq!(game.get_state(), GameState::InProgress);
}

#[test]
fn test_get_state_win() {
let player_one = Player {
symbol: PlayerSymbol::X,
};
let mock_input = MockInput {
input: "1".to_string(),
};
let mut game = Game::new(&player_one, &mock_input);
game.board.cells[0].value = Some(PlayerSymbol::X);
game.board.cells[1].value = Some(PlayerSymbol::X);
game.board.cells[2].value = Some(PlayerSymbol::X);
assert_eq!(game.get_state(), GameState::Win);
}

#[test]
fn test_get_state_draw() {
let player_one = Player {
symbol: PlayerSymbol::X,
};
let mock_input = MockInput {
input: "1".to_string(),
};
let mut game = Game::new(&player_one, &mock_input);
game.board.cells[0].value = Some(PlayerSymbol::X);
game.board.cells[1].value = Some(PlayerSymbol::O);
game.board.cells[2].value = Some(PlayerSymbol::X);
game.board.cells[3].value = Some(PlayerSymbol::O);
game.board.cells[4].value = Some(PlayerSymbol::X);
game.board.cells[5].value = Some(PlayerSymbol::O);
game.board.cells[6].value = Some(PlayerSymbol::O);
game.board.cells[7].value = Some(PlayerSymbol::X);
game.board.cells[8].value = Some(PlayerSymbol::O);
assert_eq!(game.get_state(), GameState::Draw);
}

#[test]
fn test_get_player_selection() {
let player_one = Player {
symbol: PlayerSymbol::X,
};
let mock_input = MockInput {
input: "1".to_string(),
};
let game = Game::new(&player_one, &mock_input);
assert_eq!(game.get_player_selection().unwrap(), 1);
}

#[test]
fn test_set_current_player() {
let player_one = Player {
symbol: PlayerSymbol::X,
};
Expand All @@ -200,7 +273,8 @@ mod tests {
input: "1".to_string(),
};
let mut game = Game::new(&player_one, &mock_input);
game.run(&player_one, &player_two);
assert_eq!(game.board.cells[0].value, Some(PlayerSymbol::X));
assert_eq!(game.current_player.symbol, player_one.symbol);
game.set_current_player(&player_two);
assert_eq!(game.current_player.symbol, player_two.symbol);
}
}
27 changes: 27 additions & 0 deletions src/io/input.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,50 @@
/// Trait for reading some generic input
pub trait Input {
fn read(&self) -> std::io::Result<String>;
}

/// Represents a file input source
pub struct StdinInput;

impl Input for StdinInput {
/// Concrete implementation of the read method for StdinInput
fn read(&self) -> std::io::Result<String> {
let mut user_input = String::new();
std::io::stdin().read_line(&mut user_input)?;
Ok(user_input)
}
}

/// Represents a mock input source
pub struct MockInput {
pub input: String,
}

impl Input for MockInput {
/// Concrete implementation of the read method for MockInput
fn read(&self) -> std::io::Result<String> {
Ok(self.input.clone())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_stdin_input() {
let stdin_input = StdinInput {};
let result = stdin_input.read();
assert!(result.is_ok());
}

#[test]
fn test_mock_input() {
let mock_input = MockInput {
input: String::from("Hello, world!"),
};
let result = mock_input.read();
assert!(result.is_ok());
assert_eq!(result.unwrap(), "Hello, world!");
}
}

0 comments on commit b4e27cf

Please sign in to comment.