session_rust/
color_test.rs1#[cfg(test)]
2mod tests {
3 use crate::Color;
4
5 #[test]
6 fn test_color_constructor() {
7 let mut red = Color::new(255, 0, 0, 255);
8 red.name = "red".to_string();
9 assert_eq!(red.name, "red");
10 assert!(!red.guid.to_string().is_empty());
11 assert_eq!(red.r, 255);
12 assert_eq!(red.g, 0);
13 assert_eq!(red.b, 0);
14 assert_eq!(red.a, 255);
15 }
16
17 #[test]
18 fn test_color_equality() {
19 let c1 = Color::new(0, 100, 50, 200);
20 let c2 = Color::new(0, 100, 50, 200);
21 assert_eq!(c1.r, c2.r);
24 assert_eq!(c1.g, c2.g);
25 assert_eq!(c1.b, c2.b);
26 assert_eq!(c1.a, c2.a);
27
28 let c3 = Color::new(0, 100, 50, 200);
29 let c4 = Color::new(1, 100, 50, 200);
30 assert_ne!(c3.r, c4.r);
31 }
32
33 #[test]
34 fn test_color_to_json_data() {
35 let mut color = Color::new(128, 64, 192, 255);
36 color.name = "purple".to_string();
37
38 let json_string = color.to_json_data().unwrap();
39 let data: serde_json::Value = serde_json::from_str(&json_string).unwrap();
40
41 assert_eq!(data["type"], "Color");
42 assert_eq!(data["name"], "purple");
43 assert_eq!(data["r"], 128);
44 assert_eq!(data["g"], 64);
45 assert_eq!(data["b"], 192);
46 assert_eq!(data["a"], 255);
47 assert!(data["guid"].is_string());
48 }
49
50 #[test]
51 fn test_color_from_json_data() {
52 let mut original_color = Color::new(200, 150, 100, 255);
53 original_color.name = "bronze".to_string();
54
55 let json_string = original_color.to_json_data().unwrap();
56 let restored_color = Color::from_json_data(&json_string).unwrap();
57
58 assert_eq!(restored_color.r, 200);
59 assert_eq!(restored_color.g, 150);
60 assert_eq!(restored_color.b, 100);
61 assert_eq!(restored_color.a, 255);
62 assert_eq!(restored_color.name, "bronze");
63 assert_eq!(restored_color.guid, original_color.guid);
64 }
65
66 #[test]
67 fn test_color_to_json_from_json() {
68 let mut original = Color::new(255, 128, 64, 255);
69 original.name = "sunset_orange".to_string();
70 let filename = "test_color.json";
71
72 original.to_json(filename).unwrap();
73 let loaded = Color::from_json(filename).unwrap();
74
75 assert_eq!(loaded.r, original.r);
76 assert_eq!(loaded.g, original.g);
77 assert_eq!(loaded.b, original.b);
78 assert_eq!(loaded.a, original.a);
79 assert_eq!(loaded.name, original.name);
80 assert_eq!(loaded.guid, original.guid);
81 }
82
83 #[test]
84 fn test_color_white() {
85 let white = Color::white();
86 assert_eq!(white.name, "white");
87 assert_eq!(white.r, 255);
88 assert_eq!(white.g, 255);
89 assert_eq!(white.b, 255);
90 assert_eq!(white.a, 255);
91 }
92
93 #[test]
94 fn test_color_black() {
95 let black = Color::black();
96 assert_eq!(black.name, "black");
97 assert_eq!(black.r, 0);
98 assert_eq!(black.g, 0);
99 assert_eq!(black.b, 0);
100 assert_eq!(black.a, 255);
101 }
102
103 #[test]
104 fn test_color_to_float_array() {
105 let color = Color::new(255, 128, 64, 255);
106 let float_array = color.to_float_array();
107 assert_eq!(float_array, [1.0, 0.5019608, 0.2509804, 1.0]);
108 }
109
110 #[test]
111 fn test_color_from_float() {
112 let color = Color::from_float(1.0, 0.5, 0.25, 1.0);
113 assert_eq!(color.r, 255);
114 assert_eq!(color.g, 128); assert_eq!(color.b, 64); assert_eq!(color.a, 255);
117 }
118}