rust: remove integration tests

- they really are unit tests.
- move version test to its right place.
- BONUS: add rstring![] macro.
pull/1259/head
joseLuís 4 years ago
parent c9fb385ebe
commit 05d5c36622

@ -4,9 +4,7 @@
#[allow(unused_imports)] #[allow(unused_imports)]
// enjoy briefer doc comments // enjoy briefer doc comments
use crate::{ use crate::{notcurses_render, NcDirect, NcError, NcResult, Notcurses, NCRESULT_ERR, NCRESULT_OK};
notcurses_render, NcError, NcResult, Notcurses, NCRESULT_ERR, NCRESULT_OK, NcDirect,
};
// Sleep, Render & Flush Macros ------------------------------------------------ // Sleep, Render & Flush Macros ------------------------------------------------
@ -79,6 +77,16 @@ macro_rules! cstring {
}; };
} }
/// Converts a `*const c_char` into an `&str`.
#[macro_export]
macro_rules! rstring {
($s:expr) => {
unsafe { std::ffi::CStr::from_ptr($s).to_str().unwrap() }
// possible alternative
// unsafe { std::ffi::CStr::from_ptr($s).to_string_lossy() }
};
}
/// Wrapper around [libc::printf]. /// Wrapper around [libc::printf].
#[macro_export] #[macro_export]
macro_rules! printf { macro_rules! printf {

@ -1,13 +1,12 @@
//! `Notcurses*` methods and associated functions. //! `Notcurses*` methods and associated functions.
use core::ptr::{null, null_mut}; use core::ptr::{null, null_mut};
use std::ffi::CStr;
use crate::{ use crate::{
cstring, error, notcurses_init, NcAlign, NcBlitter, NcChannelPair, NcDimension, NcEgc, NcError, cstring, error, notcurses_init, rstring, NcAlign, NcBlitter, NcChannelPair, NcDimension, NcEgc,
NcFile, NcInput, NcLogLevel, NcPlane, NcResult, NcScale, NcSignalSet, NcStats, NcStyleMask, NcError, NcFile, NcInput, NcLogLevel, NcPlane, NcResult, NcScale, NcSignalSet, NcStats,
NcTime, Notcurses, NotcursesOptions, NCOPTION_NO_ALTERNATE_SCREEN, NCOPTION_SUPPRESS_BANNERS, NcStyleMask, NcTime, Notcurses, NotcursesOptions, NCOPTION_NO_ALTERNATE_SCREEN,
NCRESULT_ERR, NCOPTION_SUPPRESS_BANNERS, NCRESULT_ERR,
}; };
/// # `NotcursesOptions` Constructors /// # `NotcursesOptions` Constructors
@ -554,22 +553,14 @@ impl Notcurses {
/// ///
/// *C style function: [notcurses_str_blitter()][crate::notcurses_str_blitter].* /// *C style function: [notcurses_str_blitter()][crate::notcurses_str_blitter].*
pub fn str_blitter(blitter: NcBlitter) -> String { pub fn str_blitter(blitter: NcBlitter) -> String {
unsafe { rstring![crate::notcurses_str_blitter(blitter)].to_string()
CStr::from_ptr(crate::notcurses_str_blitter(blitter))
.to_string_lossy()
.into_owned()
}
} }
/// Gets the name of an [NcScale] scaling mode. /// Gets the name of an [NcScale] scaling mode.
/// ///
/// *C style function: [notcurses_str_scalemode()][crate::notcurses_str_scalemode].* /// *C style function: [notcurses_str_scalemode()][crate::notcurses_str_scalemode].*
pub fn str_scalemode(scalemode: NcScale) -> String { pub fn str_scalemode(scalemode: NcScale) -> String {
unsafe { rstring![crate::notcurses_str_scalemode(scalemode)].to_string()
CStr::from_ptr(crate::notcurses_str_scalemode(scalemode))
.to_string_lossy()
.into_owned()
}
} }
/// Returns an [NcStyleMask] with the supported curses-style attributes. /// Returns an [NcStyleMask] with the supported curses-style attributes.
@ -602,11 +593,7 @@ impl Notcurses {
/// ///
/// *C style function: [notcurses_version()][crate::notcurses_version].* /// *C style function: [notcurses_version()][crate::notcurses_version].*
pub fn version() -> String { pub fn version() -> String {
unsafe { rstring![crate::notcurses_version()].to_string()
CStr::from_ptr(crate::notcurses_version())
.to_string_lossy()
.into_owned()
}
} }
/// Returns the running Notcurses version components /// Returns the running Notcurses version components

@ -44,7 +44,7 @@
//W notcurses_supported_styles //W notcurses_supported_styles
//W notcurses_top //W notcurses_top
//~ notcurses_ucs32_to_utf8 (not needed in rust) //~ notcurses_ucs32_to_utf8 (not needed in rust)
//W notcurses_version //W# notcurses_version
//W notcurses_version_components //W notcurses_version_components
// //
// functions manually reimplemented: 6 // functions manually reimplemented: 6

@ -161,3 +161,12 @@ fn notcurses_debug() {
assert_eq![&string1[0..string2.len()], &string2[..]]; assert_eq![&string1[0..string2.len()], &string2[..]];
} }
} }
#[test]
#[serial]
// TODO test version_components
fn notcurses_version() {
let c_str = unsafe { crate::notcurses_version() };
assert!(!c_str.is_null());
print!("v{} ", crate::rstring![c_str]);
}

@ -1,10 +1,9 @@
//! `NcPlane*` methods and associated functions. //! `NcPlane*` methods and associated functions.
use core::ptr::{null, null_mut}; use core::ptr::{null, null_mut};
use std::ffi::CStr;
use crate::{ use crate::{
cstring, NcAlign, NcAlphaBits, NcBoxMask, NcCell, NcChannel, NcChannelPair, NcColor, cstring, rstring, NcAlign, NcAlphaBits, NcBoxMask, NcCell, NcChannel, NcChannelPair, NcColor,
NcDimension, NcEgc, NcFadeCb, NcIntResult, NcOffset, NcPaletteIndex, NcPlane, NcPlaneOptions, NcDimension, NcEgc, NcFadeCb, NcIntResult, NcOffset, NcPaletteIndex, NcPlane, NcPlaneOptions,
NcResizeCb, NcRgb, NcStyleMask, NcTime, Notcurses, NCRESULT_OK, NcResizeCb, NcRgb, NcStyleMask, NcTime, Notcurses, NCRESULT_OK,
}; };
@ -607,17 +606,14 @@ impl NcPlane {
if through_x { if through_x {
len_x = -1; len_x = -1;
} }
unsafe { rstring![crate::ncplane_contents(
CStr::from_ptr(crate::ncplane_contents( self,
self, beg_y as i32,
beg_y as i32, beg_x as i32,
beg_x as i32, len_y,
len_y, len_x
len_x, )]
)) .to_string()
.to_string_lossy()
.into_owned()
}
} }
/// Erases every NcCell in this NcPlane, resetting all attributes to normal, /// Erases every NcCell in this NcPlane, resetting all attributes to normal,

@ -1,48 +0,0 @@
use core::ptr::{null, null_mut};
use std::ffi::{CStr, CString};
use serial_test::serial; // serialize tests w/ sys::notcurses_init()
use libnotcurses_sys as sys;
#[test]
#[serial]
fn get_notcurses_version() {
let c_str = unsafe {
let s = sys::notcurses_version();
assert!(!s.is_null());
CStr::from_ptr(s)
};
let r_str = c_str.to_str().unwrap();
print!("rust-bound notcurses v{} ", r_str);
}
#[test]
#[serial]
fn create_notcurses_context() {
unsafe {
let _ = libc::setlocale(libc::LC_ALL, CString::new("").unwrap().as_ptr());
let opts = sys::NotcursesOptions {
loglevel: 0,
termtype: null(),
renderfp: null_mut(),
margin_t: 0,
margin_r: 0,
margin_b: 0,
margin_l: 0,
flags: (sys::NCOPTION_NO_ALTERNATE_SCREEN | sys::NCOPTION_INHIBIT_SETLOCALE | sys::NCOPTION_SUPPRESS_BANNERS),
};
let nc = sys::notcurses_init(&opts, null_mut());
sys::notcurses_stop(nc);
}
}
#[test]
#[serial]
fn create_direct_context() {
unsafe {
let _ = libc::setlocale(libc::LC_ALL, CString::new("").unwrap().as_ptr());
let nc = sys::ncdirect_init(null_mut(), null_mut(), 0);
sys::ncdirect_stop(nc);
}
}
Loading…
Cancel
Save