Skip to content

Commit 5326bb1

Browse files
committed
Add a test for Varargs::get()
1 parent e5f5f56 commit 5326bb1

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

test/src/test_register.rs

+61
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::error::Error;
12
use std::ops::Add;
23

34
use gdnative::export::{StaticArgs, StaticArgsMethod, StaticallyNamed};
@@ -8,6 +9,7 @@ pub(crate) fn run_tests() -> bool {
89

910
status &= test_register_property();
1011
status &= test_advanced_methods();
12+
status &= test_varargs_gets();
1113

1214
status
1315
}
@@ -16,6 +18,7 @@ pub(crate) fn register(handle: InitHandle) {
1618
handle.add_class::<RegisterSignal>();
1719
handle.add_class::<RegisterProperty>();
1820
handle.add_class::<AdvancedMethods>();
21+
handle.add_class::<VarargsGets>();
1922
}
2023

2124
#[derive(Copy, Clone, Debug, Default)]
@@ -232,3 +235,61 @@ fn test_advanced_methods() -> bool {
232235

233236
ok
234237
}
238+
239+
#[derive(NativeClass)]
240+
#[inherit(Reference)]
241+
#[register_with(VarargsGets::register)]
242+
struct VarargsGets {}
243+
244+
#[methods]
245+
impl VarargsGets {
246+
fn new(_owner: TRef<Reference>) -> Self {
247+
Self {}
248+
}
249+
250+
fn register(builder: &ClassBuilder<VarargsGets>) {
251+
builder.method("calc", CalcMethod).done();
252+
}
253+
}
254+
255+
struct CalcMethod;
256+
257+
impl Method<VarargsGets> for CalcMethod {
258+
fn call(
259+
&self,
260+
_this: TInstance<'_, VarargsGets>,
261+
args: gdnative::export::Varargs<'_>,
262+
) -> Variant {
263+
(|| {
264+
args.check_length(1..=3)?;
265+
let a: i64 = args.get(0)?;
266+
let b: i64 = args.get(1)?;
267+
let c: i64 = args.get_opt(2)?.unwrap_or(11);
268+
269+
let ret = a * b - c;
270+
Ok::<Variant, Box<dyn Error>>(ret.to_variant())
271+
})()
272+
.unwrap_or_default()
273+
}
274+
}
275+
276+
fn test_varargs_gets() -> bool {
277+
println!(" -- test_varargs_gets");
278+
279+
let ok = std::panic::catch_unwind(|| {
280+
let thing = Instance::<VarargsGets, _>::new();
281+
let base = thing.base();
282+
283+
let args = [3_i64.to_variant(), 4_i64.to_variant(), 5_i64.to_variant()];
284+
assert_eq!(unsafe { base.call("calc", &args).to() }, Some(7));
285+
286+
let args = [3_i64.to_variant(), 4_i64.to_variant()];
287+
assert_eq!(unsafe { base.call("calc", &args).to() }, Some(1));
288+
})
289+
.is_ok();
290+
291+
if !ok {
292+
godot_error!(" !! Test test_varargs_gets failed");
293+
}
294+
ok
295+
}

0 commit comments

Comments
 (0)