1
1
use :: serde:: { Deserialize , Serialize } ;
2
2
use gdnative:: prelude:: * ;
3
3
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
+
4
31
#[ derive( Debug , PartialEq , Serialize , Deserialize , ToVariant , FromVariant ) ]
5
32
struct Foo {
6
33
some : Option < bool > ,
@@ -77,46 +104,23 @@ impl Foo {
77
104
] ) ,
78
105
vec3_arr : Vector3Array :: from_slice ( & [
79
106
Vector3 :: ONE * 41.0 ,
80
- Vector3 :: BACK ,
81
- Vector3 :: FORWARD ,
107
+ Vector3 :: BACK * 42.43 ,
108
+ Vector3 :: FORWARD * 44.45 ,
82
109
] ) ,
83
110
color_arr : ColorArray :: from_slice ( & [ Color :: from_rgba ( 0.0 , 1.0 , 0.627 , 0.8 ) ] ) ,
84
111
}
85
112
}
86
113
}
87
114
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
-
111
115
/// Sanity check that a round trip through Variant preserves equality for Foo.
112
116
fn test_variant_eq ( ) -> bool {
113
117
println ! ( " -- test_variant_eq" ) ;
114
118
115
119
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 ) ;
120
124
} )
121
125
. is_ok ( ) ;
122
126
@@ -132,10 +136,10 @@ fn test_dispatch_eq() -> bool {
132
136
println ! ( " -- test_variant_eq" ) ;
133
137
134
138
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 ) ;
139
143
} )
140
144
. is_ok ( ) ;
141
145
@@ -150,11 +154,11 @@ fn test_ron_round_trip() -> bool {
150
154
println ! ( " -- test_ron_round_trip" ) ;
151
155
152
156
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 ) ;
155
159
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 )
158
162
} )
159
163
. is_ok ( ) ;
160
164
@@ -165,14 +169,55 @@ fn test_ron_round_trip() -> bool {
165
169
ok
166
170
}
167
171
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
+
168
213
fn test_json_round_trip ( ) -> bool {
169
214
println ! ( " -- test_json_round_trip" ) ;
170
215
171
216
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 )
176
221
} )
177
222
. is_ok ( ) ;
178
223
@@ -183,15 +228,54 @@ fn test_json_round_trip() -> bool {
183
228
ok
184
229
}
185
230
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
+
186
270
fn test_bincode_round_trip ( ) -> bool {
187
271
println ! ( " -- test_bincode_round_trip" ) ;
188
272
189
273
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 ( ) ) ;
192
276
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 )
195
279
} )
196
280
. is_ok ( ) ;
197
281
0 commit comments