From 028e48b0a92125232a77e233f6dd4b56435f312a Mon Sep 17 00:00:00 2001 From: B_head Date: Sat, 21 May 2022 02:09:11 +0900 Subject: [PATCH 1/2] Add missing tests to `#[methods]` macro - Minimal derive - Without inherit - Without base prameter - Deref return - Rename method - `#[godot]` attribute --- test/src/test_derive.rs | 257 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 256 insertions(+), 1 deletion(-) diff --git a/test/src/test_derive.rs b/test/src/test_derive.rs index 0b3364dff..6347f4f9d 100644 --- a/test/src/test_derive.rs +++ b/test/src/test_derive.rs @@ -1,4 +1,5 @@ -use std::cell::Cell; +use std::cell::{self, Cell, RefCell}; +use std::rc::Rc; use gdnative::export::Property; use gdnative::prelude::*; @@ -8,7 +9,14 @@ pub(crate) fn run_tests() -> bool { status &= test_derive_to_variant(); status &= test_derive_owned_to_variant(); + status &= test_derive_nativeclass(); status &= test_derive_nativeclass_without_constructor(); + status &= test_derive_nativeclass_without_inherit(); + status &= test_derive_nativeclass_godot_attr_without_base(); + status &= test_derive_nativeclass_godot_attr_with_base(); + status &= test_derive_nativeclass_godot_attr_deref_return(); + status &= test_derive_nativeclass_godot_attr_rename_method(); + status &= test_derive_nativeclass_godot_attr_to_use_all_macro_parameters(); status &= test_derive_nativeclass_with_property_get_set(); status &= test_derive_nativeclass_property_with_only_getter(); @@ -16,7 +24,14 @@ pub(crate) fn run_tests() -> bool { } pub(crate) fn register(handle: InitHandle) { + handle.add_class::(); handle.add_class::(); + handle.add_class::(); + handle.add_class::(); + handle.add_class::(); + handle.add_class::(); + handle.add_class::(); + handle.add_class::(); handle.add_class::(); handle.add_class::(); } @@ -178,6 +193,39 @@ fn test_derive_owned_to_variant() -> bool { ok } +#[derive(NativeClass)] +#[inherit(Reference)] +struct MinimalDerive(i64); + +#[methods] +impl MinimalDerive { + fn new(_owner: &Reference) -> Self { + Self(54) + } + + #[export] + fn answer(&self, _owner: &Reference) -> i64 { + self.0 + } +} + +fn test_derive_nativeclass() -> bool { + println!(" -- test_derive_nativeclass"); + + let ok = std::panic::catch_unwind(|| { + let thing = Instance::::new(); + let base: Ref = thing.into_base(); + assert_eq!(unsafe { base.call("answer", &[]).to::() }, Some(54)); + }) + .is_ok(); + + if !ok { + godot_error!(" !! Test test_derive_nativeclass failed"); + } + + ok +} + #[derive(NativeClass)] #[inherit(Reference)] #[no_constructor] @@ -215,6 +263,213 @@ fn test_derive_nativeclass_without_constructor() -> bool { ok } +#[derive(NativeClass)] +struct WithoutInherit(i64); + +#[methods] +impl WithoutInherit { + fn new(_owner: &Reference) -> Self { + Self(54) + } + + #[export] + fn answer(&self, _owner: &Reference) -> i64 { + self.0 + } +} + +fn test_derive_nativeclass_without_inherit() -> bool { + println!(" -- test_derive_nativeclass_without_inherit"); + + let ok = std::panic::catch_unwind(|| { + let thing = Instance::::new(); + let base = thing.into_base(); + assert_eq!(unsafe { base.call("answer", &[]).to::() }, Some(54)); + }) + .is_ok(); + + if !ok { + godot_error!(" !! Test test_derive_nativeclass_without_inherit failed"); + } + + ok +} + +#[derive(NativeClass)] +#[inherit(Reference)] +struct GodotAttrWithoutBase(i64); + +#[methods] +impl GodotAttrWithoutBase { + fn new(_owner: &Reference) -> Self { + Self(54) + } + + #[godot] + fn answer(&self) -> i64 { + self.0 + } +} + +fn test_derive_nativeclass_godot_attr_without_base() -> bool { + println!(" -- test_derive_nativeclass_godot_attr_without_base"); + + let ok = std::panic::catch_unwind(|| { + let thing = Instance::::new(); + let base = thing.into_base(); + assert_eq!(unsafe { base.call("answer", &[]).to::() }, Some(54)); + }) + .is_ok(); + + if !ok { + godot_error!(" !! Test test_derive_nativeclass_godot_attr_without_base failed"); + } + + ok +} + +#[derive(NativeClass)] +#[inherit(Reference)] +struct GodotAttrWithBase(i64); + +#[methods] +impl GodotAttrWithBase { + fn new(_owner: &Reference) -> Self { + Self(54) + } + + #[godot] + fn answer(&self, #[base] _base: &Reference) -> i64 { + self.0 + } +} + +fn test_derive_nativeclass_godot_attr_with_base() -> bool { + println!(" -- test_derive_nativeclass_godot_attr_with_base"); + + let ok = std::panic::catch_unwind(|| { + let thing = Instance::::new(); + let base = thing.into_base(); + assert_eq!(unsafe { base.call("answer", &[]).to::() }, Some(54)); + }) + .is_ok(); + + if !ok { + godot_error!(" !! Test test_derive_nativeclass_godot_attr_with_base failed"); + } + + ok +} + +#[derive(NativeClass)] +#[inherit(Reference)] +struct GodotAttrDerefReturn(Rc>>); + +#[methods] +impl GodotAttrDerefReturn { + fn new(_owner: &Reference) -> Self { + let vec = Vec::from([12, 34]); + let rc_ref = Rc::new(RefCell::new(vec)); + Self(rc_ref) + } + + #[godot(deref_return)] + fn answer(&self) -> cell::Ref> { + self.0.borrow() + } +} + +fn test_derive_nativeclass_godot_attr_deref_return() -> bool { + println!(" -- test_derive_nativeclass_godot_attr_deref_return"); + + let ok = std::panic::catch_unwind(|| { + let thing = Instance::::new(); + let base = thing.into_base(); + + let res = unsafe { base.call("answer", &[]).to::>() }; + assert_eq!(res, Some([12, 34].into())); + }) + .is_ok(); + + if !ok { + godot_error!(" !! Test test_derive_nativeclass_godot_attr_deref_return failed"); + } + + ok +} + +#[derive(NativeClass)] +#[inherit(Reference)] +struct GodotAttrRenameMethod(i64); + +#[methods] +impl GodotAttrRenameMethod { + fn new(_owner: &Reference) -> Self { + Self(54) + } + + #[godot(name = "ask")] + fn answer(&self) -> i64 { + self.0 + } +} + +fn test_derive_nativeclass_godot_attr_rename_method() -> bool { + println!(" -- test_derive_nativeclass_godot_attr_rename_method"); + + let ok = std::panic::catch_unwind(|| { + let thing = Instance::::new(); + let base = thing.into_base(); + assert_eq!(unsafe { base.call("ask", &[]).to::() }, Some(54)); + }) + .is_ok(); + + if !ok { + godot_error!(" !! Test test_derive_nativeclass_godot_attr_rename_method failed"); + } + + ok +} + +#[derive(NativeClass)] +#[inherit(Reference)] +struct GodotAttrToUseAllMacroParameters(Rc>>); + +#[methods] +impl GodotAttrToUseAllMacroParameters { + fn new(_owner: &Reference) -> Self { + let vec = Vec::from([12, 34]); + let rc_ref = Rc::new(RefCell::new(vec)); + Self(rc_ref) + } + + #[godot(rpc = "disabled", name = "ask", deref_return)] + fn answer(&self, #[base] _base: &Reference) -> cell::Ref> { + self.0.borrow() + } +} + +fn test_derive_nativeclass_godot_attr_to_use_all_macro_parameters() -> bool { + println!(" -- test_derive_nativeclass_godot_attr_to_use_all_macro_parameters"); + + let ok = std::panic::catch_unwind(|| { + let thing = Instance::::new(); + let base = thing.into_base(); + + let res = unsafe { base.call("ask", &[]).to::>() }; + assert_eq!(res, Some([12, 34].into())); + }) + .is_ok(); + + if !ok { + godot_error!( + " !! Test test_derive_nativeclass_godot_attr_to_use_all_macro_parameters failed" + ); + } + + ok +} + #[derive(NativeClass)] #[inherit(Node)] struct CustomGetSet { From 30385bbe76ad5d85b1428e365d0c3acbc25d598b Mon Sep 17 00:00:00 2001 From: B_head Date: Sat, 21 May 2022 06:41:55 +0900 Subject: [PATCH 2/2] Update trybuild tests for Rust 1.61.0 --- gdnative/tests/ui/derive_fail_methods.stderr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gdnative/tests/ui/derive_fail_methods.stderr b/gdnative/tests/ui/derive_fail_methods.stderr index 9d27a57ff..480bf5c5f 100644 --- a/gdnative/tests/ui/derive_fail_methods.stderr +++ b/gdnative/tests/ui/derive_fail_methods.stderr @@ -2,4 +2,4 @@ error: cannot find attribute `export` in this scope --> $DIR/derive_fail_methods.rs:12:7 | 12 | #[export] - | ^^^^^^ + | ^^^^^^ help: a built-in attribute with a similar name exists: `expect`