function to safely add a tile

This commit is contained in:
Max Bradbury 2020-04-13 17:44:51 +01:00
parent e84fa4cc7c
commit 280e7e9cd6
1 changed files with 21 additions and 0 deletions

View File

@ -199,6 +199,16 @@ impl NewTileId for Game {
} }
} }
impl Game {
/// adds a tile safely and returns the new tile ID
fn add_tile(&mut self, mut tile: Tile) -> u64 {
let new_id = self.new_tile_id();
tile.id = new_id;
self.tiles.push(tile);
new_id
}
}
#[test] #[test]
fn test_game_from_string() { fn test_game_from_string() {
let output = Game::from( let output = Game::from(
@ -250,3 +260,14 @@ fn test_new_tile_id() {
assert_eq!(game.new_tile_id(), 10); assert_eq!(game.new_tile_id(), 10);
} }
#[test]
fn test_add_tile() {
let mut game = mock::game_default();
let new_id = game.add_tile(mock::tile_default());
assert_eq!(new_id, 0);
assert_eq!(game.tiles.len(), 2);
let new_id = game.add_tile(mock::tile_default());
assert_eq!(new_id, 1);
assert_eq!(game.tiles.len(), 3);
}