-
-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathstring.rs
More file actions
174 lines (134 loc) · 4.67 KB
/
Copy pathstring.rs
File metadata and controls
174 lines (134 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use std::borrow::Cow;
use std::collections::HashSet;
use mlua::{Lua, LuaString, Result};
#[test]
fn test_string_compare() {
fn with_str<F: FnOnce(LuaString)>(s: &str, f: F) {
f(Lua::new().create_string(s).unwrap());
}
// 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
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]
fn test_string_views() -> Result<()> {
let lua = Lua::new();
lua.load(
r#"
ok = "null bytes are valid utf-8, wh\0 knew?"
err = "but \255 isn't :("
empty = ""
"#,
)
.exec()?;
let globals = lua.globals();
let ok: LuaString = globals.get("ok")?;
let err: LuaString = globals.get("err")?;
let empty: LuaString = globals.get("empty")?;
assert_eq!(ok.to_str()?, "null bytes are valid utf-8, wh\0 knew?");
assert_eq!(ok.to_string_lossy(), "null bytes are valid utf-8, wh\0 knew?");
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 :("[..]);
assert_eq!(empty.to_str()?, "");
assert_eq!(empty.as_bytes_with_nul(), &[0]);
assert_eq!(empty.as_bytes(), &[]);
Ok(())
}
#[test]
fn test_string_from_bytes() -> Result<()> {
let lua = Lua::new();
let rs = lua.create_string(&[0, 1, 2, 3, 0, 1, 2, 3])?;
assert_eq!(rs.as_bytes(), &[0, 1, 2, 3, 0, 1, 2, 3]);
Ok(())
}
#[test]
fn test_string_hash() -> Result<()> {
let lua = Lua::new();
let set: HashSet<LuaString> = lua.load(r#"{"hello", "world", "abc", 321}"#).eval()?;
assert_eq!(set.len(), 4);
assert!(set.contains(&lua.create_string("hello")?));
assert!(set.contains(&lua.create_string("world")?));
assert!(set.contains(&lua.create_string("abc")?));
assert!(set.contains(&lua.create_string("321")?));
assert!(!set.contains(&lua.create_string("Hello")?));
Ok(())
}
#[test]
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")?;
assert_eq!(format!("{s:?}"), r#"b"hello\0world\r\n\t\xf0\x90\x80""#);
Ok(())
}
#[test]
fn test_string_pointer() -> Result<()> {
let lua = Lua::new();
let str1 = lua.create_string("hello")?;
let str2 = lua.create_string("hello")?;
// Lua uses string interning, so these should be the same
assert_eq!(str1.to_pointer(), str2.to_pointer());
Ok(())
}
#[test]
fn test_string_display() -> Result<()> {
let lua = Lua::new();
let s = lua.create_string("hello")?;
assert_eq!(format!("{}", s.display()), "hello");
// With invalid utf8
let s = lua.create_string(b"hello\0world\xFF")?;
assert_eq!(format!("{}", s.display()), "hello\0world�");
Ok(())
}
#[test]
fn test_string_wrap() -> Result<()> {
let lua = Lua::new();
let s = LuaString::wrap("hello, world");
lua.globals().set("s", s)?;
assert_eq!(lua.globals().get::<LuaString>("s")?, "hello, world");
let s2 = LuaString::wrap("hello, world (owned)".to_string());
lua.globals().set("s2", s2)?;
assert_eq!(lua.globals().get::<LuaString>("s2")?, "hello, world (owned)");
Ok(())
}
#[test]
fn test_bytes_into_iter() -> Result<()> {
let lua = Lua::new();
let s = lua.create_string("hello")?;
let bytes = s.as_bytes();
for (i, &b) in bytes.into_iter().enumerate() {
assert_eq!(b, s.as_bytes()[i]);
}
Ok(())
}
#[cfg(feature = "lua55")]
#[test]
fn test_external_string() -> Result<()> {
let lua = Lua::new();
let s = lua.create_external_string(b"abc\0")?;
assert_eq!(
s.as_bytes(),
b"abc\0",
"Trailing null byte should be preserved if present explicitly"
);
Ok(())
}