126 lines
3.4 KiB
Rust
Executable File
126 lines
3.4 KiB
Rust
Executable File
use std::fmt::Debug;
|
|
use std::sync::Arc;
|
|
|
|
use indexmap::IndexMap;
|
|
|
|
use crate::static_data::DataLibrary;
|
|
use crate::static_data::Species;
|
|
use crate::StringKey;
|
|
|
|
/// A library to store all data for Pokemon species.
|
|
pub trait SpeciesLibrary: DataLibrary<dyn Species> + Debug {}
|
|
|
|
/// A library to store all data for Pokemon species.
|
|
#[derive(Debug)]
|
|
pub struct SpeciesLibraryImpl {
|
|
/// The underlying map.
|
|
map: IndexMap<StringKey, Arc<dyn Species>>,
|
|
}
|
|
|
|
impl SpeciesLibraryImpl {
|
|
/// Instantiates a new Species Library.
|
|
pub fn new(capacity: usize) -> Self {
|
|
Self {
|
|
map: IndexMap::with_capacity(capacity),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl SpeciesLibrary for SpeciesLibraryImpl {}
|
|
|
|
impl DataLibrary<dyn Species> for SpeciesLibraryImpl {
|
|
fn map(&self) -> &IndexMap<StringKey, Arc<dyn Species>> {
|
|
&self.map
|
|
}
|
|
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<dyn Species>> {
|
|
&mut self.map
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[allow(clippy::indexing_slicing)]
|
|
#[allow(clippy::unwrap_used)]
|
|
pub mod tests {
|
|
use super::*;
|
|
use crate::static_data::{FormImpl, LearnableMovesImpl, SpeciesImpl, StaticStatisticSet};
|
|
use hashbrown::HashSet;
|
|
use std::sync::Arc;
|
|
|
|
mockall::mock! {
|
|
#[derive(Debug)]
|
|
pub SpeciesLibrary{}
|
|
impl SpeciesLibrary for SpeciesLibrary {}
|
|
impl DataLibrary<dyn Species> for SpeciesLibrary {
|
|
fn get<'a, 'b>(&'a self, key: &'b StringKey) -> Option<Arc<dyn Species>> ;
|
|
fn get_by_hash<'a>(&'a self, key: u32) -> Option<Arc<dyn Species>>;
|
|
fn map(&self) -> &IndexMap<StringKey, Arc<dyn Species>>;
|
|
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<dyn Species>>;
|
|
}
|
|
}
|
|
|
|
fn build_species() -> Arc<dyn Species> {
|
|
Arc::new(SpeciesImpl::new(
|
|
0,
|
|
&"foo".into(),
|
|
0.5,
|
|
&"test_growthrate".into(),
|
|
0,
|
|
120,
|
|
Arc::new(FormImpl::new(
|
|
&"default".into(),
|
|
0.0,
|
|
0.0,
|
|
0,
|
|
Vec::new(),
|
|
Arc::new(StaticStatisticSet::new(10, 10, 10, 10, 10, 10)),
|
|
Vec::new(),
|
|
Vec::new(),
|
|
Arc::new(LearnableMovesImpl::new(100)),
|
|
HashSet::new(),
|
|
)),
|
|
HashSet::new(),
|
|
Vec::new(),
|
|
))
|
|
}
|
|
|
|
pub fn build() -> Arc<dyn SpeciesLibrary> {
|
|
let mut lib = SpeciesLibraryImpl::new(1);
|
|
let species = build_species();
|
|
// Borrow as mut so we can insert
|
|
let w = &mut lib;
|
|
w.add(&"foo".into(), species);
|
|
// Drops borrow as mut
|
|
|
|
Arc::new(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".into());
|
|
assert!(mon.is_some());
|
|
let mon = mon.unwrap();
|
|
assert_eq!(mon.id(), 0_u16);
|
|
assert_eq!(mon.name(), &"foo".into());
|
|
assert_eq!(r.len(), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn add_species_to_library_then_remove() {
|
|
let mut lib = build();
|
|
|
|
let lib = Arc::get_mut(&mut lib).unwrap();
|
|
|
|
lib.remove(&"foo".into());
|
|
|
|
// Borrow as read so we can read
|
|
let r = &lib;
|
|
let mon = r.get(&"foo".into());
|
|
assert!(mon.is_none());
|
|
assert_eq!(r.len(), 0);
|
|
}
|
|
}
|