Skip to content

Commit 37b7679

Browse files
committed
Tests for AsArg (Ref + Instance)
1 parent d0c5f54 commit 37b7679

File tree

4 files changed

+148
-12
lines changed

4 files changed

+148
-12
lines changed

test/project/addons/editor_test_runner/plugin.gd

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func _enter_tree():
1515
print("Opening editor normally for the test project. To run tests, pass `--run-editor-tests` to the executable.")
1616

1717
func _run_tests():
18-
print(" -- Rust gdnative test suite:")
18+
print(" -- Rust GDNative test suite (called from editor):")
1919
gdn = GDNative.new()
2020
var status = false;
2121

@@ -26,12 +26,13 @@ func _run_tests():
2626

2727
gdn.terminate()
2828
else:
29-
print(" -- Could not load the gdnative library.")
29+
print(" -- Could not load the GDNative library.")
3030

31+
print()
3132
if status:
32-
print(" -- Test run completed successfully.")
33+
print(" All tests PASSED.")
3334
else:
34-
print(" -- Test run completed with errors.")
35+
print(" Tests FAILED.")
3536
OS.exit_code = 1
3637

3738
print(" -- exiting.")

test/project/main.gd

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extends Node
33
var gdn
44

55
func _ready():
6-
print(" -- Rust gdnative test suite:")
6+
print(" -- Rust GDNative test suite:")
77
_timeout()
88

99
gdn = GDNative.new()
@@ -24,12 +24,13 @@ func _ready():
2424

2525
gdn.terminate()
2626
else:
27-
print(" -- Could not load the gdnative library.")
27+
print(" -- Could not load the GDNative library.")
2828

29+
print()
2930
if status:
30-
print(" -- Test run completed successfully.")
31+
print(" All tests PASSED.")
3132
else:
32-
print(" -- Test run completed with errors.")
33+
print(" Tests FAILED.")
3334
OS.exit_code = 1
3435

3536
print(" -- exiting.")

test/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use gdnative::prelude::*;
44

5+
mod test_as_arg;
56
mod test_async;
67
mod test_constructor;
78
mod test_derive;
@@ -63,17 +64,18 @@ pub extern "C" fn run_tests(
6364
status &= test_rust_class_construction();
6465
status &= test_from_instance_id();
6566

67+
status &= test_as_arg::run_tests();
6668
status &= test_async::run_tests();
69+
status &= test_constructor::run_tests();
6770
status &= test_derive::run_tests();
6871
status &= test_free_ub::run_tests();
69-
status &= test_constructor::run_tests();
7072
status &= test_map_owned::run_tests();
7173
status &= test_register::run_tests();
7274
status &= test_return_leak::run_tests();
7375
status &= test_serde::run_tests();
76+
status &= test_vararray_return::run_tests();
7477
status &= test_variant_call_args::run_tests();
7578
status &= test_variant_ops::run_tests();
76-
status &= test_vararray_return::run_tests();
7779

7880
gdnative::core_types::Variant::new(status).forget()
7981
}
@@ -260,16 +262,17 @@ fn init(handle: InitHandle) {
260262
handle.add_class::<Foo>();
261263
handle.add_class::<OptionalArgs>();
262264

265+
test_as_arg::register(handle);
263266
test_async::register(handle);
267+
test_constructor::register(handle);
264268
test_derive::register(handle);
265269
test_free_ub::register(handle);
266-
test_constructor::register(handle);
267270
test_map_owned::register(handle);
268271
test_register::register(handle);
269272
test_return_leak::register(handle);
273+
test_vararray_return::register(handle);
270274
test_variant_call_args::register(handle);
271275
test_variant_ops::register(handle);
272-
test_vararray_return::register(handle);
273276
}
274277

275278
fn terminate(_term_info: &gdnative::init::TerminateInfo) {

test/src/test_as_arg.rs

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
use gdnative::derive::{methods, NativeClass};
2+
use gdnative::prelude::NodeExt;
3+
use gdnative::prelude::*;
4+
use std::ops::Deref;
5+
6+
pub(crate) fn register(handle: InitHandle) {
7+
handle.add_class::<MyNode>();
8+
}
9+
10+
pub(crate) fn run_tests() -> bool {
11+
println!(" -- test_as_arg");
12+
13+
let ok = std::panic::catch_unwind(|| {
14+
println!(" -- test_ref_as_arg");
15+
test_ref_as_arg();
16+
17+
println!(" -- test_instance_as_arg");
18+
test_instance_as_arg();
19+
})
20+
.is_ok();
21+
22+
if !ok {
23+
godot_error!(" !! Test test_as_arg failed");
24+
}
25+
26+
ok
27+
}
28+
29+
// ----------------------------------------------------------------------------------------------------------------------------------------------
30+
31+
#[derive(NativeClass, Debug)]
32+
#[inherit(Spatial)]
33+
#[no_constructor]
34+
struct MyNode {
35+
secret: &'static str,
36+
}
37+
38+
#[methods]
39+
impl MyNode {}
40+
41+
// ----------------------------------------------------------------------------------------------------------------------------------------------
42+
43+
fn test_ref_as_arg() {
44+
// Ref<T, Unique>
45+
add_node_with(|n: Ref<Node2D, Unique>| n);
46+
47+
// Ref<T, Shared>
48+
add_node_with(|n: Ref<Node2D, Unique>| n.into_shared());
49+
50+
// &Ref<T, Shared>
51+
let mut keeper = Node2D::new().into_shared(); // keep Ref<T, Shared> alive so we can return a reference to it
52+
add_node_with(|n: Ref<Node2D, Unique>| {
53+
keeper = n.into_shared();
54+
&keeper
55+
});
56+
57+
// TRef<T, Shared>
58+
add_node_with(|n: Ref<Node2D, Unique>| unsafe { n.into_shared().assume_safe() });
59+
}
60+
61+
fn test_instance_as_arg() {
62+
// Instance<T, Unique>
63+
add_instance_with(|n: Instance<MyNode, Unique>| n);
64+
65+
// Instance<T, Shared>
66+
add_instance_with(|n: Instance<MyNode, Unique>| n.into_shared());
67+
68+
// &Instance<T, Shared>
69+
let mut keeper = MyNode { secret: "" }.emplace().into_shared(); // keep Instance<T, Shared> alive so we can return a reference to it
70+
add_instance_with(|n: Instance<MyNode, Unique>| {
71+
keeper = n.into_shared();
72+
&keeper
73+
});
74+
75+
// TInstance<T, Shared>
76+
add_instance_with(|n: Instance<MyNode, Unique>| unsafe { n.into_shared().assume_safe() });
77+
}
78+
79+
fn add_node_with<F, T>(to_arg: F)
80+
where
81+
F: FnOnce(Ref<Node2D, Unique>) -> T,
82+
T: AsArg<Node>,
83+
{
84+
let parent = Node::new();
85+
let child = Node2D::new();
86+
let child_id: i64 = child.get_instance_id();
87+
child.set_name("ch");
88+
89+
let child: T = to_arg(child);
90+
91+
parent.add_child(child, /*legible_unique_name*/ true);
92+
93+
let found = parent.get_node("ch").expect("get_node() for Ref");
94+
let found_tref = unsafe { found.assume_safe() };
95+
96+
assert_eq!(found_tref.get_instance_id(), child_id);
97+
}
98+
99+
fn add_instance_with<F, T>(to_arg: F)
100+
where
101+
F: FnOnce(Instance<MyNode, Unique>) -> T,
102+
T: AsArg<Node>,
103+
{
104+
let parent = Node::new();
105+
let child = MyNode { secret: "yes" }.emplace();
106+
107+
let child_id: i64 = child
108+
.map(|_, node| {
109+
node.set_name("ch");
110+
node.get_instance_id()
111+
})
112+
.expect("child.map()");
113+
114+
let child: T = to_arg(child);
115+
116+
parent.add_child(child, /*legible_unique_name*/ true);
117+
118+
let found: TInstance<MyNode> = unsafe {
119+
parent
120+
.deref()
121+
.get_node_as_instance::<MyNode>("ch")
122+
.expect("get_node() for Instance")
123+
};
124+
125+
found
126+
.map(|user, node| {
127+
assert_eq!(node.get_instance_id(), child_id);
128+
assert_eq!(user.secret, "yes");
129+
})
130+
.expect("found.map()")
131+
}

0 commit comments

Comments
 (0)