From 9e16e181320529b6074dfabac7644ea8a0283390 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Wed, 16 Oct 2024 15:58:52 +0100 Subject: [PATCH] Update string tests --- src/string.rs | 9 +++++++++ tests/string.rs | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/string.rs b/src/string.rs index 264d51f..364010f 100644 --- a/src/string.rs +++ b/src/string.rs @@ -172,6 +172,15 @@ impl PartialEq for String { impl Eq for String {} +impl PartialOrd for String +where + T: AsRef<[u8]> + ?Sized, +{ + fn partial_cmp(&self, other: &T) -> Option { + self.as_bytes().partial_cmp(&other.as_ref()) + } +} + impl PartialOrd for String { fn partial_cmp(&self, other: &String) -> Option { Some(self.cmp(other)) diff --git a/tests/string.rs b/tests/string.rs index 456ad84..34a32de 100644 --- a/tests/string.rs +++ b/tests/string.rs @@ -17,6 +17,15 @@ fn test_string_compare() { with_str("teststring", |t| assert_eq!(t, t)); // mlua::String with_str("teststring", |t| assert_eq!(t, Cow::from(b"teststring".as_ref()))); // Cow (borrowed) with_str("bla", |t| assert_eq!(t, Cow::from(b"bla".to_vec()))); // Cow (owned) + + // Test ordering + with_str("a", |a| { + assert!(!(a < a)); + assert!(!(a > a)); + }); + with_str("a", |a| assert!(a < "b")); + with_str("a", |a| assert!(a < b"b")); + with_str("a", |a| with_str("b", |b| assert!(a < b))); } #[test] @@ -52,7 +61,7 @@ fn test_string_views() -> Result<()> { } #[test] -fn test_raw_string() -> Result<()> { +fn test_string_from_bytes() -> Result<()> { let lua = Lua::new(); let rs = lua.create_string(&[0, 1, 2, 3, 0, 1, 2, 3])?; @@ -77,12 +86,14 @@ fn test_string_hash() -> Result<()> { } #[test] -fn test_string_debug() -> Result<()> { +fn test_string_fmt_debug() -> Result<()> { let lua = Lua::new(); // Valid utf8 let s = lua.create_string("hello")?; assert_eq!(format!("{s:?}"), r#""hello""#); + assert_eq!(format!("{:?}", s.to_str()?), r#""hello""#); + assert_eq!(format!("{:?}", s.as_bytes()), "[104, 101, 108, 108, 111]"); // Invalid utf8 let s = lua.create_string(b"hello\0world\r\n\t\xF0\x90\x80")?;