Skip to content

Commit

Permalink
Merge #153
Browse files Browse the repository at this point in the history
153: Implement more Variant accessors r=Bromeon a=astrale-sharp

Closes #140

The test for stringify are looking pretty good in my opinion

This is not ready to merge :
[ ] Discuss mapped method returned type (sys::GDExtensionBool, sys::GDExtensionInt, etc)
[ ] Test all mapped method
[ ] remove itest(focus)
[ ] squash the commits

Currently I wrote this kind of code, I have
```rust
 pub fn booleanize(&self) -> sys::GDExtensionBool {
        interface_fn!(variant_booleanize)(self.var_sys())
    }
```
Which is bad cause we probably don't want sys::GDExtensionBool to be seen by users, instead I guess it should return a regular rust boolean but I'm unsure how to do the conversion yet, it's probably somewhere in the code



Co-authored-by: Astrale <jules180798@yahoo.fr>
  • Loading branch information
bors[bot] and Astrale authored Mar 10, 2023
2 parents 2b72fd6 + b562fac commit 9353407
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
16 changes: 15 additions & 1 deletion godot-core/src/builtin/variant/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,29 @@ impl Variant {
}
}

/// return a `GodotString` representing the variant
#[allow(unused_mut)]
pub(crate) fn stringify(&self) -> GodotString {
pub fn stringify(&self) -> GodotString {
let mut result = GodotString::new();
unsafe {
interface_fn!(variant_stringify)(self.var_sys(), result.string_sys());
}
result
}

/// return the hash value of the variant.
///
/// _Godot equivalent : `@GlobalScope.hash()`_
pub fn hash(&self) -> i64 {
unsafe { interface_fn!(variant_hash)(self.var_sys()) }
}

/// return a false only if the variant is `Variant::NIL`
/// or an empty `TypedArray` or `Dictionary`.
pub fn booleanize(&self) -> bool {
unsafe { interface_fn!(variant_booleanize)(self.var_sys()) != 0 }
}

fn from_opaque(opaque: OpaqueVariant) -> Self {
Self { opaque }
}
Expand Down
63 changes: 62 additions & 1 deletion itest/rust/src/variant_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

use crate::{expect_panic, itest};
use godot::builtin::{
FromVariant, GodotString, NodePath, StringName, ToVariant, Variant, Vector2, Vector3,
dict, varray, FromVariant, GodotString, NodePath, StringName, ToVariant, Variant, Vector2,
Vector3,
};
use godot::engine::Node2D;
use godot::obj::InstanceId;
Expand Down Expand Up @@ -337,6 +338,66 @@ fn variant_type_correct() {
);
}

#[itest]
fn variant_stringify_correct() {
assert_eq!("value".to_variant().stringify(), gstr("value"));
assert_eq!(Variant::nil().stringify(), gstr("<null>"));
assert_eq!(true.to_variant().stringify(), gstr("true"));
assert_eq!(30.to_variant().stringify(), gstr("30"));
assert_eq!(
godot::builtin::varray![1, "hello", false]
.to_variant()
.stringify(),
gstr("[1, \"hello\", false]")
);
assert_eq!(
dict! { "KEY": 50 }.to_variant().stringify(),
gstr("{ \"KEY\": 50 }")
);
}

#[itest]
fn variant_booleanize_correct() {
assert!(gstr("string").to_variant().booleanize());
assert!(10.to_variant().booleanize());
assert!(varray![""].to_variant().booleanize());
assert!(dict! { "Key": 50 }.to_variant().booleanize());

assert!(!Dictionary::new().to_variant().booleanize());
assert!(!varray![].to_variant().booleanize());
assert!(!0.to_variant().booleanize());
assert!(!Variant::nil().booleanize());
assert!(!gstr("").to_variant().booleanize());
}

#[itest]
fn variant_hash_correct() {
let hash_is_not_0 = [
dict! {}.to_variant(),
gstr("").to_variant(),
varray![].to_variant(),
];
let self_equal = [
gstr("string").to_variant(),
varray![false, true, 4, "7"].to_variant(),
0.to_variant(),
dict! { 0 : dict!{ 0: 1 }}.to_variant(),
];

for variant in hash_is_not_0 {
assert_ne!(variant.hash(), 0)
}
for variant in self_equal {
assert_eq!(variant.hash(), variant.hash())
}

assert_eq!(Variant::nil().hash(), 0);

// it's not guaranteed that different object will have different hash but it is
// extremely unlikely for a collision to happen.
assert_ne!(dict! { 0: dict!{ 0: 0 } }, dict! { 0: dict!{ 0: 1 } });
}

// ----------------------------------------------------------------------------------------------------------------------------------------------

fn roundtrip<T>(value: T)
Expand Down

0 comments on commit 9353407

Please sign in to comment.