Skip to content

Commit ef00e44

Browse files
committed
Add test for Varargs to tuple conversion
1 parent 4e45bb6 commit ef00e44

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

test/src/test_register.rs

+56
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub(crate) fn run_tests() -> bool {
88

99
status &= test_register_property();
1010
status &= test_advanced_methods();
11+
status &= test_varargs_to_tuple();
1112

1213
status
1314
}
@@ -16,6 +17,7 @@ pub(crate) fn register(handle: InitHandle) {
1617
handle.add_class::<RegisterSignal>();
1718
handle.add_class::<RegisterProperty>();
1819
handle.add_class::<AdvancedMethods>();
20+
handle.add_class::<VarargsToTuple>();
1921
}
2022

2123
#[derive(Copy, Clone, Debug, Default)]
@@ -232,3 +234,57 @@ fn test_advanced_methods() -> bool {
232234

233235
ok
234236
}
237+
238+
#[derive(NativeClass)]
239+
#[inherit(Reference)]
240+
#[register_with(VarargsToTuple::register)]
241+
struct VarargsToTuple {}
242+
243+
#[methods]
244+
impl VarargsToTuple {
245+
fn new(_owner: TRef<Reference>) -> Self {
246+
VarargsToTuple {}
247+
}
248+
249+
fn register(builder: &ClassBuilder<VarargsToTuple>) {
250+
builder.method("calc", CalcMethod).done();
251+
}
252+
}
253+
254+
struct CalcMethod;
255+
256+
impl Method<VarargsToTuple> for CalcMethod {
257+
fn call(
258+
&self,
259+
_this: TInstance<'_, VarargsToTuple>,
260+
args: gdnative::export::Varargs<'_>,
261+
) -> Variant {
262+
let (a, b, c): (i64, i64, i64) =
263+
std::convert::TryInto::try_into(args).expect("Must be able to convert");
264+
let ret = a * b - c;
265+
ret.to_variant()
266+
}
267+
}
268+
269+
fn test_varargs_to_tuple() -> bool {
270+
println!(" -- test_varargs_to_tuple");
271+
272+
let ok = std::panic::catch_unwind(|| {
273+
let thing = Instance::<VarargsToTuple, _>::new();
274+
let base = thing.base();
275+
276+
assert_eq!(Some(7), unsafe {
277+
base.call(
278+
"calc",
279+
&[3_i64.to_variant(), 4_i64.to_variant(), 5_i64.to_variant()],
280+
)
281+
.to()
282+
});
283+
})
284+
.is_ok();
285+
286+
if !ok {
287+
godot_error!(" !! Test test_varargs_to_tuple failed");
288+
}
289+
ok
290+
}

0 commit comments

Comments
 (0)