From 280e7e9cd6c80e8cd467f145934b276a2c90f61c Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Mon, 13 Apr 2020 17:44:51 +0100 Subject: [PATCH] function to safely add a tile --- src/game.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/game.rs b/src/game.rs index c37d014..0c209d0 100644 --- a/src/game.rs +++ b/src/game.rs @@ -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] fn test_game_from_string() { let output = Game::from( @@ -250,3 +260,14 @@ fn test_new_tile_id() { 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); +}