Skip to content

Commit 35b7eca

Browse files
committed
Update tests
1 parent 70c37e1 commit 35b7eca

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

testcrate/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name = "testcrate"
33
version = "0.1.0"
44
authors = ["Aleksandr Orlenko <[email protected]>"]
55
edition = "2021"
6+
publish = false
67

78
[features]
89
lua52compat = []

testcrate/src/lib.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::missing_safety_doc)]
2+
13
use std::os::raw::{c_char, c_int, c_long, c_void};
24

35
extern "C" {
@@ -9,28 +11,26 @@ extern "C" {
911
pub fn lua_pcall(state: *mut c_void, nargs: c_int, nresults: c_int, errfunc: c_int) -> c_int;
1012
}
1113

14+
pub unsafe fn lua_getglobal(state: *mut c_void, k: *const c_char) {
15+
lua_getfield(state, -10002 /* LUA_GLOBALSINDEX */, k);
16+
}
17+
18+
pub unsafe fn to_string<'a>(state: *mut c_void, index: c_int) -> &'a str {
19+
let mut len: c_long = 0;
20+
let str_ptr = lua_tolstring(state, index, &mut len);
21+
let bytes = std::slice::from_raw_parts(str_ptr as *const u8, len as usize);
22+
std::str::from_utf8(bytes).unwrap()
23+
}
24+
1225
#[cfg(test)]
1326
mod tests {
14-
use std::{ptr, slice, str};
15-
1627
use super::*;
1728

18-
pub unsafe fn lua_getglobal(state: *mut c_void, k: *const c_char) {
19-
lua_getfield(state, -10002 /* LUA_GLOBALSINDEX */, k);
20-
}
21-
22-
pub unsafe fn to_string<'a>(state: *mut c_void, index: c_int) -> &'a str {
23-
let mut len: c_long = 0;
24-
let str_ptr = lua_tolstring(state, index, &mut len);
25-
let bytes = slice::from_raw_parts(str_ptr as *const u8, len as usize);
26-
str::from_utf8(bytes).unwrap()
27-
}
28-
2929
#[test]
3030
fn test_lua() {
3131
unsafe {
3232
let state = luaL_newstate();
33-
assert!(state != ptr::null_mut());
33+
assert!(!state.is_null());
3434

3535
luaL_openlibs(state);
3636

@@ -57,7 +57,7 @@ mod tests {
5757
fn test_lua52compat() {
5858
unsafe {
5959
let state = luaL_newstate();
60-
assert!(state != ptr::null_mut());
60+
assert!(!state.is_null());
6161

6262
luaL_openlibs(state);
6363

@@ -78,13 +78,9 @@ mod tests {
7878

7979
let lua52compat = {
8080
lua_getglobal(state, "lua52compat\0".as_ptr().cast());
81-
to_string(state, -1)
81+
to_string(state, -1) == "yes"
8282
};
83-
84-
#[cfg(feature = "lua52compat")]
85-
assert_eq!(lua52compat, "yes");
86-
#[cfg(not(feature = "lua52compat"))]
87-
assert_eq!(lua52compat, "no");
83+
assert_eq!(lua52compat, cfg!(feature = "lua52compat"));
8884
}
8985
}
9086
}

0 commit comments

Comments
 (0)