Added unit test to validate contents of loot tables.

This commit is contained in:
Sam 2021-04-01 21:02:36 -04:00
parent c8b8cd436e
commit 7c8242b5b1
3 changed files with 53 additions and 18 deletions

View File

@ -11,8 +11,8 @@
(1.0, LootTable("common.loot_tables.food.wild_ingredients")), (1.0, LootTable("common.loot_tables.food.wild_ingredients")),
(0.25, LootTable("common.loot_tables.food.prepared")), (0.25, LootTable("common.loot_tables.food.prepared")),
// Weapons // Weapons
(0.5, LootTable("common.loot_tables.sword.wood")), (0.5, LootTable("common.loot_tables.weapons.sword.wood")),
(0.5, LootTable("common.loot_tables.hammer.stone")), (0.5, LootTable("common.loot_tables.weapons.hammer.stone")),
(0.5, LootTable("common.loot_tables.bow.rawwood")), (0.5, LootTable("common.loot_tables.weapons.bow.rawwood")),
(0.5, LootTable("common.loot_tables.starter")), (0.5, LootTable("common.loot_tables.weapons.starter")),
] ]

View File

@ -11,8 +11,8 @@
(2.0, LootTable("common.loot_tables.food.farm_ingredients")), (2.0, LootTable("common.loot_tables.food.farm_ingredients")),
(0.25, LootTable("common.loot_tables.food.prepared")), (0.25, LootTable("common.loot_tables.food.prepared")),
// Weapons // Weapons
(0.5, LootTable("common.loot_tables.sword.wood")), (0.5, LootTable("common.loot_tables.weapons.sword.wood")),
(0.5, LootTable("common.loot_tables.hammer.stone")), (0.5, LootTable("common.loot_tables.weapons.hammer.stone")),
(0.5, LootTable("common.loot_tables.bow.rawwood")), (0.5, LootTable("common.loot_tables.weapons.bow.rawwood")),
(0.5, LootTable("common.loot_tables.starter")), (0.5, LootTable("common.loot_tables.weapons.starter")),
] ]

View File

@ -114,18 +114,53 @@ impl LootSpec {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::{assets::AssetExt, comp::Item}; use crate::{assets::{AssetExt, Error}, comp::Item};
#[test] #[test]
fn test_loot_table() { fn test_loot_tables() {
let test = Lottery::<LootSpec>::load_expect("common.loot_tables.fallback"); #[derive(Clone)]
struct LootTableList(Vec<Lottery<LootSpec>>);
impl assets::Compound for LootTableList {
fn load<S: assets::source::Source>(
cache: &assets::AssetCache<S>,
specifier: &str,
) -> Result<Self, Error> {
let list = cache
.load::<assets::Directory>(specifier)?
.read()
.iter()
.map(|spec| Lottery::<LootSpec>::load_cloned(spec))
.collect::<Result<_, Error>>()?;
Ok(LootTableList(list))
}
}
for (_, to_itemifier) in test.read().iter() { fn validate_table_contents(table: Lottery<LootSpec>) {
assert!( for (_, item) in table.iter() {
Item::new_from_asset(to_itemifier).is_ok(), match item {
"Invalid loot table item '{}'", LootSpec::Item(item) => {
to_itemifier Item::new_from_asset_expect(&item);
); },
LootSpec::ItemQuantity(item, lower, upper) => {
assert!(*lower > 0, "Lower quantity must be more than 0. It is {}.", lower);
assert!(upper >= lower, "Upper quantity must be at least the value of lower quantity. Upper value: {}, low value: {}.", upper, lower);
Item::new_from_asset_expect(&item);
},
LootSpec::LootTable(loot_table) => {
let loot_table = Lottery::<LootSpec>::load_expect_cloned(&loot_table);
validate_table_contents(loot_table);
},
LootSpec::CreatureMaterial => {
item.to_item(None);
},
}
}
}
let loot_tables = LootTableList::load_expect_cloned("common.loot_tables.*").0;
for loot_table in loot_tables {
validate_table_contents(loot_table);
} }
} }
} }