Skip to content

Commit 19d370a

Browse files
committed
Add some failing tests that need fixed
1 parent 15757f3 commit 19d370a

File tree

1 file changed

+129
-45
lines changed

1 file changed

+129
-45
lines changed

test/src/test_serde.rs

+129-45
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
11
use ::serde::{Deserialize, Serialize};
22
use gdnative::prelude::*;
33

4+
pub(crate) fn run_tests() -> bool {
5+
let mut status = true;
6+
7+
//These [de]serialize each field individually, instead of going through ToVariant/FromVariant
8+
status &= test_ron_round_trip();
9+
status &= test_json_round_trip();
10+
11+
let mut eq_works = true;
12+
eq_works &= test_variant_eq();
13+
eq_works &= test_dispatch_eq();
14+
//All other tests depend on these invariants
15+
if !eq_works {
16+
gdnative::godot_error!(
17+
" !!!! Can't run remaining serde tests, ToVariant/FromVariant is broken!"
18+
);
19+
return false;
20+
}
21+
22+
status &= test_ron_disp_round_trip();
23+
status &= test_ron_de_disp_as_variant();
24+
status &= test_json_disp_round_trip();
25+
status &= test_json_de_disp_as_variant();
26+
status &= test_bincode_round_trip();
27+
28+
status
29+
}
30+
431
#[derive(Debug, PartialEq, Serialize, Deserialize, ToVariant, FromVariant)]
532
struct Foo {
633
some: Option<bool>,
@@ -77,46 +104,23 @@ impl Foo {
77104
]),
78105
vec3_arr: Vector3Array::from_slice(&[
79106
Vector3::ONE * 41.0,
80-
Vector3::BACK,
81-
Vector3::FORWARD,
107+
Vector3::BACK * 42.43,
108+
Vector3::FORWARD * 44.45,
82109
]),
83110
color_arr: ColorArray::from_slice(&[Color::from_rgba(0.0, 1.0, 0.627, 0.8)]),
84111
}
85112
}
86113
}
87114

88-
pub(crate) fn run_tests() -> bool {
89-
let mut status = true;
90-
91-
//These [de]serialize each field individually, instead of going through ToVariant/FromVariant
92-
status &= test_ron_round_trip();
93-
status &= test_json_round_trip();
94-
95-
let mut eq_works = true;
96-
eq_works &= test_variant_eq();
97-
eq_works &= test_dispatch_eq();
98-
//All other tests depend on these invariants
99-
if !eq_works {
100-
gdnative::godot_error!(
101-
" !!!! Can't run remaining serde tests, ToVariant/FromVariant is broken!"
102-
);
103-
return false;
104-
}
105-
106-
status &= test_bincode_round_trip();
107-
108-
status
109-
}
110-
111115
/// Sanity check that a round trip through Variant preserves equality for Foo.
112116
fn test_variant_eq() -> bool {
113117
println!(" -- test_variant_eq");
114118

115119
let ok = std::panic::catch_unwind(|| {
116-
let test = Foo::new();
117-
let variant = test.to_variant();
118-
let test_again = Foo::from_variant(&variant).unwrap();
119-
assert_eq!(test, test_again);
120+
let foo = Foo::new();
121+
let variant = foo.to_variant();
122+
let result = Foo::from_variant(&variant).unwrap();
123+
assert_eq!(foo, result);
120124
})
121125
.is_ok();
122126

@@ -132,10 +136,10 @@ fn test_dispatch_eq() -> bool {
132136
println!(" -- test_variant_eq");
133137

134138
let ok = std::panic::catch_unwind(|| {
135-
let test = Foo::new();
136-
let dispatch = test.to_variant().dispatch();
137-
let test_again = Foo::from_variant(&Variant::from(&dispatch)).unwrap();
138-
assert_eq!(test, test_again);
139+
let foo = Foo::new();
140+
let dispatch = foo.to_variant().dispatch();
141+
let result = Foo::from_variant(&Variant::from(&dispatch)).unwrap();
142+
assert_eq!(foo, result);
139143
})
140144
.is_ok();
141145

@@ -150,11 +154,11 @@ fn test_ron_round_trip() -> bool {
150154
println!(" -- test_ron_round_trip");
151155

152156
let ok = std::panic::catch_unwind(|| {
153-
let test = Foo::new();
154-
let test_str = ron::to_string(&test);
157+
let foo = Foo::new();
158+
let test_str = ron::to_string(&foo);
155159
let mut de = ron::Deserializer::from_str(test_str.as_ref().unwrap());
156-
let test_again = Foo::deserialize(de.as_mut().unwrap()).unwrap();
157-
assert_eq!(test, test_again)
160+
let result = Foo::deserialize(de.as_mut().unwrap()).unwrap();
161+
assert_eq!(foo, result)
158162
})
159163
.is_ok();
160164

@@ -165,14 +169,55 @@ fn test_ron_round_trip() -> bool {
165169
ok
166170
}
167171

172+
fn test_ron_disp_round_trip() -> bool {
173+
println!(" -- test_ron_disp_round_trip");
174+
175+
let ok = std::panic::catch_unwind(|| {
176+
let foo = Foo::new();
177+
let test_str = ron::to_string(&foo.to_variant().dispatch());
178+
let mut de = ron::Deserializer::from_str(test_str.as_ref().unwrap());
179+
let disp = VariantDispatch::deserialize(de.as_mut().unwrap()).unwrap();
180+
let result = Foo::from_variant(&Variant::from(&disp)).unwrap();
181+
assert_eq!(foo, result)
182+
})
183+
.is_ok();
184+
185+
if !ok {
186+
gdnative::godot_error!(" !! Test test_ron_disp_round_trip failed");
187+
}
188+
189+
ok
190+
}
191+
192+
fn test_ron_de_disp_as_variant() -> bool {
193+
println!(" -- test_ron_de_disp_as_variant");
194+
195+
let ok = std::panic::catch_unwind(|| {
196+
let foo = Foo::new();
197+
let test_str = ron::to_string(&foo.to_variant().dispatch());
198+
godot_dbg!(&test_str);
199+
let mut de = ron::Deserializer::from_str(test_str.as_ref().unwrap());
200+
let variant = Variant::deserialize(de.as_mut().unwrap()).unwrap();
201+
let result = Foo::from_variant(&variant).unwrap();
202+
assert_eq!(foo, result)
203+
})
204+
.is_ok();
205+
206+
if !ok {
207+
gdnative::godot_error!(" !! Test test_ron_de_disp_as_variant failed");
208+
}
209+
210+
ok
211+
}
212+
168213
fn test_json_round_trip() -> bool {
169214
println!(" -- test_json_round_trip");
170215

171216
let ok = std::panic::catch_unwind(|| {
172-
let test = Foo::new();
173-
let test_str = serde_json::to_string(&test);
174-
let test_again = serde_json::from_str::<Foo>(test_str.as_ref().unwrap()).unwrap();
175-
assert_eq!(test, test_again)
217+
let foo = Foo::new();
218+
let test_str = serde_json::to_string(&foo);
219+
let result = serde_json::from_str::<Foo>(test_str.as_ref().unwrap()).unwrap();
220+
assert_eq!(foo, result)
176221
})
177222
.is_ok();
178223

@@ -183,15 +228,54 @@ fn test_json_round_trip() -> bool {
183228
ok
184229
}
185230

231+
fn test_json_disp_round_trip() -> bool {
232+
println!(" -- test_json_disp_round_trip");
233+
234+
let ok = std::panic::catch_unwind(|| {
235+
let foo = Foo::new();
236+
let test_str = serde_json::to_string(&foo.to_variant().dispatch());
237+
godot_dbg!(&test_str);
238+
let disp = serde_json::from_str::<VariantDispatch>(test_str.as_ref().unwrap()).unwrap();
239+
let result = Foo::from_variant(&Variant::from(&disp)).unwrap();
240+
assert_eq!(foo, result)
241+
})
242+
.is_ok();
243+
244+
if !ok {
245+
gdnative::godot_error!(" !! Test test_json_disp_round_trip failed");
246+
}
247+
248+
ok
249+
}
250+
251+
fn test_json_de_disp_as_variant() -> bool {
252+
println!(" -- test_json_de_disp_as_variant");
253+
254+
let ok = std::panic::catch_unwind(|| {
255+
let foo = Foo::new();
256+
let test_str = serde_json::to_string(&foo.to_variant().dispatch());
257+
let variant = serde_json::from_str::<Variant>(test_str.as_ref().unwrap()).unwrap();
258+
let result = Foo::from_variant(&variant).unwrap();
259+
assert_eq!(foo, result)
260+
})
261+
.is_ok();
262+
263+
if !ok {
264+
gdnative::godot_error!(" !! Test test_json_de_disp_as_variant failed");
265+
}
266+
267+
ok
268+
}
269+
186270
fn test_bincode_round_trip() -> bool {
187271
println!(" -- test_bincode_round_trip");
188272

189273
let ok = std::panic::catch_unwind(|| {
190-
let test = Foo::new();
191-
let test_bytes = bincode::serialize(&test.to_variant().dispatch());
274+
let foo = Foo::new();
275+
let test_bytes = bincode::serialize(&foo.to_variant().dispatch());
192276
let disp = bincode::deserialize::<VariantDispatch>(test_bytes.as_ref().unwrap()).unwrap();
193-
let test_again = Foo::from_variant(&Variant::from(&disp)).unwrap();
194-
assert_eq!(test, test_again)
277+
let result = Foo::from_variant(&Variant::from(&disp)).unwrap();
278+
assert_eq!(foo, result)
195279
})
196280
.is_ok();
197281

0 commit comments

Comments
 (0)