-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[macros] allow multiple inherent impl blocks.
All but one blocks must have the key 'secondary'.
- Loading branch information
Showing
6 changed files
with
258 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
itest/rust/src/register_tests/multiple_impl_blocks_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (c) godot-rust; Bromeon and contributors. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
use godot::classes::IObject; | ||
use godot::obj::{Base, Gd, NewAlloc}; | ||
use godot::register::{godot_api, GodotClass}; | ||
|
||
use crate::framework::itest; | ||
|
||
// ---------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
#[derive(GodotClass)] | ||
#[class(base=Object)] | ||
struct MultipleImplBlocks {} | ||
|
||
#[godot_api] | ||
impl IObject for MultipleImplBlocks { | ||
fn init(_base: Base<Self::Base>) -> Self { | ||
Self {} | ||
} | ||
} | ||
|
||
#[godot_api] | ||
impl MultipleImplBlocks { | ||
#[func] | ||
fn foo(&self) -> String { | ||
"result of foo".to_string() | ||
} | ||
} | ||
|
||
#[godot_api(secondary)] | ||
impl MultipleImplBlocks { | ||
#[func] | ||
fn bar(&self) -> String { | ||
"result of bar".to_string() | ||
} | ||
} | ||
|
||
#[godot_api(secondary)] | ||
impl MultipleImplBlocks { | ||
#[func] | ||
fn baz(&self) -> String { | ||
"result of baz".to_string() | ||
} | ||
} | ||
|
||
/// Test that multiple inherent '#[godot_api]' impl blocks can be registered. | ||
/// https://github.com/godot-rust/gdext/pull/927 | ||
#[itest] | ||
fn godot_api_multiple_impl_blocks() { | ||
let mut obj: Gd<MultipleImplBlocks> = MultipleImplBlocks::new_alloc(); | ||
|
||
fn call_and_check_result( | ||
gd: &mut Gd<MultipleImplBlocks>, | ||
method_name: &str, | ||
expected_result: &str, | ||
) { | ||
assert!(gd.has_method(method_name)); | ||
let result = gd.call(method_name, &[]); | ||
let result_as_string = result.try_to::<String>(); | ||
assert!(result_as_string.is_ok()); | ||
assert_eq!(result_as_string.unwrap(), expected_result); | ||
} | ||
|
||
// Just call all three methods; if that works, then they have all been correctly registered. | ||
call_and_check_result(&mut obj, "foo", "result of foo"); | ||
call_and_check_result(&mut obj, "bar", "result of bar"); | ||
call_and_check_result(&mut obj, "baz", "result of baz"); | ||
|
||
obj.free(); | ||
} | ||
|
||
// ---------------------------------------------------------------------------------------------------------------------------------------------- |