From f84beebb6fb5d841138afa6c939c78dd3cb936b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Mon, 12 Jan 2026 15:28:13 +0100 Subject: [PATCH] feat(character): add Character impl and tests - new, consume_mana, attack, get_name - (test): test_{build,attack,mana,chaining} --- src/character.rs | 69 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/src/character.rs b/src/character.rs index e311d51..9a290c8 100644 --- a/src/character.rs +++ b/src/character.rs @@ -1,6 +1,73 @@ -struct Character { +pub struct Character { life: u8, damage: u8, mana: u8, name: &'static str, } + +impl Character { + pub fn new(life: u8, damage: u8, mana: u8, name: &'static str) -> Self { + Character { + life, + damage, + mana, + name, + } + } + + pub fn attack(&mut self, other: &mut Self) -> &mut Self { + other.life -= self.damage; + self + } + + pub fn consume_mana(&mut self, consumption: u8) -> &mut Self { + self.mana -= consumption; + self + } + + pub fn get_name(&self) -> &str { + self.name + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn character_build() { + let c = Character::new(10, 5, 3, "test1"); + assert_eq!(c.life, 10); + assert_eq!(c.damage, 5); + assert_eq!(c.mana, 3); + assert_eq!(c.name, c.get_name()); + } + + #[test] + fn character_attack() { + let mut c1 = Character::new(10, 3, 5, "test1"); + let mut c2 = Character::new(10, 2, 5, "test2"); + + c1.attack(&mut c2); + assert_eq!(c2.life, 7); + Character::attack(&mut c2, &mut c1); + assert_eq!(c1.life, 8); + } + + #[test] + fn character_mana() { + let mut c = Character::new(10, 3, 15, "test1"); + c.consume_mana(10); + assert_eq!(c.mana, 5); + } + + #[test] + fn character_chaining() { + let mut c1 = Character::new(10, 3, 15, "test1"); + let mut c2 = Character::new(10, 2, 15, "test2"); + + c1.attack(&mut c2).consume_mana(10).attack(&mut c2); + assert_eq!(c2.life, 4); + assert_eq!(c1.mana, 5); + } +}