PkmnLib_rs/tests/common/test_step.rs

83 lines
2.7 KiB
Rust
Raw Normal View History

use super::data_getter::TestDataGetter;
use pkmn_lib::dynamic_data::choices::{MoveChoice, PassChoice, TurnChoice};
use pkmn_lib::dynamic_data::models::battle::Battle;
use pkmn_lib::StringKey;
use serde::Deserialize;
#[derive(Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TestStep {
SetPokemon {
place: [u8; 2],
from_party: [u8; 2],
},
SetMoveChoice {
#[serde(rename = "for")]
for_pokemon: [u8; 2],
#[serde(rename = "move")]
use_move: String,
target: [u8; 2],
},
SetPassChoice {
#[serde(rename = "for")]
for_pokemon: [u8; 2],
},
Assert {
value: TestDataGetter,
expected: String,
},
}
impl TestStep {
pub fn execute(&self, battle: &mut Battle) {
match self {
TestStep::SetPokemon { place, from_party } => {
let p = battle.parties()[from_party[0] as usize]
.get_pokemon(from_party[1] as usize)
.clone();
battle.sides_mut()[place[0] as usize].set_pokemon(place[1], p);
}
TestStep::SetMoveChoice {
for_pokemon,
use_move,
target,
} => {
let p = battle.sides()[for_pokemon[0] as usize].pokemon()[for_pokemon[1] as usize]
.as_ref()
.unwrap()
.clone();
let mut used_move = None;
let pokemon_guard = p.read();
for learned_move in pokemon_guard.learned_moves().iter().flatten() {
if learned_move.read().move_data().name() == &StringKey::new(use_move) {
used_move = Some(learned_move.clone());
break;
}
}
assert!(used_move.is_some());
drop(pokemon_guard);
assert!(battle
.try_set_choice(TurnChoice::Move(MoveChoice::new(
p,
used_move.unwrap(),
target[0],
target[1],
)))
.unwrap());
}
TestStep::SetPassChoice { for_pokemon } => {
let p = battle.sides()[for_pokemon[0] as usize].pokemon()[for_pokemon[1] as usize]
.as_ref()
.unwrap()
.clone();
assert!(battle.try_set_choice(TurnChoice::Pass(PassChoice::new(p))).unwrap());
}
TestStep::Assert { value, expected } => {
let v = value.get(battle);
assert_eq!(&v, expected)
}
}
}
}