PkmnLib_rs/tests/common/test_case.rs

85 lines
2.3 KiB
Rust
Raw Normal View History

use std::sync::Arc;
use serde::Deserialize;
use pkmn_lib::defines::LevelInt;
use pkmn_lib::dynamic_data::Battle;
use pkmn_lib::dynamic_data::BattleParty;
use pkmn_lib::dynamic_data::DynamicLibrary;
use pkmn_lib::dynamic_data::Pokemon;
use pkmn_lib::dynamic_data::PokemonBuilder;
use pkmn_lib::dynamic_data::PokemonParty;
use pkmn_lib::StringKey;
use super::test_step::TestStep;
#[derive(Deserialize)]
pub struct TestCase {
pub name: String,
battle_setup: TestBattleSetup,
actions: Vec<TestStep>,
}
#[derive(Deserialize)]
struct TestBattleSetup {
seed: u128,
can_flee: bool,
number_of_sides: u8,
pokemon_per_side: u8,
parties: Vec<TestParty>,
}
#[derive(Deserialize)]
struct TestParty {
indices: Vec<[u8; 2]>,
pokemon: Vec<TestPokemon>,
}
#[derive(Deserialize)]
struct TestPokemon {
species: String,
level: LevelInt,
moves: Vec<String>,
}
impl TestCase {
2022-12-24 11:00:50 +00:00
pub fn run_test(&self, library: Arc<dyn DynamicLibrary>) {
let mut parties = Vec::new();
for party in &self.battle_setup.parties {
let pokemon = party
.pokemon
.iter()
.map(|a| Some(Arc::new(a.to_pokemon(library.clone()))))
.collect();
let indices = party.indices.iter().map(|a| (a[0], a[1])).collect();
parties.push((Arc::new(PokemonParty::new_from_vec(pokemon)), indices));
}
let mut battle_parties = Vec::new();
for party in parties {
battle_parties.push(BattleParty::new(party.0.clone(), party.1));
}
let mut battle = Battle::new(
library,
battle_parties,
self.battle_setup.can_flee,
self.battle_setup.number_of_sides,
self.battle_setup.pokemon_per_side,
Some(self.battle_setup.seed),
);
for action in &self.actions {
action.execute(&mut battle);
}
}
}
impl TestPokemon {
2022-12-24 11:00:50 +00:00
fn to_pokemon(&self, library: Arc<dyn DynamicLibrary>) -> Pokemon {
2022-10-14 14:53:30 +00:00
let mut builder = PokemonBuilder::new(library, self.species.as_str().into(), self.level);
for move_name in &self.moves {
builder = builder.learn_move(StringKey::new(move_name));
}
2023-04-15 12:34:42 +00:00
builder.build().unwrap()
}
}