use Cow for d-r-y (Static...)
This commit is contained in:
142
tests/tests.rs
142
tests/tests.rs
@@ -1,5 +1,6 @@
|
||||
use rlua::{Context, Function, Lua, Table, UserData, UserDataMethods, Value};
|
||||
use rlua_searcher::{AddSearcher, Result};
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
@@ -7,10 +8,17 @@ use std::path::PathBuf;
|
||||
|
||||
#[test]
|
||||
fn add_searcher_works() {
|
||||
let lume = read_lume_to_string();
|
||||
let name = "lume".to_string();
|
||||
// These should end up in the same hash slot.
|
||||
let name_owned = Cow::from("lume".to_string());
|
||||
let name_ref = Cow::from("lume");
|
||||
|
||||
// `lume_ref` should overwrite `lume_owned`.
|
||||
let lume_owned = Cow::from(read_lume_to_string());
|
||||
let lume_ref = Cow::from(read_lume_to_str());
|
||||
|
||||
let mut map = HashMap::new();
|
||||
map.insert(name, lume);
|
||||
map.insert(name_owned, lume_owned);
|
||||
map.insert(name_ref, lume_ref);
|
||||
|
||||
let lua = Lua::new();
|
||||
|
||||
@@ -21,31 +29,37 @@ fn add_searcher_works() {
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert_eq!("hello lume", hello);
|
||||
}
|
||||
assert_eq!("hello ref", hello);
|
||||
|
||||
// Repeat the experiment, but with an additional overwrite.
|
||||
let name_owned = Cow::from("lume".to_string());
|
||||
let name_ref = Cow::from("lume");
|
||||
let lume_owned = Cow::from(read_lume_to_string());
|
||||
let lume_ref = Cow::from(read_lume_to_str());
|
||||
|
||||
#[test]
|
||||
fn add_static_searcher_works() {
|
||||
let lume = read_lume_to_str();
|
||||
let name = "lume";
|
||||
let mut map = HashMap::new();
|
||||
map.insert(name, lume);
|
||||
map.insert(name_owned, lume_owned);
|
||||
map.insert(name_ref, lume_ref);
|
||||
|
||||
let name_owned = Cow::from("lume".to_string());
|
||||
let lume_owned = Cow::from(read_lume_to_string());
|
||||
map.insert(name_owned, lume_owned);
|
||||
|
||||
let lua = Lua::new();
|
||||
|
||||
let hello = lua
|
||||
.context::<_, Result<String>>(|lua_ctx| {
|
||||
lua_ctx.add_static_searcher(map)?;
|
||||
lua_ctx.add_searcher(map)?;
|
||||
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert_eq!("hello lume", hello);
|
||||
assert_eq!("hello owned", hello);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_path_searcher_works() {
|
||||
let name = "lume".to_string();
|
||||
let name = Cow::from("lume".to_string());
|
||||
let path = PathBuf::new()
|
||||
.join(std::env::var("CARGO_MANIFEST_DIR").unwrap())
|
||||
.join("tests")
|
||||
@@ -68,7 +82,7 @@ fn add_path_searcher_works() {
|
||||
|
||||
#[test]
|
||||
fn module_reloading_works() {
|
||||
let name = "lume".to_string();
|
||||
let name = Cow::from("lume".to_string());
|
||||
let path = PathBuf::new()
|
||||
.join(std::env::var("CARGO_MANIFEST_DIR").unwrap())
|
||||
.join("tests")
|
||||
@@ -116,7 +130,7 @@ fn module_reloading_works() {
|
||||
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)
|
||||
loaded.set(name.as_ref(), Value::Nil)
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
@@ -146,7 +160,7 @@ fn module_reloading_works() {
|
||||
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)
|
||||
loaded.set(name.as_ref(), Value::Nil)
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
@@ -161,11 +175,11 @@ fn module_reloading_works() {
|
||||
}
|
||||
|
||||
fn read_lume_to_string() -> String {
|
||||
r#"return "hello lume""#.to_string()
|
||||
r#"return "hello owned""#.to_string()
|
||||
}
|
||||
|
||||
fn read_lume_to_str() -> &'static str {
|
||||
r#"return "hello lume""#
|
||||
r#"return "hello ref""#
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -173,7 +187,7 @@ fn add_closure_searcher_works() {
|
||||
let lua = Lua::new();
|
||||
|
||||
let mut modules: HashMap<
|
||||
String,
|
||||
Cow<'static, str>,
|
||||
Box<
|
||||
dyn for<'ctx> Fn(Context<'ctx>, Table<'ctx>, &str) -> rlua::Result<Function<'ctx>>
|
||||
+ Send,
|
||||
@@ -197,7 +211,7 @@ fn add_closure_searcher_works() {
|
||||
.into_function()?)
|
||||
});
|
||||
|
||||
modules.insert("instrument".to_string(), instrument_loader);
|
||||
modules.insert(Cow::from("instrument".to_string()), instrument_loader);
|
||||
|
||||
let sound = lua
|
||||
.context::<_, Result<String>>(|lua_ctx| {
|
||||
@@ -220,58 +234,6 @@ fn add_closure_searcher_works() {
|
||||
assert_eq!(sound, "The ukulele goes twang");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_static_closure_searcher_works() {
|
||||
let lua = Lua::new();
|
||||
|
||||
let mut modules: HashMap<
|
||||
&'static str,
|
||||
Box<
|
||||
dyn for<'ctx> Fn(Context<'ctx>, Table<'ctx>, &str) -> rlua::Result<Function<'ctx>>
|
||||
+ Send,
|
||||
>,
|
||||
> = HashMap::new();
|
||||
|
||||
let instrument_loader: Box<
|
||||
dyn for<'ctx> Fn(Context<'ctx>, Table<'ctx>, &str) -> rlua::Result<Function<'ctx>> + Send,
|
||||
> = Box::new(|lua_ctx, env, name| {
|
||||
let globals = lua_ctx.globals();
|
||||
let new = lua_ctx.create_function(|_, (name, sound): (String, String)| {
|
||||
Ok(Instrument::new(name, sound))
|
||||
})?;
|
||||
let tbl = lua_ctx.create_table()?;
|
||||
tbl.set("new", new)?;
|
||||
globals.set("instrument", tbl)?;
|
||||
Ok(lua_ctx
|
||||
.load("return instrument")
|
||||
.set_name(name)?
|
||||
.set_environment(env)?
|
||||
.into_function()?)
|
||||
});
|
||||
|
||||
modules.insert("instrument", instrument_loader);
|
||||
|
||||
let sound = lua
|
||||
.context::<_, Result<String>>(|lua_ctx| {
|
||||
lua_ctx.add_static_closure_searcher(modules)?;
|
||||
|
||||
// Ensure global variable `instrument` is unset.
|
||||
let nil: String = lua_ctx.load("return type(instrument)").eval()?;
|
||||
assert_eq!(nil, "nil");
|
||||
|
||||
Ok(lua_ctx
|
||||
.load(
|
||||
r#"local instrument = require("instrument")
|
||||
local ukulele = instrument.new("ukulele", "twang")
|
||||
return ukulele:play()"#,
|
||||
)
|
||||
.eval()?)
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(sound, "The ukulele goes twang");
|
||||
}
|
||||
|
||||
struct Instrument {
|
||||
name: String,
|
||||
sound: String,
|
||||
@@ -301,11 +263,11 @@ fn add_function_searcher_works() {
|
||||
let lua = Lua::new();
|
||||
|
||||
let mut modules: HashMap<
|
||||
String,
|
||||
Cow<'static, str>,
|
||||
for<'ctx> fn(Context<'ctx>, Table<'ctx>, &str) -> rlua::Result<Function<'ctx>>,
|
||||
> = HashMap::new();
|
||||
|
||||
modules.insert("cartridge".to_string(), cartridge_loader);
|
||||
modules.insert(Cow::from("cartridge".to_string()), cartridge_loader);
|
||||
|
||||
let title = lua
|
||||
.context::<_, Result<String>>(|lua_ctx| {
|
||||
@@ -328,38 +290,6 @@ fn add_function_searcher_works() {
|
||||
assert_eq!(title, "Super Smash Brothers 64");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_static_function_searcher_works() {
|
||||
let lua = Lua::new();
|
||||
|
||||
let mut modules: HashMap<
|
||||
&'static str,
|
||||
for<'ctx> fn(Context<'ctx>, Table<'ctx>, &str) -> rlua::Result<Function<'ctx>>,
|
||||
> = HashMap::new();
|
||||
|
||||
modules.insert("cartridge", cartridge_loader);
|
||||
|
||||
let title = lua
|
||||
.context::<_, Result<String>>(|lua_ctx| {
|
||||
lua_ctx.add_static_function_searcher(modules)?;
|
||||
|
||||
// Ensure global variable `cartridge` is unset.
|
||||
let nil: String = lua_ctx.load("return type(cartridge)").eval()?;
|
||||
assert_eq!(nil, "nil");
|
||||
|
||||
Ok(lua_ctx
|
||||
.load(
|
||||
r#"local cartridge = require("cartridge")
|
||||
local smash = cartridge.pick()
|
||||
return smash:play()"#,
|
||||
)
|
||||
.eval()?)
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(title, "Super Smash Brothers 64");
|
||||
}
|
||||
|
||||
struct Cartridge {
|
||||
title: String,
|
||||
}
|
||||
|
Reference in New Issue
Block a user