Skip to content

Commit 9c604f2

Browse files
committed
Add godot_itest! macro to be used in test crate; add #[must_use]
1 parent c803753 commit 9c604f2

File tree

5 files changed

+51
-26
lines changed

5 files changed

+51
-26
lines changed

gdnative-core/src/macros.rs

+38-3
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,15 @@ macro_rules! impl_basic_traits_as_sys {
208208
)
209209
}
210210

211-
macro_rules! godot_test {
212-
($($test_name:ident $body:block)*) => {
211+
#[doc(hidden)]
212+
#[macro_export]
213+
macro_rules! godot_test_impl {
214+
( $( $test_name:ident $body:block $($attrs:tt)* )* ) => {
213215
$(
214-
#[cfg(feature = "gd-test")]
216+
$($attrs)*
215217
#[doc(hidden)]
216218
#[inline]
219+
#[must_use]
217220
pub fn $test_name() -> bool {
218221
let str_name = stringify!($test_name);
219222
println!(" -- {}", str_name);
@@ -231,3 +234,35 @@ macro_rules! godot_test {
231234
)*
232235
}
233236
}
237+
238+
/// Declares a test to be run with the Godot engine (i.e. not a pure Rust unit test).
239+
///
240+
/// Creates a wrapper function that catches panics, prints errors and returns true/false.
241+
/// To be manually invoked in higher-level test routine.
242+
///
243+
/// This macro is designed to be used within the current crate only, hence the #[cfg] attribute.
244+
#[doc(hidden)]
245+
macro_rules! godot_test {
246+
($($test_name:ident $body:block)*) => {
247+
$(
248+
godot_test_impl!($test_name $body #[cfg(feature = "gd-test")]);
249+
)*
250+
}
251+
}
252+
253+
254+
/// Declares a test to be run with the Godot engine (i.e. not a pure Rust unit test).
255+
///
256+
/// Creates a wrapper function that catches panics, prints errors and returns true/false.
257+
/// To be manually invoked in higher-level test routine.
258+
///
259+
/// This macro is designed to be used within the `test` crate, hence the method is always declared (not only in certain features).
260+
#[doc(hidden)]
261+
#[macro_export]
262+
macro_rules! godot_itest {
263+
($($test_name:ident $body:block)*) => {
264+
$(
265+
$crate::godot_test_impl!($test_name $body);
266+
)*
267+
}
268+
}

impl/proc-macros/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ mod pool_array_element;
1212

1313
#[proc_macro]
1414
pub fn impl_typed_array_element(input: TokenStream) -> TokenStream {
15-
self::pool_array_element::impl_element(input)
15+
pool_array_element::impl_element(input)
1616
.unwrap_or_else(to_compile_errors)
1717
.into()
1818
}
1919

2020
#[proc_macro]
2121
pub fn decl_typed_array_element(input: TokenStream) -> TokenStream {
22-
self::pool_array_element::decl_element(input)
22+
pool_array_element::decl_element(input)
2323
.unwrap_or_else(to_compile_errors)
2424
.into()
2525
}

test/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ custom-godot = ["gdnative/custom-godot"]
1717

1818
[dependencies]
1919
gdnative = { path = "../gdnative", features = ["gd-test", "serde", "async"] }
20+
gdnative-core = { path = "../gdnative-core" }
2021
approx = "0.5"
2122
ron = "0.7"
2223
serde = "1"

test/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub extern "C" fn run_tests(
7979
status &= test_variant_call_args::run_tests();
8080
status &= test_variant_ops::run_tests();
8181

82-
gdnative::core_types::Variant::new(status).leak()
82+
Variant::new(status).leak()
8383
}
8484

8585
fn test_underscore_method_binding() -> bool {

test/src/test_vararray_return.rs

+9-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use gdnative::api::Camera;
22
use gdnative::prelude::*;
33

4+
use gdnative_core::godot_itest;
5+
46
pub(crate) fn run_tests() -> bool {
57
let mut status = true;
68

@@ -11,24 +13,11 @@ pub(crate) fn run_tests() -> bool {
1113

1214
pub(crate) fn register(_handle: InitHandle) {}
1315

14-
fn test_vararray_return_crash() -> bool {
15-
println!(" -- test_vararray_return_crash");
16-
17-
let ok = std::panic::catch_unwind(|| {
18-
// See https://github.com/godot-rust/godot-rust/issues/422
19-
let camera = Camera::new();
20-
21-
camera.set_frustum(5.0, Vector2::new(1.0, 2.0), 0.0, 1.0);
22-
23-
camera.get_frustum(); // this should not crash!
16+
godot_itest! { test_vararray_return_crash {
17+
// See https://github.com/godot-rust/godot-rust/issues/422
18+
let camera = Camera::new();
2419

25-
camera.free();
26-
})
27-
.is_ok();
28-
29-
if !ok {
30-
godot_error!(" !! Test test_vararray_return_crash failed");
31-
}
32-
33-
ok
34-
}
20+
camera.set_frustum(5.0, Vector2::new(1.0, 2.0), 0.0, 1.0);
21+
camera.get_frustum(); // this should not crash!
22+
camera.free();
23+
}}

0 commit comments

Comments
 (0)