Skip to content

Commit

Permalink
Update chunk tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khvzak committed Oct 16, 2024
1 parent 179c54f commit 5479546
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/luau/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fn lua_loader(lua: &Lua, modname: StdString) -> Result<Value> {
match fs::read(&file_path) {
Ok(buf) => {
return lua
.load(&buf)
.load(buf)
.set_name(format!("={}", file_path.display()))
.set_mode(ChunkMode::Text)
.into_function()
Expand Down
55 changes: 54 additions & 1 deletion tests/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,38 @@ fn test_chunk_path() -> Result<()> {
return 321
"#,
)?;
let i: i32 = lua.load(&*temp_dir.path().join("module.lua")).eval()?;
let i: i32 = lua.load(temp_dir.path().join("module.lua")).eval()?;
assert_eq!(i, 321);

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

// &Path
assert_eq!(
(lua.load(&*temp_dir.path().join("module.lua").as_path())).eval::<i32>()?,
321
);

Ok(())
}

#[test]
fn test_chunk_impls() -> Result<()> {
let lua = Lua::new();

// StdString
assert_eq!(lua.load(String::from("1")).eval::<i32>()?, 1);
assert_eq!(lua.load(&*String::from("2")).eval::<i32>()?, 2);

// &[u8]
assert_eq!(lua.load(&b"3"[..]).eval::<i32>()?, 3);

// Vec<u8>
assert_eq!(lua.load(b"4".to_vec()).eval::<i32>()?, 4);
assert_eq!(lua.load(&b"5".to_vec()).eval::<i32>()?, 5);

Ok(())
}

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

Ok(())
}

#[cfg(feature = "luau")]
#[test]
fn test_compiler() -> Result<()> {
use std::vec;

let compiler = mlua::Compiler::new()
.set_optimization_level(2)
.set_debug_level(2)
.set_type_info_level(1)
.set_coverage_level(2)
.set_vector_lib("vector")
.set_vector_ctor("new")
.set_vector_type("vector")
.set_mutable_globals(vec!["mutable_global".into()])
.set_userdata_types(vec!["MyUserdata".into()]);

assert!(compiler.compile("return vector.new(1, 2, 3)").is_ok());

// Error
match compiler.compile("%") {
Err(mlua::Error::SyntaxError { ref message, .. }) => {
assert!(message.contains("Expected identifier when parsing expression, got '%'"),);
}
res => panic!("expected result: {res:?}"),
}

Ok(())
}

0 comments on commit 5479546

Please sign in to comment.