use rich_rust::segment::Segment;
use std::fmt::Debug;
pub mod layout_tests;
pub mod live_tests;
pub mod logging_tests;
pub mod panel_tests;
pub mod progress_tests;
pub mod rule_tests;
pub mod table_tests;
pub mod text_tests;
pub mod tree_tests;
pub trait TestCase: Debug {
fn name(&self) -> &str;
fn render(&self) -> Vec<Segment<'static>>;
fn render_plain(&self) -> String {
self.render()
.into_iter()
.map(|s| s.text.into_owned())
.collect()
}
fn python_rich_code(&self) -> Option<String> {
None
}
}
pub fn strip_ansi(s: &str) -> String {
let ansi_regex = regex::Regex::new(r"\x1b\[[0-9;]*m").unwrap();
ansi_regex.replace_all(s, "").to_string()
}
#[allow(dead_code)]
pub fn normalize_output(s: &str) -> String {
s.lines()
.map(|line| line.trim_end())
.collect::<Vec<_>>()
.join("\n")
.trim()
.to_string()
}
#[allow(dead_code)]
pub fn outputs_match(actual: &str, expected: &str) -> bool {
normalize_output(actual) == normalize_output(expected)
}
pub fn run_test<T: TestCase + ?Sized>(test: &T) -> String {
let output = test.render_plain();
let plain = strip_ansi(&output);
assert!(
!plain.is_empty() || test.name().contains("empty"),
"Test '{}' produced empty output",
test.name()
);
plain
}
#[macro_export]
macro_rules! define_test_case {
($name:ident { $($field:ident : $type:ty),* $(,)? }) => {
#[derive(Debug)]
pub struct $name {
pub name: &'static str,
$(pub $field: $type,)*
}
};
}