2021-02-26 09:57:13 +01:00
|
|
|
|
use rlua::{Lua, Table, Value};
|
2021-02-04 02:16:07 +01:00
|
|
|
|
use rlua_searcher::{AddSearcher, Result};
|
|
|
|
|
use std::collections::HashMap;
|
2021-02-26 09:57:13 +01:00
|
|
|
|
use std::fs::File;
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
use std::path::PathBuf;
|
2021-02-04 02:16:07 +01:00
|
|
|
|
|
|
|
|
|
#[test]
|
2021-02-13 07:15:35 +01:00
|
|
|
|
fn add_searcher_works() {
|
2021-02-04 02:16:07 +01:00
|
|
|
|
let lume = read_lume_to_string();
|
|
|
|
|
let name = "lume".to_string();
|
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
map.insert(name, lume);
|
|
|
|
|
|
|
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
|
|
let hello = lua
|
|
|
|
|
.context::<_, Result<String>>(|lua_ctx| {
|
|
|
|
|
lua_ctx.add_searcher(map)?;
|
|
|
|
|
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!("hello lume", hello);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-13 07:15:35 +01:00
|
|
|
|
#[test]
|
|
|
|
|
fn add_static_searcher_works() {
|
|
|
|
|
let lume = read_lume_to_str();
|
2021-02-15 01:33:45 +01:00
|
|
|
|
let name = "lume";
|
2021-02-13 07:15:35 +01:00
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
map.insert(name, lume);
|
|
|
|
|
|
|
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
|
|
let hello = lua
|
|
|
|
|
.context::<_, Result<String>>(|lua_ctx| {
|
|
|
|
|
lua_ctx.add_static_searcher(map)?;
|
|
|
|
|
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!("hello lume", hello);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 09:57:13 +01:00
|
|
|
|
#[test]
|
|
|
|
|
fn add_path_searcher_works() {
|
|
|
|
|
let name = "lume".to_string();
|
|
|
|
|
let path = PathBuf::new()
|
|
|
|
|
.join(std::env::var("CARGO_MANIFEST_DIR").unwrap())
|
|
|
|
|
.join("tests")
|
|
|
|
|
.join("data")
|
|
|
|
|
.join("lume.lua");
|
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
map.insert(name, path);
|
|
|
|
|
|
|
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
|
|
let hello = lua
|
|
|
|
|
.context::<_, Result<String>>(|lua_ctx| {
|
|
|
|
|
lua_ctx.add_path_searcher(map)?;
|
|
|
|
|
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!("hello lume", hello);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn module_reloading_works() {
|
|
|
|
|
let name = "lume".to_string();
|
|
|
|
|
let path = PathBuf::new()
|
|
|
|
|
.join(std::env::var("CARGO_MANIFEST_DIR").unwrap())
|
|
|
|
|
.join("tests")
|
|
|
|
|
.join("data")
|
|
|
|
|
.join("lume.lua");
|
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
map.insert(name.clone(), path.clone());
|
|
|
|
|
|
|
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
|
|
// Add searcher for lume module on disk, and read from it.
|
|
|
|
|
let hello = lua
|
|
|
|
|
.context::<_, Result<String>>(|lua_ctx| {
|
|
|
|
|
lua_ctx.add_path_searcher(map)?;
|
|
|
|
|
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!("hello lume", hello);
|
|
|
|
|
|
|
|
|
|
// Twice.
|
|
|
|
|
let hello = lua
|
|
|
|
|
.context::<_, Result<String>>(|lua_ctx| {
|
|
|
|
|
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!("hello lume", hello);
|
|
|
|
|
|
|
|
|
|
// Modify lume module on disk.
|
|
|
|
|
let mut out = File::create(path.clone()).expect("Could not create Lume module on disk");
|
|
|
|
|
write!(out, "{}\n", r#"return "hello again lume""#)
|
|
|
|
|
.expect("Could not modify Lume module on disk");
|
|
|
|
|
|
|
|
|
|
// Thrice. Should still be unchanged due to caching.
|
|
|
|
|
let hello = lua
|
|
|
|
|
.context::<_, Result<String>>(|lua_ctx| {
|
|
|
|
|
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!("hello lume", hello);
|
|
|
|
|
|
|
|
|
|
// Remove lume module from Lua’s `package.loaded` cache to facilitate reload.
|
|
|
|
|
lua.context::<_, rlua::Result<()>>(|lua_ctx| {
|
|
|
|
|
let globals = lua_ctx.globals();
|
|
|
|
|
let loaded: Table = globals.get::<_, Table>("package")?.get("loaded")?;
|
|
|
|
|
loaded.set(name.clone(), Value::Nil)
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
// Re-read from lume module on disk.
|
|
|
|
|
let hello = lua
|
|
|
|
|
.context::<_, Result<String>>(|lua_ctx| {
|
|
|
|
|
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!("hello again lume", hello);
|
|
|
|
|
|
|
|
|
|
// Twice.
|
|
|
|
|
let hello = lua
|
|
|
|
|
.context::<_, Result<String>>(|lua_ctx| {
|
|
|
|
|
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!("hello again lume", hello);
|
|
|
|
|
|
|
|
|
|
// Revert changes to lume module on disk.
|
|
|
|
|
let mut out = File::create(path).expect("Could not create Lume module on disk");
|
|
|
|
|
write!(out, "{}\n", r#"return "hello lume""#).expect("Could not modify Lume module on disk");
|
|
|
|
|
|
|
|
|
|
// Clear cache again.
|
|
|
|
|
lua.context::<_, rlua::Result<()>>(|lua_ctx| {
|
|
|
|
|
let globals = lua_ctx.globals();
|
|
|
|
|
let loaded: Table = globals.get::<_, Table>("package")?.get("loaded")?;
|
|
|
|
|
loaded.set(name, Value::Nil)
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
// Ensure changes have been successfully reverted.
|
|
|
|
|
let hello = lua
|
|
|
|
|
.context::<_, Result<String>>(|lua_ctx| {
|
|
|
|
|
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!("hello lume", hello);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-04 02:16:07 +01:00
|
|
|
|
fn read_lume_to_string() -> String {
|
|
|
|
|
r#"return "hello lume""#.to_string()
|
|
|
|
|
}
|
2021-02-13 07:15:35 +01:00
|
|
|
|
|
|
|
|
|
fn read_lume_to_str() -> &'static str {
|
|
|
|
|
r#"return "hello lume""#
|
|
|
|
|
}
|