Skip to content

Commit f6fe765

Browse files
committed
Add test for Varargs to tuple conversion
1 parent 63dd353 commit f6fe765

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

test/src/test_register.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub(crate) fn run_tests() -> bool {
1010
status &= test_register_property();
1111
status &= test_advanced_methods();
1212
status &= test_varargs_gets();
13+
status &= test_varargs_to_tuple();
1314

1415
status
1516
}
@@ -19,6 +20,7 @@ pub(crate) fn register(handle: InitHandle) {
1920
handle.add_class::<RegisterProperty>();
2021
handle.add_class::<AdvancedMethods>();
2122
handle.add_class::<VarargsGets>();
23+
handle.add_class::<VarargsToTuple>();
2224
}
2325

2426
#[derive(Copy, Clone, Debug, Default)]
@@ -293,3 +295,52 @@ fn test_varargs_gets() -> bool {
293295
}
294296
ok
295297
}
298+
299+
#[derive(NativeClass)]
300+
#[inherit(Reference)]
301+
#[register_with(VarargsToTuple::register)]
302+
struct VarargsToTuple {}
303+
304+
#[methods]
305+
impl VarargsToTuple {
306+
fn new(_owner: TRef<Reference>) -> Self {
307+
VarargsToTuple {}
308+
}
309+
310+
fn register(builder: &ClassBuilder<VarargsToTuple>) {
311+
builder.method("calc", CalcMethod2).done();
312+
}
313+
}
314+
315+
struct CalcMethod2;
316+
317+
impl Method<VarargsToTuple> for CalcMethod2 {
318+
fn call(
319+
&self,
320+
_this: TInstance<'_, VarargsToTuple>,
321+
args: gdnative::export::Varargs<'_>,
322+
) -> Variant {
323+
let (a, b, c): (i64, i64, i64) =
324+
std::convert::TryInto::try_into(args).expect("Must be able to convert");
325+
let ret = a * b - c;
326+
ret.to_variant()
327+
}
328+
}
329+
330+
fn test_varargs_to_tuple() -> bool {
331+
println!(" -- test_varargs_to_tuple");
332+
333+
let ok = std::panic::catch_unwind(|| {
334+
let thing = Instance::<VarargsToTuple, _>::new();
335+
let base = thing.base();
336+
337+
let args = [3_i64.to_variant(), 4_i64.to_variant(), 5_i64.to_variant()];
338+
assert_eq!(unsafe { base.call("calc", &args).to() }, Some(7));
339+
})
340+
.is_ok();
341+
342+
if !ok {
343+
godot_error!(" !! Test test_varargs_to_tuple failed");
344+
}
345+
ok
346+
}

0 commit comments

Comments
 (0)