Skip to content

Commit c702077

Browse files
committed
More Lua values conversion tests
1 parent 0854522 commit c702077

File tree

2 files changed

+97
-7
lines changed

2 files changed

+97
-7
lines changed

src/conversion.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,10 @@ impl IntoLua for Error {
243243

244244
impl FromLua for Error {
245245
#[inline]
246-
fn from_lua(value: Value, lua: &Lua) -> Result<Error> {
246+
fn from_lua(value: Value, _: &Lua) -> Result<Error> {
247247
match value {
248248
Value::Error(err) => Ok(*err),
249-
val => Ok(Error::runtime(
250-
lua.coerce_string(val)?
251-
.and_then(|s| Some(s.to_str().ok()?.to_owned()))
252-
.unwrap_or_else(|| "<unprintable error>".to_owned()),
253-
)),
249+
val => Ok(Error::runtime(val.to_string()?)),
254250
}
255251
}
256252
}

tests/conversion.rs

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ fn test_function_into_lua() -> Result<()> {
9494
Ok(())
9595
}
9696

97+
#[test]
98+
fn test_function_from_lua() -> Result<()> {
99+
let lua = Lua::new();
100+
101+
assert!(lua.globals().get::<Function>("print").is_ok());
102+
match lua.globals().get::<Function>("math") {
103+
Err(err @ Error::FromLuaConversionError { .. }) => {
104+
assert_eq!(err.to_string(), "error converting Lua table to function");
105+
}
106+
_ => panic!("expected `Error::FromLuaConversionError`"),
107+
}
108+
109+
Ok(())
110+
}
111+
97112
#[test]
98113
fn test_thread_into_lua() -> Result<()> {
99114
let lua = Lua::new();
@@ -112,6 +127,20 @@ fn test_thread_into_lua() -> Result<()> {
112127
Ok(())
113128
}
114129

130+
#[test]
131+
fn test_thread_from_lua() -> Result<()> {
132+
let lua = Lua::new();
133+
134+
match lua.globals().get::<Thread>("print") {
135+
Err(err @ Error::FromLuaConversionError { .. }) => {
136+
assert_eq!(err.to_string(), "error converting Lua function to thread");
137+
}
138+
_ => panic!("expected `Error::FromLuaConversionError`"),
139+
}
140+
141+
Ok(())
142+
}
143+
115144
#[test]
116145
fn test_anyuserdata_into_lua() -> Result<()> {
117146
let lua = Lua::new();
@@ -130,6 +159,45 @@ fn test_anyuserdata_into_lua() -> Result<()> {
130159
Ok(())
131160
}
132161

162+
#[test]
163+
fn test_anyuserdata_from_lua() -> Result<()> {
164+
let lua = Lua::new();
165+
166+
match lua.globals().get::<AnyUserData>("print") {
167+
Err(err @ Error::FromLuaConversionError { .. }) => {
168+
assert_eq!(err.to_string(), "error converting Lua function to userdata");
169+
}
170+
_ => panic!("expected `Error::FromLuaConversionError`"),
171+
}
172+
173+
Ok(())
174+
}
175+
176+
#[test]
177+
fn test_error_conversion() -> Result<()> {
178+
let lua = Lua::new();
179+
180+
// Any Lua value can be converted to `Error`
181+
match lua.convert::<Error>(Error::external("external error")) {
182+
Ok(Error::ExternalError(msg)) => assert_eq!(msg.to_string(), "external error"),
183+
res => panic!("expected `Error::ExternalError`, got {res:?}"),
184+
}
185+
match lua.convert::<Error>("abc") {
186+
Ok(Error::RuntimeError(msg)) => assert_eq!(msg, "abc"),
187+
res => panic!("expected `Error::RuntimeError`, got {res:?}"),
188+
}
189+
match lua.convert::<Error>(true) {
190+
Ok(Error::RuntimeError(msg)) => assert_eq!(msg, "true"),
191+
res => panic!("expected `Error::RuntimeError`, got {res:?}"),
192+
}
193+
match lua.convert::<Error>(lua.globals()) {
194+
Ok(Error::RuntimeError(msg)) => assert!(msg.starts_with("table:")),
195+
res => panic!("expected `Error::RuntimeError`, got {res:?}"),
196+
}
197+
198+
Ok(())
199+
}
200+
133201
#[test]
134202
fn test_registry_value_into_lua() -> Result<()> {
135203
let lua = Lua::new();
@@ -140,7 +208,7 @@ fn test_registry_value_into_lua() -> Result<()> {
140208
let value1 = lua.pack(&r)?;
141209
let value2 = lua.pack(r)?;
142210
assert_eq!(value1.as_str().as_deref(), Some("hello, world"));
143-
assert_eq!(value2.to_pointer(), value2.to_pointer());
211+
assert_eq!(value1.to_pointer(), value2.to_pointer());
144212

145213
// Push into stack
146214
let t = lua.create_table()?;
@@ -175,6 +243,32 @@ fn test_registry_key_from_lua() -> Result<()> {
175243
Ok(())
176244
}
177245

246+
#[test]
247+
fn test_bool_into_lua() -> Result<()> {
248+
let lua = Lua::new();
249+
250+
// Direct conversion
251+
assert!(true.into_lua(&lua)?.is_boolean());
252+
253+
// Push into stack
254+
let table = lua.create_table()?;
255+
table.set("b", true)?;
256+
assert_eq!(true, table.get::<bool>("b")?);
257+
258+
Ok(())
259+
}
260+
261+
#[test]
262+
fn test_bool_from_lua() -> Result<()> {
263+
let lua = Lua::new();
264+
265+
assert!(lua.globals().get::<bool>("print")?);
266+
assert!(lua.convert::<bool>(123)?);
267+
assert!(!lua.convert::<bool>(Value::Nil)?);
268+
269+
Ok(())
270+
}
271+
178272
#[test]
179273
fn test_integer_from_lua() -> Result<()> {
180274
let lua = Lua::new();

0 commit comments

Comments
 (0)