|
6 | 6 |
|
7 | 7 | use crate::{expect_panic, itest};
|
8 | 8 | use godot::builtin::{
|
9 |
| - FromVariant, GodotString, NodePath, StringName, ToVariant, Variant, Vector2, Vector3, |
| 9 | + dict, varray, FromVariant, GodotString, NodePath, StringName, ToVariant, Variant, Vector2, |
| 10 | + Vector3, |
10 | 11 | };
|
11 | 12 | use godot::engine::Node2D;
|
12 | 13 | use godot::obj::InstanceId;
|
@@ -337,6 +338,66 @@ fn variant_type_correct() {
|
337 | 338 | );
|
338 | 339 | }
|
339 | 340 |
|
| 341 | +#[itest] |
| 342 | +fn variant_stringify_correct() { |
| 343 | + assert_eq!("value".to_variant().stringify(), gstr("value")); |
| 344 | + assert_eq!(Variant::nil().stringify(), gstr("<null>")); |
| 345 | + assert_eq!(true.to_variant().stringify(), gstr("true")); |
| 346 | + assert_eq!(30.to_variant().stringify(), gstr("30")); |
| 347 | + assert_eq!( |
| 348 | + godot::builtin::varray![1, "hello", false] |
| 349 | + .to_variant() |
| 350 | + .stringify(), |
| 351 | + gstr("[1, \"hello\", false]") |
| 352 | + ); |
| 353 | + assert_eq!( |
| 354 | + dict! { "KEY": 50 }.to_variant().stringify(), |
| 355 | + gstr("{ \"KEY\": 50 }") |
| 356 | + ); |
| 357 | +} |
| 358 | + |
| 359 | +#[itest] |
| 360 | +fn variant_booleanize_correct() { |
| 361 | + assert!(gstr("string").to_variant().booleanize()); |
| 362 | + assert!(10.to_variant().booleanize()); |
| 363 | + assert!(varray![""].to_variant().booleanize()); |
| 364 | + assert!(dict! { "Key": 50 }.to_variant().booleanize()); |
| 365 | + |
| 366 | + assert!(!Dictionary::new().to_variant().booleanize()); |
| 367 | + assert!(!varray![].to_variant().booleanize()); |
| 368 | + assert!(!0.to_variant().booleanize()); |
| 369 | + assert!(!Variant::nil().booleanize()); |
| 370 | + assert!(!gstr("").to_variant().booleanize()); |
| 371 | +} |
| 372 | + |
| 373 | +#[itest] |
| 374 | +fn variant_hash_correct() { |
| 375 | + let hash_is_not_0 = [ |
| 376 | + dict! {}.to_variant(), |
| 377 | + gstr("").to_variant(), |
| 378 | + varray![].to_variant(), |
| 379 | + ]; |
| 380 | + let self_equal = [ |
| 381 | + gstr("string").to_variant(), |
| 382 | + varray![false, true, 4, "7"].to_variant(), |
| 383 | + 0.to_variant(), |
| 384 | + dict! { 0 : dict!{ 0: 1 }}.to_variant(), |
| 385 | + ]; |
| 386 | + |
| 387 | + for variant in hash_is_not_0 { |
| 388 | + assert_ne!(variant.hash(), 0) |
| 389 | + } |
| 390 | + for variant in self_equal { |
| 391 | + assert_eq!(variant.hash(), variant.hash()) |
| 392 | + } |
| 393 | + |
| 394 | + assert_eq!(Variant::nil().hash(), 0); |
| 395 | + |
| 396 | + // it's not guaranteed that different object will have different hash but it is |
| 397 | + // extremely unlikely for a collision to happen. |
| 398 | + assert_ne!(dict! { 0: dict!{ 0: 0 } }, dict! { 0: dict!{ 0: 1 } }); |
| 399 | +} |
| 400 | + |
340 | 401 | // ----------------------------------------------------------------------------------------------------------------------------------------------
|
341 | 402 |
|
342 | 403 | fn roundtrip<T>(value: T)
|
|
0 commit comments