tek/crates/tek_core/src/test.rs

190 lines
6.3 KiB
Rust

use crate::*;
struct TestEngine([u16;4], Vec<Vec<char>>);
impl Engine for TestEngine {
type Unit = u16;
type Size = [Self::Unit;2];
type Area = [Self::Unit;4];
type Input = Self;
type Handled = bool;
fn exited (&self) -> bool {
true
}
fn area (&self) -> Self::Area {
self.0
}
fn area_mut (&mut self) -> &mut Self::Area {
&mut self.0
}
}
#[derive(Copy, Clone)]
struct TestArea(u16, u16);
impl Widget for TestArea {
type Engine = TestEngine;
fn layout (&self, to: [u16;2]) -> Perhaps<[u16;2]> {
Ok(Some([to[0], to[1], self.0, self.1]))
}
fn render (&self, to: &mut Self::Engine) -> Perhaps<[u16;4]> {
if let Some(layout) = self.layout(to.area())? {
for y in layout.y()..layout.y()+layout.h()-1 {
for x in layout.x()..layout.x()+layout.w()-1 {
to.1[y as usize][x as usize] = '*';
}
}
Ok(Some(layout))
} else {
Ok(None)
}
}
}
#[test]
fn test_plus_minus () -> Usually<()> {
let area = [0, 0, 10, 10];
let engine = TestEngine(area, vec![vec![' ';10];10]);
let test = TestArea(4, 4);
assert_eq!(test.layout(area)?, Some([0, 0, 4, 4]));
assert_eq!(Push::X(1, test).layout(area)?, Some([1, 0, 4, 4]));
Ok(())
}
#[test]
fn test_outset_align () -> Usually<()> {
let area = [0, 0, 10, 10];
let engine = TestEngine(area, vec![vec![' ';10];10]);
let test = TestArea(4, 4);
assert_eq!(test.layout(area)?, Some([0, 0, 4, 4]));
assert_eq!(Outset::X(1, test).layout(area)?, Some([0, 0, 6, 4]));
assert_eq!(Align::X(test).layout(area)?, Some([3, 0, 4, 4]));
assert_eq!(Align::X(Outset::X(1, test)).layout(area)?, Some([2, 0, 6, 4]));
assert_eq!(Outset::X(1, Align::X(test)).layout(area)?, Some([2, 0, 6, 4]));
Ok(())
}
//#[test]
//fn test_misc () -> Usually<()> {
//let area: [u16;4] = [0, 0, 10, 10];
//let test = TestArea(4, 4);
//assert_eq!(test.layout(area)?,
//Some([0, 0, 4, 4]));
//assert_eq!(Align::Center(test).layout(area)?,
//Some([3, 3, 4, 4]));
//assert_eq!(Align::Center(Split::down(|add|{
//add(&test)?;
//add(&test)
//})).layout(area)?,
//Some([3, 1, 4, 8]));
//assert_eq!(Align::Center(Split::down(|add|{
//add(&Outset::XY(2, 2, test))?;
//add(&test)
//})).layout(area)?,
//Some([2, 0, 6, 10]));
//assert_eq!(Align::Center(Split::down(|add|{
//add(&Outset::XY(2, 2, test))?;
//add(&Inset::XY(2, 2, test))
//})).layout(area)?,
//Some([2, 1, 6, 8]));
//assert_eq!(Split::down(|add|{
//add(&Outset::XY(2, 2, test))?;
//add(&Inset::XY(2, 2, test))
//}).layout(area)?,
//Some([0, 0, 6, 8]));
//assert_eq!(Split::right(|add|{
//add(&Split::down(|add|{
//add(&Outset::XY(2, 2, test))?;
//add(&Inset::XY(2, 2, test))
//}))?;
//add(&Align::Center(TestArea(2 ,2)))
//}).layout(area)?,
//Some([0, 0, 8, 8]));
//Ok(())
//}
//#[test]
//fn test_offset () -> Usually<()> {
//let area: [u16;4] = [50, 50, 100, 100];
//let test = TestArea(3, 3);
//assert_eq!(Push::X(1, test).layout(area)?, Some([51, 50, 3, 3]));
//assert_eq!(Push::Y(1, test).layout(area)?, Some([50, 51, 3, 3]));
//assert_eq!(Push::XY(1, 1, test).layout(area)?, Some([51, 51, 3, 3]));
//Ok(())
//}
//#[test]
//fn test_outset () -> Usually<()> {
//let area: [u16;4] = [50, 50, 100, 100];
//let test = TestArea(3, 3);
//assert_eq!(Outset::X(1, test).layout(area)?, Some([49, 50, 5, 3]));
//assert_eq!(Outset::Y(1, test).layout(area)?, Some([50, 49, 3, 5]));
//assert_eq!(Outset::XY(1, 1, test).layout(area)?, Some([49, 49, 5, 5]));
//Ok(())
//}
//#[test]
//fn test_inset () -> Usually<()> {
//let area: [u16;4] = [50, 50, 100, 100];
//let test = TestArea(3, 3);
//assert_eq!(Inset::X(1, test).layout(area)?, Some([51, 50, 1, 3]));
//assert_eq!(Inset::Y(1, test).layout(area)?, Some([50, 51, 3, 1]));
//assert_eq!(Inset::XY(1, 1, test).layout(area)?, Some([51, 51, 1, 1]));
//Ok(())
//}
//#[test]
//fn test_stuff () -> Usually<()> {
//let area: [u16;4] = [0, 0, 100, 100];
//assert_eq!("1".layout(area)?,
//Some([0, 0, 1, 1]));
//assert_eq!("333".layout(area)?,
//Some([0, 0, 3, 1]));
//assert_eq!(Layers::new(|add|{add(&"1")?;add(&"333")}).layout(area)?,
//Some([0, 0, 3, 1]));
//assert_eq!(Split::down(|add|{add(&"1")?;add(&"333")}).layout(area)?,
//Some([0, 0, 3, 2]));
//assert_eq!(Split::right(|add|{add(&"1")?;add(&"333")}).layout(area)?,
//Some([0, 0, 4, 1]));
//assert_eq!(Split::down(|add|{
//add(&Split::right(|add|{add(&"1")?;add(&"333")}))?;
//add(&"55555")
//}).layout(area)?,
//Some([0, 0, 5, 2]));
//let area: [u16;4] = [1, 1, 100, 100];
//assert_eq!(Outset::X(1, Split::right(|add|{add(&"1")?;add(&"333")})).layout(area)?,
//Some([0, 1, 6, 1]));
//assert_eq!(Outset::Y(1, Split::right(|add|{add(&"1")?;add(&"333")})).layout(area)?,
//Some([1, 0, 4, 3]));
//assert_eq!(Outset::XY(1, 1, Split::right(|add|{add(&"1")?;add(&"333")})).layout(area)?,
//Some([0, 0, 6, 3]));
//assert_eq!(Split::down(|add|{
//add(&Outset::XY(1, 1, "1"))?;
//add(&Outset::XY(1, 1, "333"))
//}).layout(area)?,
//Some([1, 1, 5, 6]));
//let area: [u16;4] = [1, 1, 95, 100];
//assert_eq!(Align::Center(Split::down(|add|{
//add(&Outset::XY(1, 1, "1"))?;
//add(&Outset::XY(1, 1, "333"))
//})).layout(area)?,
//Some([46, 48, 5, 6]));
//assert_eq!(Align::Center(Split::down(|add|{
//add(&Layers::new(|add|{
////add(&Outset::XY(1, 1, Background(Color::Rgb(0,128,0))))?;
//add(&Outset::XY(1, 1, "1"))?;
//add(&Outset::XY(1, 1, "333"))?;
////add(&Background(Color::Rgb(0,128,0)))?;
//Ok(())
//}))?;
//add(&Layers::new(|add|{
////add(&Outset::XY(1, 1, Background(Color::Rgb(0,0,128))))?;
//add(&Outset::XY(1, 1, "555"))?;
//add(&Outset::XY(1, 1, "777777"))?;
////add(&Background(Color::Rgb(0,0,128)))?;
//Ok(())
//}))
//})).layout(area)?,
//Some([46, 48, 5, 6]));
//Ok(())
//}