Skip to content

Commit

Permalink
Merge pull request #23 from j-richey/improve-unit-test-coverage
Browse files Browse the repository at this point in the history
Add unit tests for Game::default / Error::display
  • Loading branch information
j-richey authored Nov 26, 2019
2 parents 999cdef + 2c2af7e commit 5761499
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,19 @@ mod tests {
assert_eq!(expected_free_squares as usize, actual_free_squares);
}

#[test]
fn game_default_should_create_3x3_board() {
let expected_size = board::Size {
rows: 3,
columns: 3,
};

let game = Game::default();
let actual_size = game.board().size();

assert_eq!(expected_size, actual_size);
}

#[test]
fn game_free_positions_should_not_contain_any_owned_positions() {
let mut game = Game::new();
Expand Down Expand Up @@ -1053,6 +1066,50 @@ mod tests {
assert_eq!(expected_is_game_over, actual_is_game_over);
}

#[test]
fn error_display_when_game_over_should_be_non_empty() {
let error = Error::GameOver;

let error_message = error.to_string();

assert_ne!(0, error_message.len());
}

#[test]
fn error_display_when_position_already_owned_should_contain_position_text() {
let position = board::Position { row: 0, column: 0 };
let owner = board::Owner::PlayerX;
let position_text = format!("{:?}", position);
let error = Error::PositionAlreadyOwned(position, owner);

let error_message = error.to_string();

assert!(error_message.contains(&position_text));
}

#[test]
fn error_display_when_position_already_owned_should_contain_owner_text() {
let position = board::Position { row: 0, column: 0 };
let owner = board::Owner::PlayerX;
let owner_text = format!("{:?}", owner);
let error = Error::PositionAlreadyOwned(position, owner);

let error_message = error.to_string();

assert!(error_message.contains(&owner_text));
}

#[test]
fn error_display_when_invalid_position_should_contain_position_text() {
let position = board::Position { row: 0, column: 0 };
let position_text = format!("{:?}", position);
let error = Error::InvalidPosition(position);

let error_message = error.to_string();

assert!(error_message.contains(&position_text));
}

#[test]
fn state_is_game_over_when_player_X_move_should_be_false() {
let state = State::PlayerXMove;
Expand Down

0 comments on commit 5761499

Please sign in to comment.