-
-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathchunk.rs
More file actions
128 lines (97 loc) · 2.93 KB
/
chunk.rs
File metadata and controls
128 lines (97 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#![allow(unused_imports)]
use std::fs;
use std::io;
use mlua::{Lua, Result};
use std::path::{Path, PathBuf};
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_chunk_path() -> Result<()> {
let lua = Lua::new();
let temp_dir = tempfile::tempdir().unwrap();
fs::write(
temp_dir.path().join("module.lua"),
r#"
return 321
"#,
)?;
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),
};
Ok(())
}
#[test]
#[cfg(not(feature = "luau"))]
fn test_compile() {
let lua = Lua::new();
let work_dir = Path::new("./tests/scripts/compile/");
let assert = || {
assert_eq!(
lua.load(fs::read(work_dir.join("a.bin")).unwrap())
.eval::<String>()
.unwrap(),
"Helloworld".to_string()
);
assert_eq!(
lua.load(fs::read(work_dir.join("b.bin")).unwrap())
.eval::<String>()
.unwrap(),
"Helloworld".to_string()
);
};
lua.compile_single(work_dir.join("a.lua"))
.compile_single(work_dir.join("b.lua"));
assert();
// remove them to make sure the code above don't influence them test below
fs::remove_file(work_dir.join("a.bin")).unwrap();
fs::remove_file(work_dir.join("b.bin")).unwrap();
lua.compile_directory(work_dir);
assert();
}
#[test]
#[cfg(not(feature = "luau"))]
fn multi_file_def() {
let lua = Lua::new();
let work_dir = Path::new("./tests/scripts/multi_file_def");
lua.compile_directory(work_dir);
lua.load(fs::read(work_dir.join("c.bin")).unwrap())
.exec()
.unwrap();
let value = lua
.load(fs::read(work_dir.join("d.bin")).unwrap())
.eval::<String>()
.unwrap();
assert_eq!(value.as_str(), "Hello world !");
}
#[test]
#[cfg(feature = "macros")]
fn test_chunk_macro() -> Result<()> {
let lua = Lua::new();
let name = "Rustacean";
let table = vec![1];
let data = lua.create_table()?;
data.raw_set("num", 1)?;
let ud = mlua::AnyUserData::wrap("hello");
let f = mlua::Function::wrap(|_lua, ()| Ok(()));
lua.globals().set("g", 123)?;
let string = String::new();
let str = string.as_str();
lua.load(mlua::chunk! {
assert($name == "Rustacean")
assert(type($table) == "table")
assert($table[1] == 1)
assert(type($data) == "table")
assert($data.num == 1)
assert(type($ud) == "userdata")
assert(type($f) == "function")
assert(type($str) == "string")
assert($str == "")
assert(g == 123)
s = 321
})
.exec()?;
assert_eq!(lua.globals().get::<_, i32>("s")?, 321);
Ok(())
}