Skip to content

Commit 9e16e18

Browse files
committed
Update string tests
1 parent 7535a23 commit 9e16e18

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/string.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ impl PartialEq for String {
172172

173173
impl Eq for String {}
174174

175+
impl<T> PartialOrd<T> for String
176+
where
177+
T: AsRef<[u8]> + ?Sized,
178+
{
179+
fn partial_cmp(&self, other: &T) -> Option<cmp::Ordering> {
180+
self.as_bytes().partial_cmp(&other.as_ref())
181+
}
182+
}
183+
175184
impl PartialOrd for String {
176185
fn partial_cmp(&self, other: &String) -> Option<cmp::Ordering> {
177186
Some(self.cmp(other))

tests/string.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ fn test_string_compare() {
1717
with_str("teststring", |t| assert_eq!(t, t)); // mlua::String
1818
with_str("teststring", |t| assert_eq!(t, Cow::from(b"teststring".as_ref()))); // Cow (borrowed)
1919
with_str("bla", |t| assert_eq!(t, Cow::from(b"bla".to_vec()))); // Cow (owned)
20+
21+
// Test ordering
22+
with_str("a", |a| {
23+
assert!(!(a < a));
24+
assert!(!(a > a));
25+
});
26+
with_str("a", |a| assert!(a < "b"));
27+
with_str("a", |a| assert!(a < b"b"));
28+
with_str("a", |a| with_str("b", |b| assert!(a < b)));
2029
}
2130

2231
#[test]
@@ -52,7 +61,7 @@ fn test_string_views() -> Result<()> {
5261
}
5362

5463
#[test]
55-
fn test_raw_string() -> Result<()> {
64+
fn test_string_from_bytes() -> Result<()> {
5665
let lua = Lua::new();
5766

5867
let rs = lua.create_string(&[0, 1, 2, 3, 0, 1, 2, 3])?;
@@ -77,12 +86,14 @@ fn test_string_hash() -> Result<()> {
7786
}
7887

7988
#[test]
80-
fn test_string_debug() -> Result<()> {
89+
fn test_string_fmt_debug() -> Result<()> {
8190
let lua = Lua::new();
8291

8392
// Valid utf8
8493
let s = lua.create_string("hello")?;
8594
assert_eq!(format!("{s:?}"), r#""hello""#);
95+
assert_eq!(format!("{:?}", s.to_str()?), r#""hello""#);
96+
assert_eq!(format!("{:?}", s.as_bytes()), "[104, 101, 108, 108, 111]");
8697

8798
// Invalid utf8
8899
let s = lua.create_string(b"hello\0world\r\n\t\xF0\x90\x80")?;

0 commit comments

Comments
 (0)