Skip to content

Commit ff20177

Browse files
committed
feat(hlua): add hlua tests
1 parent d24091f commit ff20177

File tree

10 files changed

+1402
-0
lines changed

10 files changed

+1402
-0
lines changed

tests/src/hlua/any.rs

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
use tarantool::hlua::{
2+
AnyLuaValue,
3+
AnyHashableLuaValue,
4+
AnyLuaString,
5+
};
6+
7+
pub fn read_numbers() {
8+
let mut lua = crate::hlua::global();
9+
10+
lua.set("a", "-2");
11+
lua.set("b", 3.5f32);
12+
lua.set("c", -2.0f32);
13+
14+
let x: AnyLuaValue = lua.get("a").unwrap();
15+
assert_eq!(x, AnyLuaValue::LuaString("-2".to_owned()));
16+
17+
let y: AnyLuaValue = lua.get("b").unwrap();
18+
assert_eq!(y, AnyLuaValue::LuaNumber(3.5));
19+
20+
let z: AnyLuaValue = lua.get("c").unwrap();
21+
assert_eq!(z, AnyLuaValue::LuaNumber(-2.0));
22+
}
23+
24+
pub fn read_hashable_numbers() {
25+
let mut lua = crate::hlua::global();
26+
27+
lua.set("a", -2.0f32);
28+
lua.set("b", 4.0f32);
29+
lua.set("c", "4");
30+
31+
let x: AnyHashableLuaValue = lua.get("a").unwrap();
32+
assert_eq!(x, AnyHashableLuaValue::LuaNumber(-2));
33+
34+
let y: AnyHashableLuaValue = lua.get("b").unwrap();
35+
assert_eq!(y, AnyHashableLuaValue::LuaNumber(4));
36+
37+
let z: AnyHashableLuaValue = lua.get("c").unwrap();
38+
assert_eq!(z, AnyHashableLuaValue::LuaString("4".to_owned()));
39+
}
40+
41+
pub fn read_strings() {
42+
let mut lua = crate::hlua::global();
43+
44+
lua.set("a", "hello");
45+
lua.set("b", "3x");
46+
lua.set("c", "false");
47+
48+
let x: AnyLuaValue = lua.get("a").unwrap();
49+
assert_eq!(x, AnyLuaValue::LuaString("hello".to_string()));
50+
51+
let y: AnyLuaValue = lua.get("b").unwrap();
52+
assert_eq!(y, AnyLuaValue::LuaString("3x".to_string()));
53+
54+
let z: AnyLuaValue = lua.get("c").unwrap();
55+
assert_eq!(z, AnyLuaValue::LuaString("false".to_string()));
56+
}
57+
58+
pub fn read_hashable_strings() {
59+
let mut lua = crate::hlua::global();
60+
61+
lua.set("a", "hello");
62+
lua.set("b", "3x");
63+
lua.set("c", "false");
64+
65+
let x: AnyHashableLuaValue = lua.get("a").unwrap();
66+
assert_eq!(x, AnyHashableLuaValue::LuaString("hello".to_string()));
67+
68+
let y: AnyHashableLuaValue = lua.get("b").unwrap();
69+
assert_eq!(y, AnyHashableLuaValue::LuaString("3x".to_string()));
70+
71+
let z: AnyHashableLuaValue = lua.get("c").unwrap();
72+
assert_eq!(z, AnyHashableLuaValue::LuaString("false".to_string()));
73+
}
74+
75+
pub fn read_booleans() {
76+
let mut lua = crate::hlua::global();
77+
78+
lua.set("a", true);
79+
lua.set("b", false);
80+
81+
let x: AnyLuaValue = lua.get("a").unwrap();
82+
assert_eq!(x, AnyLuaValue::LuaBoolean(true));
83+
84+
let y: AnyLuaValue = lua.get("b").unwrap();
85+
assert_eq!(y, AnyLuaValue::LuaBoolean(false));
86+
}
87+
88+
pub fn read_hashable_booleans() {
89+
let mut lua = crate::hlua::global();
90+
91+
lua.set("a", true);
92+
lua.set("b", false);
93+
94+
let x: AnyHashableLuaValue = lua.get("a").unwrap();
95+
assert_eq!(x, AnyHashableLuaValue::LuaBoolean(true));
96+
97+
let y: AnyHashableLuaValue = lua.get("b").unwrap();
98+
assert_eq!(y, AnyHashableLuaValue::LuaBoolean(false));
99+
}
100+
101+
pub fn read_tables() {
102+
let mut lua = crate::hlua::global();
103+
lua.execute::<()>("
104+
a = {x = 12, y = 19}
105+
b = {z = a, w = 'test string'}
106+
c = {'first', 'second'}
107+
").unwrap();
108+
109+
fn get<'a>(table: &'a AnyLuaValue, key: &str) -> &'a AnyLuaValue {
110+
let test_key = AnyLuaValue::LuaString(key.to_owned());
111+
match table {
112+
&AnyLuaValue::LuaArray(ref vec) => {
113+
let &(_, ref value) = vec.iter().find(|&&(ref key, _)| key == &test_key).expect("key not found");
114+
value
115+
},
116+
_ => panic!("not a table")
117+
}
118+
}
119+
120+
fn get_numeric<'a>(table: &'a AnyLuaValue, key: usize) -> &'a AnyLuaValue {
121+
let test_key = AnyLuaValue::LuaNumber(key as f64);
122+
match table {
123+
&AnyLuaValue::LuaArray(ref vec) => {
124+
let &(_, ref value) = vec.iter().find(|&&(ref key, _)| key == &test_key).expect("key not found");
125+
value
126+
},
127+
_ => panic!("not a table")
128+
}
129+
}
130+
131+
let a: AnyLuaValue = lua.get("a").unwrap();
132+
assert_eq!(get(&a, "x"), &AnyLuaValue::LuaNumber(12.0));
133+
assert_eq!(get(&a, "y"), &AnyLuaValue::LuaNumber(19.0));
134+
135+
let b: AnyLuaValue = lua.get("b").unwrap();
136+
assert_eq!(get(&get(&b, "z"), "x"), get(&a, "x"));
137+
assert_eq!(get(&get(&b, "z"), "y"), get(&a, "y"));
138+
139+
let c: AnyLuaValue = lua.get("c").unwrap();
140+
assert_eq!(get_numeric(&c, 1), &AnyLuaValue::LuaString("first".to_owned()));
141+
assert_eq!(get_numeric(&c, 2), &AnyLuaValue::LuaString("second".to_owned()));
142+
}
143+
144+
pub fn read_hashable_tables() {
145+
let mut lua = crate::hlua::global();
146+
lua.execute::<()>("
147+
a = {x = 12, y = 19}
148+
b = {z = a, w = 'test string'}
149+
c = {'first', 'second'}
150+
").unwrap();
151+
152+
fn get<'a>(table: &'a AnyHashableLuaValue, key: &str) -> &'a AnyHashableLuaValue {
153+
let test_key = AnyHashableLuaValue::LuaString(key.to_owned());
154+
match table {
155+
&AnyHashableLuaValue::LuaArray(ref vec) => {
156+
let &(_, ref value) = vec.iter().find(|&&(ref key, _)| key == &test_key).expect("key not found");
157+
value
158+
},
159+
_ => panic!("not a table")
160+
}
161+
}
162+
163+
fn get_numeric<'a>(table: &'a AnyHashableLuaValue, key: usize) -> &'a AnyHashableLuaValue {
164+
let test_key = AnyHashableLuaValue::LuaNumber(key as i32);
165+
match table {
166+
&AnyHashableLuaValue::LuaArray(ref vec) => {
167+
let &(_, ref value) = vec.iter().find(|&&(ref key, _)| key == &test_key).expect("key not found");
168+
value
169+
},
170+
_ => panic!("not a table")
171+
}
172+
}
173+
174+
let a: AnyHashableLuaValue = lua.get("a").unwrap();
175+
assert_eq!(get(&a, "x"), &AnyHashableLuaValue::LuaNumber(12));
176+
assert_eq!(get(&a, "y"), &AnyHashableLuaValue::LuaNumber(19));
177+
178+
let b: AnyHashableLuaValue = lua.get("b").unwrap();
179+
assert_eq!(get(&get(&b, "z"), "x"), get(&a, "x"));
180+
assert_eq!(get(&get(&b, "z"), "y"), get(&a, "y"));
181+
182+
let c: AnyHashableLuaValue = lua.get("c").unwrap();
183+
assert_eq!(get_numeric(&c, 1), &AnyHashableLuaValue::LuaString("first".to_owned()));
184+
assert_eq!(get_numeric(&c, 2), &AnyHashableLuaValue::LuaString("second".to_owned()));
185+
}
186+
187+
pub fn push_numbers() {
188+
let mut lua = crate::hlua::global();
189+
190+
lua.set("a", AnyLuaValue::LuaNumber(3.0));
191+
192+
let x: i32 = lua.get("a").unwrap();
193+
assert_eq!(x, 3);
194+
}
195+
196+
pub fn push_hashable_numbers() {
197+
let mut lua = crate::hlua::global();
198+
199+
lua.set("a", AnyHashableLuaValue::LuaNumber(3));
200+
201+
let x: i32 = lua.get("a").unwrap();
202+
assert_eq!(x, 3);
203+
}
204+
205+
pub fn push_strings() {
206+
let mut lua = crate::hlua::global();
207+
208+
lua.set("a", AnyLuaValue::LuaString("hello".to_string()));
209+
210+
let x: String = lua.get("a").unwrap();
211+
assert_eq!(x, "hello");
212+
}
213+
214+
pub fn push_hashable_strings() {
215+
let mut lua = crate::hlua::global();
216+
217+
lua.set("a", AnyHashableLuaValue::LuaString("hello".to_string()));
218+
219+
let x: String = lua.get("a").unwrap();
220+
assert_eq!(x, "hello");
221+
}
222+
223+
pub fn push_booleans() {
224+
let mut lua = crate::hlua::global();
225+
226+
lua.set("a", AnyLuaValue::LuaBoolean(true));
227+
228+
let x: bool = lua.get("a").unwrap();
229+
assert_eq!(x, true);
230+
}
231+
232+
pub fn push_hashable_booleans() {
233+
let mut lua = crate::hlua::global();
234+
235+
lua.set("a", AnyHashableLuaValue::LuaBoolean(true));
236+
237+
let x: bool = lua.get("a").unwrap();
238+
assert_eq!(x, true);
239+
}
240+
241+
pub fn push_nil() {
242+
let mut lua = crate::hlua::global();
243+
244+
lua.set("a", AnyLuaValue::LuaNil);
245+
246+
let x: Option<i32> = lua.get("a");
247+
assert!(x.is_none(),
248+
"x is a Some value when it should be a None value. X: {:?}",
249+
x);
250+
}
251+
252+
pub fn push_hashable_nil() {
253+
let mut lua = crate::hlua::global();
254+
255+
lua.set("a", AnyHashableLuaValue::LuaNil);
256+
257+
let x: Option<i32> = lua.get("a");
258+
assert!(x.is_none(),
259+
"x is a Some value when it should be a None value. X: {:?}",
260+
x);
261+
}
262+
263+
pub fn non_utf_8_string() {
264+
let mut lua = crate::hlua::global();
265+
let a = lua.execute::<AnyLuaValue>(r"return '\xff\xfe\xff\xfe'").unwrap();
266+
match a {
267+
AnyLuaValue::LuaAnyString(AnyLuaString(v)) => {
268+
assert_eq!(Vec::from(&b"\xff\xfe\xff\xfe"[..]), v);
269+
},
270+
_ => panic!("Decoded to wrong variant"),
271+
}
272+
}

0 commit comments

Comments
 (0)