Improve code coverage

This commit is contained in:
Alex Orlenko 2021-06-17 00:11:58 +01:00
parent 3b94b4e86f
commit bf286751fa
6 changed files with 27 additions and 5 deletions

View File

@ -180,6 +180,7 @@ pub enum Error {
/// A specialized `Result` type used by `mlua`'s API. /// A specialized `Result` type used by `mlua`'s API.
pub type Result<T> = StdResult<T, Error>; pub type Result<T> = StdResult<T, Error>;
#[cfg(not(tarpaulin_include))]
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self { match *self {

View File

@ -93,7 +93,7 @@ struct MemoryInfo {
/// More information can be found in the Lua 5.x [documentation]. /// More information can be found in the Lua 5.x [documentation].
/// ///
/// [documentation]: https://www.lua.org/manual/5.4/manual.html#2.5 /// [documentation]: https://www.lua.org/manual/5.4/manual.html#2.5
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GCMode { pub enum GCMode {
Incremental, Incremental,
/// Requires `feature = "lua54"` /// Requires `feature = "lua54"`
@ -525,6 +525,7 @@ impl Lua {
// Executes module entrypoint function, which returns only one Value. // Executes module entrypoint function, which returns only one Value.
// The returned value then pushed to the Lua stack. // The returned value then pushed to the Lua stack.
#[doc(hidden)] #[doc(hidden)]
#[cfg(not(tarpaulin_include))]
pub fn entrypoint1<'lua, 'callback, R, F>(&'lua self, func: F) -> Result<c_int> pub fn entrypoint1<'lua, 'callback, R, F>(&'lua self, func: F) -> Result<c_int>
where where
'lua: 'callback, 'lua: 'callback,

View File

@ -1,6 +1,6 @@
use std::borrow::Cow; use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::ffi::CString; use std::ffi::{CStr, CString};
use maplit::{btreemap, btreeset, hashmap, hashset}; use maplit::{btreemap, btreeset, hashmap, hashset};
use mlua::{Lua, Result}; use mlua::{Lua, Result};
@ -72,7 +72,12 @@ fn test_conv_cstring() -> Result<()> {
let s = CString::new(b"hello".to_vec()).unwrap(); let s = CString::new(b"hello".to_vec()).unwrap();
lua.globals().set("s", s.clone())?; lua.globals().set("s", s.clone())?;
let s2: CString = lua.globals().get("s")?; let s2: CString = lua.globals().get("s")?;
assert!(s == s2); assert_eq!(s, s2);
let cs = CStr::from_bytes_with_nul(b"hello\0").unwrap();
lua.globals().set("cs", cs)?;
let cs2: CString = lua.globals().get("cs")?;
assert_eq!(cs, cs2.as_c_str());
Ok(()) Ok(())
} }

View File

@ -1,6 +1,6 @@
use std::sync::Arc; use std::sync::Arc;
use mlua::{Lua, Result, UserData}; use mlua::{GCMode, Lua, Result, UserData};
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52"))] #[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52"))]
use mlua::Error; use mlua::Error;
@ -38,6 +38,9 @@ fn test_gc_control() -> Result<()> {
let lua = Lua::new(); let lua = Lua::new();
let globals = lua.globals(); let globals = lua.globals();
#[cfg(feature = "lua54")]
assert_eq!(lua.gc_gen(0, 0), GCMode::Incremental);
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52"))] #[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52"))]
{ {
assert!(lua.gc_is_running()); assert!(lua.gc_is_running());
@ -59,6 +62,9 @@ fn test_gc_control() -> Result<()> {
lua.gc_collect()?; lua.gc_collect()?;
assert_eq!(Arc::strong_count(&rc), 1); assert_eq!(Arc::strong_count(&rc), 1);
#[cfg(feature = "lua54")]
assert_eq!(lua.gc_inc(0, 0, 0), GCMode::Generational);
Ok(()) Ok(())
} }

View File

@ -93,8 +93,8 @@ fn test_thread() -> Result<()> {
Ok(()) Ok(())
} }
#[cfg(any(feature = "lua54", all(feature = "luajit", feature = "vendored")))]
#[test] #[test]
#[cfg(any(feature = "lua54", all(feature = "luajit", feature = "vendored")))]
fn test_thread_reset() -> Result<()> { fn test_thread_reset() -> Result<()> {
use mlua::{AnyUserData, UserData}; use mlua::{AnyUserData, UserData};
use std::sync::Arc; use std::sync::Arc;

View File

@ -362,6 +362,12 @@ fn test_fields() -> Result<()> {
index.set("f", 321)?; index.set("f", 321)?;
Ok(index) Ok(index)
}); });
fields.add_meta_field_with(MetaMethod::NewIndex, |lua| {
lua.create_function(|lua, (_, field, val): (AnyUserData, String, Value)| {
lua.globals().set(field, val)?;
Ok(())
})
})
} }
} }
@ -379,6 +385,9 @@ fn test_fields() -> Result<()> {
assert(ud.uval == "hello") assert(ud.uval == "hello")
assert(ud.f == 321) assert(ud.f == 321)
ud.unknown = 789
assert(unknown == 789)
"#, "#,
) )
.exec()?; .exec()?;