2021-02-04 02:16:07 +01:00
|
|
|
use rlua::Lua;
|
|
|
|
use rlua_searcher::{AddSearcher, Result};
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
#[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-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""#
|
|
|
|
}
|