Skip to content

Commit 5479546

Browse files
committed
Update chunk tests
1 parent 179c54f commit 5479546

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

src/luau/package.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ fn lua_loader(lua: &Lua, modname: StdString) -> Result<Value> {
203203
match fs::read(&file_path) {
204204
Ok(buf) => {
205205
return lua
206-
.load(&buf)
206+
.load(buf)
207207
.set_name(format!("={}", file_path.display()))
208208
.set_mode(ChunkMode::Text)
209209
.into_function()

tests/chunk.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,38 @@ fn test_chunk_path() -> Result<()> {
1919
return 321
2020
"#,
2121
)?;
22-
let i: i32 = lua.load(&*temp_dir.path().join("module.lua")).eval()?;
22+
let i: i32 = lua.load(temp_dir.path().join("module.lua")).eval()?;
2323
assert_eq!(i, 321);
2424

2525
match lua.load(&*temp_dir.path().join("module2.lua")).exec() {
2626
Err(err) if err.downcast_ref::<io::Error>().unwrap().kind() == io::ErrorKind::NotFound => {}
2727
res => panic!("expected io::Error, got {:?}", res),
2828
};
2929

30+
// &Path
31+
assert_eq!(
32+
(lua.load(&*temp_dir.path().join("module.lua").as_path())).eval::<i32>()?,
33+
321
34+
);
35+
36+
Ok(())
37+
}
38+
39+
#[test]
40+
fn test_chunk_impls() -> Result<()> {
41+
let lua = Lua::new();
42+
43+
// StdString
44+
assert_eq!(lua.load(String::from("1")).eval::<i32>()?, 1);
45+
assert_eq!(lua.load(&*String::from("2")).eval::<i32>()?, 2);
46+
47+
// &[u8]
48+
assert_eq!(lua.load(&b"3"[..]).eval::<i32>()?, 3);
49+
50+
// Vec<u8>
51+
assert_eq!(lua.load(b"4".to_vec()).eval::<i32>()?, 4);
52+
assert_eq!(lua.load(&b"5".to_vec()).eval::<i32>()?, 5);
53+
3054
Ok(())
3155
}
3256

@@ -68,3 +92,32 @@ fn test_chunk_macro() -> Result<()> {
6892

6993
Ok(())
7094
}
95+
96+
#[cfg(feature = "luau")]
97+
#[test]
98+
fn test_compiler() -> Result<()> {
99+
use std::vec;
100+
101+
let compiler = mlua::Compiler::new()
102+
.set_optimization_level(2)
103+
.set_debug_level(2)
104+
.set_type_info_level(1)
105+
.set_coverage_level(2)
106+
.set_vector_lib("vector")
107+
.set_vector_ctor("new")
108+
.set_vector_type("vector")
109+
.set_mutable_globals(vec!["mutable_global".into()])
110+
.set_userdata_types(vec!["MyUserdata".into()]);
111+
112+
assert!(compiler.compile("return vector.new(1, 2, 3)").is_ok());
113+
114+
// Error
115+
match compiler.compile("%") {
116+
Err(mlua::Error::SyntaxError { ref message, .. }) => {
117+
assert!(message.contains("Expected identifier when parsing expression, got '%'"),);
118+
}
119+
res => panic!("expected result: {res:?}"),
120+
}
121+
122+
Ok(())
123+
}

0 commit comments

Comments
 (0)