Skip to content

Commit 81f9d7c

Browse files
committed
test(core): test Object::call_deferred
1 parent 20cd346 commit 81f9d7c

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 crate::framework::itest;
9+
use godot::prelude::*;
10+
use godot::task::{SignalFuture, TaskHandle};
11+
12+
#[derive(GodotClass)]
13+
#[class(base=Node)]
14+
struct DeferredTestNode {
15+
base: Base<Node>,
16+
state: GString,
17+
}
18+
19+
#[godot_api]
20+
impl DeferredTestNode {
21+
#[signal]
22+
fn test_completed(state: GString);
23+
24+
#[func]
25+
fn accept(&mut self) {
26+
self.state = "accepted".into();
27+
}
28+
29+
fn as_expectation_task(&self) -> TaskHandle {
30+
assert_eq!(
31+
GString::from("initial"),
32+
self.state,
33+
"accept evaluated synchronously"
34+
);
35+
36+
let test_will_succeed: SignalFuture<(GString,)> =
37+
Signal::from_object_signal(&self.to_gd(), "test_completed").to_future();
38+
godot::task::spawn(async move {
39+
let (final_state,) = test_will_succeed.await;
40+
41+
assert_eq!(GString::from("accepted"), final_state,);
42+
})
43+
}
44+
}
45+
46+
#[godot_api]
47+
impl INode for DeferredTestNode {
48+
fn init(base: Base<Self::Base>) -> Self {
49+
Self {
50+
base,
51+
state: "initial".into(),
52+
}
53+
}
54+
55+
fn process(&mut self, _delta: f64) {
56+
let final_state = self.state.clone();
57+
self.signals().test_completed().emit(&final_state);
58+
}
59+
}
60+
61+
#[itest(async)]
62+
fn calls_method_names_deferred(ctx: &crate::framework::TestContext) -> TaskHandle {
63+
let mut test_node = DeferredTestNode::new_alloc();
64+
ctx.scene_tree.clone().add_child(&test_node);
65+
66+
test_node.call_deferred("accept", &[]);
67+
68+
let handle = test_node.bind().as_expectation_task();
69+
handle
70+
}

itest/rust/src/object_tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ mod property_test;
2525
mod reentrant_test;
2626
mod singleton_test;
2727
// `validate_property` is only supported in Godot 4.2+.
28+
mod deferred_call_test;
2829
#[cfg(since_api = "4.2")]
2930
mod validate_property_test;
3031
mod virtual_methods_niche_test;

0 commit comments

Comments
 (0)