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

@@ -19,6 +19,7 @@ pub struct Ability {
}
impl Ability {
#[cfg(not(feature = "mock_data"))]
pub fn new(reference: ExternRef<Self>) -> Self {
Self::from_ref(reference, &|reference| Self {
inner: Rc::new(AbilityInner {
@@ -38,6 +39,7 @@ impl Ability {
}
}
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for Ability {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
@@ -53,6 +55,7 @@ pub struct AbilityIndex {
pub index: u8,
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn ability_get_name(r: ExternRef<Ability>) -> ExternRef<StringKey>;
fn ability_get_effect(r: ExternRef<Ability>) -> ExternRef<StringKey>;

View File

@@ -15,6 +15,7 @@ pub struct ItemLibrary {
}
impl ItemLibrary {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(ptr: ExternRef<Self>) -> Self {
Self {
inner: Rc::new(ItemLibraryInner {
@@ -34,10 +35,12 @@ impl DataLibrary<Item> for ItemLibrary {
self.inner.ptr
}
#[cfg(not(feature = "mock_data"))]
fn _get_ref_by_name(ptr: ExternRef<Self>, name: ExternRef<StringKey>) -> ExternRef<Item> {
unsafe { move_library_get_move(ptr, name) }
}
#[cfg(not(feature = "mock_data"))]
fn _get_ref_by_hash(ptr: ExternRef<Self>, hash: u32) -> ExternRef<Item> {
unsafe { move_library_get_move_by_hash(ptr, hash) }
}
@@ -45,12 +48,14 @@ impl DataLibrary<Item> for ItemLibrary {
crate::handling::cacheable::cacheable!(ItemLibrary);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for ItemLibrary {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn move_library_get_move(
ptr: ExternRef<ItemLibrary>,
@@ -58,3 +63,15 @@ extern "wasm" {
) -> ExternRef<Item>;
fn move_library_get_move_by_hash(ptr: ExternRef<ItemLibrary>, hash: u32) -> ExternRef<Item>;
}
#[cfg(feature = "mock_data")]
impl ItemLibrary {
pub fn mock() -> Self {
Self {
inner: Rc::new(ItemLibraryInner {
ptr: ExternRef::mock(),
cache: Default::default(),
}),
}
}
}

View File

@@ -23,6 +23,7 @@ struct StaticDataInner {
item_library: CachedValue<ItemLibrary>,
species_library: CachedValue<SpeciesLibrary>,
type_library: CachedValue<TypeLibrary>,
settings: CachedValue<LibrarySettings>,
}
#[derive(Clone)]
@@ -31,6 +32,7 @@ pub struct StaticData {
}
impl StaticData {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(reference: ExternRef<StaticData>) -> Self {
Self::from_ref(reference, &|reference| Self {
inner: Rc::new(StaticDataInner {
@@ -53,6 +55,26 @@ impl StaticData {
})
}
#[cfg(feature = "mock_data")]
pub fn mock(
moves: MoveLibrary,
items: ItemLibrary,
species: SpeciesLibrary,
types: TypeLibrary,
settings: LibrarySettings,
) -> Self {
Self {
inner: Rc::new(StaticDataInner {
reference: ExternRef::mock(),
move_library: moves.into(),
item_library: items.into(),
species_library: species.into(),
type_library: types.into(),
settings: settings.into(),
}),
}
}
cached_value_getters! {
pub fn move_library(&self) -> MoveLibrary;
pub fn item_library(&self) -> ItemLibrary;
@@ -63,9 +85,10 @@ impl StaticData {
crate::handling::cacheable::cacheable!(StaticData);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for StaticData {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
StaticData::new(reference)
StaticData::mock(reference)
}
}
@@ -79,6 +102,7 @@ pub struct LibrarySettings {
}
impl LibrarySettings {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(ptr: ExternRef<LibrarySettings>) -> Self {
Self {
inner: Rc::new(LibrarySettingsInner {
@@ -87,11 +111,21 @@ impl LibrarySettings {
}
}
#[cfg(feature = "mock_data")]
pub fn mock(maximum_level: LevelInt) -> Self {
Self {
inner: Rc::new(LibrarySettingsInner {
maximum_level: maximum_level.into(),
}),
}
}
cached_value_getters! {
pub fn maximum_level(&self) -> LevelInt;
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn static_data_get_move_library(ptr: ExternRef<StaticData>) -> ExternRef<MoveLibrary>;
fn static_data_get_item_library(ptr: ExternRef<StaticData>) -> ExternRef<ItemLibrary>;
@@ -101,6 +135,7 @@ extern "wasm" {
fn library_settings_get_maximum_level(ptr: ExternRef<LibrarySettings>) -> LevelInt;
}
#[cfg(not(feature = "mock_data"))]
pub trait DataLibrary<T>: Cacheable
where
T: ExternalReferenceType,
@@ -147,3 +182,32 @@ where
v
}
}
#[cfg(feature = "mock_data")]
pub trait DataLibrary<T>: Cacheable
where
T: Clone,
{
fn get_cache(&self) -> &RwLock<BTreeMap<u32, T>>;
fn get_self_ref(&self) -> ExternRef<Self>
where
Self: Sized;
fn get(&self, name: &StringKey) -> Option<T>
where
Self: Sized,
{
self.get_cache().read().get(&name.hash()).cloned()
}
fn get_by_hash(&self, hash: u32) -> Option<T>
where
Self: Sized,
{
self.get_cache().read().get(&hash).cloned()
}
fn insert(&self, hash: u32, item: T) {
self.get_cache().write().insert(hash, item);
}
}

View File

@@ -16,6 +16,7 @@ pub struct MoveLibrary {
}
impl MoveLibrary {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(ptr: ExternRef<Self>) -> Self {
Self {
inner: Rc::new(MoveLibraryInner {
@@ -24,6 +25,16 @@ impl MoveLibrary {
}),
}
}
#[cfg(feature = "mock_data")]
pub fn mock() -> Self {
Self {
inner: Rc::new(MoveLibraryInner {
ptr: ExternRef::mock(),
cache: Default::default(),
}),
}
}
}
impl DataLibrary<MoveData> for MoveLibrary {
@@ -35,10 +46,12 @@ impl DataLibrary<MoveData> for MoveLibrary {
self.inner.ptr
}
#[cfg(not(feature = "mock_data"))]
fn _get_ref_by_name(ptr: ExternRef<Self>, name: ExternRef<StringKey>) -> ExternRef<MoveData> {
unsafe { move_library_get_move(ptr, name) }
}
#[cfg(not(feature = "mock_data"))]
fn _get_ref_by_hash(ptr: ExternRef<Self>, hash: u32) -> ExternRef<MoveData> {
unsafe { move_library_get_move_by_hash(ptr, hash) }
}
@@ -46,12 +59,14 @@ impl DataLibrary<MoveData> for MoveLibrary {
crate::handling::cacheable::cacheable!(MoveLibrary);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for MoveLibrary {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn move_library_get_move(
ptr: ExternRef<MoveLibrary>,

View File

@@ -15,7 +15,8 @@ pub struct SpeciesLibrary {
}
impl SpeciesLibrary {
pub(crate) fn new(ptr: ExternRef<SpeciesLibrary>) -> Self {
#[cfg(not(feature = "mock_data"))]
pub fn new(ptr: ExternRef<SpeciesLibrary>) -> Self {
Self {
inner: Rc::new(SpeciesLibraryInner {
ptr,
@@ -23,6 +24,16 @@ impl SpeciesLibrary {
}),
}
}
#[cfg(feature = "mock_data")]
pub fn mock() -> Self {
Self {
inner: Rc::new(SpeciesLibraryInner {
ptr: ExternRef::mock(),
cache: Default::default(),
}),
}
}
}
impl DataLibrary<Species> for SpeciesLibrary {
@@ -34,10 +45,12 @@ impl DataLibrary<Species> for SpeciesLibrary {
self.inner.ptr
}
#[cfg(not(feature = "mock_data"))]
fn _get_ref_by_name(ptr: ExternRef<Self>, name: ExternRef<StringKey>) -> ExternRef<Species> {
unsafe { species_library_get_species(ptr, name) }
}
#[cfg(not(feature = "mock_data"))]
fn _get_ref_by_hash(ptr: ExternRef<Self>, hash: u32) -> ExternRef<Species> {
unsafe { species_library_get_species_by_hash(ptr, hash) }
}
@@ -45,12 +58,14 @@ impl DataLibrary<Species> for SpeciesLibrary {
crate::handling::cacheable::cacheable!(SpeciesLibrary);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for SpeciesLibrary {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn species_library_get_species(
ptr: ExternRef<SpeciesLibrary>,

View File

@@ -17,6 +17,7 @@ pub struct TypeLibrary {
}
impl TypeLibrary {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(reference: ExternRef<Self>) -> Self {
Self {
inner: Rc::new(TypeLibraryInner {
@@ -26,7 +27,18 @@ impl TypeLibrary {
}),
}
}
#[cfg(feature = "mock_data")]
pub fn mock() -> Self {
Self {
inner: Rc::new(TypeLibraryInner {
reference: ExternRef::mock(),
name_to_type_cache: Default::default(),
effectiveness_cache: Default::default(),
}),
}
}
#[cfg(not(feature = "mock_data"))]
pub fn get_type_from_name(&self, name: &str) -> Option<u8> {
if let Some(cached) = self.inner.name_to_type_cache.read().get(name) {
return Some(*cached);
@@ -43,6 +55,7 @@ impl TypeLibrary {
Some(v)
}
#[cfg(not(feature = "mock_data"))]
pub fn get_single_effectiveness(&self, attacking_type: u8, defending_type: u8) -> f32 {
if let Some(cached) = self
.inner
@@ -66,6 +79,7 @@ impl TypeLibrary {
effectiveness
}
#[cfg(not(feature = "mock_data"))]
pub fn get_effectiveness(&self, attacking_type: u8, defending_types: &[u8]) -> f32 {
let mut f = 1.0;
for defending_type in defending_types {
@@ -77,12 +91,14 @@ impl TypeLibrary {
crate::handling::cacheable::cacheable!(TypeLibrary);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for TypeLibrary {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn type_library_get_single_effectiveness(
r: ExternRef<TypeLibrary>,

View File

@@ -21,7 +21,8 @@ pub enum EffectParameter {
}
impl EffectParameter {
pub(crate) fn create(ptr: ExternRef<Self>) -> Self {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(ptr: ExternRef<Self>) -> Self {
unsafe {
match effect_parameter_get_type(ptr) {
EffectParameterType::None => Self::None,
@@ -36,12 +37,13 @@ impl EffectParameter {
}
}
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for EffectParameter {
fn from_extern_value(reference: ExternRef<Self>) -> Self
where
Self: Sized,
{
EffectParameter::create(reference)
EffectParameter::new(reference)
}
}
@@ -59,6 +61,7 @@ impl Display for EffectParameter {
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn effect_parameter_get_type(ptr: ExternRef<EffectParameter>) -> EffectParameterType;
fn effect_parameter_as_bool(ptr: ExternRef<EffectParameter>) -> bool;

View File

@@ -56,6 +56,7 @@ pub struct Item {
}
impl Item {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(reference: ExternRef<Self>) -> Self {
Self::from_ref(reference, &|reference| Self {
inner: Rc::new(ItemInner {
@@ -68,6 +69,7 @@ impl Item {
})
}
#[cfg(not(feature = "mock_data"))]
pub(crate) fn reference(&self) -> ExternRef<Self> {
self.inner.reference
}
@@ -83,6 +85,7 @@ impl Item {
pub fn price(&self) -> i32;
}
#[cfg(not(feature = "mock_data"))]
pub fn has_flag(&self, flag: &StringKey) -> bool {
unsafe { item_has_flag(self.inner.reference, flag.ptr()) }
}
@@ -90,12 +93,14 @@ impl Item {
crate::handling::cacheable::cacheable!(Item);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for Item {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Item::new(reference)
Item::mock(reference)
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn item_get_name(ptr: ExternRef<Item>) -> ExternRef<StringKey>;
fn item_get_category(ptr: ExternRef<Item>) -> ItemCategory;
@@ -103,3 +108,30 @@ extern "wasm" {
fn item_get_price(ptr: ExternRef<Item>) -> i32;
fn item_has_flag(ptr: ExternRef<Item>, flag: ExternRef<StringKey>) -> bool;
}
#[cfg(feature = "mock_data")]
mod test {
use super::Item;
use super::ItemInner;
use crate::app_interface::{BattleItemCategory, ItemCategory};
use crate::{cached_value, ExternRef, StringKey};
use alloc::rc::Rc;
impl Item {
pub fn mock() -> Self {
Self {
inner: Rc::new(ItemInner {
reference: ExternRef::mock(),
name: StringKey::new("test").into(),
category: ItemCategory::MiscItem.into(),
battle_category: BattleItemCategory::None.into(),
price: 0.into(),
}),
}
}
pub fn has_flag(&self) -> bool {
unimplemented!()
}
}
}

View File

@@ -1,4 +1,4 @@
use crate::app_interface::{get_hash, StringKey};
use crate::app_interface::{get_hash_const, StringKey};
use crate::handling::cached_value::CachedValue;
use crate::handling::Cacheable;
use crate::{cached_value, cached_value_getters, ExternRef, ExternalReferenceType};
@@ -50,6 +50,7 @@ pub struct MoveData {
}
impl MoveData {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(ptr: ExternRef<Self>) -> Self {
MoveData::from_ref(ptr, &|ptr| Self {
inner: Rc::new(MoveDataInner {
@@ -65,6 +66,31 @@ impl MoveData {
}),
})
}
#[cfg(feature = "mock_data")]
pub fn mock(
name: &str,
move_type: u8,
category: MoveCategory,
base_power: u8,
accuracy: u8,
base_usages: u8,
target: MoveTarget,
priority: i8,
) -> Self {
Self {
inner: Rc::new(MoveDataInner {
ptr: ExternRef::mock(),
name: StringKey::new(name).into(),
move_type: move_type.into(),
category: category.into(),
base_power: base_power.into(),
accuracy: accuracy.into(),
base_usages: base_usages.into(),
target: target.into(),
priority: priority.into(),
}),
}
}
cached_value_getters! {
pub fn name(&self) -> StringKey;
@@ -76,12 +102,15 @@ impl MoveData {
pub fn target(&self) -> MoveTarget;
pub fn priority(&self) -> i8;
}
#[cfg(not(feature = "mock_data"))]
pub fn has_flag<const N: usize>(&self, flag: &[u8; N]) -> bool {
let hash = get_hash(flag);
let hash = get_hash_const(flag);
unsafe { move_data_has_flag_by_hash(self.inner.ptr, hash) }
}
}
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for MoveData {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
MoveData::new(reference)
@@ -90,6 +119,7 @@ impl ExternalReferenceType for MoveData {
crate::handling::cacheable::cacheable!(MoveData);
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn move_data_get_name(ptr: ExternRef<MoveData>) -> ExternRef<StringKey>;
fn move_data_get_type(ptr: ExternRef<MoveData>) -> u8;

View File

@@ -23,6 +23,7 @@ pub struct Nature {
crate::handling::cacheable::cacheable!(Nature);
impl Nature {
#[cfg(not(feature = "mock_data"))]
pub fn new(reference: ExternRef<Self>) -> Self {
Self::from_ref(reference, &|reference| unsafe {
Self {
@@ -38,12 +39,14 @@ impl Nature {
}
}
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for Nature {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn nature_get_increase_stat(r: ExternRef<Nature>) -> Statistic;
fn nature_get_decrease_stat(r: ExternRef<Nature>) -> Statistic;

View File

@@ -1,4 +1,4 @@
use crate::app_interface::get_hash;
use crate::app_interface::get_hash_const;
use crate::handling::cached_value::CachedValue;
use crate::handling::Cacheable;
use crate::{
@@ -49,6 +49,7 @@ pub struct ImmutableStatisticSet {
}
impl ImmutableStatisticSet {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(reference: ExternRef<Self>) -> Self {
Self::from_ref(reference, &|reference| Self {
inner: Rc::new(ImmutableStatisticSetInner {
@@ -106,6 +107,7 @@ pub struct Form {
}
impl Form {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(reference: ExternRef<Self>) -> Self {
Self::from_ref(reference, &|reference| Self {
inner: Rc::new(FormInner {
@@ -144,8 +146,9 @@ impl Form {
self.inner.types.value_ref()
}
#[cfg(not(feature = "mock_data"))]
pub fn has_flag<const N: usize>(&self, flag: &[u8; N]) -> bool {
let hash = get_hash(flag);
let hash = get_hash_const(flag);
unsafe { form_has_flag_by_hash(self.inner.reference, hash) }
}
}
@@ -166,6 +169,7 @@ pub struct Species {
}
impl Species {
#[cfg(not(feature = "mock_data"))]
pub(crate) fn new(reference: ExternRef<Species>) -> Self {
Self {
inner: Rc::new(SpeciesInner {
@@ -200,8 +204,9 @@ impl Species {
pub fn capture_rate(&self) -> u8;
}
#[cfg(not(feature = "mock_data"))]
pub fn get_form<const N: usize>(&self, form_name: &[u8; N]) -> Option<Form> {
let hash = get_hash(form_name);
let hash = get_hash_const(form_name);
unsafe {
if let Some(v) = self.inner.forms.read().get(&hash) {
v.clone()
@@ -214,24 +219,28 @@ impl Species {
}
}
#[cfg(not(feature = "mock_data"))]
pub fn has_flag<const N: usize>(&self, flag: &[u8; N]) -> bool {
let hash = get_hash(flag);
let hash = get_hash_const(flag);
unsafe { species_has_flag_by_hash(self.inner.reference, hash) }
}
}
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for ImmutableStatisticSet {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
}
}
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for Form {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
}
}
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for Species {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
@@ -242,6 +251,7 @@ crate::handling::cacheable::cacheable!(ImmutableStatisticSet);
crate::handling::cacheable::cacheable!(Form);
crate::handling::cacheable::cacheable!(Species);
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn static_statistics_set_get_hp(r: ExternRef<ImmutableStatisticSet>) -> u16;
fn static_statistics_set_get_attack(r: ExternRef<ImmutableStatisticSet>) -> u16;