Skip to content

Commit c475bb1

Browse files
authored
feat: implement conversion and display functionality for BeastType enum (#168)
1 parent e8120b1 commit c475bb1

1 file changed

Lines changed: 139 additions & 1 deletion

File tree

backend/dojo_examples/combat_game/src/types/beast_type.cairo

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,45 @@
1-
#[derive(Introspect, Copy, Drop, Serde, Debug, PartialEq)]
1+
#[derive(Copy, Drop, Serde, Debug, Introspect, PartialEq)]
22
pub enum BeastType {
33
Light,
44
Magic,
55
Shadow,
66
}
77

8+
pub impl IntoBeastTypeFelt252 of Into<BeastType, felt252> {
9+
#[inline(always)]
10+
fn into(self: BeastType) -> felt252 {
11+
match self {
12+
BeastType::Light => 0,
13+
BeastType::Magic => 1,
14+
BeastType::Shadow => 2,
15+
}
16+
}
17+
}
18+
19+
pub impl IntoBeastTypeU8 of Into<BeastType, u8> {
20+
#[inline(always)]
21+
fn into(self: BeastType) -> u8 {
22+
match self {
23+
BeastType::Light => 0,
24+
BeastType::Magic => 1,
25+
BeastType::Shadow => 2,
26+
}
27+
}
28+
}
29+
30+
pub impl Intou8BeastType of Into<u8, BeastType> {
31+
#[inline(always)]
32+
fn into(self: u8) -> BeastType {
33+
let beast_type: u8 = self.into();
34+
match beast_type {
35+
0 => BeastType::Light,
36+
1 => BeastType::Magic,
37+
2 => BeastType::Shadow,
38+
_ => BeastType::Light, // Default fallback
39+
}
40+
}
41+
}
42+
843
pub impl BeastTypeDisplay of core::fmt::Display<BeastType> {
944
fn fmt(self: @BeastType, ref f: core::fmt::Formatter) -> Result<(), core::fmt::Error> {
1045
let s = match self {
@@ -16,3 +51,106 @@ pub impl BeastTypeDisplay of core::fmt::Display<BeastType> {
1651
Result::Ok(())
1752
}
1853
}
54+
55+
#[cfg(test)]
56+
mod tests {
57+
use super::BeastType;
58+
59+
#[test]
60+
fn test_into_beast_type_light() {
61+
let beast_type = BeastType::Light;
62+
let beast_type_felt252: felt252 = beast_type.into();
63+
assert_eq!(beast_type_felt252, 0);
64+
}
65+
66+
#[test]
67+
fn test_into_beast_type_magic() {
68+
let beast_type = BeastType::Magic;
69+
let beast_type_felt252: felt252 = beast_type.into();
70+
assert_eq!(beast_type_felt252, 1);
71+
}
72+
73+
#[test]
74+
fn test_into_beast_type_shadow() {
75+
let beast_type = BeastType::Shadow;
76+
let beast_type_felt252: felt252 = beast_type.into();
77+
assert_eq!(beast_type_felt252, 2);
78+
}
79+
80+
#[test]
81+
fn test_into_beast_type_u8_light() {
82+
let beast_type = BeastType::Light;
83+
let beast_type_u8: u8 = beast_type.into();
84+
assert_eq!(beast_type_u8, 0);
85+
}
86+
87+
#[test]
88+
fn test_into_beast_type_u8_magic() {
89+
let beast_type = BeastType::Magic;
90+
let beast_type_u8: u8 = beast_type.into();
91+
assert_eq!(beast_type_u8, 1);
92+
}
93+
94+
#[test]
95+
fn test_into_beast_type_u8_shadow() {
96+
let beast_type = BeastType::Shadow;
97+
let beast_type_u8: u8 = beast_type.into();
98+
assert_eq!(beast_type_u8, 2);
99+
}
100+
101+
#[test]
102+
fn test_into_beast_type_from_u8_light() {
103+
let beast_type_u8: u8 = 0;
104+
let beast_type: BeastType = beast_type_u8.into();
105+
assert_eq!(beast_type, BeastType::Light);
106+
}
107+
108+
#[test]
109+
fn test_into_beast_type_from_u8_magic() {
110+
let beast_type_u8: u8 = 1;
111+
let beast_type: BeastType = beast_type_u8.into();
112+
assert_eq!(beast_type, BeastType::Magic);
113+
}
114+
115+
#[test]
116+
fn test_into_beast_type_from_u8_shadow() {
117+
let beast_type_u8: u8 = 2;
118+
let beast_type: BeastType = beast_type_u8.into();
119+
assert_eq!(beast_type, BeastType::Shadow);
120+
}
121+
122+
#[test]
123+
fn test_into_beast_type_from_u8_invalid() {
124+
let beast_type_u8: u8 = 3;
125+
let beast_type: BeastType = beast_type_u8.into();
126+
assert_eq!(beast_type, BeastType::Light); // Default fallback
127+
}
128+
129+
#[test]
130+
fn test_into_beast_type_from_u8_255_edge_case() {
131+
let beast_type_u8: u8 = 255;
132+
let beast_type: BeastType = beast_type_u8.into();
133+
assert_eq!(beast_type, BeastType::Light); // Default fallback
134+
}
135+
136+
#[test]
137+
fn test_beast_type_display_light() {
138+
let beast_type = BeastType::Light;
139+
let display_string = format!("{}", beast_type);
140+
assert_eq!(display_string, "Light");
141+
}
142+
143+
#[test]
144+
fn test_beast_type_display_magic() {
145+
let beast_type = BeastType::Magic;
146+
let display_string = format!("{}", beast_type);
147+
assert_eq!(display_string, "Magic");
148+
}
149+
150+
#[test]
151+
fn test_beast_type_display_shadow() {
152+
let beast_type = BeastType::Shadow;
153+
let display_string = format!("{}", beast_type);
154+
assert_eq!(display_string, "Shadow");
155+
}
156+
}

0 commit comments

Comments
 (0)