From 118d157622413af0ff415ce189464825ec7bd746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Araya=20Jim=C3=A9nez?= Date: Wed, 9 Jul 2025 22:35:08 -0600 Subject: [PATCH] feat: implement conversion and display functionality for BeastType enum --- .../combat_game/src/types/beast_type.cairo | 140 +++++++++++++++++- 1 file changed, 139 insertions(+), 1 deletion(-) diff --git a/backend/dojo_examples/combat_game/src/types/beast_type.cairo b/backend/dojo_examples/combat_game/src/types/beast_type.cairo index 7a6a43f..d06f1cd 100644 --- a/backend/dojo_examples/combat_game/src/types/beast_type.cairo +++ b/backend/dojo_examples/combat_game/src/types/beast_type.cairo @@ -1,10 +1,45 @@ -#[derive(Introspect, Copy, Drop, Serde, Debug, PartialEq)] +#[derive(Copy, Drop, Serde, Debug, Introspect, PartialEq)] pub enum BeastType { Light, Magic, Shadow, } +pub impl IntoBeastTypeFelt252 of Into { + #[inline(always)] + fn into(self: BeastType) -> felt252 { + match self { + BeastType::Light => 0, + BeastType::Magic => 1, + BeastType::Shadow => 2, + } + } +} + +pub impl IntoBeastTypeU8 of Into { + #[inline(always)] + fn into(self: BeastType) -> u8 { + match self { + BeastType::Light => 0, + BeastType::Magic => 1, + BeastType::Shadow => 2, + } + } +} + +pub impl Intou8BeastType of Into { + #[inline(always)] + fn into(self: u8) -> BeastType { + let beast_type: u8 = self.into(); + match beast_type { + 0 => BeastType::Light, + 1 => BeastType::Magic, + 2 => BeastType::Shadow, + _ => BeastType::Light, // Default fallback + } + } +} + pub impl BeastTypeDisplay of core::fmt::Display { fn fmt(self: @BeastType, ref f: core::fmt::Formatter) -> Result<(), core::fmt::Error> { let s = match self { @@ -16,3 +51,106 @@ pub impl BeastTypeDisplay of core::fmt::Display { Result::Ok(()) } } + +#[cfg(test)] +mod tests { + use super::BeastType; + + #[test] + fn test_into_beast_type_light() { + let beast_type = BeastType::Light; + let beast_type_felt252: felt252 = beast_type.into(); + assert_eq!(beast_type_felt252, 0); + } + + #[test] + fn test_into_beast_type_magic() { + let beast_type = BeastType::Magic; + let beast_type_felt252: felt252 = beast_type.into(); + assert_eq!(beast_type_felt252, 1); + } + + #[test] + fn test_into_beast_type_shadow() { + let beast_type = BeastType::Shadow; + let beast_type_felt252: felt252 = beast_type.into(); + assert_eq!(beast_type_felt252, 2); + } + + #[test] + fn test_into_beast_type_u8_light() { + let beast_type = BeastType::Light; + let beast_type_u8: u8 = beast_type.into(); + assert_eq!(beast_type_u8, 0); + } + + #[test] + fn test_into_beast_type_u8_magic() { + let beast_type = BeastType::Magic; + let beast_type_u8: u8 = beast_type.into(); + assert_eq!(beast_type_u8, 1); + } + + #[test] + fn test_into_beast_type_u8_shadow() { + let beast_type = BeastType::Shadow; + let beast_type_u8: u8 = beast_type.into(); + assert_eq!(beast_type_u8, 2); + } + + #[test] + fn test_into_beast_type_from_u8_light() { + let beast_type_u8: u8 = 0; + let beast_type: BeastType = beast_type_u8.into(); + assert_eq!(beast_type, BeastType::Light); + } + + #[test] + fn test_into_beast_type_from_u8_magic() { + let beast_type_u8: u8 = 1; + let beast_type: BeastType = beast_type_u8.into(); + assert_eq!(beast_type, BeastType::Magic); + } + + #[test] + fn test_into_beast_type_from_u8_shadow() { + let beast_type_u8: u8 = 2; + let beast_type: BeastType = beast_type_u8.into(); + assert_eq!(beast_type, BeastType::Shadow); + } + + #[test] + fn test_into_beast_type_from_u8_invalid() { + let beast_type_u8: u8 = 3; + let beast_type: BeastType = beast_type_u8.into(); + assert_eq!(beast_type, BeastType::Light); // Default fallback + } + + #[test] + fn test_into_beast_type_from_u8_255_edge_case() { + let beast_type_u8: u8 = 255; + let beast_type: BeastType = beast_type_u8.into(); + assert_eq!(beast_type, BeastType::Light); // Default fallback + } + + #[test] + fn test_beast_type_display_light() { + let beast_type = BeastType::Light; + let display_string = format!("{}", beast_type); + assert_eq!(display_string, "Light"); + } + + #[test] + fn test_beast_type_display_magic() { + let beast_type = BeastType::Magic; + let display_string = format!("{}", beast_type); + assert_eq!(display_string, "Magic"); + } + + #[test] + fn test_beast_type_display_shadow() { + let beast_type = BeastType::Shadow; + let display_string = format!("{}", beast_type); + assert_eq!(display_string, "Shadow"); + } +} \ No newline at end of file