From 042ecc40e00080ad701cd0f8bc94479fe324184d Mon Sep 17 00:00:00 2001 From: projectmoon Date: Sun, 31 Jan 2021 14:57:15 +0000 Subject: [PATCH] Properly format dice plurality in CofD dice pools. Fixes #30. --- src/cofd/dice.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/cofd/dice.rs b/src/cofd/dice.rs index 6a2a3e5..82dc764 100644 --- a/src/cofd/dice.rs +++ b/src/cofd/dice.rs @@ -141,10 +141,11 @@ impl RolledDicePool { impl fmt::Display for RolledDicePool { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let dice_plural = if self.num_dice == 1 { "die" } else { "dice" }; write!( f, - "{} dice ({}, exceptional on {} successes)", - self.num_dice, self.modifiers.quality, self.modifiers.exceptional_on + "{} {} ({}, exceptional on {} successes)", + self.num_dice, dice_plural, self.modifiers.quality, self.modifiers.exceptional_on ) } } @@ -654,6 +655,30 @@ mod tests { } //Format tests + #[test] + fn formats_rolled_dice_pool_single_die() { + let pool = DicePool::easy_pool(5, DicePoolQuality::TenAgain); + let rolled_pool = RolledDicePool::from(&pool, 1, vec![1]); + let message = format!("{}", rolled_pool); + assert!(message.starts_with("1 die")); + } + + #[test] + fn formats_rolled_dice_pool_multiple_dice() { + let pool = DicePool::easy_pool(5, DicePoolQuality::TenAgain); + let rolled_pool = RolledDicePool::from(&pool, 2, vec![1, 2]); + let message = format!("{}", rolled_pool); + assert!(message.starts_with("2 dice")); + } + + #[test] + fn formats_rolled_dice_pool_zero_dice() { + let pool = DicePool::easy_pool(5, DicePoolQuality::TenAgain); + let rolled_pool = RolledDicePool::from(&pool, 0, vec![]); + let message = format!("{}", rolled_pool); + assert!(message.starts_with("0 dice")); + } + #[test] fn formats_dramatic_failure_test() { let result = DicePoolRoll {