From 7c8242b5b1b7ef328c7538164e1fdcd615496c4e Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 1 Apr 2021 21:02:36 -0400 Subject: [PATCH] Added unit test to validate contents of loot tables. --- assets/common/loot_tables/humanoids.ron | 8 ++-- assets/common/loot_tables/villager.ron | 8 ++-- common/src/lottery.rs | 55 ++++++++++++++++++++----- 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/assets/common/loot_tables/humanoids.ron b/assets/common/loot_tables/humanoids.ron index cbdd57df82..e82bc54c84 100644 --- a/assets/common/loot_tables/humanoids.ron +++ b/assets/common/loot_tables/humanoids.ron @@ -11,8 +11,8 @@ (1.0, LootTable("common.loot_tables.food.wild_ingredients")), (0.25, LootTable("common.loot_tables.food.prepared")), // Weapons - (0.5, LootTable("common.loot_tables.sword.wood")), - (0.5, LootTable("common.loot_tables.hammer.stone")), - (0.5, LootTable("common.loot_tables.bow.rawwood")), - (0.5, LootTable("common.loot_tables.starter")), + (0.5, LootTable("common.loot_tables.weapons.sword.wood")), + (0.5, LootTable("common.loot_tables.weapons.hammer.stone")), + (0.5, LootTable("common.loot_tables.weapons.bow.rawwood")), + (0.5, LootTable("common.loot_tables.weapons.starter")), ] \ No newline at end of file diff --git a/assets/common/loot_tables/villager.ron b/assets/common/loot_tables/villager.ron index fb0006a182..844c780773 100644 --- a/assets/common/loot_tables/villager.ron +++ b/assets/common/loot_tables/villager.ron @@ -11,8 +11,8 @@ (2.0, LootTable("common.loot_tables.food.farm_ingredients")), (0.25, LootTable("common.loot_tables.food.prepared")), // Weapons - (0.5, LootTable("common.loot_tables.sword.wood")), - (0.5, LootTable("common.loot_tables.hammer.stone")), - (0.5, LootTable("common.loot_tables.bow.rawwood")), - (0.5, LootTable("common.loot_tables.starter")), + (0.5, LootTable("common.loot_tables.weapons.sword.wood")), + (0.5, LootTable("common.loot_tables.weapons.hammer.stone")), + (0.5, LootTable("common.loot_tables.weapons.bow.rawwood")), + (0.5, LootTable("common.loot_tables.weapons.starter")), ] \ No newline at end of file diff --git a/common/src/lottery.rs b/common/src/lottery.rs index ef82002bb4..c15569272b 100644 --- a/common/src/lottery.rs +++ b/common/src/lottery.rs @@ -114,18 +114,53 @@ impl LootSpec { #[cfg(test)] mod tests { use super::*; - use crate::{assets::AssetExt, comp::Item}; - + use crate::{assets::{AssetExt, Error}, comp::Item}; + #[test] - fn test_loot_table() { - let test = Lottery::::load_expect("common.loot_tables.fallback"); + fn test_loot_tables() { + #[derive(Clone)] + struct LootTableList(Vec>); + impl assets::Compound for LootTableList { + fn load( + cache: &assets::AssetCache, + specifier: &str, + ) -> Result { + let list = cache + .load::(specifier)? + .read() + .iter() + .map(|spec| Lottery::::load_cloned(spec)) + .collect::>()?; + + Ok(LootTableList(list)) + } + } - for (_, to_itemifier) in test.read().iter() { - assert!( - Item::new_from_asset(to_itemifier).is_ok(), - "Invalid loot table item '{}'", - to_itemifier - ); + fn validate_table_contents(table: Lottery) { + for (_, item) in table.iter() { + match item { + LootSpec::Item(item) => { + 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::::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); } } }