2022-06-17 17:53:33 +00:00
|
|
|
#![feature(custom_test_frameworks)]
|
2023-06-24 12:44:23 +00:00
|
|
|
#![feature(lazy_cell)]
|
2022-12-24 13:03:47 +00:00
|
|
|
#![allow(clippy::borrowed_box)]
|
2023-09-24 16:24:02 +00:00
|
|
|
#![allow(clippy::arc_with_non_send_sync)]
|
2022-06-17 17:53:33 +00:00
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
use std::sync::{Arc, LazyLock};
|
2022-06-17 17:53:33 +00:00
|
|
|
|
2022-09-16 09:01:37 +00:00
|
|
|
use pkmn_lib::dynamic_data::{
|
2024-04-07 16:55:41 +00:00
|
|
|
Battle, BattleParty, DamageSource, DynamicLibrary, ExecutingMove, MoveChoice, PassChoice, PokemonBuilder,
|
|
|
|
PokemonParty, ScriptCategory, ScriptContainer, ScriptOwnerData, TurnChoice, VolatileScriptsOwner,
|
2022-09-16 09:01:37 +00:00
|
|
|
};
|
2022-06-19 19:34:08 +00:00
|
|
|
|
2023-04-02 10:07:08 +00:00
|
|
|
use crate::common::library_loader;
|
2022-06-19 19:34:08 +00:00
|
|
|
|
2022-06-17 17:53:33 +00:00
|
|
|
pub mod common;
|
2023-04-02 10:07:08 +00:00
|
|
|
pub mod datatests;
|
2022-06-17 17:53:33 +00:00
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
static LIBRARY: LazyLock<Arc<dyn DynamicLibrary>> = LazyLock::new(|| library_loader::load_library().library);
|
|
|
|
|
2024-04-07 16:55:41 +00:00
|
|
|
fn get_library() -> Arc<dyn DynamicLibrary> { LIBRARY.clone() }
|
2022-06-17 17:53:33 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn validate_library_load() {
|
2022-07-18 08:16:47 +00:00
|
|
|
let start_time = chrono::Utc::now();
|
2022-12-25 10:26:14 +00:00
|
|
|
let result = library_loader::load_library();
|
2022-07-18 08:16:47 +00:00
|
|
|
let end_time = chrono::Utc::now();
|
2022-12-25 10:26:14 +00:00
|
|
|
println!(
|
|
|
|
"Built library in {} ms\
|
|
|
|
\n\t- Types load time: {} ms\
|
|
|
|
\n\t- Natures load time: {} ms\
|
|
|
|
\n\t- Items load time: {} ms\
|
|
|
|
\n\t- Growth Rate load time: {} ms\
|
|
|
|
\n\t- Abilities load time: {} ms\
|
|
|
|
\n\t- Moves load time: {} ms\
|
|
|
|
\n\t- Species load time: {} ms\
|
2024-04-07 16:55:41 +00:00
|
|
|
\n\t- Script load time: {} ms\
|
2022-12-25 10:26:14 +00:00
|
|
|
",
|
|
|
|
(end_time - start_time).num_milliseconds(),
|
|
|
|
result.types_load_time.num_milliseconds(),
|
|
|
|
result.natures_load_time.num_milliseconds(),
|
|
|
|
result.items_load_time.num_milliseconds(),
|
|
|
|
result.growth_rate_load_time.num_milliseconds(),
|
|
|
|
result.abilities_load_time.num_milliseconds(),
|
|
|
|
result.moves_load_time.num_milliseconds(),
|
|
|
|
result.species_load_time.num_milliseconds(),
|
|
|
|
result.wasm_load_time.num_milliseconds(),
|
|
|
|
);
|
2022-06-17 17:53:33 +00:00
|
|
|
}
|
|
|
|
|
2024-04-07 16:55:41 +00:00
|
|
|
#[test]
|
|
|
|
fn rune_test() {
|
|
|
|
let result = library_loader::load_library();
|
|
|
|
let library = result.library;
|
2024-05-08 13:46:09 +00:00
|
|
|
let p1 = PokemonBuilder::new(library.clone(), "charizard".into(), 100)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
|
2024-04-07 16:55:41 +00:00
|
|
|
let script = library
|
2024-05-08 13:46:09 +00:00
|
|
|
.load_script(
|
|
|
|
ScriptOwnerData::Pokemon(p1.weak()),
|
|
|
|
ScriptCategory::Move,
|
|
|
|
&"TestMove".into(),
|
|
|
|
)
|
2024-04-07 16:55:41 +00:00
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(script.name().unwrap().str(), "TestMove");
|
|
|
|
let p1 = PokemonBuilder::new(library.clone(), "charizard".into(), 100)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let turn_choice = Arc::new(TurnChoice::Pass(PassChoice::new(p1)));
|
|
|
|
let mut speed = 0;
|
|
|
|
script.change_speed(&turn_choice, &mut speed).unwrap();
|
|
|
|
assert_eq!(speed, 100);
|
|
|
|
}
|
|
|
|
|
2023-04-15 12:34:42 +00:00
|
|
|
#[test]
|
|
|
|
fn load_non_existing_wasm_script() {
|
|
|
|
let start_time = chrono::Utc::now();
|
|
|
|
let result = library_loader::load_library();
|
|
|
|
let end_time = chrono::Utc::now();
|
|
|
|
println!(
|
|
|
|
"Built library in {} ms\
|
|
|
|
\n\t- Types load time: {} ms\
|
|
|
|
\n\t- Natures load time: {} ms\
|
|
|
|
\n\t- Items load time: {} ms\
|
|
|
|
\n\t- Growth Rate load time: {} ms\
|
|
|
|
\n\t- Abilities load time: {} ms\
|
|
|
|
\n\t- Moves load time: {} ms\
|
|
|
|
\n\t- Species load time: {} ms\
|
2024-04-07 16:55:41 +00:00
|
|
|
\n\t- Script load time: {} ms\
|
2023-04-15 12:34:42 +00:00
|
|
|
",
|
|
|
|
(end_time - start_time).num_milliseconds(),
|
|
|
|
result.types_load_time.num_milliseconds(),
|
|
|
|
result.natures_load_time.num_milliseconds(),
|
|
|
|
result.items_load_time.num_milliseconds(),
|
|
|
|
result.growth_rate_load_time.num_milliseconds(),
|
|
|
|
result.abilities_load_time.num_milliseconds(),
|
|
|
|
result.moves_load_time.num_milliseconds(),
|
|
|
|
result.species_load_time.num_milliseconds(),
|
|
|
|
result.wasm_load_time.num_milliseconds(),
|
|
|
|
);
|
|
|
|
let script = result
|
|
|
|
.library
|
|
|
|
.load_script(ScriptOwnerData::None, ScriptCategory::Move, &"_____non_existing".into())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(script.is_none());
|
|
|
|
}
|
|
|
|
|
2022-09-16 09:01:37 +00:00
|
|
|
/// Assurance has the interesting properties that it creates a special data script internally, and
|
|
|
|
/// deletes that data script through the get_owner functionality.
|
|
|
|
#[test]
|
|
|
|
fn validate_assurance() {
|
|
|
|
let lib = get_library();
|
2023-06-22 13:43:41 +00:00
|
|
|
let p1 = PokemonBuilder::new(lib.clone(), "charizard".into(), 100)
|
|
|
|
.learn_move("assurance".into())
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let p2 = PokemonBuilder::new(lib.clone(), "venusaur".into(), 100)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let party1 = Arc::new(
|
|
|
|
BattleParty::new(
|
|
|
|
Arc::new(PokemonParty::new_from_vec(vec![Some(p1.clone())])),
|
|
|
|
vec![(0, 0)],
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2023-04-15 12:34:42 +00:00
|
|
|
);
|
2023-06-22 13:43:41 +00:00
|
|
|
let party2 = Arc::new(
|
|
|
|
BattleParty::new(
|
|
|
|
Arc::new(PokemonParty::new_from_vec(vec![Some(p2.clone())])),
|
|
|
|
vec![(1, 0)],
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-09-16 09:01:37 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let battle = Battle::new(lib.clone(), vec![party1, party2], false, 2, 1, None);
|
|
|
|
|
2023-04-15 17:33:29 +00:00
|
|
|
battle.sides()[0].set_pokemon(0, Some(p1.clone())).unwrap();
|
|
|
|
battle.sides()[1].set_pokemon(0, Some(p2.clone())).unwrap();
|
2022-09-16 09:01:37 +00:00
|
|
|
|
|
|
|
let script = lib
|
2023-07-09 09:17:43 +00:00
|
|
|
.load_script(
|
|
|
|
ScriptOwnerData::None,
|
|
|
|
ScriptCategory::Move,
|
|
|
|
&"double_power_if_target_damaged_in_turn".into(),
|
|
|
|
)
|
2022-09-16 09:01:37 +00:00
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mv = p1.learned_moves().read()[0].as_ref().unwrap().clone();
|
2023-06-22 13:43:41 +00:00
|
|
|
let choice = Arc::new(TurnChoice::Move(MoveChoice::new(p1.clone(), mv.clone(), 1, 0)));
|
2023-04-21 08:35:46 +00:00
|
|
|
script.on_before_turn(&choice).unwrap();
|
2023-07-09 09:17:43 +00:00
|
|
|
assert!(battle.sides()[1].has_volatile_script(&"double_power_if_target_damaged_in_turn_data".into()));
|
2022-09-16 09:01:37 +00:00
|
|
|
|
2023-06-22 13:43:41 +00:00
|
|
|
let executing_move = Arc::new(ExecutingMove::new(
|
2022-09-16 09:01:37 +00:00
|
|
|
vec![],
|
|
|
|
1,
|
2022-10-14 14:53:30 +00:00
|
|
|
p1,
|
2022-09-16 09:01:37 +00:00
|
|
|
mv.clone(),
|
|
|
|
mv.move_data().clone(),
|
|
|
|
ScriptContainer::default(),
|
2023-06-22 13:43:41 +00:00
|
|
|
));
|
2022-09-16 09:01:37 +00:00
|
|
|
let mut v = 20_u8;
|
2023-04-21 08:35:46 +00:00
|
|
|
script.change_base_power(&executing_move, &p2, 0, &mut v).unwrap();
|
2022-09-16 09:01:37 +00:00
|
|
|
assert_eq!(v, 20_u8);
|
|
|
|
|
2023-07-09 09:17:43 +00:00
|
|
|
let s = battle.sides()[1].get_volatile_script(&"double_power_if_target_damaged_in_turn_data".into());
|
2023-01-03 11:58:23 +00:00
|
|
|
let binding = s.as_ref().unwrap().get().unwrap().read();
|
|
|
|
let data_script = binding.as_ref().unwrap();
|
2022-09-16 09:01:37 +00:00
|
|
|
|
2023-04-21 08:35:46 +00:00
|
|
|
data_script.on_damage(&p2, DamageSource::Misc, 100, 50).unwrap();
|
2022-09-16 09:01:37 +00:00
|
|
|
|
|
|
|
let mut v = 20_u8;
|
2023-04-21 08:35:46 +00:00
|
|
|
script.change_base_power(&executing_move, &p2, 0, &mut v).unwrap();
|
2022-09-16 09:01:37 +00:00
|
|
|
assert_eq!(v, 40_u8);
|
|
|
|
|
2023-04-21 08:35:46 +00:00
|
|
|
data_script.on_end_turn().unwrap();
|
2023-07-09 09:17:43 +00:00
|
|
|
assert!(!battle.sides()[1].has_volatile_script(&"double_power_if_target_damaged_in_turn_data".into()));
|
2022-09-16 09:01:37 +00:00
|
|
|
}
|