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

@@ -21,6 +21,7 @@ pub struct Battle {
}
impl Battle {
#[cfg(not(feature = "mock_data"))]
pub fn new(reference: ExternRef<Battle>) -> Self {
Self::from_ref(reference, &|reference| Self {
inner: Rc::new(BattleInner {
@@ -39,6 +40,7 @@ impl Battle {
pub fn sides(&self) -> ImmutableList<BattleSide>;
}
#[cfg(not(feature = "mock_data"))]
pub fn get_pokemon(&self, side: u8, index: u8) -> Option<Pokemon> {
unsafe { battle_get_pokemon(self.inner.reference, side, index).get_value() }
}
@@ -57,12 +59,14 @@ wasm_value_getters! {
crate::handling::cacheable::cacheable!(Battle);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for Battle {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn battle_get_library(r: ExternRef<Battle>) -> ExternRef<DynamicLibrary>;
fn battle_get_parties(r: ExternRef<Battle>) -> VecExternRef<BattleParty>;

View File

@@ -15,6 +15,7 @@ pub struct BattleParty {
}
impl BattleParty {
#[cfg(not(feature = "mock_data"))]
pub fn new(reference: ExternRef<BattleParty>) -> Self {
Self::from_ref(reference, &|reference| Self {
inner: Rc::new(BattlePartyInner {
@@ -31,12 +32,15 @@ impl BattleParty {
crate::handling::cacheable::cacheable!(BattleParty);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for BattleParty {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn battle_party_get_party(r: ExternRef<BattleParty>) -> ExternRef<Party>;
}

View File

@@ -6,12 +6,15 @@ pub struct BattleRandom {
}
impl BattleRandom {
#[cfg(not(feature = "mock_data"))]
pub fn get(&self) -> i32 {
unsafe { battle_random_get(self.reference) }
}
#[cfg(not(feature = "mock_data"))]
pub fn get_max(&self, max: i32) -> i32 {
unsafe { battle_random_get_max(self.reference, max) }
}
#[cfg(not(feature = "mock_data"))]
pub fn get_between(&self, min: i32, max: i32) -> i32 {
unsafe { battle_random_get_between(self.reference, min, max) }
}
@@ -24,6 +27,7 @@ impl ExternalReferenceType for BattleRandom {
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn battle_random_get(r: ExternRef<BattleRandom>) -> i32;
fn battle_random_get_max(r: ExternRef<BattleRandom>, max: i32) -> i32;

View File

@@ -19,6 +19,7 @@ pub struct BattleSide {
}
impl BattleSide {
#[cfg(not(feature = "mock_data"))]
pub fn new(reference: ExternRef<Self>) -> Self {
Self::from_ref(reference, &|reference| Self {
inner: Rc::new(BattleSideInner {
@@ -36,6 +37,7 @@ impl BattleSide {
pub fn battle(&self) -> Battle;
}
#[cfg(not(feature = "mock_data"))]
pub fn get_pokemon(&self, index: usize) -> Option<Pokemon> {
unsafe { battleside_get_pokemon(self.inner.reference, index).get_value() }
}
@@ -49,12 +51,14 @@ wasm_value_getters! {
crate::handling::cacheable::cacheable!(BattleSide);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for BattleSide {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn battleside_get_side_index(r: ExternRef<BattleSide>) -> u8;
fn battleside_get_pokemon_per_side(r: ExternRef<BattleSide>) -> u8;

View File

@@ -17,6 +17,7 @@ pub struct DynamicLibrary {
crate::handling::cacheable::cacheable!(DynamicLibrary);
impl DynamicLibrary {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(ptr: ExternRef<Self>) -> Self {
Self::from_ref(ptr, &|ptr| Self {
inner: Rc::new(DynamicLibraryInner {
@@ -28,17 +29,29 @@ impl DynamicLibrary {
})
}
#[cfg(feature = "mock_data")]
pub fn new(data: StaticData) -> Self {
Self {
inner: Rc::new(DynamicLibraryInner {
ptr: ExternRef::mock(),
static_data: data.into(),
}),
}
}
pub fn data_library(&self) -> StaticData {
self.inner.static_data.value()
}
}
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for DynamicLibrary {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
DynamicLibrary::new(reference)
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn dynamic_library_get_static_data(ptr: ExternRef<DynamicLibrary>) -> ExternRef<StaticData>;
}

View File

@@ -30,6 +30,7 @@ pub struct LearnedMove {
crate::handling::cacheable::cacheable!(LearnedMove);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for LearnedMove {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
@@ -37,6 +38,7 @@ impl ExternalReferenceType for LearnedMove {
}
impl LearnedMove {
#[cfg(not(feature = "mock_data"))]
pub fn new(reference: ExternRef<Self>) -> Self {
Self::from_ref(reference, &|reference| Self {
inner: Rc::new(LearnedMoveInner {
@@ -54,12 +56,14 @@ impl LearnedMove {
pub fn learn_method(&self) -> MoveLearnMethod;
}
#[cfg(not(feature = "mock_data"))]
pub fn restore_all_uses(&self) {
unsafe {
learned_move_restore_all_uses(self.inner.reference);
}
}
#[cfg(not(feature = "mock_data"))]
pub fn restore_uses(&self, uses: u8) {
unsafe {
learned_move_restore_uses(self.inner.reference, uses);
@@ -73,6 +77,7 @@ wasm_value_getters! {
pub fn remaining_pp(&self) -> u8;
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn learned_move_get_move_data(r: ExternRef<LearnedMove>) -> ExternRef<MoveData>;
fn learned_move_get_learn_method(r: ExternRef<LearnedMove>) -> MoveLearnMethod;

View File

@@ -11,6 +11,7 @@ impl Party {
Self { reference }
}
#[cfg(not(feature = "mock_data"))]
pub fn get_pokemon(&self, index: usize) -> Option<Pokemon> {
unsafe { party_get_pokemon(self.reference, index).get_value() }
}
@@ -22,6 +23,7 @@ impl ExternalReferenceType for Party {
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn party_get_pokemon(r: ExternRef<Party>, index: usize) -> ExternRef<Pokemon>;
}

View File

@@ -34,6 +34,7 @@ pub struct Pokemon {
}
impl Pokemon {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(reference: ExternRef<Self>) -> Self {
Self::from_ref(reference, &|reference| Self {
inner: Rc::new(PokemonInner {
@@ -69,33 +70,41 @@ impl Pokemon {
pub fn effort_values(&self) -> ClampedStatisticSet<u8>;
}
#[cfg(not(feature = "mock_data"))]
pub fn has_held_item(&self, name: &str) -> bool {
let cstr = CString::new(name).unwrap();
unsafe { pokemon_has_held_item(self.inner.reference, cstr.as_ptr()) }
}
#[cfg(not(feature = "mock_data"))]
pub fn set_held_item(&self, item: &Item) -> Option<Item> {
unsafe { pokemon_set_held_item(self.inner.reference, item.reference()).get_value() }
}
#[cfg(not(feature = "mock_data"))]
pub fn remove_held_item(&self) -> Option<Item> {
unsafe { pokemon_remove_held_item(self.inner.reference).get_value() }
}
#[cfg(not(feature = "mock_data"))]
pub fn consume_held_item(&self) -> bool {
unsafe { pokemon_consume_held_item(self.inner.reference) }
}
#[cfg(not(feature = "mock_data"))]
pub fn max_health(&self) -> u32 {
self.boosted_stats().hp()
}
#[cfg(not(feature = "mock_data"))]
pub fn get_type(&self, index: usize) -> u8 {
unsafe { pokemon_get_type(self.inner.reference, index) }
}
#[cfg(not(feature = "mock_data"))]
pub fn has_type(&self, type_identifier: u8) -> bool {
unsafe { pokemon_has_type(self.inner.reference, type_identifier) }
}
#[cfg(not(feature = "mock_data"))]
pub fn has_type_by_name(&self, type_name: &str) -> bool {
let type_identifier = self
.library()
@@ -107,9 +116,12 @@ impl Pokemon {
}
false
}
#[cfg(not(feature = "mock_data"))]
pub fn get_learned_move(&self, index: usize) -> Option<LearnedMove> {
unsafe { pokemon_get_learned_move(self.inner.reference, index).get_value() }
}
#[cfg(not(feature = "mock_data"))]
pub fn change_stat_boost(
&self,
stat: Statistic,
@@ -121,16 +133,19 @@ impl Pokemon {
}
}
#[cfg(not(feature = "mock_data"))]
pub fn ability_script(&self) -> Option<&Box<dyn Script>> {
unsafe { pokemon_get_ability_script(self.inner.reference).as_ref() }
}
#[cfg(not(feature = "mock_data"))]
pub fn change_species(&self, species: Species, form: Form) {
unsafe {
pokemon_change_species(self.inner.reference, species.reference(), form.reference());
}
}
#[cfg(not(feature = "mock_data"))]
pub fn change_form(&self, form: Form) {
unsafe {
pokemon_change_form(self.inner.reference, form.reference());
@@ -141,15 +156,18 @@ impl Pokemon {
self.current_health() == 0
}
#[cfg(not(feature = "mock_data"))]
pub fn damage(&self, damage: u32, source: DamageSource) {
unsafe { pokemon_damage(self.inner.reference, damage, source) }
}
#[cfg(not(feature = "mock_data"))]
pub fn heal(&self, amount: u32, allow_revive: bool) -> bool {
unsafe { pokemon_heal(self.inner.reference, amount, allow_revive) }
}
}
#[cfg(not(feature = "mock_data"))]
wasm_reference_getters! {
Pokemon,
pub fn species(&self) -> Species;
@@ -158,6 +176,7 @@ wasm_reference_getters! {
pub fn nature(&self) -> Nature;
}
#[cfg(not(feature = "mock_data"))]
wasm_optional_reference_getters! {
Pokemon,
pub fn display_species(&self) -> Option<Species>;
@@ -166,6 +185,7 @@ wasm_optional_reference_getters! {
pub fn battle(&self) -> Option<Battle>;
}
#[cfg(not(feature = "mock_data"))]
wasm_value_getters! {
Pokemon,
pub fn level(&self) -> LevelInt;
@@ -198,12 +218,14 @@ pub enum DamageSource {
crate::handling::cacheable::cacheable!(Pokemon);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for Pokemon {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn pokemon_get_library(r: ExternRef<Pokemon>) -> ExternRef<DynamicLibrary>;
fn pokemon_get_flat_stats(r: ExternRef<Pokemon>) -> ExternRef<StatisticSet<u32>>;
@@ -234,3 +256,13 @@ extern "wasm" {
fn pokemon_damage(r: ExternRef<Pokemon>, damage: u32, source: DamageSource);
fn pokemon_heal(r: ExternRef<Pokemon>, amount: u32, allow_revive: bool) -> bool;
}
#[cfg(feature = "mock_data")]
impl Pokemon {
pub fn current_health(&self) -> u32 {
unimplemented!()
}
pub fn species(&self) -> Species {
unimplemented!()
}
}

View File

@@ -28,25 +28,32 @@ where
}
}
#[cfg(not(feature = "mock_data"))]
pub fn hp(&self) -> T {
self.get_stat(Statistic::HP)
}
#[cfg(not(feature = "mock_data"))]
pub fn attack(&self) -> T {
self.get_stat(Statistic::Attack)
}
#[cfg(not(feature = "mock_data"))]
pub fn defense(&self) -> T {
self.get_stat(Statistic::Defense)
}
#[cfg(not(feature = "mock_data"))]
pub fn special_attack(&self) -> T {
self.get_stat(Statistic::SpecialAttack)
}
#[cfg(not(feature = "mock_data"))]
pub fn special_defense(&self) -> T {
self.get_stat(Statistic::SpecialDefense)
}
#[cfg(not(feature = "mock_data"))]
pub fn speed(&self) -> T {
self.get_stat(Statistic::Speed)
}
#[cfg(not(feature = "mock_data"))]
pub fn get_stat(&self, stat: Statistic) -> T {
unsafe {
statistic_set_get(self.reference.cast(), stat)
@@ -55,15 +62,18 @@ where
}
}
#[cfg(not(feature = "mock_data"))]
pub fn set_stat(&self, stat: Statistic, value: T) {
unsafe { statistic_set_set(self.reference.cast(), stat, value.try_into().unwrap()) }
}
#[cfg(not(feature = "mock_data"))]
pub fn increase_stat(&self, stat: Statistic, value: T) {
unsafe {
statistic_set_increase_stat(self.reference.cast(), stat, value.try_into().unwrap())
}
}
#[cfg(not(feature = "mock_data"))]
pub fn decrease_stat(&self, stat: Statistic, value: T) {
unsafe {
statistic_set_decrease_stat(self.reference.cast(), stat, value.try_into().unwrap())
@@ -106,25 +116,33 @@ where
_p: Default::default(),
}
}
#[cfg(not(feature = "mock_data"))]
pub fn hp(&self) -> T {
self.get_stat(Statistic::HP)
}
#[cfg(not(feature = "mock_data"))]
pub fn attack(&self) -> T {
self.get_stat(Statistic::Attack)
}
#[cfg(not(feature = "mock_data"))]
pub fn defense(&self) -> T {
self.get_stat(Statistic::Defense)
}
#[cfg(not(feature = "mock_data"))]
pub fn special_attack(&self) -> T {
self.get_stat(Statistic::SpecialAttack)
}
#[cfg(not(feature = "mock_data"))]
pub fn special_defense(&self) -> T {
self.get_stat(Statistic::SpecialDefense)
}
#[cfg(not(feature = "mock_data"))]
pub fn speed(&self) -> T {
self.get_stat(Statistic::Speed)
}
#[cfg(not(feature = "mock_data"))]
pub fn get_stat(&self, stat: Statistic) -> T {
unsafe {
clamped_statistic_set_get(self.reference.cast(), stat)
@@ -133,10 +151,12 @@ where
}
}
#[cfg(not(feature = "mock_data"))]
pub fn set_stat(&self, stat: Statistic, value: T) {
unsafe { clamped_statistic_set_set(self.reference.cast(), stat, value.try_into().unwrap()) }
}
#[cfg(not(feature = "mock_data"))]
pub fn increase_stat(&self, stat: Statistic, value: T) -> bool {
unsafe {
clamped_statistic_set_increase_stat(
@@ -146,6 +166,7 @@ where
)
}
}
#[cfg(not(feature = "mock_data"))]
pub fn decrease_stat(&self, stat: Statistic, value: T) -> bool {
unsafe {
clamped_statistic_set_decrease_stat(
@@ -169,6 +190,7 @@ where
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn statistic_set_get(r: ExternRef<StatisticSet<i64>>, stat: Statistic) -> i64;
fn statistic_set_set(r: ExternRef<StatisticSet<i64>>, stat: Statistic, value: i64);

View File

@@ -1,7 +1,10 @@
use crate::app_interface::{LearnedMove, Pokemon};
use crate::handling::cached_value::CachedValue;
use crate::handling::temporary::Temporary;
use crate::{cached_value, wasm_value_getters, ExternRef, ExternalReferenceType, Script};
use crate::ExternRef;
#[cfg(not(feature = "mock_data"))]
use crate::{cached_value, wasm_value_getters, ExternalReferenceType, Script};
#[cfg(not(feature = "mock_data"))]
use alloc::boxed::Box;
use alloc::rc::Rc;
@@ -47,14 +50,17 @@ impl TurnChoice {
self.base().user.value()
}
#[cfg(not(feature = "mock_data"))]
pub fn speed(&self) -> u32 {
unsafe { turn_choice_get_speed(self.base().reference) }
}
#[cfg(not(feature = "mock_data"))]
pub fn has_failed(&self) -> bool {
unsafe { turn_choice_has_failed(self.base().reference) }
}
#[cfg(not(feature = "mock_data"))]
pub fn fail(&self) {
unsafe { turn_choice_fail(self.base().reference) }
}
@@ -70,15 +76,18 @@ impl MoveTurnChoiceData {
pub fn target_index(&self) -> u8 {
self.temp.value().inner.target_index.value()
}
#[cfg(not(feature = "mock_data"))]
pub fn priority(&self) -> i8 {
unsafe { turn_choice_move_priority(self.temp.value().inner.base.reference.cast()) }
}
#[cfg(not(feature = "mock_data"))]
pub fn move_script(&self) -> Option<&Box<dyn Script>> {
unsafe { turn_choice_move_script(self.temp.value().inner.base.reference.cast()).as_ref() }
}
}
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for TurnChoice {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
let kind = unsafe { turn_choice_get_kind(reference) };
@@ -91,6 +100,7 @@ impl ExternalReferenceType for TurnChoice {
}
}
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for MoveTurnChoiceDataTemporary {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self {
@@ -113,6 +123,7 @@ impl ExternalReferenceType for MoveTurnChoiceDataTemporary {
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn turn_choice_get_kind(r: ExternRef<TurnChoice>) -> u8;
fn turn_choice_get_user(r: ExternRef<TurnChoice>) -> ExternRef<Pokemon>;
@@ -128,6 +139,7 @@ extern "wasm" {
}
#[no_mangle]
#[cfg(not(feature = "mock_data"))]
unsafe extern "wasm" fn turn_choice_mark_deleted(r: ExternRef<TurnChoice>, kind: u8) {
match kind {
0 => Temporary::<MoveTurnChoiceDataTemporary>::mark_as_deleted(r.cast()),