PkmnLib_rs/tests/common/test_case.rs

83 lines
2.4 KiB
Rust
Raw Normal View History

use super::test_step::TestStep;
use parking_lot::RwLock;
use pkmn_lib::defines::LevelInt;
use pkmn_lib::dynamic_data::libraries::dynamic_library::DynamicLibrary;
use pkmn_lib::dynamic_data::models::battle::Battle;
use pkmn_lib::dynamic_data::models::battle_party::BattleParty;
use pkmn_lib::dynamic_data::models::pokemon::Pokemon;
use pkmn_lib::dynamic_data::models::pokemon_builder::PokemonBuilder;
use pkmn_lib::dynamic_data::models::pokemon_party::PokemonParty;
use pkmn_lib::StringKey;
use serde::Deserialize;
use std::sync::Arc;
#[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 {
pub fn run_test(&self, library: &DynamicLibrary) {
let mut parties = Vec::new();
for party in &self.battle_setup.parties {
let pokemon = party
.pokemon
.iter()
.map(|a| Some(Arc::new(RwLock::new(a.to_pokemon(library)))))
.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 {
fn to_pokemon<'a>(&'a self, library: &'a DynamicLibrary) -> Pokemon {
let mut builder = PokemonBuilder::new(library, StringKey::new(self.species.as_str()), self.level);
for move_name in &self.moves {
builder = builder.learn_move(StringKey::new(move_name));
}
builder.build()
}
}