refactor: Extract shared test setup to common module

This commit is contained in:
demenik
2025-12-03 22:45:59 +01:00
parent a134851bd2
commit 5605d39f56
4 changed files with 116 additions and 176 deletions

69
tests/common/mod.rs Normal file
View File

@@ -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<Vec<BerrySeedConfig>>,
) -> 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::<Grid>().get_tile((x, y)) {
*app.world_mut().get_mut::<TileState>(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
}

View File

@@ -1,61 +1,12 @@
use bevy::ecs::system::RunSystemOnce; use bevy::ecs::system::RunSystemOnce;
use pomomon_garden::features::config::components::{BerrySeedConfig, GameConfig}; 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::inventory::components::{Inventory, ItemStack, ItemType};
use pomomon_garden::features::pom::actions::InteractionAction; use pomomon_garden::features::pom::actions::InteractionAction;
use pomomon_garden::prelude::*; use pomomon_garden::prelude::*;
fn setup_harvest_app( mod common;
grid_width: u32, use common::setup_app;
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::<Inventory>();
// 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::<Grid>().get_tile((x, y)) {
*app.world_mut().get_mut::<TileState>(entity).unwrap() = state.clone();
}
}
app
}
#[test] #[test]
fn test_harvest_fully_grown() { 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( let _ = app.world_mut().run_system_once(
|mut commands: Commands, |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( let _ = app.world_mut().run_system_once(
|mut commands: Commands, |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( let _ = app.world_mut().run_system_once(
|mut commands: Commands, |mut commands: Commands,
@@ -219,3 +194,4 @@ fn test_cannot_harvest_growing() {
let inventory = app.world().resource::<Inventory>(); let inventory = app.world().resource::<Inventory>();
assert!(inventory.items.is_empty()); assert!(inventory.items.is_empty());
} }

View File

@@ -1,78 +1,24 @@
use bevy::ecs::system::RunSystemOnce; use bevy::ecs::system::RunSystemOnce;
use pomomon_garden::features::config::components::BerrySeedConfig; use pomomon_garden::features::config::components::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::inventory::components::{Inventory, ItemStack, ItemType};
use pomomon_garden::features::pom::actions::InteractionAction; use pomomon_garden::features::pom::actions::InteractionAction;
use pomomon_garden::prelude::*; use pomomon_garden::prelude::*;
fn setup_interaction_app( mod common;
grid_width: u32, use common::setup_app;
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::<Grid>().get_tile((x, y)) {
*app.world_mut().get_mut::<TileState>(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
}
#[test] #[test]
fn test_plant_seed_interaction() { fn test_plant_seed_interaction() {
let seed_type = ItemType::BerrySeed { let seed_type = ItemType::BerrySeed {
name: "TestSeed".into(), name: "TestSeed".into(),
}; };
let mut app = setup_interaction_app( let mut app = setup_app(
3, 3,
3, 3,
&[(1, 1, TileState::Empty)], &[(1, 1, TileState::Empty)],
vec![(seed_type.clone(), 1)], vec![(seed_type.clone(), 1)],
None,
); );
let _ = app.world_mut().run_system_once( let _ = app.world_mut().run_system_once(
@@ -138,11 +84,12 @@ fn test_plant_seed_no_inventory() {
let seed_type = ItemType::BerrySeed { let seed_type = ItemType::BerrySeed {
name: "TestSeed".into(), name: "TestSeed".into(),
}; };
let mut app = setup_interaction_app( let mut app = setup_app(
3, 3,
3, 3,
&[(1, 1, TileState::Empty)], &[(1, 1, TileState::Empty)],
vec![], // Empty inventory vec![], // Empty inventory
None,
); );
let _ = app.world_mut().run_system_once( 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); panic!("Tile should remain Empty, found {:?}", tile_state);
} }
} }

View File

@@ -1,74 +1,19 @@
use bevy::ecs::system::RunSystemOnce; use bevy::ecs::system::RunSystemOnce;
use pomomon_garden::features::config::components::BerrySeedConfig; use pomomon_garden::features::config::components::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::inventory::components::{Inventory, ItemStack, ItemType};
use pomomon_garden::features::pom::actions::InteractionAction; use pomomon_garden::features::pom::actions::InteractionAction;
use pomomon_garden::prelude::*; use pomomon_garden::prelude::*;
fn setup_interaction_app( mod common;
grid_width: u32, use common::setup_app;
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::<Grid>().get_tile((x, y)) {
*app.world_mut().get_mut::<TileState>(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
}
#[test] #[test]
fn test_water_crop() { fn test_water_crop() {
let seed_type = ItemType::BerrySeed { let seed_type = ItemType::BerrySeed {
name: "TestSeed".into(), name: "TestSeed".into(),
}; };
let mut app = setup_interaction_app( let mut app = setup_app(
3, 3,
3, 3,
&[( &[(
@@ -83,6 +28,7 @@ fn test_water_crop() {
}, },
)], )],
vec![], vec![],
None,
); );
// Verify Water option is available // Verify Water option is available
@@ -157,3 +103,4 @@ fn test_water_crop() {
}, },
); );
} }