diff --git a/gdnative-core/src/macros.rs b/gdnative-core/src/macros.rs index 8a2f3d5fd..9efaed7ca 100644 --- a/gdnative-core/src/macros.rs +++ b/gdnative-core/src/macros.rs @@ -250,7 +250,6 @@ macro_rules! godot_test { } } - /// Declares a test to be run with the Godot engine (i.e. not a pure Rust unit test). /// /// Creates a wrapper function that catches panics, prints errors and returns true/false. @@ -265,4 +264,4 @@ macro_rules! godot_itest { $crate::godot_test_impl!($test_name $body); )* } -} \ No newline at end of file +} diff --git a/test/src/lib.rs b/test/src/lib.rs index 39c4526cb..597f38b30 100644 --- a/test/src/lib.rs +++ b/test/src/lib.rs @@ -2,6 +2,7 @@ #![allow(deprecated)] use gdnative::prelude::*; +use gdnative_core::godot_itest; mod test_as_arg; mod test_async; @@ -82,22 +83,11 @@ pub extern "C" fn run_tests( Variant::new(status).leak() } -fn test_underscore_method_binding() -> bool { - println!(" -- test_underscore_method_binding"); - - let ok = std::panic::catch_unwind(|| { - let script = gdnative::api::NativeScript::new(); - let result = script._new(&[]); - assert_eq!(Variant::nil(), result); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_underscore_method_binding failed"); - } - - ok -} +godot_itest! { test_underscore_method_binding { + let script = gdnative::api::NativeScript::new(); + let result = script._new(&[]); + assert_eq!(Variant::nil(), result); +}} #[derive(NativeClass)] #[inherit(Reference)] @@ -152,31 +142,19 @@ impl Foo { } } -fn test_rust_class_construction() -> bool { - println!(" -- test_rust_class_construction"); - - let ok = std::panic::catch_unwind(|| { - let foo = Foo::new_instance(); +godot_itest! { test_rust_class_construction { + let foo = Foo::new_instance(); + assert_eq!(Ok(42), foo.map(|foo, owner| { foo.answer(&*owner) })); - assert_eq!(Ok(42), foo.map(|foo, owner| { foo.answer(&*owner) })); + let base = foo.into_base(); + assert_eq!(Some(42), unsafe { base.call("answer", &[]).to() }); - let base = foo.into_base(); - assert_eq!(Some(42), unsafe { base.call("answer", &[]).to() }); + let foo = Instance::::try_from_base(base).expect("should be able to downcast"); + assert_eq!(Ok(42), foo.map(|foo, owner| { foo.answer(&*owner) })); - let foo = Instance::::try_from_base(base).expect("should be able to downcast"); - assert_eq!(Ok(42), foo.map(|foo, owner| { foo.answer(&*owner) })); - - let base = foo.into_base(); - assert!(Instance::::try_from_base(base).is_err()); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_rust_class_construction failed"); - } - - ok -} + let base = foo.into_base(); + assert!(Instance::::try_from_base(base).is_err()); +}} #[derive(NativeClass)] #[inherit(Reference)] @@ -205,60 +183,49 @@ impl OptionalArgs { } } -fn test_from_instance_id() -> bool { - println!(" -- test_from_instance_id"); +godot_itest! { test_from_instance_id { + assert!(unsafe { Node::try_from_instance_id(22).is_none() }); + assert!(unsafe { Node::try_from_instance_id(42).is_none() }); + assert!(unsafe { Node::try_from_instance_id(503).is_none() }); - let ok = std::panic::catch_unwind(|| { - assert!(unsafe { Node::try_from_instance_id(22).is_none() }); - assert!(unsafe { Node::try_from_instance_id(42).is_none() }); - assert!(unsafe { Node::try_from_instance_id(503).is_none() }); + let instance_id; - let instance_id; + { + let foo = unsafe { Node::new().into_shared().assume_safe() }; + foo.set_name("foo"); - { - let foo = unsafe { Node::new().into_shared().assume_safe() }; - foo.set_name("foo"); + instance_id = foo.get_instance_id(); - instance_id = foo.get_instance_id(); - - assert!(unsafe { Reference::try_from_instance_id(instance_id).is_none() }); - - let reconstructed = unsafe { Node::from_instance_id(instance_id) }; - assert_eq!("foo", reconstructed.name().to_string()); - - unsafe { foo.assume_unique().free() }; - } + assert!(unsafe { Reference::try_from_instance_id(instance_id).is_none() }); - assert!(unsafe { Node::try_from_instance_id(instance_id).is_none() }); + let reconstructed = unsafe { Node::from_instance_id(instance_id) }; + assert_eq!("foo", reconstructed.name().to_string()); - let instance_id; + unsafe { foo.assume_unique().free() }; + } - { - let foo = Reference::new().into_shared(); - let foo = unsafe { foo.assume_safe() }; - foo.set_meta("foo", "bar"); + assert!(unsafe { Node::try_from_instance_id(instance_id).is_none() }); - instance_id = foo.get_instance_id(); + let instance_id; - assert!(unsafe { Node::try_from_instance_id(instance_id).is_none() }); + { + let foo = Reference::new().into_shared(); + let foo = unsafe { foo.assume_safe() }; + foo.set_meta("foo", "bar"); - let reconstructed = unsafe { Reference::from_instance_id(instance_id) }; - assert_eq!( - "bar", - String::from_variant(&reconstructed.get_meta("foo")).unwrap() - ); - } + instance_id = foo.get_instance_id(); - assert!(unsafe { Reference::try_from_instance_id(instance_id).is_none() }); - }) - .is_ok(); + assert!(unsafe { Node::try_from_instance_id(instance_id).is_none() }); - if !ok { - godot_error!(" !! Test test_from_instance_id failed"); + let reconstructed = unsafe { Reference::from_instance_id(instance_id) }; + assert_eq!( + "bar", + String::from_variant(&reconstructed.get_meta("foo")).unwrap() + ); } - ok -} + assert!(unsafe { Reference::try_from_instance_id(instance_id).is_none() }); +}} fn init(handle: InitHandle) { handle.add_class::(); diff --git a/test/src/test_as_arg.rs b/test/src/test_as_arg.rs index 76de7efa4..c0730d6ed 100644 --- a/test/src/test_as_arg.rs +++ b/test/src/test_as_arg.rs @@ -8,20 +8,10 @@ pub(crate) fn register(handle: InitHandle) { } pub(crate) fn run_tests() -> bool { - println!(" -- test_as_arg"); + let mut ok = true; - let ok = std::panic::catch_unwind(|| { - println!(" -- test_ref_as_arg"); - test_ref_as_arg(); - - println!(" -- test_instance_as_arg"); - test_instance_as_arg(); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_as_arg failed"); - } + ok &= test_as_arg_ref(); + ok &= test_as_arg_instance(); ok } @@ -40,7 +30,7 @@ impl MyNode {} // ---------------------------------------------------------------------------------------------------------------------------------------------- -fn test_ref_as_arg() { +crate::godot_itest! { test_as_arg_ref { // Ref add_node_with(|n: Ref| n); @@ -56,9 +46,9 @@ fn test_ref_as_arg() { // TRef add_node_with(|n: Ref| unsafe { n.into_shared().assume_safe() }); -} +}} -fn test_instance_as_arg() { +crate::godot_itest! { test_as_arg_instance { // Instance add_instance_with(|n: Instance| n); @@ -74,7 +64,7 @@ fn test_instance_as_arg() { // TInstance add_instance_with(|n: Instance| unsafe { n.into_shared().assume_safe() }); -} +}} fn add_node_with(to_arg: F) where diff --git a/test/src/test_constructor.rs b/test/src/test_constructor.rs index e49390fad..d85a58e52 100644 --- a/test/src/test_constructor.rs +++ b/test/src/test_constructor.rs @@ -27,43 +27,32 @@ fn test_constructor() -> bool { true } -fn test_from_class_name() -> bool { - println!(" -- test_from_class_name"); - - let ok = std::panic::catch_unwind(|| { - // Since this method is restricted to Godot types, there is no way we can detect - // here whether any invalid objects are leaked. Instead, the CI script is modified - // to look at stdout for any reported leaks. - - let node = Ref::::by_class_name("Node2D").unwrap(); - assert_eq!("Node2D", node.get_class().to_string()); - let node = node.cast::().unwrap(); - assert_eq!("Node2D", node.get_class().to_string()); - let _ = node.position(); - node.free(); - - let shader = Ref::::by_class_name("Shader").unwrap(); - assert_eq!("Shader", &shader.get_class().to_string()); - let shader = shader.cast::().unwrap(); - assert_eq!("Shader", &shader.get_class().to_string()); - - let none = Ref::::by_class_name("Shader"); - assert!(none.is_none()); - - let none = Ref::::by_class_name("Spatial"); - assert!(none.is_none()); - - let none = Ref::::by_class_name("AudioEffectReverb"); - assert!(none.is_none()); - - let none = Ref::::by_class_name("ClassThatDoesNotExistProbably"); - assert!(none.is_none()); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_from_class_name failed"); - } - - ok -} +crate::godot_itest! { test_from_class_name { + // Since this method is restricted to Godot types, there is no way we can detect + // here whether any invalid objects are leaked. Instead, the CI script is modified + // to look at stdout for any reported leaks. + + let node = Ref::::by_class_name("Node2D").unwrap(); + assert_eq!("Node2D", node.get_class().to_string()); + let node = node.cast::().unwrap(); + assert_eq!("Node2D", node.get_class().to_string()); + let _ = node.position(); + node.free(); + + let shader = Ref::::by_class_name("Shader").unwrap(); + assert_eq!("Shader", &shader.get_class().to_string()); + let shader = shader.cast::().unwrap(); + assert_eq!("Shader", &shader.get_class().to_string()); + + let none = Ref::::by_class_name("Shader"); + assert!(none.is_none()); + + let none = Ref::::by_class_name("Spatial"); + assert!(none.is_none()); + + let none = Ref::::by_class_name("AudioEffectReverb"); + assert!(none.is_none()); + + let none = Ref::::by_class_name("ClassThatDoesNotExistProbably"); + assert!(none.is_none()); +}} diff --git a/test/src/test_derive.rs b/test/src/test_derive.rs index 6347f4f9d..85234d88f 100644 --- a/test/src/test_derive.rs +++ b/test/src/test_derive.rs @@ -16,7 +16,7 @@ pub(crate) fn run_tests() -> bool { status &= test_derive_nativeclass_godot_attr_with_base(); status &= test_derive_nativeclass_godot_attr_deref_return(); status &= test_derive_nativeclass_godot_attr_rename_method(); - status &= test_derive_nativeclass_godot_attr_to_use_all_macro_parameters(); + status &= test_derive_nativeclass_godot_attr_all_arguments(); status &= test_derive_nativeclass_with_property_get_set(); status &= test_derive_nativeclass_property_with_only_getter(); @@ -31,14 +31,14 @@ pub(crate) fn register(handle: InitHandle) { handle.add_class::(); handle.add_class::(); handle.add_class::(); - handle.add_class::(); + handle.add_class::(); handle.add_class::(); handle.add_class::(); } -fn test_derive_to_variant() -> bool { - println!(" -- test_derive_to_variant"); +// ---------------------------------------------------------------------------------------------------------------------------------------------- +crate::godot_itest! { test_derive_to_variant { #[derive(Clone, Eq, PartialEq, Debug, ToVariant, FromVariant)] struct ToVar where @@ -90,108 +90,92 @@ fn test_derive_to_variant() -> bool { } } - let ok = std::panic::catch_unwind(|| { - let data = ToVar:: { + let data = ToVar:: { + foo: 42, + bar: 54.0, + baz: ToVarEnum::Foo(true), + ptr: std::ptr::null_mut(), + skipped: 42, + }; + + let variant = data.to_variant(); + let dictionary = variant + .try_to::() + .expect("should be dictionary"); + assert_eq!(Some(42), dictionary.get("foo").and_then(|v| v.to::())); + assert_eq!( + Some(54.0), + dictionary.get("bar").and_then(|v| v.to::()) + ); + assert_eq!( + Some("*mut ()".into()), + dictionary.get("ptr").and_then(|v| v.to::()) + ); + assert!(!dictionary.contains("skipped")); + + let enum_dict = dictionary + .get("baz") + .and_then(|v| v.to::()) + .expect("should be dictionary"); + assert_eq!( + Some(true), + enum_dict.get("Foo").and_then(|v| v.to::()) + ); + + assert_eq!( + Ok(ToVar:: { foo: 42, bar: 54.0, baz: ToVarEnum::Foo(true), ptr: std::ptr::null_mut(), - skipped: 42, - }; - - let variant = data.to_variant(); - let dictionary = variant - .try_to::() - .expect("should be dictionary"); - assert_eq!(Some(42), dictionary.get("foo").and_then(|v| v.to::())); - assert_eq!( - Some(54.0), - dictionary.get("bar").and_then(|v| v.to::()) - ); - assert_eq!( - Some("*mut ()".into()), - dictionary.get("ptr").and_then(|v| v.to::()) - ); - assert!(!dictionary.contains("skipped")); - - let enum_dict = dictionary - .get("baz") - .and_then(|v| v.to::()) - .expect("should be dictionary"); - assert_eq!( - Some(true), - enum_dict.get("Foo").and_then(|v| v.to::()) - ); - - assert_eq!( - Ok(ToVar:: { - foo: 42, - bar: 54.0, - baz: ToVarEnum::Foo(true), - ptr: std::ptr::null_mut(), - skipped: 0, - }), - ToVar::from_variant(&variant) - ); - - let data = ToVarTuple::(1, 2, false); - let variant = data.to_variant(); - let tuple_array = variant.to::().expect("should be array"); - - assert_eq!(2, tuple_array.len()); - assert_eq!(Some(1), tuple_array.get(0).to::()); - assert_eq!(Some(false), tuple_array.get(1).to::()); - assert_eq!( - Ok(ToVarTuple::(1, 0, false)), - ToVarTuple::from_variant(&variant) - ); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_derive_to_variant failed"); - } - - ok -} - -fn test_derive_owned_to_variant() -> bool { - println!(" -- test_derive_owned_to_variant"); - + skipped: 0, + }), + ToVar::from_variant(&variant) + ); + + let data = ToVarTuple::(1, 2, false); + let variant = data.to_variant(); + let tuple_array = variant.to::().expect("should be array"); + + assert_eq!(2, tuple_array.len()); + assert_eq!(Some(1), tuple_array.get(0).to::()); + assert_eq!(Some(false), tuple_array.get(1).to::()); + assert_eq!( + Ok(ToVarTuple::(1, 0, false)), + ToVarTuple::from_variant(&variant) + ); +}} + +// ---------------------------------------------------------------------------------------------------------------------------------------------- + +crate::godot_itest! { test_derive_owned_to_variant { #[derive(OwnedToVariant)] struct ToVar { arr: VariantArray, } - let ok = std::panic::catch_unwind(|| { - let data = ToVar { - arr: [1, 2, 3].iter().collect(), - }; - - let variant = data.owned_to_variant(); - let dictionary = variant.to::().expect("should be dictionary"); - let array = dictionary - .get("arr") - .and_then(|v| v.to::()) - .expect("should be array"); - assert_eq!(3, array.len()); - assert_eq!( - &[1, 2, 3], - array - .iter() - .map(|v| v.to::().unwrap()) - .collect::>() - .as_slice() - ); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_derive_owned_to_variant failed"); - } - - ok -} + let data = ToVar { + arr: [1, 2, 3].iter().collect(), + }; + + let variant = data.owned_to_variant(); + let dictionary = variant.to::().expect("should be dictionary"); + let array = dictionary + .get("arr") + .and_then(|v| v.to::()) + .expect("should be array"); + assert_eq!(3, array.len()); + assert_eq!( + &[1, 2, 3], + array + .iter() + .map(|v| v.to::().unwrap()) + .collect::>() + .as_slice() + ); +}} + +// ---------------------------------------------------------------------------------------------------------------------------------------------- #[derive(NativeClass)] #[inherit(Reference)] @@ -209,22 +193,13 @@ impl MinimalDerive { } } -fn test_derive_nativeclass() -> bool { - println!(" -- test_derive_nativeclass"); - - let ok = std::panic::catch_unwind(|| { - let thing = Instance::::new(); - let base: Ref = thing.into_base(); - assert_eq!(unsafe { base.call("answer", &[]).to::() }, Some(54)); - }) - .is_ok(); +crate::godot_itest! { test_derive_nativeclass { + let thing = Instance::::new(); + let base: Ref = thing.into_base(); + assert_eq!(unsafe { base.call("answer", &[]).to::() }, Some(54)); +}} - if !ok { - godot_error!(" !! Test test_derive_nativeclass failed"); - } - - ok -} +// ---------------------------------------------------------------------------------------------------------------------------------------------- #[derive(NativeClass)] #[inherit(Reference)] @@ -239,29 +214,19 @@ impl EmplacementOnly { } } -fn test_derive_nativeclass_without_constructor() -> bool { - println!(" -- test_derive_nativeclass_without_constructor"); +crate::godot_itest! { test_derive_nativeclass_without_constructor { + let foo = Instance::emplace(EmplacementOnly(54)); + assert_eq!(Ok(54), foo.map(|foo, owner| { foo.answer(&*owner) })); - let ok = std::panic::catch_unwind(|| { - let foo = Instance::emplace(EmplacementOnly(54)); + let base = foo.into_base(); + assert_eq!(Some(54), unsafe { base.call("answer", &[]).to::() }); - assert_eq!(Ok(54), foo.map(|foo, owner| { foo.answer(&*owner) })); + let foo = Instance::::try_from_base(base) + .expect("should be able to downcast"); + assert_eq!(Ok(54), foo.map(|foo, owner| { foo.answer(&*owner) })); +}} - let base = foo.into_base(); - assert_eq!(Some(54), unsafe { base.call("answer", &[]).to::() }); - - let foo = Instance::::try_from_base(base) - .expect("should be able to downcast"); - assert_eq!(Ok(54), foo.map(|foo, owner| { foo.answer(&*owner) })); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_derive_nativeclass_without_constructor failed"); - } - - ok -} +// ---------------------------------------------------------------------------------------------------------------------------------------------- #[derive(NativeClass)] struct WithoutInherit(i64); @@ -278,22 +243,13 @@ impl WithoutInherit { } } -fn test_derive_nativeclass_without_inherit() -> bool { - println!(" -- test_derive_nativeclass_without_inherit"); - - let ok = std::panic::catch_unwind(|| { - let thing = Instance::::new(); - let base = thing.into_base(); - assert_eq!(unsafe { base.call("answer", &[]).to::() }, Some(54)); - }) - .is_ok(); +crate::godot_itest! { test_derive_nativeclass_without_inherit { + let thing = Instance::::new(); + let base = thing.into_base(); + assert_eq!(unsafe { base.call("answer", &[]).to::() }, Some(54)); +}} - if !ok { - godot_error!(" !! Test test_derive_nativeclass_without_inherit failed"); - } - - ok -} +// ---------------------------------------------------------------------------------------------------------------------------------------------- #[derive(NativeClass)] #[inherit(Reference)] @@ -311,22 +267,13 @@ impl GodotAttrWithoutBase { } } -fn test_derive_nativeclass_godot_attr_without_base() -> bool { - println!(" -- test_derive_nativeclass_godot_attr_without_base"); +crate::godot_itest! { test_derive_nativeclass_godot_attr_without_base { + let thing = Instance::::new(); + let base = thing.into_base(); + assert_eq!(unsafe { base.call("answer", &[]).to::() }, Some(54)); +}} - let ok = std::panic::catch_unwind(|| { - let thing = Instance::::new(); - let base = thing.into_base(); - assert_eq!(unsafe { base.call("answer", &[]).to::() }, Some(54)); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_derive_nativeclass_godot_attr_without_base failed"); - } - - ok -} +// ---------------------------------------------------------------------------------------------------------------------------------------------- #[derive(NativeClass)] #[inherit(Reference)] @@ -344,22 +291,13 @@ impl GodotAttrWithBase { } } -fn test_derive_nativeclass_godot_attr_with_base() -> bool { - println!(" -- test_derive_nativeclass_godot_attr_with_base"); - - let ok = std::panic::catch_unwind(|| { - let thing = Instance::::new(); - let base = thing.into_base(); - assert_eq!(unsafe { base.call("answer", &[]).to::() }, Some(54)); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_derive_nativeclass_godot_attr_with_base failed"); - } +crate::godot_itest! { test_derive_nativeclass_godot_attr_with_base { + let thing = Instance::::new(); + let base = thing.into_base(); + assert_eq!(unsafe { base.call("answer", &[]).to::() }, Some(54)); +}} - ok -} +// ---------------------------------------------------------------------------------------------------------------------------------------------- #[derive(NativeClass)] #[inherit(Reference)] @@ -379,24 +317,15 @@ impl GodotAttrDerefReturn { } } -fn test_derive_nativeclass_godot_attr_deref_return() -> bool { - println!(" -- test_derive_nativeclass_godot_attr_deref_return"); - - let ok = std::panic::catch_unwind(|| { - let thing = Instance::::new(); - let base = thing.into_base(); +crate::godot_itest! { test_derive_nativeclass_godot_attr_deref_return { + let thing = Instance::::new(); + let base = thing.into_base(); - let res = unsafe { base.call("answer", &[]).to::>() }; - assert_eq!(res, Some([12, 34].into())); - }) - .is_ok(); + let res = unsafe { base.call("answer", &[]).to::>() }; + assert_eq!(res, Some([12, 34].into())); +}} - if !ok { - godot_error!(" !! Test test_derive_nativeclass_godot_attr_deref_return failed"); - } - - ok -} +// ---------------------------------------------------------------------------------------------------------------------------------------------- #[derive(NativeClass)] #[inherit(Reference)] @@ -414,29 +343,20 @@ impl GodotAttrRenameMethod { } } -fn test_derive_nativeclass_godot_attr_rename_method() -> bool { - println!(" -- test_derive_nativeclass_godot_attr_rename_method"); +crate::godot_itest! { test_derive_nativeclass_godot_attr_rename_method { + let thing = Instance::::new(); + let base = thing.into_base(); + assert_eq!(unsafe { base.call("ask", &[]).to::() }, Some(54)); +}} - let ok = std::panic::catch_unwind(|| { - let thing = Instance::::new(); - let base = thing.into_base(); - assert_eq!(unsafe { base.call("ask", &[]).to::() }, Some(54)); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_derive_nativeclass_godot_attr_rename_method failed"); - } - - ok -} +// ---------------------------------------------------------------------------------------------------------------------------------------------- #[derive(NativeClass)] #[inherit(Reference)] -struct GodotAttrToUseAllMacroParameters(Rc>>); +struct GodotAttrAllArguments(Rc>>); #[methods] -impl GodotAttrToUseAllMacroParameters { +impl GodotAttrAllArguments { fn new(_owner: &Reference) -> Self { let vec = Vec::from([12, 34]); let rc_ref = Rc::new(RefCell::new(vec)); @@ -449,26 +369,15 @@ impl GodotAttrToUseAllMacroParameters { } } -fn test_derive_nativeclass_godot_attr_to_use_all_macro_parameters() -> bool { - println!(" -- test_derive_nativeclass_godot_attr_to_use_all_macro_parameters"); - - let ok = std::panic::catch_unwind(|| { - let thing = Instance::::new(); - let base = thing.into_base(); - - let res = unsafe { base.call("ask", &[]).to::>() }; - assert_eq!(res, Some([12, 34].into())); - }) - .is_ok(); +crate::godot_itest! { test_derive_nativeclass_godot_attr_all_arguments { + let thing = Instance::::new(); + let base = thing.into_base(); - if !ok { - godot_error!( - " !! Test test_derive_nativeclass_godot_attr_to_use_all_macro_parameters failed" - ); - } + let res = unsafe { base.call("ask", &[]).to::>() }; + assert_eq!(res, Some([12, 34].into())); +}} - ok -} +// ---------------------------------------------------------------------------------------------------------------------------------------------- #[derive(NativeClass)] #[inherit(Node)] @@ -503,42 +412,34 @@ impl CustomGetSet { } } -fn test_derive_nativeclass_with_property_get_set() -> bool { - println!(" -- test_derive_nativeclass_with_property_get_set"); - let ok = std::panic::catch_unwind(|| { - use gdnative::export::user_data::Map; - let (owner, script) = CustomGetSet::new_instance().decouple(); - script - .map(|script| { - assert_eq!(0, script.get_called.get()); - assert_eq!(0, script.set_called.get()); - }) - .unwrap(); - owner.set("foo", 1); - script - .map(|script| { - assert_eq!(0, script.get_called.get()); - assert_eq!(1, script.set_called.get()); - assert_eq!(1, script._foo); - }) - .unwrap(); - assert_eq!(1, i32::from_variant(&owner.get("foo")).unwrap()); - script - .map(|script| { - assert_eq!(1, script.get_called.get()); - assert_eq!(1, script.set_called.get()); - }) - .unwrap(); - owner.free(); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_derive_nativeclass_with_property_get_set failed"); - } - - ok -} +crate::godot_itest! { test_derive_nativeclass_with_property_get_set { + use gdnative::export::user_data::Map; + let (owner, script) = CustomGetSet::new_instance().decouple(); + script + .map(|script| { + assert_eq!(0, script.get_called.get()); + assert_eq!(0, script.set_called.get()); + }) + .unwrap(); + owner.set("foo", 1); + script + .map(|script| { + assert_eq!(0, script.get_called.get()); + assert_eq!(1, script.set_called.get()); + assert_eq!(1, script._foo); + }) + .unwrap(); + assert_eq!(1, i32::from_variant(&owner.get("foo")).unwrap()); + script + .map(|script| { + assert_eq!(1, script.get_called.get()); + assert_eq!(1, script.set_called.get()); + }) + .unwrap(); + owner.free(); +}} + +// ---------------------------------------------------------------------------------------------------------------------------------------------- #[derive(NativeClass)] struct MyVec { @@ -566,24 +467,15 @@ impl MyVec { } } -fn test_derive_nativeclass_property_with_only_getter() -> bool { - println!(" -- test_derive_nativeclass_property_with_only_getter"); - - let ok = std::panic::catch_unwind(|| { - use gdnative::export::user_data::MapMut; - let (owner, script) = MyVec::new_instance().decouple(); - assert_eq!(u32::from_variant(&owner.get("size")).unwrap(), 0); - script.map_mut(|script| script.add(42)).unwrap(); - assert_eq!(u32::from_variant(&owner.get("size")).unwrap(), 1); - // check the setter doesn't work for `size` - let _ = std::panic::catch_unwind(|| owner.set("size", 3)); - assert_eq!(u32::from_variant(&owner.get("size")).unwrap(), 1); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_derive_nativeclass_property_with_only_getter failed"); - } +crate::godot_itest! { test_derive_nativeclass_property_with_only_getter { + use gdnative::export::user_data::MapMut; + let (owner, script) = MyVec::new_instance().decouple(); + assert_eq!(u32::from_variant(&owner.get("size")).unwrap(), 0); - ok -} + script.map_mut(|script| script.add(42)).unwrap(); + assert_eq!(u32::from_variant(&owner.get("size")).unwrap(), 1); + + // check the setter doesn't work for `size` + let _ = std::panic::catch_unwind(|| owner.set("size", 3)); + assert_eq!(u32::from_variant(&owner.get("size")).unwrap(), 1); +}} diff --git a/test/src/test_free_ub.rs b/test/src/test_free_ub.rs index 8ec2ab6f6..7a94f59d2 100644 --- a/test/src/test_free_ub.rs +++ b/test/src/test_free_ub.rs @@ -45,38 +45,27 @@ impl Drop for Bar { } } -fn test_owner_free_ub() -> bool { - println!(" -- test_owner_free_ub"); +crate::godot_itest! { test_owner_free_ub { + let drop_counter = Arc::new(AtomicUsize::new(0)); - let ok = std::panic::catch_unwind(|| { - let drop_counter = Arc::new(AtomicUsize::new(0)); + { + let bar = Bar(42, Arc::clone(&drop_counter)).emplace(); - { - let bar = Bar(42, Arc::clone(&drop_counter)).emplace(); + assert_eq!(Some(true), unsafe { + bar.base().call("set_script_is_not_ub", &[]).to() + }); - assert_eq!(Some(true), unsafe { - bar.base().call("set_script_is_not_ub", &[]).to() - }); - - bar.into_base().free(); - } - - { - let bar = Bar(42, Arc::clone(&drop_counter)).emplace(); - - assert_eq!(Some(true), unsafe { - bar.base().call("free_is_not_ub", &[]).to() - }); - } + bar.into_base().free(); + } - // the values are eventually dropped - assert_eq!(2, drop_counter.load(AtomicOrdering::Acquire)); - }) - .is_ok(); + { + let bar = Bar(42, Arc::clone(&drop_counter)).emplace(); - if !ok { - godot_error!(" !! Test test_owner_free_ub failed"); + assert_eq!(Some(true), unsafe { + bar.base().call("free_is_not_ub", &[]).to() + }); } - ok -} + // the values are eventually dropped + assert_eq!(2, drop_counter.load(AtomicOrdering::Acquire)); +}} diff --git a/test/src/test_map_owned.rs b/test/src/test_map_owned.rs index a591d901b..2352f5a46 100644 --- a/test/src/test_map_owned.rs +++ b/test/src/test_map_owned.rs @@ -30,40 +30,29 @@ impl VecBuilder { } } -fn test_map_owned() -> bool { - println!(" -- test_map_owned"); - - let ok = std::panic::catch_unwind(|| { - let v1 = Instance::emplace(VecBuilder { v: Vec::new() }).into_shared(); - let v1 = unsafe { v1.assume_safe() }; - - let v2 = v1 - .map_owned(|s, owner| s.append(owner, vec![1, 2, 3])) - .unwrap(); - let v2 = unsafe { v2.assume_safe() }; - assert!(v1 - .map_owned(|_, _| panic!("should never be called")) - .is_err()); - - let v3 = v2 - .map_owned(|s, owner| s.append(owner, vec![4, 5, 6])) - .unwrap(); - let v3 = unsafe { v3.assume_safe() }; - assert!(v2 - .map_owned(|_, _| panic!("should never be called")) - .is_err()); - - let v = v3.map_owned(|s, _| s.v).unwrap(); - assert_eq!(&v, &[1, 2, 3, 4, 5, 6]); - assert!(v3 - .map_owned(|_, _| panic!("should never be called")) - .is_err()); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_map_owned failed"); - } - - ok -} +crate::godot_itest! { test_map_owned { + let v1 = Instance::emplace(VecBuilder { v: Vec::new() }).into_shared(); + let v1 = unsafe { v1.assume_safe() }; + + let v2 = v1 + .map_owned(|s, owner| s.append(owner, vec![1, 2, 3])) + .unwrap(); + let v2 = unsafe { v2.assume_safe() }; + assert!(v1 + .map_owned(|_, _| panic!("should never be called")) + .is_err()); + + let v3 = v2 + .map_owned(|s, owner| s.append(owner, vec![4, 5, 6])) + .unwrap(); + let v3 = unsafe { v3.assume_safe() }; + assert!(v2 + .map_owned(|_, _| panic!("should never be called")) + .is_err()); + + let v = v3.map_owned(|s, _| s.v).unwrap(); + assert_eq!(&v, &[1, 2, 3, 4, 5, 6]); + assert!(v3 + .map_owned(|_, _| panic!("should never be called")) + .is_err()); +}} diff --git a/test/src/test_register.rs b/test/src/test_register.rs index 680d07c7a..bfb7aa098 100644 --- a/test/src/test_register.rs +++ b/test/src/test_register.rs @@ -79,32 +79,17 @@ impl RegisterProperty { } } -fn test_register_property() -> bool { - println!(" -- test_register_property"); +crate::godot_itest! { test_register_property { + let obj = RegisterProperty::new_instance(); + let base = obj.into_base(); + assert_eq!(Some(42), unsafe { base.call("get_value", &[]).to() }); - let ok = std::panic::catch_unwind(|| { - let obj = RegisterProperty::new_instance(); + base.set("value", 54.to_variant()); + assert_eq!(Some(54), unsafe { base.call("get_value", &[]).to() }); - let base = obj.into_base(); - - assert_eq!(Some(42), unsafe { base.call("get_value", &[]).to() }); - - base.set("value", 54.to_variant()); - - assert_eq!(Some(54), unsafe { base.call("get_value", &[]).to() }); - - unsafe { base.call("set_value", &[4242.to_variant()]) }; - - assert_eq!(Some(4242), unsafe { base.call("get_value", &[]).to() }); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_register_property failed"); - } - - ok -} + unsafe { base.call("set_value", &[4242.to_variant()]) }; + assert_eq!(Some(4242), unsafe { base.call("get_value", &[]).to() }); +}} #[derive(NativeClass)] #[inherit(Reference)] @@ -169,66 +154,55 @@ fn register_methods(builder: &ClassBuilder) { .done(); } -fn test_advanced_methods() -> bool { - println!(" -- test_advanced_methods"); - - let ok = std::panic::catch_unwind(|| { - let thing = Instance::::new(); - let thing = thing.base(); - - assert_eq!( - 45, - i32::from_variant(unsafe { - &thing.call( - "add_ints", - &[1.to_variant(), 2.to_variant(), Variant::nil()], - ) - }) - .unwrap() - ); - - assert_eq!( - 48, - i32::from_variant(unsafe { - &thing.call( - "add_ints", - &[1.to_variant(), 2.to_variant(), 3.to_variant()], - ) - }) - .unwrap() - ); - - approx::assert_relative_eq!( - 6.5, - f32::from_variant(unsafe { - &thing.call( - "add_floats", - &[(5.0).to_variant(), (-2.5).to_variant(), Variant::nil()], - ) - }) - .unwrap() - ); - - let v = Vector2::from_variant(unsafe { +crate::godot_itest! { test_advanced_methods { + let thing = Instance::::new(); + let thing = thing.base(); + + assert_eq!( + 45, + i32::from_variant(unsafe { &thing.call( - "add_vectors", - &[ - Vector2::new(5.0, -5.0).to_variant(), - Vector2::new(-2.5, 2.5).to_variant(), - Variant::nil(), - ], + "add_ints", + &[1.to_variant(), 2.to_variant(), Variant::nil()], ) }) - .unwrap(); + .unwrap() + ); - approx::assert_relative_eq!(3.5, v.x); - approx::assert_relative_eq!(-0.5, v.y); - }) - .is_ok(); + assert_eq!( + 48, + i32::from_variant(unsafe { + &thing.call( + "add_ints", + &[1.to_variant(), 2.to_variant(), 3.to_variant()], + ) + }) + .unwrap() + ); - if !ok { - godot_error!(" !! Test test_advanced_methods failed"); - } + approx::assert_relative_eq!( + 6.5, + f32::from_variant(unsafe { + &thing.call( + "add_floats", + &[(5.0).to_variant(), (-2.5).to_variant(), Variant::nil()], + ) + }) + .unwrap() + ); - ok -} + let v = Vector2::from_variant(unsafe { + &thing.call( + "add_vectors", + &[ + Vector2::new(5.0, -5.0).to_variant(), + Vector2::new(-2.5, 2.5).to_variant(), + Variant::nil(), + ], + ) + }) + .unwrap(); + + approx::assert_relative_eq!(3.5, v.x); + approx::assert_relative_eq!(-0.5, v.y); +}} diff --git a/test/src/test_return_leak.rs b/test/src/test_return_leak.rs index ff4faef37..0f60c32d8 100644 --- a/test/src/test_return_leak.rs +++ b/test/src/test_return_leak.rs @@ -44,48 +44,37 @@ impl Drop for Probe { #[methods] impl Probe {} -fn test_return_leak() -> bool { - println!(" -- test_return_leak"); +crate::godot_itest! { test_return_leak { + let drop_counter = Arc::new(AtomicUsize::new(0)); + + // The object used for its ptrcall getter + let animation_tree = api::AnimationTree::new(); + + // Create an instance of the probe, and drop the reference after setting the property + // to it. After this block, the only reference should be the one in `animation_tree`. + { + let probe = Instance::emplace(Probe { + drop_count: Arc::clone(&drop_counter), + }); + let base = probe.into_base().into_shared(); + animation_tree.set_tree_root(base); + } - let ok = std::panic::catch_unwind(|| { - let drop_counter = Arc::new(AtomicUsize::new(0)); + assert_eq!(0, drop_counter.load(AtomicOrdering::Acquire)); - // The object used for its ptrcall getter - let animation_tree = api::AnimationTree::new(); + // Take the reference out of the property and drop it. The probe should be dropped after + // this block. + { + // This happens via ptrcall, which is what's being tested. + let _probe_reference = animation_tree.tree_root().unwrap(); - // Create an instance of the probe, and drop the reference after setting the property - // to it. After this block, the only reference should be the one in `animation_tree`. - { - let probe = Instance::emplace(Probe { - drop_count: Arc::clone(&drop_counter), - }); - let base = probe.into_base().into_shared(); - animation_tree.set_tree_root(base); - } + // Free `animation_tree` so the reference inside is dropped. + animation_tree.free(); + // `probe_reference` should now be the only reference left. assert_eq!(0, drop_counter.load(AtomicOrdering::Acquire)); - - // Take the reference out of the property and drop it. The probe should be dropped after - // this block. - { - // This happens via ptrcall, which is what's being tested. - let _probe_reference = animation_tree.tree_root().unwrap(); - - // Free `animation_tree` so the reference inside is dropped. - animation_tree.free(); - - // `probe_reference` should now be the only reference left. - assert_eq!(0, drop_counter.load(AtomicOrdering::Acquire)); - } - - // The probe should not be leaked after `probe_reference` is dropped. - assert_eq!(1, drop_counter.load(AtomicOrdering::Acquire)); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_return_leak failed"); } - ok -} + // The probe should not be leaked after `probe_reference` is dropped. + assert_eq!(1, drop_counter.load(AtomicOrdering::Acquire)); +}} diff --git a/test/src/test_serde.rs b/test/src/test_serde.rs index 0d44ae57e..f8e38e7ff 100644 --- a/test/src/test_serde.rs +++ b/test/src/test_serde.rs @@ -113,175 +113,99 @@ impl Foo { } /// Sanity check that a round trip through Variant preserves equality for Foo. -fn test_variant_eq() -> bool { - println!(" -- test_variant_eq"); - - let ok = std::panic::catch_unwind(|| { - let foo = Foo::new(); - let variant = foo.to_variant(); - let result = Foo::from_variant(&variant).expect("Foo::from_variant"); - assert_eq!(foo, result); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_variant_eq failed"); - } - - ok -} +crate::godot_itest! { test_variant_eq { + let foo = Foo::new(); + let variant = foo.to_variant(); + let result = Foo::from_variant(&variant).expect("Foo::from_variant"); + assert_eq!(foo, result); +}} /// Sanity check that a round trip through VariantDispatch preserves equality for Foo. -fn test_dispatch_eq() -> bool { - println!(" -- test_dispatch_eq"); - - let ok = std::panic::catch_unwind(|| { - let foo = Foo::new(); - let dispatch = foo.to_variant().dispatch(); - let result = Foo::from_variant(&Variant::from(&dispatch)).expect("Foo from Dispatch"); - assert_eq!(foo, result); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_dispatch_eq failed"); - } - - ok -} - -fn test_ron() -> bool { - println!(" -- test_ron"); - - let ok = std::panic::catch_unwind(|| { - let foo = Foo::new(); - - let ron_str = ron::to_string(&foo).expect("Foo to RON str"); - let mut de = ron::Deserializer::from_str(ron_str.as_ref()); - let result = Foo::deserialize(de.as_mut().expect("deserialize Foo from RON")).unwrap(); - assert_eq!(foo, result); - - let ron_disp_str = ron::to_string(&foo.to_variant().dispatch()).expect("Dispatch to RON"); - let mut de = ron::Deserializer::from_str(ron_disp_str.as_ref()); - let de = de - .as_mut() - .expect("disp_round_trip ron::Deserializer::from_str"); - let disp = VariantDispatch::deserialize(de).expect("Dispatch from RON"); - let result = Foo::from_variant(&Variant::from(&disp)).expect("Foo from Dispatch from RON"); - assert_eq!(foo, result); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_ron failed"); - } - - ok -} - -fn test_json() -> bool { - println!(" -- test_json"); - - let ok = std::panic::catch_unwind(|| { - let foo = Foo::new(); - - let json_str = serde_json::to_string(&foo).expect("Foo to JSON"); - let result = serde_json::from_str::(json_str.as_ref()).expect("Foo from JSON"); - assert_eq!(foo, result); - - let foo = Foo::new(); - let json_disp_str = - serde_json::to_string(&foo.to_variant().dispatch()).expect("Foo Dispatch to JSON"); - let disp = serde_json::from_str::(json_disp_str.as_ref()) - .expect("Dispatch from JSON"); - let result = Foo::from_variant(&Variant::from(&disp)).expect("Foo from Dispatch from JSON"); - assert_eq!(foo, result); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_json failed"); - } - - ok -} - -fn test_yaml() -> bool { - println!(" -- test_yaml"); - - let ok = std::panic::catch_unwind(|| { - let foo = Foo::new(); - - let yaml_str = serde_yaml::to_string(&foo).expect("Foo to YAML"); - let result = serde_yaml::from_str::(&yaml_str).expect("Foo from YAML"); - assert_eq!(foo, result); - - let yaml_str = - serde_yaml::to_string(&foo.to_variant().dispatch()).expect("Dispatch to YAML"); - let disp = serde_yaml::from_str::(&yaml_str).expect("Dispatch from YAML"); - let result = Foo::from_variant(&Variant::from(&disp)).expect("Foo from Dispatch from YAML"); - assert_eq!(foo, result); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_yaml failed"); - } - - ok -} - -fn test_msgpack() -> bool { - println!(" -- test_msgpack"); - - let ok = std::panic::catch_unwind(|| { - let foo = Foo::new(); - - let msgpack_bytes = rmp_serde::to_vec_named(&foo).expect("Foo to MessagePack"); - let result = - rmp_serde::from_read_ref::<_, Foo>(&msgpack_bytes).expect("Foo from MessagePack"); - assert_eq!(foo, result); - - let msgpack_disp_bytes = - rmp_serde::to_vec_named(&foo.to_variant().dispatch()).expect("Dispatch to MessagePack"); - let disp = rmp_serde::from_read_ref::<_, VariantDispatch>(&msgpack_disp_bytes) - .expect("Dispatch from MessagePack"); - let result = - Foo::from_variant(&Variant::from(&disp)).expect("Foo from Dispatch from MessagePack"); - assert_eq!(foo, result); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_msgpack failed"); - } - - ok -} - -fn test_bincode() -> bool { - println!(" -- test_bincode"); - - let ok = std::panic::catch_unwind(|| { - let foo = Foo::new(); - - let bincode_bytes = bincode::serialize(&foo).expect("Foo to bincode"); - let result = bincode::deserialize::(bincode_bytes.as_ref()).expect("Foo from bincode"); - assert_eq!(foo, result); - - let bincode_bytes = - bincode::serialize(&foo.to_variant().dispatch()).expect("Dispatch to bincode"); - let disp = bincode::deserialize::(bincode_bytes.as_ref()) - .expect("Dispatch from bincode"); - let result = - Foo::from_variant(&Variant::from(&disp)).expect("Foo from Dispatch from bincode"); - assert_eq!(foo, result); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_bincode failed"); - } - - ok -} +crate::godot_itest! { test_dispatch_eq { + let foo = Foo::new(); + let dispatch = foo.to_variant().dispatch(); + let result = Foo::from_variant(&Variant::from(&dispatch)).expect("Foo from Dispatch"); + assert_eq!(foo, result); +}} + +crate::godot_itest! { test_ron { + let foo = Foo::new(); + + let ron_str = ron::to_string(&foo).expect("Foo to RON str"); + let mut de = ron::Deserializer::from_str(ron_str.as_ref()); + let result = Foo::deserialize(de.as_mut().expect("deserialize Foo from RON")).unwrap(); + assert_eq!(foo, result); + + let ron_disp_str = ron::to_string(&foo.to_variant().dispatch()).expect("Dispatch to RON"); + let mut de = ron::Deserializer::from_str(ron_disp_str.as_ref()); + let de = de + .as_mut() + .expect("disp_round_trip ron::Deserializer::from_str"); + let disp = VariantDispatch::deserialize(de).expect("Dispatch from RON"); + let result = Foo::from_variant(&Variant::from(&disp)).expect("Foo from Dispatch from RON"); + assert_eq!(foo, result); +}} + +crate::godot_itest! { test_json { + let foo = Foo::new(); + + let json_str = serde_json::to_string(&foo).expect("Foo to JSON"); + let result = serde_json::from_str::(json_str.as_ref()).expect("Foo from JSON"); + assert_eq!(foo, result); + + let foo = Foo::new(); + let json_disp_str = + serde_json::to_string(&foo.to_variant().dispatch()).expect("Foo Dispatch to JSON"); + let disp = serde_json::from_str::(json_disp_str.as_ref()) + .expect("Dispatch from JSON"); + + let result = Foo::from_variant(&Variant::from(&disp)).expect("Foo from Dispatch from JSON"); + assert_eq!(foo, result); +}} + +crate::godot_itest! { test_yaml { + let foo = Foo::new(); + + let yaml_str = serde_yaml::to_string(&foo).expect("Foo to YAML"); + let result = serde_yaml::from_str::(&yaml_str).expect("Foo from YAML"); + assert_eq!(foo, result); + + let yaml_str = + serde_yaml::to_string(&foo.to_variant().dispatch()).expect("Dispatch to YAML"); + let disp = serde_yaml::from_str::(&yaml_str).expect("Dispatch from YAML"); + let result = Foo::from_variant(&Variant::from(&disp)).expect("Foo from Dispatch from YAML"); + assert_eq!(foo, result); +}} + +crate::godot_itest! { test_msgpack { + let foo = Foo::new(); + + let msgpack_bytes = rmp_serde::to_vec_named(&foo).expect("Foo to MessagePack"); + let result = + rmp_serde::from_read_ref::<_, Foo>(&msgpack_bytes).expect("Foo from MessagePack"); + assert_eq!(foo, result); + + let msgpack_disp_bytes = + rmp_serde::to_vec_named(&foo.to_variant().dispatch()).expect("Dispatch to MessagePack"); + let disp = rmp_serde::from_read_ref::<_, VariantDispatch>(&msgpack_disp_bytes) + .expect("Dispatch from MessagePack"); + let result = + Foo::from_variant(&Variant::from(&disp)).expect("Foo from Dispatch from MessagePack"); + assert_eq!(foo, result); +}} + +crate::godot_itest! { test_bincode { + let foo = Foo::new(); + + let bincode_bytes = bincode::serialize(&foo).expect("Foo to bincode"); + let result = bincode::deserialize::(bincode_bytes.as_ref()).expect("Foo from bincode"); + assert_eq!(foo, result); + + let bincode_bytes = + bincode::serialize(&foo.to_variant().dispatch()).expect("Dispatch to bincode"); + let disp = bincode::deserialize::(bincode_bytes.as_ref()) + .expect("Dispatch from bincode"); + let result = + Foo::from_variant(&Variant::from(&disp)).expect("Foo from Dispatch from bincode"); + assert_eq!(foo, result); +}} diff --git a/test/src/test_vararray_return.rs b/test/src/test_vararray_return.rs index f4e952f04..132c272b8 100644 --- a/test/src/test_vararray_return.rs +++ b/test/src/test_vararray_return.rs @@ -1,8 +1,6 @@ use gdnative::api::Camera; use gdnative::prelude::*; -use gdnative_core::godot_itest; - pub(crate) fn run_tests() -> bool { let mut status = true; @@ -13,7 +11,7 @@ pub(crate) fn run_tests() -> bool { pub(crate) fn register(_handle: InitHandle) {} -godot_itest! { test_vararray_return_crash { +crate::godot_itest! { test_vararray_return_crash { // See https://github.com/godot-rust/godot-rust/issues/422 let camera = Camera::new(); diff --git a/test/src/test_variant_call_args.rs b/test/src/test_variant_call_args.rs index 22edf362e..7a0e11c4b 100644 --- a/test/src/test_variant_call_args.rs +++ b/test/src/test_variant_call_args.rs @@ -51,40 +51,29 @@ impl VariantCallArgs { } } -fn test_variant_call_args() -> bool { - println!(" -- test_variant_call_args"); +crate::godot_itest! { test_variant_call_args { + let obj = Instance::::new(); - let ok = std::panic::catch_unwind(|| { - let obj = Instance::::new(); + let mut base = obj.into_base().into_shared().to_variant(); - let mut base = obj.into_base().into_shared().to_variant(); + assert_eq!(Some(42), call_i64(&mut base, "zero", &[])); - assert_eq!(Some(42), call_i64(&mut base, "zero", &[])); + assert_eq!(Some(126), call_i64(&mut base, "one", &[Variant::new(3)])); - assert_eq!(Some(126), call_i64(&mut base, "one", &[Variant::new(3)])); + assert_eq!( + Some(-10), + call_i64(&mut base, "two", &[Variant::new(-1), Variant::new(32)]) + ); - assert_eq!( - Some(-10), - call_i64(&mut base, "two", &[Variant::new(-1), Variant::new(32)]) - ); - - assert_eq!( - Some(-52), - call_i64( - &mut base, - "three", - &[Variant::new(-2), Variant::new(4), Variant::new(8),] - ) - ); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_variant_call_args failed"); - } - - ok -} + assert_eq!( + Some(-52), + call_i64( + &mut base, + "three", + &[Variant::new(-2), Variant::new(4), Variant::new(8),] + ) + ); +}} fn call_i64(variant: &mut Variant, method: &str, args: &[Variant]) -> Option { let result = unsafe { variant.call(method, args) }; diff --git a/test/src/test_variant_ops.rs b/test/src/test_variant_ops.rs index eb080a50a..b82dff597 100644 --- a/test/src/test_variant_ops.rs +++ b/test/src/test_variant_ops.rs @@ -11,38 +11,27 @@ pub(crate) fn run_tests() -> bool { pub(crate) fn register(_handle: InitHandle) {} -fn test_variant_ops() -> bool { - println!(" -- test_variant_ops"); - - let ok = std::panic::catch_unwind(|| { - let arr = VariantArray::new(); - arr.push(&"bar".to_variant()); - arr.push(&"baz".to_variant()); - let arr = arr.into_shared().to_variant(); - - assert_eq!( - Ok(42.to_variant()), - 6.to_variant() - .evaluate(VariantOperator::Multiply, &7.to_variant()), - ); - - assert_eq!( - Ok(false.to_variant()), - "foo".to_variant().evaluate(VariantOperator::In, &arr), - ); - - assert_eq!( - Err(InvalidOp), - "foo" - .to_variant() - .evaluate(VariantOperator::Multiply, &"bar".to_variant()), - ); - }) - .is_ok(); - - if !ok { - godot_error!(" !! Test test_variant_ops failed"); - } - - ok -} +crate::godot_itest! { test_variant_ops { + let arr = VariantArray::new(); + arr.push(&"bar".to_variant()); + arr.push(&"baz".to_variant()); + let arr = arr.into_shared().to_variant(); + + assert_eq!( + Ok(42.to_variant()), + 6.to_variant() + .evaluate(VariantOperator::Multiply, &7.to_variant()), + ); + + assert_eq!( + Ok(false.to_variant()), + "foo".to_variant().evaluate(VariantOperator::In, &arr), + ); + + assert_eq!( + Err(InvalidOp), + "foo" + .to_variant() + .evaluate(VariantOperator::Multiply, &"bar".to_variant()), + ); +}}