Initial work on mocking so we can unit test.

This commit is contained in:
2022-08-17 18:05:38 +02:00
parent 45b16f415f
commit 98130706fb
39 changed files with 580 additions and 34 deletions

View File

@@ -2,10 +2,13 @@
name = "gen7_scripts"
version = "0.1.0"
authors = ["Deukhoofd <Deukhoofd@gmail.com>"]
edition = "2018"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
pkmn_lib_interface = { path = "../pkmn_lib_interface" }
pkmn_lib_interface = { path = "../pkmn_lib_interface" }
[dev-dependencies]
pkmn_lib_interface = { path = "../pkmn_lib_interface", features = ["mock_data"] }

View File

@@ -1,7 +1,7 @@
#![feature(inline_const)]
#![feature(inline_const_pat)]
#![feature(wasm_abi)]
#![no_std]
#![cfg_attr(not(test), no_std)]
#![allow(incomplete_features)]
extern crate alloc;
@@ -15,6 +15,7 @@ pub mod registered_scripts;
pub mod test_script;
#[no_mangle]
#[cfg(not(test))]
extern "wasm" fn _init() {
set_load_script_fn(Box::new(registered_scripts::get_script));
}

View File

@@ -1,6 +1,6 @@
use crate::test_script::TestScript;
use alloc::boxed::Box;
use pkmn_lib_interface::app_interface::{get_hash, StringKey};
use pkmn_lib_interface::app_interface::{get_hash_const, StringKey};
use pkmn_lib_interface::handling::{Script, ScriptCategory};
macro_rules! resolve_match {
@@ -12,7 +12,7 @@ macro_rules! resolve_match {
) => (
match $mid {
$(
const { get_hash($key) } => {
const { get_hash_const($key) } => {
return Some(Box::new($script {}))
}
)*

View File

@@ -1,6 +1,6 @@
use pkmn_lib_interface::app_interface::list::ImmutableList;
use pkmn_lib_interface::app_interface::{
get_hash, DamageSource, DataLibrary, DynamicLibrary, EffectParameter, TurnChoice,
get_hash_const, DamageSource, DataLibrary, DynamicLibrary, EffectParameter, TurnChoice,
};
use pkmn_lib_interface::dbg;
use pkmn_lib_interface::handling::{Script, ScriptCapabilities};
@@ -31,14 +31,13 @@ impl Script for TestScript {
) {
let l = library.data_library();
let ml = l.move_library();
let m = ml.get_by_hash(const { get_hash(b"tackle") }).unwrap();
let m = ml.get_by_hash(const { get_hash_const(b"tackle") }).unwrap();
dbg!("found move!");
dbg!("{:?} has {} base power", m.name().str(), m.base_power());
dbg!(
"Found a parameter with value: {}",
parameters.unwrap().get(0).unwrap()
);
if m.has_flag(b"foo") {}
}
fn on_before_turn(&self, choice: TurnChoice) {
@@ -46,7 +45,7 @@ impl Script for TestScript {
"On before turn for user: {}",
choice.user().species().name()
);
choice.user().damage(50, DamageSource::Misc);
// choice.user().damage(50, DamageSource::Misc);
if let TurnChoice::Move(d) = choice {
dbg!(
"On before turn for move choice: {}",
@@ -55,3 +54,49 @@ impl Script for TestScript {
}
}
}
#[cfg(test)]
mod test {
use super::*;
use pkmn_lib_interface::app_interface::move_library::MoveLibrary;
use pkmn_lib_interface::app_interface::species_library::SpeciesLibrary;
use pkmn_lib_interface::app_interface::type_library::TypeLibrary;
use pkmn_lib_interface::app_interface::{
Item, ItemLibrary, LibrarySettings, MoveCategory, MoveData, MoveTarget, StaticData,
};
use pkmn_lib_interface::handling::Script;
#[test]
fn test_foo() {
let item = Item::mock();
assert_eq!(item.name().str().to_str().unwrap(), "test");
let script = TestScript::new();
assert_eq!(script.get_name(), "TestScript");
let lib = DynamicLibrary::new(StaticData::mock(
MoveLibrary::mock(),
ItemLibrary::mock(),
SpeciesLibrary::mock(),
TypeLibrary::mock(),
LibrarySettings::mock(100),
));
lib.data_library().move_library().insert(
const { get_hash_const(b"tackle") },
MoveData::mock(
"tackle",
0,
MoveCategory::Physical,
60,
100,
10,
MoveTarget::Adjacent,
0,
),
);
script.on_initialize(
&lib,
Some(ImmutableList::mock((&[EffectParameter::Int(100)]).to_vec())),
);
}
}