use luau::{Error, Lua, Value};
#[test]
fn registry_values_convert_back_to_lua_values() -> Result<(), Error> {
let lua = Lua::new()?;
let key = lua.create_registry_value("hello, world")?;
let borrowed = lua.pack(&key)?;
let owned = lua.pack(key)?;
assert_eq!(borrowed.to_string()?, "hello, world");
assert_eq!(borrowed.to_pointer(), owned.to_pointer());
let nil = lua.create_registry_value(Value::Nil)?;
assert!(matches!(lua.pack(&nil)?, Value::Nil));
let other = Lua::new()?;
let foreign = other.create_registry_value("foreign")?;
assert!(matches!(
lua.pack(&foreign),
Err(Error::MismatchedRegistryKey)
));
Ok(())
}
#[test]
fn registry_keys_belong_to_their_originating_lua() -> Result<(), Error> {
let lua1 = Lua::new()?;
let lua2 = Lua::new()?;
let key1 = lua1.create_registry_value("hello")?;
let key2 = lua2.create_registry_value("hello")?;
assert!(lua1.owns_registry_value(&key1));
assert!(!lua2.owns_registry_value(&key1));
assert!(lua2.owns_registry_value(&key2));
assert!(!lua1.owns_registry_value(&key2));
assert!(matches!(
lua2.remove_registry_value(key1),
Err(Error::MismatchedRegistryKey)
));
Ok(())
}