PkmnLib_rs/tests/main.rs

102 lines
2.9 KiB
Rust
Raw Normal View History

#![feature(custom_test_frameworks)]
#![feature(once_cell)]
#![test_runner(datatest::runner)]
use std::fs::File;
use std::io::Read;
use std::path::Path;
2022-08-20 10:22:12 +00:00
use std::sync::Arc;
2022-07-18 08:16:47 +00:00
use conquer_once::OnceCell;
2022-08-20 10:22:12 +00:00
use pkmn_lib::dynamic_data::{DynamicLibrary, MoveChoice, PokemonBuilder, ScriptCategory, TurnChoice};
use pkmn_lib::static_data::EffectParameter;
use crate::common::{library_loader, TestCase};
pub mod common;
static LIBRARY: OnceCell<Arc<DynamicLibrary>> = OnceCell::uninit();
2022-07-18 08:16:47 +00:00
fn get_library() -> Arc<DynamicLibrary> {
LIBRARY
.get_or_init(|| {
let start_time = chrono::Utc::now();
let lib = library_loader::load_library();
let end_time = chrono::Utc::now();
println!("Built library in {} ms", (end_time - start_time).num_milliseconds());
Arc::new(lib)
})
.clone()
2022-07-18 08:16:47 +00:00
}
#[test]
#[cfg_attr(miri, ignore)]
fn validate_library_load() {
2022-07-18 08:16:47 +00:00
let start_time = chrono::Utc::now();
2022-07-18 08:49:58 +00:00
library_loader::load_library();
2022-07-18 08:16:47 +00:00
let end_time = chrono::Utc::now();
println!("Built library in {} ms", (end_time - start_time).num_milliseconds());
}
#[datatest::files("tests/test_cases", {
input in r"^(.*)\.yaml"
})]
2022-06-17 18:38:00 +00:00
#[cfg_attr(miri, ignore)]
fn integration_tests(input: &Path) {
let mut str: String = "".to_string();
let mut file = File::open(input).unwrap();
file.read_to_string(&mut str).unwrap();
let test_case = serde_yaml::from_str::<TestCase>(&str).unwrap();
println!("\tRunning integration test {}", test_case.name);
2022-07-18 08:16:47 +00:00
test_case.run_test(get_library());
}
#[test]
#[cfg_attr(miri, ignore)]
fn validate_script() {
2022-08-20 10:22:12 +00:00
let lib = get_library();
2022-07-18 08:16:47 +00:00
let script = lib
.load_script(std::ptr::null(), ScriptCategory::Move, &"test".into())
2022-07-18 08:16:47 +00:00
.unwrap()
.unwrap();
2022-08-20 10:22:12 +00:00
let parameters = [EffectParameter::String("foo".into())];
script.on_initialize(&lib, &parameters);
script.on_initialize(&lib, &parameters);
script.on_initialize(&lib, &parameters);
}
#[test]
#[cfg_attr(miri, ignore)]
fn validate_script_2() {
let lib = get_library();
let script = lib
.load_script(std::ptr::null(), ScriptCategory::Move, &"test".into())
2022-08-20 10:22:12 +00:00
.unwrap()
.unwrap();
let user = Arc::new(
PokemonBuilder::new(lib, "charizard".into(), 100)
2022-08-20 10:22:12 +00:00
.learn_move("fire_blast".into())
.build(),
);
script.on_before_turn(&TurnChoice::Move(MoveChoice::new(
user.clone(),
user.learned_moves().read().get(0).unwrap().as_ref().unwrap().clone(),
0,
0,
)));
assert_eq!(user.current_health(), user.max_health() - 50);
let mut speed: u32 = 100;
script.change_speed(
&TurnChoice::Move(MoveChoice::new(
user.clone(),
user.learned_moves().read().get(0).unwrap().as_ref().unwrap().clone(),
0,
0,
)),
&mut speed,
);
assert_eq!(speed, 684);
}