|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + */ |
| 6 | + |
| 7 | +use crate::framework::itest; |
| 8 | +use godot::builtin::{array, Array, GString, NodePath, StringName, Vector2i}; |
| 9 | +use serde::{Deserialize, Serialize}; |
| 10 | + |
| 11 | +fn serde_roundtrip<T>(value: &T, expected_json: &str) |
| 12 | +where |
| 13 | + T: for<'a> Deserialize<'a> + Serialize + PartialEq + std::fmt::Debug, |
| 14 | +{ |
| 15 | + let json: String = serde_json::to_string(value).unwrap(); |
| 16 | + let back: T = serde_json::from_str(json.as_str()).unwrap(); |
| 17 | + |
| 18 | + assert_eq!(back, *value, "serde round-trip changes value"); |
| 19 | + assert_eq!( |
| 20 | + json, expected_json, |
| 21 | + "value does not conform to expected JSON" |
| 22 | + ); |
| 23 | +} |
| 24 | + |
| 25 | +#[itest] |
| 26 | +fn serde_gstring() { |
| 27 | + let value = GString::from("hello world"); |
| 28 | + |
| 29 | + let expected_json = "\"hello world\""; |
| 30 | + |
| 31 | + serde_roundtrip(&value, expected_json); |
| 32 | +} |
| 33 | + |
| 34 | +#[itest] |
| 35 | +fn serde_node_path() { |
| 36 | + let value = NodePath::from("res://icon.png"); |
| 37 | + let expected_json = "\"res://icon.png\""; |
| 38 | + |
| 39 | + serde_roundtrip(&value, expected_json); |
| 40 | +} |
| 41 | + |
| 42 | +#[itest] |
| 43 | +fn serde_string_name() { |
| 44 | + let value = StringName::from("hello world"); |
| 45 | + let expected_json = "\"hello world\""; |
| 46 | + |
| 47 | + serde_roundtrip(&value, expected_json); |
| 48 | +} |
| 49 | + |
| 50 | +#[itest] |
| 51 | +fn serde_array_rust_native_type() { |
| 52 | + let value: Array<i32> = array![1, 2, 3, 4, 5, 6]; |
| 53 | + |
| 54 | + let expected_json = r#"[1,2,3,4,5,6]"#; |
| 55 | + |
| 56 | + serde_roundtrip(&value, expected_json) |
| 57 | +} |
| 58 | + |
| 59 | +#[itest] |
| 60 | +fn serde_array_godot_builtin_type() { |
| 61 | + let value: Array<GString> = array!["Godot".into(), "Rust".into(), "Rocks".into()]; |
| 62 | + |
| 63 | + let expected_json = r#"["Godot","Rust","Rocks"]"#; |
| 64 | + |
| 65 | + serde_roundtrip(&value, expected_json) |
| 66 | +} |
| 67 | + |
| 68 | +#[itest] |
| 69 | +fn serde_array_godot_type() { |
| 70 | + let value: Array<Vector2i> = array![ |
| 71 | + Vector2i::new(1, 1), |
| 72 | + Vector2i::new(2, 2), |
| 73 | + Vector2i::new(3, 3) |
| 74 | + ]; |
| 75 | + |
| 76 | + let expected_json = r#"[{"x":1,"y":1},{"x":2,"y":2},{"x":3,"y":3}]"#; |
| 77 | + |
| 78 | + serde_roundtrip(&value, expected_json) |
| 79 | +} |
0 commit comments