refactor: Extract shared test setup to common module
This commit is contained in:
69
tests/common/mod.rs
Normal file
69
tests/common/mod.rs
Normal 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
|
||||
}
|
||||
@@ -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::<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
|
||||
}
|
||||
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::<Inventory>();
|
||||
assert!(inventory.items.is_empty());
|
||||
}
|
||||
|
||||
|
||||
@@ -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::<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
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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::<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
|
||||
}
|
||||
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() {
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user