Skip to content

Commit 2560d1d

Browse files
bors[bot]Bromeon
andauthored
Merge #912
912: Test + workaround: `Eq/Ord` for `StringName` r=Bromeon a=Bromeon Upstream bug, see godotengine/godot#63104 This adds tests for `StringName`'s equality and ordering relation. Co-authored-by: Jan Haller <bromeon@gmail.com>
2 parents 659ac47 + 37c1360 commit 2560d1d

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

gdnative-core/src/core_types/string.rs

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::core_types::Variant;
22
use crate::object::NewRef;
33
use crate::private::get_api;
44
use crate::sys;
5+
use std::cmp::Ordering;
56

67
use std::ffi::CStr;
78
use std::fmt;
@@ -591,8 +592,36 @@ impl StringName {
591592
impl_basic_traits_as_sys! {
592593
for StringName as godot_string_name {
593594
Drop => godot_string_name_destroy;
594-
Eq => godot_string_name_operator_equal;
595-
Ord => godot_string_name_operator_less;
595+
596+
// Note: Godot's equal/less implementations contained a bug until Godot 3.5, see https://github.com/godot-rust/godot-rust/pull/912
597+
// Thus explicit impl as a workaround for now
598+
// Eq => godot_string_name_operator_equal;
599+
// Ord => godot_string_name_operator_less;
600+
}
601+
}
602+
603+
impl PartialEq for StringName {
604+
#[inline]
605+
fn eq(&self, other: &Self) -> bool {
606+
// Slow but correct -- see comment above
607+
self.to_godot_string() == other.to_godot_string()
608+
}
609+
}
610+
611+
impl Eq for StringName {}
612+
613+
impl PartialOrd for StringName {
614+
#[inline]
615+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
616+
Some(Ord::cmp(self, other))
617+
}
618+
}
619+
620+
impl Ord for StringName {
621+
#[inline]
622+
fn cmp(&self, other: &Self) -> Ordering {
623+
// Slow but correct -- see comment above
624+
Ord::cmp(&self.to_godot_string(), &other.to_godot_string())
596625
}
597626
}
598627

@@ -728,3 +757,47 @@ godot_test!(test_string {
728757
assert_eq!(fmt2, GodotString::from("foo bar"));
729758
assert_eq!(fmt_string2, GodotString::from("{0} {1}"));
730759
});
760+
761+
godot_test!(test_string_name_eq {
762+
use crate::core_types::{GodotString, StringName};
763+
764+
let a: StringName = StringName::from_str("some string");
765+
let b: StringName = StringName::from_godot_string(&GodotString::from("some string"));
766+
let c: StringName = StringName::from_str(String::from("some other string"));
767+
let d: StringName = StringName::from_str("yet another one");
768+
769+
// test Eq
770+
assert_eq!(a, b);
771+
assert_ne!(a, c);
772+
assert_ne!(a, d);
773+
assert_ne!(b, c);
774+
assert_ne!(b, d);
775+
assert_ne!(c, d);
776+
777+
let back = b.to_godot_string();
778+
assert_eq!(back, GodotString::from("some string"));
779+
});
780+
781+
godot_test!(test_string_name_ord {
782+
use crate::core_types::{GodotString, StringName};
783+
784+
let a: StringName = StringName::from_str("some string");
785+
let b: StringName = StringName::from_godot_string(&GodotString::from("some string"));
786+
let c: StringName = StringName::from_str(String::from("some other string"));
787+
788+
// test Ord
789+
let a_b = Ord::cmp(&a, &b);
790+
let b_a = Ord::cmp(&b, &a);
791+
assert_eq!(a_b, Ordering::Equal);
792+
assert_eq!(b_a, Ordering::Equal);
793+
794+
let a_c = Ord::cmp(&a, &c);
795+
let c_a = Ord::cmp(&c, &a);
796+
let b_c = Ord::cmp(&b, &c);
797+
let c_b = Ord::cmp(&c, &b);
798+
assert_ne!(a_c, Ordering::Equal);
799+
assert_eq!(a_c, b_c);
800+
assert_eq!(c_a, c_b);
801+
assert_eq!(a_c, c_a.reverse());
802+
assert_eq!(b_c, c_b.reverse());
803+
});

test/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ pub extern "C" fn run_tests(
2424
) -> gdnative::sys::godot_variant {
2525
let mut status = true;
2626
status &= gdnative::core_types::test_string();
27+
status &= gdnative::core_types::test_string_name_eq();
28+
status &= gdnative::core_types::test_string_name_ord();
2729

2830
status &= gdnative::core_types::test_dictionary();
2931
// status &= gdnative::test_dictionary_clone_clear();

0 commit comments

Comments
 (0)