tek/output/src/lib.rs

85 lines
3.4 KiB
Rust

//#![feature(lazy_type_alias)]
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]
mod coordinate; pub use self::coordinate::*;
mod size; pub use self::size::*;
mod area; pub use self::area::*;
mod output; pub use self::output::*;
mod measure; pub use self::measure::*;
mod thunk; pub use self::thunk::*;
mod op_cond; pub use self::op_cond::*;
mod op_iter; pub use self::op_iter::*;
mod op_align; pub use self::op_align::*;
mod op_bsp; pub use self::op_bsp::*;
mod op_transform; pub use self::op_transform::*;
mod view; pub use self::view::*;
pub(crate) use std::marker::PhantomData;
pub(crate) use std::error::Error;
pub(crate) use ::tek_edn::*;
/// Standard result type.
pub type Usually<T> = Result<T, Box<dyn Error>>;
/// Standard optional result type.
pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
#[cfg(test)] #[test] fn test_stub_output () -> Usually<()> {
use crate::*;
struct TestOutput([u16;4]);
impl Output for TestOutput {
type Unit = u16;
type Size = [u16;2];
type Area = [u16;4];
fn area (&self) -> [u16;4] {
self.0
}
fn area_mut (&mut self) -> &mut [u16;4] {
&mut self.0
}
fn place (&mut self, _: [u16;4], _: &impl Render<TestOutput>) {
()
}
}
impl Content<TestOutput> for String {
fn render (&self, to: &mut TestOutput) {
to.area_mut().set_w(self.len() as u16);
}
}
Ok(())
}
#[cfg(test)] #[test] fn test_dimensions () {
use crate::*;
assert_eq!(Area::center(&[10u16, 10, 20, 20]), [20, 20]);
}
#[cfg(test)] #[test] fn test_layout () -> Usually<()> {
use ::tek_tui::{*, tek_output::*};
let area: [u16;4] = [10, 10, 20, 20];
let unit = ();
fn test (area: [u16;4], item: &impl Content<TuiOut>, expected: [u16;4]) {
assert_eq!(Content::layout(item, area), expected);
assert_eq!(Render::layout(item, area), expected);
};
test(area, &(), [20, 20, 0, 0]);
test(area, &Fill::xy(()), area);
test(area, &Fill::x(()), [10, 20, 20, 0]);
test(area, &Fill::y(()), [20, 10, 0, 20]);
test(area, &Fixed::x(4, unit), [18, 20, 4, 0]);
test(area, &Fixed::y(4, unit), [20, 18, 0, 4]);
test(area, &Fixed::xy(4, 4, unit), [18, 18, 4, 4]);
let four = ||Fixed::<TuiOut, _, _>::xy(4, 4, unit);
test(area, &Align::nw(four()), [10, 10, 4, 4]);
test(area, &Align::n(four()), [18, 10, 4, 4]);
test(area, &Align::ne(four()), [26, 10, 4, 4]);
test(area, &Align::e(four()), [26, 18, 4, 4]);
test(area, &Align::se(four()), [26, 26, 4, 4]);
test(area, &Align::s(four()), [18, 26, 4, 4]);
test(area, &Align::sw(four()), [10, 26, 4, 4]);
test(area, &Align::w(four()), [10, 18, 4, 4]);
let two_by_four = ||Fixed::<TuiOut, _, _>::xy(4, 2, unit);
test(area, &Align::nw(two_by_four()), [10, 10, 4, 2]);
test(area, &Align::n(two_by_four()), [18, 10, 4, 2]);
test(area, &Align::ne(two_by_four()), [26, 10, 4, 2]);
test(area, &Align::e(two_by_four()), [26, 19, 4, 2]);
test(area, &Align::se(two_by_four()), [26, 28, 4, 2]);
test(area, &Align::s(two_by_four()), [18, 28, 4, 2]);
test(area, &Align::sw(two_by_four()), [10, 28, 4, 2]);
test(area, &Align::w(two_by_four()), [10, 19, 4, 2]);
Ok(())
}