forked from mlua-rs/rlua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.rs
109 lines (97 loc) · 3.02 KB
/
string.rs
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
use std::borrow::Cow;
use rlua::{Lua, String, Table};
fn with_str<F>(s: &str, f: F)
where
F: FnOnce(String),
{
Lua::new().context(|lua| {
let string = lua.create_string(s).unwrap();
f(string);
});
}
#[test]
fn compare() {
// 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)); // rlua::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]
fn string_views() {
Lua::new().context(|lua| {
lua.load(
r#"
ok = "null bytes are valid utf-8, wh\0 knew?"
err = "but \255 isn't :("
empty = ""
"#,
)
.exec()
.unwrap();
let globals = lua.globals();
let ok: String = globals.get("ok").unwrap();
let err: String = globals.get("err").unwrap();
let empty: String = globals.get("empty").unwrap();
assert_eq!(
ok.to_str().unwrap(),
"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().unwrap(), "");
assert_eq!(empty.as_bytes_with_nul(), &[0]);
assert_eq!(empty.as_bytes(), &[]);
});
}
#[test]
fn raw_string() {
Lua::new().context(|lua| {
let rs = lua.create_string(&[0, 1, 2, 3, 0, 1, 2, 3]).unwrap();
assert_eq!(rs.as_bytes(), &[0, 1, 2, 3, 0, 1, 2, 3]);
});
}
#[test]
fn string_eq_hash() {
use std::collections::HashSet;
Lua::new().context(|lua| {
lua.load(
r#"
strlist = {
"foo",
"bar",
"foo",
"bar",
"baz"
}
"#,
)
.exec()
.unwrap();
let t: Table = lua.globals().get("strlist").unwrap();
let stringset: HashSet<rlua::String> = t.sequence_values().map(|v| v.unwrap()).collect();
let sorted = lua
.create_table_from((1..).zip(stringset.into_iter()))
.unwrap();
lua.globals().set("filtered", sorted).unwrap();
lua.load(
r#"
table.sort(filtered)
result = table.concat(filtered, ",")
"#,
)
.exec()
.unwrap();
let result: std::string::String = lua.globals().get("result").unwrap();
assert_eq!(result, "bar,baz,foo");
});
}