Skip to content

Commit 069b647

Browse files
committed
Add test for signals with multiple parameters
1 parent 2da9014 commit 069b647

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

itest/rust/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ mod projection_test;
3131
mod quaternion_test;
3232
mod rect2i_test;
3333
mod rid_test;
34+
mod signal_test;
3435
mod singleton_test;
3536
mod string;
3637
mod transform2d_test;

itest/rust/src/signal_test.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*/
6+
7+
use std::cell::Cell;
8+
9+
use godot::bind::{godot_api, GodotClass};
10+
use godot::builtin::{GodotString, Variant};
11+
12+
use godot::engine::Object;
13+
use godot::obj::{Base, Gd, Share};
14+
use godot::sys;
15+
16+
use crate::itest;
17+
18+
#[derive(GodotClass)]
19+
#[class(init, base=Object)]
20+
struct Emitter {
21+
#[base]
22+
base: Base<Object>,
23+
}
24+
25+
#[godot_api]
26+
impl Emitter {
27+
#[signal]
28+
fn signal_0_arg();
29+
#[signal]
30+
fn signal_1_arg(arg1: i64);
31+
#[signal]
32+
fn signal_2_arg(arg1: Gd<Object>, arg2: GodotString);
33+
}
34+
35+
#[derive(GodotClass)]
36+
#[class(init, base=Object)]
37+
struct Receiver {
38+
used: [Cell<bool>; 3],
39+
#[base]
40+
base: Base<Object>,
41+
}
42+
43+
#[godot_api]
44+
impl Receiver {
45+
#[func]
46+
fn receive_0_arg(&self) {
47+
self.used[0].set(true);
48+
}
49+
#[func]
50+
fn receive_1_arg(&self, arg1: i64) {
51+
self.used[1].set(true);
52+
assert_eq!(arg1, 987);
53+
}
54+
#[func]
55+
fn receive_2_arg(&self, arg1: Gd<Object>, arg2: GodotString) {
56+
assert_eq!(self.base.share(), arg1);
57+
assert_eq!(SIGNAL_ARG_STRING, arg2.to_string());
58+
59+
self.used[2].set(true);
60+
}
61+
}
62+
63+
const SIGNAL_ARG_STRING: &str = "Signal string arg";
64+
65+
#[itest]
66+
/// Test that godot can call a method that is connect with a signal
67+
fn signals() {
68+
let mut emitter = Gd::<Emitter>::new_default();
69+
let receiver = Gd::<Receiver>::new_default();
70+
71+
let args = [
72+
vec![],
73+
vec![Variant::from(987)],
74+
vec![
75+
Variant::from(receiver.share()),
76+
Variant::from(SIGNAL_ARG_STRING),
77+
],
78+
];
79+
80+
for (i, arg) in args.iter().enumerate() {
81+
let signal_name = format!("signal_{i}_arg");
82+
let receiver_name = format!("receive_{i}_arg");
83+
84+
emitter.bind_mut().connect(
85+
signal_name.clone().into(),
86+
receiver.callable(receiver_name),
87+
0,
88+
);
89+
90+
emitter.bind_mut().emit_signal(signal_name.into(), arg);
91+
92+
assert!(receiver.bind().used[i].get());
93+
}
94+
95+
receiver.free();
96+
emitter.free();
97+
}

0 commit comments

Comments
 (0)