Respond to MR feedback.

Mainly clean up code with better use of iterators, and rename the debug
    command to be just `debug`.
This commit is contained in:
Joseph Gerardot 2019-11-06 17:57:54 -05:00
parent 7325757066
commit e73884a1d4
No known key found for this signature in database
GPG Key ID: 2078DC064C1359E4
3 changed files with 80 additions and 83 deletions

View File

@ -101,16 +101,13 @@ impl Inventory {
pub fn count(&self) -> usize {
self.slots
.iter()
.fold(0, |count, slot| count + if slot.is_some() { 1 } else { 0 })
.filter_map(|slot| slot.as_ref())
.count()
}
/// O(n) check if an item is in this inventory.
pub fn contains(&self, item: &Item) -> bool {
self.slots.iter().any(|slot| {
slot.as_ref()
.map(|other_item| item == other_item)
.unwrap_or(false)
})
self.slots.iter().any(|slot| slot.as_ref() == Some(item))
}
/// Get content of a slot
@ -156,76 +153,4 @@ impl Component for InventoryUpdate {
}
#[cfg(test)]
mod test {
use super::*;
use lazy_static::lazy_static;
lazy_static! {
static ref TEST_ITEMS: Vec<Item> = vec![
assets::load_expect_cloned("common.items.debug.boost"),
assets::load_expect_cloned("common.items.debug.possess")
];
}
/// The `Default` inventory should contain two items
#[test]
fn create_default_count() {
assert_eq!(Inventory::default().count(), 2)
}
/// Attempting to push into a full inventory should return the same item.
#[test]
fn push_full() {
let mut inv = Inventory {
slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect(),
};
assert_eq!(
inv.push(TEST_ITEMS[0].clone()).unwrap(),
TEST_ITEMS[0].clone()
)
}
/// Attempting to push a series into a full inventory should return them all.
#[test]
fn push_all_full() {
let mut inv = Inventory {
slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect(),
};
let Error::Full(leftovers) = inv
.push_all(TEST_ITEMS.iter().map(|a| a.clone()))
.expect_err("Pushing into a full inventory somehow worked!");
assert_eq!(leftovers, TEST_ITEMS.clone())
}
/// Attempting to push uniquely into an inventory containing all the items should work fine.
#[test]
fn push_unique_all_full() {
let mut inv = Inventory {
slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect(),
};
inv.push_all_unique(TEST_ITEMS.iter().map(|a| a.clone()))
.expect(
"Pushing unique items into an inventory that already contains them didn't work!",
);
}
/// Attempting to push uniquely into an inventory containing all the items should work fine.
#[test]
fn push_all_empty() {
let mut inv = Inventory {
slots: vec![None, None],
};
inv.push_all(TEST_ITEMS.iter().map(|a| a.clone()))
.expect("Pushing items into an empty inventory didn't work!");
}
/// Attempting to push uniquely into an inventory containing all the items should work fine.
#[test]
fn push_all_unique_empty() {
let mut inv = Inventory {
slots: vec![None, None],
};
inv.push_all_unique(TEST_ITEMS.iter().map(|a| a.clone()))
.expect(
"Pushing unique items into an empty inventory that didn't contain them didn't work!",
);
}
}
mod test;

View File

@ -0,0 +1,72 @@
use super::*;
use lazy_static::lazy_static;
lazy_static! {
static ref TEST_ITEMS: Vec<Item> = vec![
assets::load_expect_cloned("common.items.debug.boost"),
assets::load_expect_cloned("common.items.debug.possess")
];
}
/// The `Default` inventory should contain two items
#[test]
fn create_default_count() {
assert_eq!(Inventory::default().count(), 2)
}
/// Attempting to push into a full inventory should return the same item.
#[test]
fn push_full() {
let mut inv = Inventory {
slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect(),
};
assert_eq!(
inv.push(TEST_ITEMS[0].clone()).unwrap(),
TEST_ITEMS[0].clone()
)
}
/// Attempting to push a series into a full inventory should return them all.
#[test]
fn push_all_full() {
let mut inv = Inventory {
slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect(),
};
let Error::Full(leftovers) = inv
.push_all(TEST_ITEMS.iter().map(|a| a.clone()))
.expect_err("Pushing into a full inventory somehow worked!");
assert_eq!(leftovers, TEST_ITEMS.clone())
}
/// Attempting to push uniquely into an inventory containing all the items should work fine.
#[test]
fn push_unique_all_full() {
let mut inv = Inventory {
slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect(),
};
inv.push_all_unique(TEST_ITEMS.iter().map(|a| a.clone()))
.expect(
"Pushing unique items into an inventory that already contains them didn't work!",
);
}
/// Attempting to push uniquely into an inventory containing all the items should work fine.
#[test]
fn push_all_empty() {
let mut inv = Inventory {
slots: vec![None, None],
};
inv.push_all(TEST_ITEMS.iter().map(|a| a.clone()))
.expect("Pushing items into an empty inventory didn't work!");
}
/// Attempting to push uniquely into an inventory containing all the items should work fine.
#[test]
fn push_all_unique_empty() {
let mut inv = Inventory {
slots: vec![None, None],
};
inv.push_all_unique(TEST_ITEMS.iter().map(|a| a.clone()))
.expect(
"Pushing unique items into an empty inventory that didn't contain them didn't work!",
);
}

View File

@ -238,11 +238,11 @@ lazy_static! {
handle_remove_lights,
),
ChatCommand::new(
"debug_items",
"debug",
"",
"/debug_items : Place all debug items into your pack.",
"/debug : Place all debug items into your pack.",
true,
handle_debug_items,
handle_debug,
),
];
@ -997,7 +997,7 @@ fn handle_exp(server: &mut Server, entity: EcsEntity, args: String, action: &Cha
}
use common::comp::Item;
fn handle_debug_items(
fn handle_debug(
server: &mut Server,
entity: EcsEntity,
_args: String,