diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 0000000..6e20f7e --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,69 @@ +use bevy::prelude::*; +use pomomon_garden::features::config::components::{BerrySeedConfig, GameConfig}; +use pomomon_garden::features::grid::components::{Grid, Tile, TileState}; +use pomomon_garden::features::inventory::components::{Inventory, ItemStack, ItemType}; + +pub fn setup_app( + grid_width: u32, + grid_height: u32, + initial_tile_states: &[(u32, u32, TileState)], + initial_inventory: Vec<(ItemType, u32)>, + seed_configs: Option>, +) -> App { + let mut app = App::new(); + app.add_plugins(MinimalPlugins); + app.add_plugins(AssetPlugin::default()); + + // Grid Setup + let mut grid_tiles = Vec::with_capacity(grid_width as usize); + for x in 0..grid_width { + let mut column = Vec::with_capacity(grid_height as usize); + for y in 0..grid_height { + let entity = app + .world_mut() + .spawn((Tile { x, y }, TileState::Unclaimed)) + .id(); + column.push(entity); + } + grid_tiles.push(column); + } + app.insert_resource(Grid { + width: grid_width, + height: grid_height, + tiles: grid_tiles, + }); + + for &(x, y, ref state) in initial_tile_states { + if let Ok(entity) = app.world().resource::().get_tile((x, y)) { + *app.world_mut().get_mut::(entity).unwrap() = state.clone(); + } + } + + // Inventory Setup + let mut inventory_items = Vec::new(); + for (item_type, amount) in initial_inventory { + let id = app.world_mut().spawn(ItemStack { item_type, amount }).id(); + inventory_items.push(id); + } + app.insert_resource(Inventory { + items: inventory_items, + }); + + // Game Config + let seeds = seed_configs.unwrap_or_else(|| { + vec![BerrySeedConfig { + name: "TestSeed".to_string(), + cost: 1, + grants: 1, + slice: "seed.aseprite".to_string(), + growth_stages: 2, + }] + }); + + app.insert_resource(GameConfig { + berry_seeds: seeds, + ..Default::default() + }); + + app +} diff --git a/tests/harvest.rs b/tests/harvest.rs index 675ad5a..7b8e68f 100644 --- a/tests/harvest.rs +++ b/tests/harvest.rs @@ -1,61 +1,12 @@ use bevy::ecs::system::RunSystemOnce; use pomomon_garden::features::config::components::{BerrySeedConfig, GameConfig}; -use pomomon_garden::features::grid::components::{Grid, Tile, TileState}; +use pomomon_garden::features::grid::components::{Grid, TileState}; use pomomon_garden::features::inventory::components::{Inventory, ItemStack, ItemType}; use pomomon_garden::features::pom::actions::InteractionAction; use pomomon_garden::prelude::*; -fn setup_harvest_app( - grid_width: u32, - grid_height: u32, - initial_tile_states: &[(u32, u32, TileState)], -) -> App { - let mut app = App::new(); - app.add_plugins(MinimalPlugins); - app.add_plugins(AssetPlugin::default()); - - // GameConfig - app.insert_resource(GameConfig { - berry_seeds: vec![BerrySeedConfig { - name: "TestSeed".into(), - cost: 1, - grants: 5, - slice: "".into(), - growth_stages: 2, - }], - ..Default::default() - }); - - // Inventory - app.init_resource::(); - - // Grid - let mut grid_tiles = Vec::with_capacity(grid_width as usize); - for x in 0..grid_width { - let mut column = Vec::with_capacity(grid_height as usize); - for y in 0..grid_height { - let entity = app - .world_mut() - .spawn((Tile { x, y }, TileState::Unclaimed)) - .id(); - column.push(entity); - } - grid_tiles.push(column); - } - app.insert_resource(Grid { - width: grid_width, - height: grid_height, - tiles: grid_tiles, - }); - - for &(x, y, ref state) in initial_tile_states { - if let Ok(entity) = app.world().resource::().get_tile((x, y)) { - *app.world_mut().get_mut::(entity).unwrap() = state.clone(); - } - } - - app -} +mod common; +use common::setup_app; #[test] fn test_harvest_fully_grown() { @@ -74,7 +25,15 @@ fn test_harvest_fully_grown() { }, )]; - let mut app = setup_harvest_app(1, 1, &initial_states); + let seed_config = BerrySeedConfig { + name: "TestSeed".into(), + cost: 1, + grants: 5, + slice: "".into(), + growth_stages: 2, + }; + + let mut app = setup_app(1, 1, &initial_states, vec![], Some(vec![seed_config])); let _ = app.world_mut().run_system_once( |mut commands: Commands, @@ -130,7 +89,15 @@ fn test_harvest_withered() { }, )]; - let mut app = setup_harvest_app(1, 1, &initial_states); + let seed_config = BerrySeedConfig { + name: "TestSeed".into(), + cost: 1, + grants: 5, + slice: "".into(), + growth_stages: 2, + }; + + let mut app = setup_app(1, 1, &initial_states, vec![], Some(vec![seed_config])); let _ = app.world_mut().run_system_once( |mut commands: Commands, @@ -181,7 +148,15 @@ fn test_cannot_harvest_growing() { }, )]; - let mut app = setup_harvest_app(1, 1, &initial_states); + let seed_config = BerrySeedConfig { + name: "TestSeed".into(), + cost: 1, + grants: 5, + slice: "".into(), + growth_stages: 2, + }; + + let mut app = setup_app(1, 1, &initial_states, vec![], Some(vec![seed_config])); let _ = app.world_mut().run_system_once( |mut commands: Commands, @@ -219,3 +194,4 @@ fn test_cannot_harvest_growing() { let inventory = app.world().resource::(); assert!(inventory.items.is_empty()); } + diff --git a/tests/planting.rs b/tests/planting.rs index e6bd65b..1c2a0b3 100644 --- a/tests/planting.rs +++ b/tests/planting.rs @@ -1,78 +1,24 @@ use bevy::ecs::system::RunSystemOnce; -use pomomon_garden::features::config::components::BerrySeedConfig; -use pomomon_garden::features::grid::components::{Grid, Tile, TileState}; +use pomomon_garden::features::config::components::GameConfig; +use pomomon_garden::features::grid::components::{Grid, TileState}; use pomomon_garden::features::inventory::components::{Inventory, ItemStack, ItemType}; use pomomon_garden::features::pom::actions::InteractionAction; use pomomon_garden::prelude::*; -fn setup_interaction_app( - grid_width: u32, - grid_height: u32, - initial_tile_states: &[(u32, u32, TileState)], - initial_inventory: Vec<(ItemType, u32)>, -) -> App { - let mut app = App::new(); - app.add_plugins(MinimalPlugins); - app.add_plugins(AssetPlugin::default()); // Needed for asset server if used, though we mock or avoid visuals here - - // Grid Setup - let mut grid_tiles = Vec::with_capacity(grid_width as usize); - for x in 0..grid_width { - let mut column = Vec::with_capacity(grid_height as usize); - for y in 0..grid_height { - let entity = app - .world_mut() - .spawn((Tile { x, y }, TileState::Unclaimed)) - .id(); - column.push(entity); - } - grid_tiles.push(column); - } - app.world_mut().insert_resource(Grid { - width: grid_width, - height: grid_height, - tiles: grid_tiles, - }); - - for &(x, y, ref state) in initial_tile_states { - if let Ok(entity) = app.world().resource::().get_tile((x, y)) { - *app.world_mut().get_mut::(entity).unwrap() = state.clone(); - } - } - - // Inventory Setup - let mut inventory_items = Vec::new(); - for (item_type, amount) in initial_inventory { - let id = app.world_mut().spawn(ItemStack { item_type, amount }).id(); - inventory_items.push(id); - } - app.world_mut().insert_resource(Inventory { - items: inventory_items, - }); - - let mut game_config = GameConfig::default(); - game_config.berry_seeds.push(BerrySeedConfig { - name: "TestSeed".to_string(), - cost: 1, - grants: 1, - slice: "seed.aseprite".to_string(), - growth_stages: 2, - }); - app.insert_resource(game_config); - - app -} +mod common; +use common::setup_app; #[test] fn test_plant_seed_interaction() { let seed_type = ItemType::BerrySeed { name: "TestSeed".into(), }; - let mut app = setup_interaction_app( + let mut app = setup_app( 3, 3, &[(1, 1, TileState::Empty)], vec![(seed_type.clone(), 1)], + None, ); let _ = app.world_mut().run_system_once( @@ -138,11 +84,12 @@ fn test_plant_seed_no_inventory() { let seed_type = ItemType::BerrySeed { name: "TestSeed".into(), }; - let mut app = setup_interaction_app( + let mut app = setup_app( 3, 3, &[(1, 1, TileState::Empty)], vec![], // Empty inventory + None, ); let _ = app.world_mut().run_system_once( @@ -178,3 +125,4 @@ fn test_plant_seed_no_inventory() { panic!("Tile should remain Empty, found {:?}", tile_state); } } + diff --git a/tests/watering.rs b/tests/watering.rs index 7d5eb8a..9729cda 100644 --- a/tests/watering.rs +++ b/tests/watering.rs @@ -1,74 +1,19 @@ use bevy::ecs::system::RunSystemOnce; -use pomomon_garden::features::config::components::BerrySeedConfig; -use pomomon_garden::features::grid::components::{Grid, Tile, TileState}; +use pomomon_garden::features::config::components::GameConfig; +use pomomon_garden::features::grid::components::{Grid, TileState}; use pomomon_garden::features::inventory::components::{Inventory, ItemStack, ItemType}; use pomomon_garden::features::pom::actions::InteractionAction; use pomomon_garden::prelude::*; -fn setup_interaction_app( - grid_width: u32, - grid_height: u32, - initial_tile_states: &[(u32, u32, TileState)], - initial_inventory: Vec<(ItemType, u32)>, -) -> App { - let mut app = App::new(); - app.add_plugins(MinimalPlugins); - app.add_plugins(AssetPlugin::default()); // Needed for asset server if used, though we mock or avoid visuals here - - // Grid Setup - let mut grid_tiles = Vec::with_capacity(grid_width as usize); - for x in 0..grid_width { - let mut column = Vec::with_capacity(grid_height as usize); - for y in 0..grid_height { - let entity = app - .world_mut() - .spawn((Tile { x, y }, TileState::Unclaimed)) - .id(); - column.push(entity); - } - grid_tiles.push(column); - } - app.world_mut().insert_resource(Grid { - width: grid_width, - height: grid_height, - tiles: grid_tiles, - }); - - for &(x, y, ref state) in initial_tile_states { - if let Ok(entity) = app.world().resource::().get_tile((x, y)) { - *app.world_mut().get_mut::(entity).unwrap() = state.clone(); - } - } - - // Inventory Setup - let mut inventory_items = Vec::new(); - for (item_type, amount) in initial_inventory { - let id = app.world_mut().spawn(ItemStack { item_type, amount }).id(); - inventory_items.push(id); - } - app.world_mut().insert_resource(Inventory { - items: inventory_items, - }); - - let mut game_config = GameConfig::default(); - game_config.berry_seeds.push(BerrySeedConfig { - name: "TestSeed".to_string(), - cost: 1, - grants: 1, - slice: "seed.aseprite".to_string(), - growth_stages: 2, - }); - app.insert_resource(game_config); - - app -} +mod common; +use common::setup_app; #[test] fn test_water_crop() { let seed_type = ItemType::BerrySeed { name: "TestSeed".into(), }; - let mut app = setup_interaction_app( + let mut app = setup_app( 3, 3, &[( @@ -83,6 +28,7 @@ fn test_water_crop() { }, )], vec![], + None, ); // Verify Water option is available @@ -157,3 +103,4 @@ fn test_water_crop() { }, ); } +