Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sphinxc0re committed Jan 24, 2017
1 parent 715d98a commit 1a7b0c6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,16 @@ impl Engine {
#[cfg(test)]
mod tests {
use super::*;
use character::Character;


#[test]
fn workflow() {
let mut engine = Engine::new();

engine.setup(|context| {
// Setup your game
// ...
// ..
// .
let character = Character::new("Thomas");

// Return the altered/non-altered context
context
Expand Down
74 changes: 55 additions & 19 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,19 @@ impl ItemType {

stackable_types.contains(self)
}
}

impl Rand for ItemType {
fn rand<R: Rng>(rng: &mut R) -> ItemType {
let base = rng.gen_range(0, 1000);

match base {
/// A helper method to get an ItemType
pub fn by_num(item_class_num: u32, item_type_num: u32) -> ItemType {
match item_class_num {
0...250 => {
let base = rng.gen_range(0, 1000);
match base {
match item_type_num {
0...500 => ItemType::ConsumableFood,
501...1000 => ItemType::ConsumablePotion,
_ => ItemType::Prop,
}
}
251...500 => {
let base = rng.gen_range(0, 1000);
match base {
match item_type_num {
0...250 => ItemType::ArmorHead,
251...500 => ItemType::ArmorChest,
501...750 => ItemType::ArmorLegs,
Expand All @@ -151,17 +146,15 @@ impl Rand for ItemType {
}
}
501...750 => {
let base = rng.gen_range(0, 1000);
match base {
match item_type_num {
0...333 => ItemType::WeaponHammer,
334...666 => ItemType::WeaponSword,
667...1000 => ItemType::WeaponWand,
_ => ItemType::Prop,
}
}
751...1000 => {
let base = rng.gen_range(0, 1000);
match base {
match item_type_num {
0...500 => ItemType::Usable,
501...1000 => ItemType::Prop,
_ => ItemType::Prop,
Expand All @@ -172,6 +165,15 @@ impl Rand for ItemType {
}
}

impl Rand for ItemType {
fn rand<R: Rng>(rng: &mut R) -> ItemType {
let item_class_num = rng.gen_range(0, 1000);
let item_type_num = rng.gen_range(0, 1000);

ItemType::by_num(item_class_num, item_type_num)
}
}

/// A type defining the rarity of an item
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ItemRarity {
Expand All @@ -187,11 +189,10 @@ pub enum ItemRarity {
Legendary,
}

impl Rand for ItemRarity {
fn rand<R: Rng>(rng: &mut R) -> ItemRarity {
let base = rng.gen_range(0, 1000);

match base {
impl ItemRarity {
/// A helper method to get an ItemRarity
pub fn by_num(item_rarity_num: u32) -> ItemRarity {
match item_rarity_num {
0...750 => ItemRarity::Common,
751...917 => ItemRarity::Uncommon,
918...972 => ItemRarity::Rare,
Expand All @@ -202,6 +203,14 @@ impl Rand for ItemRarity {
}
}

impl Rand for ItemRarity {
fn rand<R: Rng>(rng: &mut R) -> ItemRarity {
let base = rng.gen_range(0, 1000);

ItemRarity::by_num(base)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -225,4 +234,31 @@ mod tests {
let head_piece = item_generator::ItemGenerator::new().stack_size(1).gen();
assert!(!head_piece.can_be_stacked());
}

#[test]
fn item_rarity() {
assert_eq!(ItemRarity::by_num(0), ItemRarity::Common);
assert_eq!(ItemRarity::by_num(750), ItemRarity::Common);

assert_eq!(ItemRarity::by_num(751), ItemRarity::Uncommon);
assert_eq!(ItemRarity::by_num(917), ItemRarity::Uncommon);

assert_eq!(ItemRarity::by_num(918), ItemRarity::Rare);
assert_eq!(ItemRarity::by_num(972), ItemRarity::Rare);

assert_eq!(ItemRarity::by_num(973), ItemRarity::Epic);
assert_eq!(ItemRarity::by_num(979), ItemRarity::Epic);

assert_eq!(ItemRarity::by_num(980), ItemRarity::Legendary);
assert_eq!(ItemRarity::by_num(1000), ItemRarity::Legendary);
}

#[test]
fn item_type() {
for class_num in (0..1000) {
for type_num in (0..1000) {
ItemType::by_num(class_num, type_num);
}
}
}
}

0 comments on commit 1a7b0c6

Please sign in to comment.