diff options
Diffstat (limited to 'src/dice.rs')
-rw-r--r-- | src/dice.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/dice.rs b/src/dice.rs index cd6cf29..a2a3529 100644 --- a/src/dice.rs +++ b/src/dice.rs @@ -2,6 +2,7 @@ use rand::Rng; use std::fmt; use std::fmt::Formatter; +#[derive(PartialEq, Debug)] pub struct DieRoll { sides: u32, face: u32, @@ -92,3 +93,35 @@ pub fn roll_formula(formula: &str) -> Option<RollResult> { Some(roll_result) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_roll_result_total() { + let mut roll_result = RollResult::new(); + roll_result.add_roll(DieRoll::new(6, 4)); + roll_result.add_roll(DieRoll::new(6, 2)); + roll_result.add_roll(DieRoll::new(6, 6)); + assert_eq!(roll_result.total(), 12); + } + + #[test] + fn test_increase_sides_below_max() { + let mut roll_result = RollResult::new(); + roll_result.add_roll(DieRoll::new(6, 4)); + roll_result.add_roll(DieRoll::new(6, 2)); + roll_result.add_roll(DieRoll::new(6, 6)); + + roll_result.increase_sides_below_max(6, 1); + + let expected_rolls = vec![ + DieRoll::new(6, 5), // Increased from 4. + DieRoll::new(6, 3), // Increased from 2. + DieRoll::new(6, 6), // Remains the same, as it's 6. + ]; + + assert_eq!(roll_result.rolls, expected_rolls); + } +} |