Initial work on outlining the dynamic side of the library.

This commit is contained in:
2021-01-31 17:31:22 +01:00
parent 2a08fb2645
commit c194c5d209
24 changed files with 321 additions and 39 deletions

View File

@@ -33,12 +33,11 @@ impl Debug for GrowthRateLibrary {
}
#[cfg(test)]
mod tests {
pub mod tests {
use crate::static_data::growth_rates::lookup_growth_rate::LookupGrowthRate;
use crate::static_data::libraries::growth_rate_library::GrowthRateLibrary;
#[test]
fn add_growth_rate_to_library_and_calculate_level() {
pub fn build() -> GrowthRateLibrary {
let mut lib = GrowthRateLibrary::new(1);
// Borrow as mut so we can insert
@@ -46,24 +45,20 @@ mod tests {
w.add_growth_rate("foo", Box::new(LookupGrowthRate::new(vec![0, 5, 10, 100])));
// Drops borrow as mut
// Borrow as read so we can read
let r = &lib;
assert_eq!(r.calculate_level("foo", 3), 1);
assert_eq!(r.calculate_level("foo", 50), 3);
lib
}
#[test]
fn add_growth_rate_to_library_and_calculate_level() {
let lib = build();
assert_eq!(lib.calculate_level("foo", 3), 1);
assert_eq!(lib.calculate_level("foo", 50), 3);
}
#[test]
fn add_growth_rate_to_library_and_calculate_experience() {
let mut lib = GrowthRateLibrary::new(1);
// Borrow as mut so we can insert
let w = &mut lib;
w.add_growth_rate("foo", Box::new(LookupGrowthRate::new(vec![0, 5, 10, 100])));
// Drops borrow as mut
// Borrow as read so we can read
let r = &lib;
assert_eq!(r.calculate_experience("foo", 1), 0);
assert_eq!(r.calculate_experience("foo", 3), 10);
let lib = build();
assert_eq!(lib.calculate_experience("foo", 1), 0);
assert_eq!(lib.calculate_experience("foo", 3), 10);
}
}

View File

@@ -30,3 +30,33 @@ impl DataLibrary<'_, Item> for ItemLibrary {
(&mut self.map, &mut self.list)
}
}
#[cfg(test)]
pub mod tests {
use crate::static_data::items::item::Item;
use crate::static_data::items::item_category::{BattleItemCategory, ItemCategory};
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::libraries::item_library::ItemLibrary;
use std::collections::HashSet;
fn build_item() -> Item {
Item::new(
"foo",
ItemCategory::MiscItem,
BattleItemCategory::MiscBattleItem,
100,
HashSet::new(),
)
}
pub fn build() -> ItemLibrary {
let mut lib = ItemLibrary::new(1);
let m = build_item();
// Borrow as mut so we can insert
let w = &mut lib;
w.add("foo", m);
// Drops borrow as mut
lib
}
}

View File

@@ -3,6 +3,5 @@ use derive_getters::Getters;
#[derive(Getters, Debug)]
pub struct LibrarySettings {
maximum_level: LevelInt,
maximum_move_count: u8,
pub(crate) maximum_level: LevelInt,
}

View File

@@ -30,3 +30,38 @@ impl DataLibrary<'_, MoveData> for MoveLibrary {
(&mut self.map, &mut self.list)
}
}
#[cfg(test)]
pub mod tests {
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::libraries::move_library::MoveLibrary;
use crate::static_data::moves::move_data::{MoveCategory, MoveData, MoveTarget};
use crate::static_data::moves::secondary_effect::SecondaryEffect;
use std::collections::HashSet;
fn build_move() -> MoveData {
MoveData::new(
"foo",
0,
MoveCategory::Physical,
100,
100,
30,
MoveTarget::Any,
0,
SecondaryEffect::empty(),
HashSet::new(),
)
}
pub fn build() -> MoveLibrary {
let mut lib = MoveLibrary::new(1);
let m = build_move();
// Borrow as mut so we can insert
let w = &mut lib;
w.add("foo", m);
// Drops borrow as mut
lib
}
}

View File

@@ -32,7 +32,7 @@ impl<'a> DataLibrary<'a, Species<'a>> for SpeciesLibrary<'a> {
}
#[cfg(test)]
mod tests {
pub mod tests {
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::libraries::species_library::SpeciesLibrary;
use crate::static_data::species_data::form::Form;
@@ -64,16 +64,21 @@ mod tests {
)
}
#[test]
fn add_species_to_library_and_fetch() {
pub fn build<'a>() -> SpeciesLibrary<'a> {
let mut lib = SpeciesLibrary::new(1);
let species = build_species();
// Borrow as mut so we can insert
let w = &mut lib;
w.add("foo", species);
// Drops borrow as mut
lib
}
#[test]
fn add_species_to_library_and_fetch() {
let lib = build();
// Borrow as read so we can read
let r = &lib;
let mon = r.get("foo");
@@ -85,14 +90,9 @@ mod tests {
#[test]
fn add_species_to_library_then_remove() {
let mut lib = SpeciesLibrary::new(1);
let species = build_species();
let mut lib = build();
// Borrow as mut so we can insert
let w = &mut lib;
w.add("foo", species);
w.remove("foo");
// Drops borrow as mut
lib.remove("foo");
// Borrow as read so we can read
let r = &lib;

View File

@@ -7,7 +7,7 @@ use crate::static_data::libraries::type_library::TypeLibrary;
use derive_getters::Getters;
#[derive(Getters, Debug)]
struct StaticData<'a> {
pub struct StaticData<'a> {
settings: LibrarySettings,
species: SpeciesLibrary<'a>,
moves: MoveLibrary,
@@ -15,3 +15,23 @@ struct StaticData<'a> {
growth_rates: GrowthRateLibrary,
types: TypeLibrary,
}
#[cfg(test)]
pub mod test {
use crate::static_data::libraries::library_settings::LibrarySettings;
use crate::static_data::libraries::static_data::StaticData;
use crate::static_data::libraries::{
growth_rate_library, item_library, move_library, species_library, type_library,
};
pub fn build<'a>() -> StaticData<'a> {
StaticData {
settings: LibrarySettings { maximum_level: 100 },
species: species_library::tests::build(),
moves: move_library::tests::build(),
items: item_library::tests::build(),
growth_rates: growth_rate_library::tests::build(),
types: type_library::tests::build(),
}
}
}

View File

@@ -46,10 +46,25 @@ impl TypeLibrary {
}
#[cfg(test)]
mod tests {
pub mod tests {
use crate::static_data::libraries::type_library::TypeLibrary;
use assert_approx_eq::assert_approx_eq;
pub fn build() -> TypeLibrary {
let mut lib = TypeLibrary::new(2);
// Borrow as mut so we can insert
let w = &mut lib;
w.register_type("foo");
w.register_type("bar");
// Drops borrow as mut
w.set_effectiveness(0, 1, 0.5);
w.set_effectiveness(1, 0, 2.0);
lib
}
#[test]
fn add_two_types_retrieve_them() {
let mut lib = TypeLibrary::new(2);