add CatSearcher

This commit is contained in:
atweiden
2022-06-27 13:38:18 +10:00
parent c252048e2f
commit f05e0966cb
4 changed files with 99 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
use io_cat::CatMap;
use rlua::{Context, Function, Lua, Table, UserData, UserDataMethods, Value};
use rlua_searcher::{AddSearcher, Result};
use std::borrow::Cow;
@@ -330,3 +331,35 @@ fn cartridge_loader<'ctx>(
.set_environment(env)?
.into_function()?)
}
#[test]
fn add_cat_searcher_works() {
let name = Cow::from("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: CatMap<Cow<'static, str>> = CatMap::new();
map.insert(name, Box::new(path));
map.insert(Cow::from("loon"), Box::new(r#"return "hello loon""#));
let lua = Lua::new();
let hello = lua
.context::<_, Result<String>>(|lua_ctx| {
lua_ctx.add_cat_searcher(map)?;
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
})
.unwrap();
assert_eq!("hello lume", hello);
let hello = lua
.context::<_, Result<String>>(|lua_ctx| {
Ok(lua_ctx.load(r#"return require("loon")"#).eval()?)
})
.unwrap();
assert_eq!("hello loon", hello);
}