Skip to content

Commit 18feb31

Browse files
committed
Fix tests on Lua <5.4 and cargo fmt.
1 parent 11b3045 commit 18feb31

File tree

11 files changed

+57
-81
lines changed

11 files changed

+57
-81
lines changed

examples/guided_tour.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::f32;
22
use std::iter::FromIterator;
33

4-
use rlua::{Function, Lua, MetaMethod, Result, UserData, UserDataMethods, Variadic, RluaCompat};
4+
use rlua::{Function, Lua, MetaMethod, Result, RluaCompat, UserData, UserDataMethods, Variadic};
55

66
fn main() -> Result<()> {
77
// You can create a new Lua state with `Lua::new()`. This loads the default Lua std library

src/lib.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
pub use mlua::*;
22

33
pub mod prelude {
4-
pub use mlua::prelude::*;
5-
pub use super::ToLua;
64
pub use super::RluaCompat;
5+
pub use super::ToLua;
6+
pub use mlua::prelude::*;
77
}
88

99
pub type Context<'lua> = &'lua Lua;
1010

1111
pub trait RluaCompat {
1212
#[deprecated = "Context is no longer needed; call methods on Lua directly."]
1313
fn context<R, F>(&self, f: F) -> R
14-
where F: FnOnce(&Lua) -> R;
14+
where
15+
F: FnOnce(&Lua) -> R;
1516
}
1617

1718
impl RluaCompat for Lua {
1819
fn context<R, F>(&self, f: F) -> R
19-
where F: FnOnce(&Lua) -> R {
20+
where
21+
F: FnOnce(&Lua) -> R,
22+
{
2023
f(self)
2124
}
2225
}
@@ -28,7 +31,7 @@ pub trait ToLuaCompat<'lua> {
2831
fn to_lua(self, context: &'lua Lua) -> mlua::Result<Value<'lua>>;
2932
}
3033

31-
impl<'lua, T:IntoLua<'lua>> ToLuaCompat<'lua> for T {
34+
impl<'lua, T: IntoLua<'lua>> ToLuaCompat<'lua> for T {
3235
fn to_lua(self, context: &'lua Lua) -> mlua::Result<Value<'lua>> {
3336
self.into_lua(context)
3437
}

tests/conversion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rlua::{Integer, Lua, Result, String, Table, Value, RluaCompat, ToLuaCompat};
1+
use rlua::{Integer, Lua, Result, RluaCompat, String, Table, ToLuaCompat, Value};
22

33
fn valid_float(verify: Result<Value>, expected: f64) {
44
let verify_unwrap = verify.unwrap();

tests/function.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rlua::{Function, Lua, String, RluaCompat};
1+
use rlua::{Function, Lua, RluaCompat, String};
22

33
#[test]
44
fn test_function() {

tests/hooks.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::cell::RefCell;
22
use std::ops::Deref;
33
use std::sync::{Arc, Mutex};
44

5-
use rlua::{Error, HookTriggers, Lua, Value, RluaCompat};
5+
use rlua::{Error, HookTriggers, Lua, RluaCompat, Value};
66

77
#[cfg(not(rlua_luajit))] // LuaJIT gives different results
88
#[test]
@@ -71,10 +71,7 @@ fn function_calls() {
7171
#[cfg(not(rlua_luajit))]
7272
assert_eq!(
7373
*output,
74-
vec![
75-
(None, "main"),
76-
(Some("len".to_string()), "C")
77-
]
74+
vec![(None, "main"), (Some("len".to_string()), "C")]
7875
);
7976
#[cfg(rlua_luajit)]
8077
assert_eq!(

tests/memory.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::sync::Arc;
22

3-
use rlua::{Error, Lua, Nil, UserData, RluaCompat};
3+
use rlua::{Error, Lua, Nil, RluaCompat, UserData};
44

55
#[cfg(not(rlua_luajit))] // Custom allocators for LuaJIT not available
66
#[test]

tests/scope.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::cell::Cell;
22
use std::rc::Rc;
33

4-
use rlua::{Error, Function, Lua, MetaMethod, String, UserData, UserDataMethods, RluaCompat};
4+
use rlua::{Error, Function, Lua, MetaMethod, RluaCompat, String, UserData, UserDataMethods};
55

66
#[test]
77
fn scope_func() {
@@ -19,7 +19,8 @@ fn scope_func() {
1919
f.call::<_, ()>(()).unwrap();
2020
assert_eq!(Rc::strong_count(&rc), 2);
2121
Ok(())
22-
}).unwrap();
22+
})
23+
.unwrap();
2324
assert_eq!(rc.get(), 42);
2425
assert_eq!(Rc::strong_count(&rc), 1);
2526

@@ -51,14 +52,13 @@ fn scope_drop() {
5152
lua.globals()
5253
.set(
5354
"test",
54-
scope
55-
.create_userdata(MyUserdata(rc.clone()))
56-
.unwrap(),
55+
scope.create_userdata(MyUserdata(rc.clone())).unwrap(),
5756
)
5857
.unwrap();
5958
assert_eq!(Rc::strong_count(&rc), 2);
6059
Ok(())
61-
}).unwrap();
60+
})
61+
.unwrap();
6262
assert_eq!(Rc::strong_count(&rc), 1);
6363

6464
match lua.load("test:method()").exec() {
@@ -82,7 +82,8 @@ fn scope_capture() {
8282
})
8383
.unwrap()
8484
.call::<_, ()>(())
85-
}).unwrap();
85+
})
86+
.unwrap();
8687
});
8788
assert_eq!(i, 42);
8889
}
@@ -99,7 +100,8 @@ fn outer_lua_access() {
99100
})
100101
.unwrap()
101102
.call::<_, ()>(())
102-
}).unwrap();
103+
})
104+
.unwrap();
103105
assert_eq!(table.get::<_, String>("a").unwrap(), "b");
104106
});
105107
}
@@ -142,7 +144,8 @@ fn scope_userdata_methods() {
142144

143145
lua.scope(|scope| {
144146
f.call::<_, ()>(scope.create_nonstatic_userdata(MyUserData(&i)).unwrap())
145-
}).unwrap();
147+
})
148+
.unwrap();
146149
});
147150

148151
assert_eq!(i.get(), 44);
@@ -185,7 +188,8 @@ fn scope_userdata_functions() {
185188

186189
lua.scope(|scope| {
187190
f.call::<_, ()>(scope.create_nonstatic_userdata(MyUserData(&dummy)).unwrap())
188-
}).unwrap();
191+
})
192+
.unwrap();
189193

190194
assert_eq!(lua.globals().get::<_, i64>("i").unwrap(), 3);
191195
});
@@ -239,6 +243,7 @@ fn scope_userdata_mismatch() {
239243
Ok(_) => panic!("incorrectly returned Ok"),
240244
}
241245
Ok(())
242-
}).unwrap();
246+
})
247+
.unwrap();
243248
});
244249
}

tests/string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Cow;
22

3-
use rlua::{Lua, String, Table, RluaCompat};
3+
use rlua::{Lua, RluaCompat, String, Table};
44

55
fn with_str<F>(s: &str, f: F)
66
where

tests/table.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rlua::{Lua, Nil, Result, Table, Value, RluaCompat};
1+
use rlua::{Lua, Nil, Result, RluaCompat, Table, Value};
22

33
#[test]
44
fn test_set_get() {

tests/tests.rs

+23-32
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ use std::sync::Arc;
55
use std::{error, f32, f64, fmt};
66

77
use rlua::{
8-
Error, ExternalError, Function, LuaOptions, Lua, Nil, Result, StdLib, String, Table, UserData,
9-
Value, Variadic,
10-
RluaCompat
8+
Error, ExternalError, Function, Lua, LuaOptions, Nil, Result, RluaCompat, StdLib, String,
9+
Table, UserData, Value, Variadic,
1110
};
1211

1312
#[test]
@@ -295,7 +294,12 @@ fn test_error() {
295294
});
296295

297296
match catch_unwind(|| -> Result<usize> {
298-
Lua::new_with(StdLib::ALL_SAFE, LuaOptions::default().catch_rust_panics(false)).unwrap().context(|lua| {
297+
Lua::new_with(
298+
StdLib::ALL_SAFE,
299+
LuaOptions::default().catch_rust_panics(false),
300+
)
301+
.unwrap()
302+
.context(|lua| {
299303
let globals = lua.globals();
300304

301305
lua.load(
@@ -324,7 +328,12 @@ fn test_error() {
324328
};
325329

326330
match catch_unwind(|| -> Result<()> {
327-
Lua::new_with(StdLib::ALL_SAFE, LuaOptions::default().catch_rust_panics(false)).unwrap().context(|lua| {
331+
Lua::new_with(
332+
StdLib::ALL_SAFE,
333+
LuaOptions::default().catch_rust_panics(false),
334+
)
335+
.unwrap()
336+
.context(|lua| {
328337
let globals = lua.globals();
329338

330339
lua.load(
@@ -485,11 +494,7 @@ fn test_load_wrappers() {
485494
#[test]
486495
fn test_no_load_wrappers() {
487496
unsafe {
488-
Lua::unsafe_new_with(
489-
StdLib::ALL,
490-
LuaOptions::default()
491-
)
492-
.context(|lua| {
497+
Lua::unsafe_new_with(StdLib::ALL, LuaOptions::default()).context(|lua| {
493498
let globals = lua.globals();
494499
lua.load(
495500
r#"
@@ -608,11 +613,7 @@ fn test_no_loadfile_wrappers() {
608613
tmppath2.push("test_no_loadfile_wrappers2.lua");
609614

610615
unsafe {
611-
Lua::unsafe_new_with(
612-
StdLib::ALL,
613-
LuaOptions::default()
614-
)
615-
.context(|lua| {
616+
Lua::unsafe_new_with(StdLib::ALL, LuaOptions::default()).context(|lua| {
616617
let globals = lua.globals();
617618
globals.set("filename", tmppath.to_str().unwrap()).unwrap();
618619
globals
@@ -729,11 +730,7 @@ fn test_no_dofile_wrappers() {
729730
tmppath.push("test_no_dofile_wrappers.lua");
730731

731732
unsafe {
732-
Lua::unsafe_new_with(
733-
StdLib::ALL,
734-
LuaOptions::default()
735-
)
736-
.context(|lua| {
733+
Lua::unsafe_new_with(StdLib::ALL, LuaOptions::default()).context(|lua| {
737734
let globals = lua.globals();
738735
globals.set("filename", tmppath.to_str().unwrap()).unwrap();
739736
lua.load(
@@ -839,11 +836,7 @@ fn test_loadstring_wrappers() {
839836
#[test]
840837
fn test_no_loadstring_wrappers() {
841838
unsafe {
842-
Lua::unsafe_new_with(
843-
StdLib::ALL,
844-
LuaOptions::default()
845-
)
846-
.context(|lua| {
839+
Lua::unsafe_new_with(StdLib::ALL, LuaOptions::default()).context(|lua| {
847840
let globals = lua.globals();
848841
if globals.get::<_, Function>("loadstring").is_err() {
849842
// Loadstring is not present in Lua 5.4, and only with a
@@ -914,11 +907,7 @@ fn test_default_loadlib() {
914907
#[test]
915908
fn test_no_remove_loadlib() {
916909
unsafe {
917-
Lua::unsafe_new_with(
918-
StdLib::ALL,
919-
LuaOptions::default()
920-
)
921-
.context(|lua| {
910+
Lua::unsafe_new_with(StdLib::ALL, LuaOptions::default()).context(|lua| {
922911
let globals = lua.globals();
923912
let package = globals.get::<_, Table>("package").unwrap();
924913
let _loadlib = package.get::<_, Function>("loadlib").unwrap();
@@ -1031,8 +1020,10 @@ fn test_num_conversion() {
10311020
lua.unpack::<f64>(lua.pack(f32::MAX).unwrap()).unwrap(),
10321021
f32::MAX as f64
10331022
);
1034-
assert_eq!(lua.unpack::<f32>(lua.pack(f64::MAX).unwrap()).unwrap(),
1035-
f32::INFINITY);
1023+
assert_eq!(
1024+
lua.unpack::<f32>(lua.pack(f64::MAX).unwrap()).unwrap(),
1025+
f32::INFINITY
1026+
);
10361027

10371028
assert_eq!(
10381029
lua.unpack::<i128>(lua.pack(1i128 << 64).unwrap()).unwrap(),

tests/userdata.rs

+2-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::sync::Arc;
22

33
use rlua::{
4-
AnyUserData, ExternalError, Function, Lua, MetaMethod, String, UserData, UserDataMethods, RluaCompat
4+
AnyUserData, ExternalError, Function, Lua, MetaMethod, RluaCompat, String, UserData,
5+
UserDataMethods,
56
};
67

78
#[test]
@@ -179,7 +180,6 @@ fn detroys_userdata() {
179180
assert_eq!(Arc::strong_count(&rc), 1);
180181
}
181182

182-
#[cfg(rlua_lua54)]
183183
#[test]
184184
fn user_value() {
185185
struct MyUserData;
@@ -206,26 +206,6 @@ fn user_value() {
206206
});
207207
}
208208

209-
#[cfg(rlua_lua53)]
210-
#[test]
211-
fn user_value() {
212-
struct MyUserData;
213-
impl UserData for MyUserData {}
214-
215-
Lua::new().context(|lua| {
216-
let ud = lua.create_userdata(MyUserData).unwrap();
217-
ud.set_nth_user_value(1, "hello").unwrap();
218-
assert!(ud.set_nth_user_value(2, "world").is_err());
219-
assert_eq!(ud.nth_user_value::<String>(1).unwrap(), "hello");
220-
assert!(ud.nth_user_value::<String>(2).is_err());
221-
assert!(ud.nth_user_value::<u32>(1).is_err());
222-
assert!(ud.nth_user_value::<u32>(2).is_err());
223-
assert!(ud.nth_user_value::<String>(0).is_err());
224-
assert!(ud.nth_user_value::<String>(3).is_err());
225-
assert!(ud.nth_user_value::<u32>(0).is_err());
226-
assert!(ud.nth_user_value::<u32>(3).is_err());
227-
});
228-
}
229209
#[test]
230210
fn test_functions() {
231211
struct MyUserData(i64);

0 commit comments

Comments
 (0)