2018-02-09 23:52:05 -05:00
|
|
|
use std::borrow::Cow;
|
2022-04-08 11:43:09 -04:00
|
|
|
use std::collections::HashSet;
|
2018-02-09 23:52:05 -05:00
|
|
|
|
2019-10-14 17:21:30 -04:00
|
|
|
use mlua::{Lua, Result, String};
|
2018-02-09 23:52:05 -05:00
|
|
|
|
|
|
|
#[test]
|
2021-06-18 18:13:56 -04:00
|
|
|
fn test_string_compare() {
|
2019-09-28 10:23:17 -04:00
|
|
|
fn with_str<F: FnOnce(String)>(s: &str, f: F) {
|
2019-10-14 17:21:30 -04:00
|
|
|
f(Lua::new().create_string(s).unwrap());
|
2019-09-28 10:23:17 -04:00
|
|
|
}
|
|
|
|
|
2018-02-09 23:52:05 -05:00
|
|
|
// Tests that all comparisons we want to have are usable
|
|
|
|
with_str("teststring", |t| assert_eq!(t, "teststring")); // &str
|
|
|
|
with_str("teststring", |t| assert_eq!(t, b"teststring")); // &[u8]
|
|
|
|
with_str("teststring", |t| assert_eq!(t, b"teststring".to_vec())); // Vec<u8>
|
|
|
|
with_str("teststring", |t| assert_eq!(t, "teststring".to_string())); // String
|
2019-10-01 11:11:12 -04:00
|
|
|
with_str("teststring", |t| assert_eq!(t, t)); // mlua::String
|
2018-02-09 23:52:05 -05:00
|
|
|
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]
|
2021-06-18 18:13:56 -04:00
|
|
|
fn test_string_views() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
lua.load(
|
2018-02-09 23:52:05 -05:00
|
|
|
r#"
|
2019-09-28 10:23:17 -04:00
|
|
|
ok = "null bytes are valid utf-8, wh\0 knew?"
|
2019-10-14 17:21:30 -04:00
|
|
|
err = "but \255 isn't :("
|
2019-09-28 10:23:17 -04:00
|
|
|
empty = ""
|
|
|
|
"#,
|
2019-09-27 12:38:24 -04:00
|
|
|
)
|
2019-09-28 10:23:17 -04:00
|
|
|
.exec()?;
|
2018-02-09 23:52:05 -05:00
|
|
|
|
|
|
|
let globals = lua.globals();
|
2019-09-28 10:23:17 -04:00
|
|
|
let ok: String = globals.get("ok")?;
|
|
|
|
let err: String = globals.get("err")?;
|
|
|
|
let empty: String = globals.get("empty")?;
|
2018-02-09 23:52:05 -05:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
assert_eq!(ok.to_str()?, "null bytes are valid utf-8, wh\0 knew?");
|
2021-06-03 19:16:40 -04:00
|
|
|
assert_eq!(
|
|
|
|
ok.to_string_lossy(),
|
|
|
|
"null bytes are valid utf-8, wh\0 knew?"
|
|
|
|
);
|
2018-02-09 23:52:05 -05:00
|
|
|
assert_eq!(
|
|
|
|
ok.as_bytes(),
|
|
|
|
&b"null bytes are valid utf-8, wh\0 knew?"[..]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(err.to_str().is_err());
|
|
|
|
assert_eq!(err.as_bytes(), &b"but \xff isn't :("[..]);
|
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
assert_eq!(empty.to_str()?, "");
|
2018-02-09 23:52:05 -05:00
|
|
|
assert_eq!(empty.as_bytes_with_nul(), &[0]);
|
|
|
|
assert_eq!(empty.as_bytes(), &[]);
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
Ok(())
|
2018-02-09 23:52:05 -05:00
|
|
|
}
|
2018-09-30 15:42:04 -04:00
|
|
|
|
|
|
|
#[test]
|
2021-06-18 18:13:56 -04:00
|
|
|
fn test_raw_string() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
let rs = lua.create_string(&[0, 1, 2, 3, 0, 1, 2, 3])?;
|
2018-09-30 15:42:04 -04:00
|
|
|
assert_eq!(rs.as_bytes(), &[0, 1, 2, 3, 0, 1, 2, 3]);
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
Ok(())
|
2018-09-30 15:42:04 -04:00
|
|
|
}
|
2022-04-08 11:43:09 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_string_hash() -> Result<()> {
|
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
let set: HashSet<String> = lua.load(r#"{"hello", "world", "abc", 321}"#).eval()?;
|
|
|
|
assert_eq!(set.len(), 4);
|
|
|
|
assert!(set.contains(b"hello".as_ref()));
|
|
|
|
assert!(set.contains(b"world".as_ref()));
|
|
|
|
assert!(set.contains(b"abc".as_ref()));
|
|
|
|
assert!(set.contains(b"321".as_ref()));
|
|
|
|
assert!(!set.contains(b"Hello".as_ref()));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-01-04 10:56:57 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_string_debug() -> Result<()> {
|
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
// Valid utf8
|
|
|
|
let s = lua.create_string("hello")?;
|
|
|
|
assert_eq!(format!("{s:?}"), r#""hello""#);
|
|
|
|
|
|
|
|
// Invalid utf8
|
|
|
|
let s = lua.create_string(b"hello\0world\r\n\t\xF0\x90\x80")?;
|
|
|
|
assert_eq!(format!("{s:?}"), r#"b"hello\0world\r\n\t\xf0\x90\x80""#);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|