|
| 1 | +/* |
| 2 | + * Copyright (c) godot-rust; Bromeon and contributors. |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | + |
| 8 | +use godot::builtin::{Array, Dictionary, GString, StringName}; |
| 9 | +use godot::classes::IObject; |
| 10 | +use godot::global::{PropertyHint, PropertyUsageFlags}; |
| 11 | +use godot::meta::PropertyInfo; |
| 12 | +use godot::obj::NewAlloc; |
| 13 | +use godot::register::{godot_api, GodotClass}; |
| 14 | +use godot::test::itest; |
| 15 | + |
| 16 | +#[derive(GodotClass)] |
| 17 | +#[class(base = Object, init)] |
| 18 | +pub struct ValidatePropertyTest { |
| 19 | + #[var(hint = NONE, hint_string = "initial")] |
| 20 | + #[export] |
| 21 | + my_var: i64, |
| 22 | +} |
| 23 | + |
| 24 | +#[godot_api] |
| 25 | +impl IObject for ValidatePropertyTest { |
| 26 | + fn validate_property(&self, property: &mut PropertyInfo) { |
| 27 | + if property.property_name.to_string() == "my_var" { |
| 28 | + property.usage = PropertyUsageFlags::NO_EDITOR; |
| 29 | + property.property_name = StringName::from("SuperNewTestPropertyName"); |
| 30 | + property.hint_info.hint_string = GString::from("SomePropertyHint"); |
| 31 | + property.hint_info.hint = PropertyHint::TYPE_STRING; |
| 32 | + |
| 33 | + // Makes no sense, but allows to check if given ClassName can be properly moved to GDExtensionPropertyInfo. |
| 34 | + property.class_name = <ValidatePropertyTest as godot::obj::GodotClass>::class_name(); |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +#[itest] |
| 40 | +fn validate_property_test() { |
| 41 | + let obj = ValidatePropertyTest::new_alloc(); |
| 42 | + let properties: Array<Dictionary> = obj.get_property_list(); |
| 43 | + |
| 44 | + let property = properties |
| 45 | + .iter_shared() |
| 46 | + .find(|dict| { |
| 47 | + dict.get("name") |
| 48 | + .is_some_and(|v| v.to_string() == "SuperNewTestPropertyName") |
| 49 | + }) |
| 50 | + .expect("Test failed – unable to find validated property."); |
| 51 | + |
| 52 | + let hint_string = property |
| 53 | + .get("hint_string") |
| 54 | + .expect("validated property dict should contain a `hint_string` entry.") |
| 55 | + .to::<GString>(); |
| 56 | + assert_eq!(hint_string, GString::from("SomePropertyHint")); |
| 57 | + |
| 58 | + let class = property |
| 59 | + .get("class_name") |
| 60 | + .expect("Validated property dict should contain a class_name entry.") |
| 61 | + .to::<StringName>(); |
| 62 | + assert_eq!(class, StringName::from("ValidatePropertyTest")); |
| 63 | + |
| 64 | + let usage = property |
| 65 | + .get("usage") |
| 66 | + .expect("Validated property dict should contain an usage entry.") |
| 67 | + .to::<PropertyUsageFlags>(); |
| 68 | + assert_eq!(usage, PropertyUsageFlags::NO_EDITOR); |
| 69 | + |
| 70 | + let hint = property |
| 71 | + .get("hint") |
| 72 | + .expect("Validated property dict should contain a hint entry.") |
| 73 | + .to::<PropertyHint>(); |
| 74 | + assert_eq!(hint, PropertyHint::TYPE_STRING); |
| 75 | + |
| 76 | + obj.free(); |
| 77 | +} |
0 commit comments