From 4cd89020173cda979b6b4445fa70c012625be871 Mon Sep 17 00:00:00 2001 From: Forest Anderson Date: Wed, 10 Jun 2020 12:41:49 -0400 Subject: [PATCH] Add unequip tests --- common/src/comp/inventory/slot.rs | 62 +++++++++++++++++++++++++++++++ common/src/comp/inventory/test.rs | 3 -- common/src/loadout_builder.rs | 2 +- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/common/src/comp/inventory/slot.rs b/common/src/comp/inventory/slot.rs index 6aca20f5bb..5190bd3223 100644 --- a/common/src/comp/inventory/slot.rs +++ b/common/src/comp/inventory/slot.rs @@ -249,9 +249,71 @@ pub fn equip(slot: usize, inventory: &mut Inventory, loadout: &mut Loadout) { } } +/// Unequip an item from slot and place into inventory. Will fail if if +/// inventory has no slots available. +/// +/// ``` +/// use veloren_common::{ +/// comp::{ +/// slot::{unequip, EquipSlot}, +/// Inventory, +/// }, +/// LoadoutBuilder, +/// }; +/// +/// let mut inv = Inventory { +/// slots: vec![None], +/// amount: 0, +/// }; +/// +/// let mut loadout = LoadoutBuilder::new() +/// .defaults() +/// .active_item(LoadoutBuilder::default_item_config_from_str(Some( +/// "common.items.weapons.sword.zweihander_sword_0", +/// ))) +/// .build(); +/// +/// let slot = EquipSlot::Mainhand; +/// +/// unequip(slot, &mut inv, &mut loadout); +/// assert_eq!(None, loadout.active_item); +/// ``` pub fn unequip(slot: EquipSlot, inventory: &mut Inventory, loadout: &mut Loadout) { loadout_remove(slot, loadout) // Remove item from loadout .and_then(|i| inventory.push(i)) // Insert into inventory .and_then(|i| loadout_insert(slot, i, loadout)) // If that fails put back in loadout .unwrap_none(); // Never fails } + +#[cfg(test)] +mod tests { + use super::*; + use crate::LoadoutBuilder; + + #[test] + fn test_unequip() { + let mut inv = Inventory { + slots: vec![None], + amount: 0, + }; + + let sword = LoadoutBuilder::default_item_config_from_str(Some( + "common.items.weapons.sword.zweihander_sword_0", + )); + + let mut loadout = LoadoutBuilder::new() + .defaults() + .active_item(sword.clone()) + .second_item(sword.clone()) + .build(); + + assert_eq!(sword, loadout.active_item); + unequip(EquipSlot::Mainhand, &mut inv, &mut loadout); + // We have space in the inventory, so this should have unequipped + assert_eq!(None, loadout.active_item); + + unequip(EquipSlot::Offhand, &mut inv, &mut loadout); + // There is no more space in the inventory, so this should still be equipped + assert_eq!(sword, loadout.second_item); + } +} diff --git a/common/src/comp/inventory/test.rs b/common/src/comp/inventory/test.rs index 4b3db9f94d..ac782b9b27 100644 --- a/common/src/comp/inventory/test.rs +++ b/common/src/comp/inventory/test.rs @@ -6,9 +6,6 @@ lazy_static! { assets::load_expect_cloned("common.items.debug.possess") ]; } -/// The `Default` inventory should contain 3 items: cheese, apple, lantern -#[test] -fn create_default_count() { assert_eq!(Inventory::default().count(), 2) } /// Attempting to push into a full inventory should return the same item. #[test] diff --git a/common/src/loadout_builder.rs b/common/src/loadout_builder.rs index 179a73df34..76562e8d90 100644 --- a/common/src/loadout_builder.rs +++ b/common/src/loadout_builder.rs @@ -110,7 +110,7 @@ impl LoadoutBuilder { } pub fn second_item(mut self, item: Option) -> Self { - self.0.active_item = item; + self.0.second_item = item; self }