|
| 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 crate::object_tests::deferred_call_test::TestState::{Accepted, Initial}; |
| 10 | +use godot::prelude::*; |
| 11 | +use godot::task::{SignalFuture, TaskHandle}; |
| 12 | + |
| 13 | +#[derive(GodotConvert, Var, Export, Clone, PartialEq, Debug)] |
| 14 | +#[godot(via = GString)] |
| 15 | +enum TestState { |
| 16 | + Initial, |
| 17 | + Accepted, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(GodotClass)] |
| 21 | +#[class(base=Node)] |
| 22 | +struct DeferredTestNode { |
| 23 | + base: Base<Node>, |
| 24 | + state: TestState, |
| 25 | +} |
| 26 | + |
| 27 | +#[godot_api] |
| 28 | +impl DeferredTestNode { |
| 29 | + #[signal] |
| 30 | + fn test_completed(state: TestState); |
| 31 | + |
| 32 | + #[func] |
| 33 | + fn accept(&mut self) { |
| 34 | + self.state = Accepted; |
| 35 | + } |
| 36 | + |
| 37 | + fn as_expectation_task(&self) -> TaskHandle { |
| 38 | + assert_eq!(Initial, self.state, "accept evaluated synchronously"); |
| 39 | + |
| 40 | + let test_will_succeed: SignalFuture<(Variant,)> = |
| 41 | + Signal::from_object_signal(&self.to_gd(), "test_completed").to_future(); |
| 42 | + godot::task::spawn(async move { |
| 43 | + let (final_state,) = test_will_succeed.await; |
| 44 | + let final_state: TestState = final_state.to(); |
| 45 | + |
| 46 | + assert_eq!(Accepted, final_state); |
| 47 | + }) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +#[godot_api] |
| 52 | +impl INode for DeferredTestNode { |
| 53 | + fn init(base: Base<Self::Base>) -> Self { |
| 54 | + Self { |
| 55 | + base, |
| 56 | + state: Initial, |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + fn process(&mut self, _delta: f64) { |
| 61 | + let args = vslice![self.state]; |
| 62 | + self.base_mut().emit_signal("test_completed", args); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +#[itest(async)] |
| 67 | +fn calls_method_names_deferred(ctx: &crate::framework::TestContext) -> TaskHandle { |
| 68 | + let mut test_node = DeferredTestNode::new_alloc(); |
| 69 | + ctx.scene_tree.clone().add_child(&test_node); |
| 70 | + |
| 71 | + test_node.call_deferred("accept", &[]); |
| 72 | + |
| 73 | + let handle = test_node.bind().as_expectation_task(); |
| 74 | + handle |
| 75 | +} |
0 commit comments