Skip to content

Commit 6764d48

Browse files
committed
feat: test for type-safe rpcs
1 parent 67def04 commit 6764d48

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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::meta::error::RpcError;
9+
use godot::prelude::*;
10+
use godot::test::itest;
11+
12+
use crate::framework::TestContext;
13+
14+
#[derive(GodotClass)]
15+
#[class(init, base = Node)]
16+
pub struct RpcCallableNode {
17+
base: Base<Node>,
18+
}
19+
20+
#[godot_api]
21+
impl INode for RpcCallableNode {}
22+
23+
#[godot_api]
24+
impl RpcCallableNode {
25+
#[rpc]
26+
pub fn say_hello_world(&mut self) {
27+
godot_print!("hello, world");
28+
}
29+
30+
#[rpc]
31+
pub fn say_hello_to(&mut self, to: String) {
32+
godot_print!("hello, {to}");
33+
}
34+
35+
#[rpc]
36+
pub fn say_number(&self, number: i32) {
37+
godot_print!("{number}");
38+
}
39+
}
40+
41+
#[itest]
42+
fn type_safe_rpc_test(context: &TestContext) {
43+
let mut node = RpcCallableNode::new_alloc();
44+
45+
// Within the testing environment, this function does not get run automatically, so we call it here manually.
46+
47+
let mut root = context.scene_tree.clone();
48+
49+
// Before we add the node to the tree, RPCs will fail.
50+
assert_eq!(
51+
Err(RpcError::Unconfigured),
52+
node.bind_mut().rpcs().say_hello_world().call()
53+
);
54+
55+
root.add_child(&node);
56+
57+
assert_eq!(Ok(()), node.rpcs().say_hello_world().call());
58+
59+
let arg = "godot".to_string();
60+
assert_eq!(Ok(()), node.rpcs().say_hello_to(arg.clone()).call());
61+
assert_eq!(Ok(()), node.bind_mut().rpcs().say_hello_to(arg).call());
62+
assert_eq!(Ok(()), node.rpcs().say_number(3).call());
63+
root.remove_child(&node);
64+
node.free();
65+
}

itest/rust/src/builtin_tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ mod containers {
3131
mod dictionary_test;
3232
mod packed_array_test;
3333
mod rid_test;
34+
mod rpc_test;
3435
mod signal_disconnect_test;
3536
mod signal_test;
3637
mod variant_test;

0 commit comments

Comments
 (0)