wip: test: fixing up the tests

- should bring back the rest of the mental model
- and allow me to prune it!
This commit is contained in:
same mf who else 2026-02-20 14:25:08 +02:00
parent ce2eeaee7f
commit 2a7e981b9c
5 changed files with 93 additions and 95 deletions

View file

@ -1,23 +1,16 @@
#![feature(type_changing_struct_update, trait_alias)]
pub extern crate dizzle;
pub extern crate tengri_input;
pub extern crate tengri_output;
pub extern crate ratatui;
pub extern crate crossterm;
pub extern crate palette;
pub extern crate better_panic;
pub use ::tengri_output::PerfModel;
use std::{time::Duration, thread::{spawn, JoinHandle}, io::Write};
use unicode_width::*;
pub use ::{
dizzle,
tengri_input,
tengri_output::{self, PerfModel},
ratatui,
crossterm,
palette,
better_panic,
};
pub(crate) use ::{
dizzle::*,
tengri_input::*,
tengri_output::*,
atomic_float::AtomicF64,
dizzle::*, tengri_input::*, tengri_output::*,
std::{io::{stdout, Stdout}, sync::{Arc, RwLock, atomic::{AtomicBool, Ordering::*}}},
better_panic::{Settings, Verbosity},
palette::{*, convert::*, okhsl::*},
@ -38,8 +31,7 @@ pub(crate) use ::{
#[macro_export] macro_rules! tui_main {
($expr:expr) => {
fn main () -> Usually<()> {
let state = Arc::new(RwLock::new($expr));
tengri_tui::Tui::new().unwrap().run(&state)?;
tengri_tui::Tui::run(true, $expr)?;
Ok(())
}
};
@ -80,12 +72,14 @@ macro_rules! border {
)+}
}
mod tui_structs; pub use self::tui_structs::*;
mod tui_traits; pub use self::tui_traits::*;
mod tui_impls; pub use self::tui_impls::*;
mod tui_structs; pub use self::tui_structs::*;
mod tui_traits; pub use self::tui_traits::*;
mod tui_impls;
#[cfg(test)] mod tui_test;
/// Run an app in the main loop.
pub fn tui_run <T: Send + Sync + Draw<TuiOut> + Handle<TuiIn> + 'static> (
join: bool,
state: &Arc<RwLock<T>>
) -> Usually<Arc<RwLock<Tui>>> {
let backend = CrosstermBackend::new(stdout());
@ -100,15 +94,17 @@ pub fn tui_run <T: Send + Sync + Draw<TuiOut> + Handle<TuiIn> + 'static> (
let _input_thread = tui_input(tui.clone(), state, Duration::from_millis(100));
tui.write().unwrap().setup()?;
let render_thread = tui_output(tui.clone(), state, Duration::from_millis(10))?;
match render_thread.join() {
Ok(result) => {
tui.write().unwrap().teardown()?;
println!("\n\rRan successfully: {result:?}\n\r");
},
Err(error) => {
tui.write().unwrap().teardown()?;
panic!("\n\rDraw thread failed: error={error:?}.\n\r")
},
if join {
match render_thread.join() {
Ok(result) => {
tui.write().unwrap().teardown()?;
println!("\n\rRan successfully: {result:?}\n\r");
},
Err(error) => {
tui.write().unwrap().teardown()?;
panic!("\n\rDraw thread failed: error={error:?}.\n\r")
},
}
}
Ok(tui)
}
@ -492,41 +488,3 @@ pub(crate) fn width_chars_max (max: u16, text: impl AsRef<str>) -> u16 {
}
return width
}
#[cfg(test)] mod tui_test {
use crate::*;
#[test] fn test_tui_engine () -> Usually<()> {
//use std::sync::{Arc, RwLock};
struct TestComponent(String);
impl HasContent<TuiOut> for TestComponent {
fn content (&self) -> impl Content<TuiOut> {
Some(self.0.as_str())
}
}
impl Handle<TuiIn> for TestComponent {
fn handle (&mut self, _from: &TuiIn) -> Perhaps<bool> {
Ok(None)
}
}
let engine = Tui::new()?;
engine.read().unwrap().exited.store(true, std::sync::atomic::Ordering::Relaxed);
let state = TestComponent("hello world".into());
let _state = std::sync::Arc::new(std::sync::RwLock::new(state));
//engine.run(&state)?;
Ok(())
}
//#[test] fn test_parse_key () {
////use KeyModifiers as Mods;
//let _test = |x: &str, y|assert_eq!(KeyMatcher::new(x).build(), Some(Event::Key(y)));
////test(":x",
////KeyEvent::new(KeyCode::Char('x'), Mods::NONE));
////test(":ctrl-x",
////KeyEvent::new(KeyCode::Char('x'), Mods::CONTROL));
////test(":alt-x",
////KeyEvent::new(KeyCode::Char('x'), Mods::ALT));
////test(":shift-x",
////KeyEvent::new(KeyCode::Char('x'), Mods::SHIFT));
////test(":ctrl-alt-shift-x",
////KeyEvent::new(KeyCode::Char('x'), Mods::CONTROL | Mods::ALT | Mods::SHIFT ));
//}
}

View file

@ -4,10 +4,10 @@ use rand::{thread_rng, distributions::uniform::UniformSampler};
impl Tui {
/// Create and launch a terminal user interface.
pub fn run <T: Send + Sync + Handle<TuiIn> + Draw<TuiOut> + 'static> (
state: &Arc<RwLock<T>>
) -> Usually<Arc<RwLock<Self>>> {
tui_run(state)
pub fn run <T> (join: bool, state: T) -> Usually<Arc<RwLock<Self>>> where
T: Handle<TuiIn> + Draw<TuiOut> + Send + Sync + 'static
{
tui_run(join, &Arc::new(RwLock::new(state)))
}
/// True if done
pub fn exited (&self) -> bool { self.exited.fetch_and(true, Relaxed) }

View file

@ -0,0 +1,33 @@
use crate::*;
#[test] fn test_tui_engine () -> Usually<()> {
//use std::sync::{Arc, RwLock};
struct TestComponent(String);
impl Draw<TuiOut> for TestComponent {
fn draw (&self, _to: &mut TuiOut) {}
}
impl Handle<TuiIn> for TestComponent {
fn handle (&mut self, _from: &TuiIn) -> Perhaps<bool> {
Ok(None)
}
}
let engine = Tui::run(false, TestComponent("hello world".into()))?;
engine.read().unwrap().exited.store(true, std::sync::atomic::Ordering::Relaxed);
//engine.run(&state)?;
Ok(())
}
//#[test] fn test_parse_key () {
////use KeyModifiers as Mods;
//let _test = |x: &str, y|assert_eq!(KeyMatcher::new(x).build(), Some(Event::Key(y)));
////test(":x",
////KeyEvent::new(KeyCode::Char('x'), Mods::NONE));
////test(":ctrl-x",
////KeyEvent::new(KeyCode::Char('x'), Mods::CONTROL));
////test(":alt-x",
////KeyEvent::new(KeyCode::Char('x'), Mods::ALT));
////test(":shift-x",
////KeyEvent::new(KeyCode::Char('x'), Mods::SHIFT));
////test(":ctrl-alt-shift-x",
////KeyEvent::new(KeyCode::Char('x'), Mods::CONTROL | Mods::ALT | Mods::SHIFT ));
//}