Skip to content

Commit c803753

Browse files
bors[bot]B-head
andauthored
Merge #891
891: Add missing tests to #[methods] macro r=Bromeon a=B-head Added tests for the following features. - Minimal derive - Omitted inherit - Omitted base prameter - Method attribute - Deref return - Rename method I could not add a test for Rpc mode. We need a way to add a node to the scene tree during a test. Co-authored-by: B_head <[email protected]>
2 parents a8a1b4a + 30385bb commit c803753

File tree

2 files changed

+257
-2
lines changed

2 files changed

+257
-2
lines changed

gdnative/tests/ui/derive_fail_methods.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ error: cannot find attribute `export` in this scope
22
--> $DIR/derive_fail_methods.rs:12:7
33
|
44
12 | #[export]
5-
| ^^^^^^
5+
| ^^^^^^ help: a built-in attribute with a similar name exists: `expect`

test/src/test_derive.rs

+256-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::cell::Cell;
1+
use std::cell::{self, Cell, RefCell};
2+
use std::rc::Rc;
23

34
use gdnative::export::Property;
45
use gdnative::prelude::*;
@@ -8,15 +9,29 @@ pub(crate) fn run_tests() -> bool {
89

910
status &= test_derive_to_variant();
1011
status &= test_derive_owned_to_variant();
12+
status &= test_derive_nativeclass();
1113
status &= test_derive_nativeclass_without_constructor();
14+
status &= test_derive_nativeclass_without_inherit();
15+
status &= test_derive_nativeclass_godot_attr_without_base();
16+
status &= test_derive_nativeclass_godot_attr_with_base();
17+
status &= test_derive_nativeclass_godot_attr_deref_return();
18+
status &= test_derive_nativeclass_godot_attr_rename_method();
19+
status &= test_derive_nativeclass_godot_attr_to_use_all_macro_parameters();
1220
status &= test_derive_nativeclass_with_property_get_set();
1321
status &= test_derive_nativeclass_property_with_only_getter();
1422

1523
status
1624
}
1725

1826
pub(crate) fn register(handle: InitHandle) {
27+
handle.add_class::<MinimalDerive>();
1928
handle.add_class::<EmplacementOnly>();
29+
handle.add_class::<WithoutInherit>();
30+
handle.add_class::<GodotAttrWithoutBase>();
31+
handle.add_class::<GodotAttrWithBase>();
32+
handle.add_class::<GodotAttrDerefReturn>();
33+
handle.add_class::<GodotAttrRenameMethod>();
34+
handle.add_class::<GodotAttrToUseAllMacroParameters>();
2035
handle.add_class::<CustomGetSet>();
2136
handle.add_class::<MyVec>();
2237
}
@@ -178,6 +193,39 @@ fn test_derive_owned_to_variant() -> bool {
178193
ok
179194
}
180195

196+
#[derive(NativeClass)]
197+
#[inherit(Reference)]
198+
struct MinimalDerive(i64);
199+
200+
#[methods]
201+
impl MinimalDerive {
202+
fn new(_owner: &Reference) -> Self {
203+
Self(54)
204+
}
205+
206+
#[export]
207+
fn answer(&self, _owner: &Reference) -> i64 {
208+
self.0
209+
}
210+
}
211+
212+
fn test_derive_nativeclass() -> bool {
213+
println!(" -- test_derive_nativeclass");
214+
215+
let ok = std::panic::catch_unwind(|| {
216+
let thing = Instance::<MinimalDerive, _>::new();
217+
let base: Ref<Reference, Unique> = thing.into_base();
218+
assert_eq!(unsafe { base.call("answer", &[]).to::<i64>() }, Some(54));
219+
})
220+
.is_ok();
221+
222+
if !ok {
223+
godot_error!(" !! Test test_derive_nativeclass failed");
224+
}
225+
226+
ok
227+
}
228+
181229
#[derive(NativeClass)]
182230
#[inherit(Reference)]
183231
#[no_constructor]
@@ -215,6 +263,213 @@ fn test_derive_nativeclass_without_constructor() -> bool {
215263
ok
216264
}
217265

266+
#[derive(NativeClass)]
267+
struct WithoutInherit(i64);
268+
269+
#[methods]
270+
impl WithoutInherit {
271+
fn new(_owner: &Reference) -> Self {
272+
Self(54)
273+
}
274+
275+
#[export]
276+
fn answer(&self, _owner: &Reference) -> i64 {
277+
self.0
278+
}
279+
}
280+
281+
fn test_derive_nativeclass_without_inherit() -> bool {
282+
println!(" -- test_derive_nativeclass_without_inherit");
283+
284+
let ok = std::panic::catch_unwind(|| {
285+
let thing = Instance::<WithoutInherit, _>::new();
286+
let base = thing.into_base();
287+
assert_eq!(unsafe { base.call("answer", &[]).to::<i64>() }, Some(54));
288+
})
289+
.is_ok();
290+
291+
if !ok {
292+
godot_error!(" !! Test test_derive_nativeclass_without_inherit failed");
293+
}
294+
295+
ok
296+
}
297+
298+
#[derive(NativeClass)]
299+
#[inherit(Reference)]
300+
struct GodotAttrWithoutBase(i64);
301+
302+
#[methods]
303+
impl GodotAttrWithoutBase {
304+
fn new(_owner: &Reference) -> Self {
305+
Self(54)
306+
}
307+
308+
#[godot]
309+
fn answer(&self) -> i64 {
310+
self.0
311+
}
312+
}
313+
314+
fn test_derive_nativeclass_godot_attr_without_base() -> bool {
315+
println!(" -- test_derive_nativeclass_godot_attr_without_base");
316+
317+
let ok = std::panic::catch_unwind(|| {
318+
let thing = Instance::<GodotAttrWithoutBase, _>::new();
319+
let base = thing.into_base();
320+
assert_eq!(unsafe { base.call("answer", &[]).to::<i64>() }, Some(54));
321+
})
322+
.is_ok();
323+
324+
if !ok {
325+
godot_error!(" !! Test test_derive_nativeclass_godot_attr_without_base failed");
326+
}
327+
328+
ok
329+
}
330+
331+
#[derive(NativeClass)]
332+
#[inherit(Reference)]
333+
struct GodotAttrWithBase(i64);
334+
335+
#[methods]
336+
impl GodotAttrWithBase {
337+
fn new(_owner: &Reference) -> Self {
338+
Self(54)
339+
}
340+
341+
#[godot]
342+
fn answer(&self, #[base] _base: &Reference) -> i64 {
343+
self.0
344+
}
345+
}
346+
347+
fn test_derive_nativeclass_godot_attr_with_base() -> bool {
348+
println!(" -- test_derive_nativeclass_godot_attr_with_base");
349+
350+
let ok = std::panic::catch_unwind(|| {
351+
let thing = Instance::<GodotAttrWithBase, _>::new();
352+
let base = thing.into_base();
353+
assert_eq!(unsafe { base.call("answer", &[]).to::<i64>() }, Some(54));
354+
})
355+
.is_ok();
356+
357+
if !ok {
358+
godot_error!(" !! Test test_derive_nativeclass_godot_attr_with_base failed");
359+
}
360+
361+
ok
362+
}
363+
364+
#[derive(NativeClass)]
365+
#[inherit(Reference)]
366+
struct GodotAttrDerefReturn(Rc<RefCell<Vec<i64>>>);
367+
368+
#[methods]
369+
impl GodotAttrDerefReturn {
370+
fn new(_owner: &Reference) -> Self {
371+
let vec = Vec::from([12, 34]);
372+
let rc_ref = Rc::new(RefCell::new(vec));
373+
Self(rc_ref)
374+
}
375+
376+
#[godot(deref_return)]
377+
fn answer(&self) -> cell::Ref<Vec<i64>> {
378+
self.0.borrow()
379+
}
380+
}
381+
382+
fn test_derive_nativeclass_godot_attr_deref_return() -> bool {
383+
println!(" -- test_derive_nativeclass_godot_attr_deref_return");
384+
385+
let ok = std::panic::catch_unwind(|| {
386+
let thing = Instance::<GodotAttrDerefReturn, _>::new();
387+
let base = thing.into_base();
388+
389+
let res = unsafe { base.call("answer", &[]).to::<Vec<i64>>() };
390+
assert_eq!(res, Some([12, 34].into()));
391+
})
392+
.is_ok();
393+
394+
if !ok {
395+
godot_error!(" !! Test test_derive_nativeclass_godot_attr_deref_return failed");
396+
}
397+
398+
ok
399+
}
400+
401+
#[derive(NativeClass)]
402+
#[inherit(Reference)]
403+
struct GodotAttrRenameMethod(i64);
404+
405+
#[methods]
406+
impl GodotAttrRenameMethod {
407+
fn new(_owner: &Reference) -> Self {
408+
Self(54)
409+
}
410+
411+
#[godot(name = "ask")]
412+
fn answer(&self) -> i64 {
413+
self.0
414+
}
415+
}
416+
417+
fn test_derive_nativeclass_godot_attr_rename_method() -> bool {
418+
println!(" -- test_derive_nativeclass_godot_attr_rename_method");
419+
420+
let ok = std::panic::catch_unwind(|| {
421+
let thing = Instance::<GodotAttrRenameMethod, _>::new();
422+
let base = thing.into_base();
423+
assert_eq!(unsafe { base.call("ask", &[]).to::<i64>() }, Some(54));
424+
})
425+
.is_ok();
426+
427+
if !ok {
428+
godot_error!(" !! Test test_derive_nativeclass_godot_attr_rename_method failed");
429+
}
430+
431+
ok
432+
}
433+
434+
#[derive(NativeClass)]
435+
#[inherit(Reference)]
436+
struct GodotAttrToUseAllMacroParameters(Rc<RefCell<Vec<i64>>>);
437+
438+
#[methods]
439+
impl GodotAttrToUseAllMacroParameters {
440+
fn new(_owner: &Reference) -> Self {
441+
let vec = Vec::from([12, 34]);
442+
let rc_ref = Rc::new(RefCell::new(vec));
443+
Self(rc_ref)
444+
}
445+
446+
#[godot(rpc = "disabled", name = "ask", deref_return)]
447+
fn answer(&self, #[base] _base: &Reference) -> cell::Ref<Vec<i64>> {
448+
self.0.borrow()
449+
}
450+
}
451+
452+
fn test_derive_nativeclass_godot_attr_to_use_all_macro_parameters() -> bool {
453+
println!(" -- test_derive_nativeclass_godot_attr_to_use_all_macro_parameters");
454+
455+
let ok = std::panic::catch_unwind(|| {
456+
let thing = Instance::<GodotAttrToUseAllMacroParameters, _>::new();
457+
let base = thing.into_base();
458+
459+
let res = unsafe { base.call("ask", &[]).to::<Vec<i64>>() };
460+
assert_eq!(res, Some([12, 34].into()));
461+
})
462+
.is_ok();
463+
464+
if !ok {
465+
godot_error!(
466+
" !! Test test_derive_nativeclass_godot_attr_to_use_all_macro_parameters failed"
467+
);
468+
}
469+
470+
ok
471+
}
472+
218473
#[derive(NativeClass)]
219474
#[inherit(Node)]
220475
struct CustomGetSet {

0 commit comments

Comments
 (0)