Skip to content

Commit 1ebebb2

Browse files
committed
Add a test for Varargs::get()
1 parent b4fc5aa commit 1ebebb2

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

test/src/test_register.rs

+65-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
use std::error::Error;
12
use std::ops::Add;
23

34
use gdnative::export::{StaticArgs, StaticArgsMethod, StaticallyNamed};
4-
use gdnative::prelude::*;
5+
use gdnative::{log, prelude::*};
56

67
pub(crate) fn run_tests() -> bool {
78
let mut status = true;
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,64 @@ 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_else(|err| {
273+
log::error(log::godot_site!(calc), err);
274+
Variant::nil()
275+
})
276+
}
277+
}
278+
279+
fn test_varargs_gets() -> bool {
280+
println!(" -- test_varargs_gets");
281+
282+
let ok = std::panic::catch_unwind(|| {
283+
let thing = Instance::<VarargsGets, _>::new();
284+
let base = thing.base();
285+
286+
let args = [3_i64.to_variant(), 4_i64.to_variant(), 5_i64.to_variant()];
287+
assert_eq!(unsafe { base.call("calc", &args).to() }, Some(7));
288+
289+
let args = [3_i64.to_variant(), 4_i64.to_variant()];
290+
assert_eq!(unsafe { base.call("calc", &args).to() }, Some(1));
291+
})
292+
.is_ok();
293+
294+
if !ok {
295+
godot_error!(" !! Test test_varargs_gets failed");
296+
}
297+
ok
298+
}

0 commit comments

Comments
 (0)