From 1649eadcdd376d5289322109875fe5ed8e696d36 Mon Sep 17 00:00:00 2001 From: Forest Anderson Date: Wed, 10 Jun 2020 12:41:49 -0400 Subject: [PATCH 1/3] 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 } From c2beeef600d170b2a00ed89de1e006c4078fc79d Mon Sep 17 00:00:00 2001 From: Forest Anderson Date: Wed, 10 Jun 2020 17:37:45 -0400 Subject: [PATCH 2/3] Added equip documentation and test --- assets/common/items/testing/test_boots.ron | 8 +++ common/src/comp/inventory/slot.rs | 61 ++++++++++++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 assets/common/items/testing/test_boots.ron diff --git a/assets/common/items/testing/test_boots.ron b/assets/common/items/testing/test_boots.ron new file mode 100644 index 0000000000..b8a4bfa3cf --- /dev/null +++ b/assets/common/items/testing/test_boots.ron @@ -0,0 +1,8 @@ +Item( + name: "Testing Boots", + description: "Hopefully this test doesn't break!", + kind: Armor( + kind: Foot(Dark), + stats: (20), + ), +) diff --git a/common/src/comp/inventory/slot.rs b/common/src/comp/inventory/slot.rs index 5190bd3223..f0090005ee 100644 --- a/common/src/comp/inventory/slot.rs +++ b/common/src/comp/inventory/slot.rs @@ -216,6 +216,34 @@ pub fn swap( } } +/// Equip an item from a slot in inventory. The currently equipped item will go +/// into inventory. If the item is going to mainhand, put mainhand in +/// offhand and place offhand into inventory. +/// +/// ``` +/// use veloren_common::{ +/// assets, +/// comp::{ +/// slot::{equip, EquipSlot}, +/// Inventory, Item, +/// }, +/// LoadoutBuilder, +/// }; +/// +/// let boots: Option = Some(assets::load_expect_cloned( +/// "common.items.testing.test_boots", +/// )); +/// +/// let mut inv = Inventory { +/// slots: vec![boots.clone()], +/// amount: 1, +/// }; +/// +/// let mut loadout = LoadoutBuilder::new().defaults().build(); +/// +/// equip(0, &mut inv, &mut loadout); +/// assert_eq!(boots, loadout.foot); +/// ``` pub fn equip(slot: usize, inventory: &mut Inventory, loadout: &mut Loadout) { use item::{armor::Armor, ItemKind}; @@ -249,8 +277,8 @@ 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. +/// Unequip an item from slot and place into inventory. Will leave the item +/// equipped if inventory has no slots available. /// /// ``` /// use veloren_common::{ @@ -288,7 +316,7 @@ pub fn unequip(slot: EquipSlot, inventory: &mut Inventory, loadout: &mut Loadout #[cfg(test)] mod tests { use super::*; - use crate::LoadoutBuilder; + use crate::{assets, LoadoutBuilder}; #[test] fn test_unequip() { @@ -316,4 +344,31 @@ mod tests { // There is no more space in the inventory, so this should still be equipped assert_eq!(sword, loadout.second_item); } + + #[test] + fn test_equip() { + let boots: Option = Some(assets::load_expect_cloned( + "common.items.testing.test_boots", + )); + + let starting_sandles: Option = Some(assets::load_expect_cloned( + "common.items.armor.starter.sandals_0", + )); + + let mut inv = Inventory { + slots: vec![boots.clone()], + amount: 1, + }; + + let mut loadout = LoadoutBuilder::new().defaults().build(); + + // We should start with the starting sandles + assert_eq!(starting_sandles, loadout.foot); + equip(0, &mut inv, &mut loadout); + + // We should now have the testing boots equiped + assert_eq!(boots, loadout.foot); + // The starting sandles should now be in the inventory + assert_eq!(starting_sandles, inv.slots[0]); + } } From 361952077e7550384799d2ec2945e7e29e01e159 Mon Sep 17 00:00:00 2001 From: Forest Anderson Date: Wed, 10 Jun 2020 22:50:45 -0400 Subject: [PATCH 3/3] Added loadout replace test and documentation Finished documenting --- common/src/comp/inventory/slot.rs | 103 ++++++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 5 deletions(-) diff --git a/common/src/comp/inventory/slot.rs b/common/src/comp/inventory/slot.rs index f0090005ee..3f810cfbf7 100644 --- a/common/src/comp/inventory/slot.rs +++ b/common/src/comp/inventory/slot.rs @@ -80,6 +80,8 @@ impl ArmorSlot { // TODO: There are plans to save the selected abilities for each tool even // when they are not equipped, when that is implemented this helper function // should no longer be needed + +/// Create an ItemConfig for an item. Apply abilties to item. fn item_config(item: item::Item) -> comp::ItemConfig { let mut abilities = if let item::ItemKind::Tool(tool) = &item.kind { tool.get_abilities() @@ -98,6 +100,8 @@ fn item_config(item: item::Item) -> comp::ItemConfig { } } +/// Replace an equiptment slot with an item. Return the item that was in the +/// slot, if any. Doesn't update the inventory. fn loadout_replace( equip_slot: EquipSlot, item: Option, @@ -126,6 +130,7 @@ fn loadout_replace( } } +/// Insert an item into a loadout #[must_use] fn loadout_insert( equip_slot: EquipSlot, @@ -135,10 +140,39 @@ fn loadout_insert( loadout_replace(equip_slot, Some(item), loadout) } +/// Remove an item from a loadout. +/// +/// ``` +/// use veloren_common::{ +/// comp::{ +/// slot::{loadout_remove, 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; +/// +/// loadout_remove(slot, &mut loadout); +/// assert_eq!(None, loadout.active_item); +/// ``` pub fn loadout_remove(equip_slot: EquipSlot, loadout: &mut Loadout) -> Option { loadout_replace(equip_slot, None, loadout) } +/// Swap item in an inventory slot with one in a loadout slot. fn swap_inventory_loadout( inventory_slot: usize, equip_slot: EquipSlot, @@ -167,6 +201,7 @@ fn swap_inventory_loadout( } } +/// Swap items in loadout. fn swap_loadout(slot_a: EquipSlot, slot_b: EquipSlot, loadout: &mut Loadout) { // Ensure that the slots are not the same if slot_a == slot_b { @@ -191,8 +226,10 @@ fn swap_loadout(slot_a: EquipSlot, slot_b: EquipSlot, loadout: &mut Loadout) { } } -// Should this report if a change actually occurred? (might be useful when +// TODO: Should this report if a change actually occurred? (might be useful when // minimizing network use) + +/// Swap items from two slots, regardless of if either is inventory or loadout. pub fn swap( slot_a: Slot, slot_b: Slot, @@ -319,7 +356,7 @@ mod tests { use crate::{assets, LoadoutBuilder}; #[test] - fn test_unequip() { + fn test_unequip_items_both_hands() { let mut inv = Inventory { slots: vec![None], amount: 0, @@ -343,10 +380,14 @@ mod tests { 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); + + // Verify inventory + assert_eq!(inv.slots[0], Some(sword.clone().unwrap().item)); + assert_eq!(inv.slots.len(), 1); } #[test] - fn test_equip() { + fn test_equip_item() { let boots: Option = Some(assets::load_expect_cloned( "common.items.testing.test_boots", )); @@ -368,7 +409,59 @@ mod tests { // We should now have the testing boots equiped assert_eq!(boots, loadout.foot); - // The starting sandles should now be in the inventory - assert_eq!(starting_sandles, inv.slots[0]); + + // Verify inventory + assert_eq!(inv.slots[0], starting_sandles); + assert_eq!(inv.slots.len(), 1); + } + + #[test] + fn test_loadout_replace() { + let boots: Option = Some(assets::load_expect_cloned( + "common.items.testing.test_boots", + )); + + let starting_sandles: Option = Some(assets::load_expect_cloned( + "common.items.armor.starter.sandals_0", + )); + + let mut loadout = LoadoutBuilder::new().defaults().build(); + + // We should start with the starting sandles + assert_eq!(starting_sandles, loadout.foot); + + // The swap should return the sandles + assert_eq!( + starting_sandles, + loadout_replace( + EquipSlot::Armor(ArmorSlot::Feet), + boots.clone(), + &mut loadout, + ) + ); + + // We should now have the testing boots equiped + assert_eq!(boots, loadout.foot); + } + + #[test] + fn test_loadout_remove() { + 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()) + .build(); + + // The swap should return the sword + assert_eq!( + Some(sword.clone().unwrap().item), + loadout_remove(EquipSlot::Mainhand, &mut loadout,) + ); + + // We should now have nothing equiped + assert_eq!(None, loadout.active_item); } }