diff --git a/ratatui-widgets/src/list.rs b/ratatui-widgets/src/list.rs index 1230afd3..e97628ff 100644 --- a/ratatui-widgets/src/list.rs +++ b/ratatui-widgets/src/list.rs @@ -466,7 +466,11 @@ mod tests { use alloc::{format, vec}; use pretty_assertions::assert_eq; + use ratatui_core::buffer::Buffer; + use ratatui_core::layout::Rect; use ratatui_core::style::{Color, Modifier, Stylize}; + use ratatui_core::text::{Text, ToSpan}; + use ratatui_core::widgets::StatefulWidget; use super::*; @@ -493,4 +497,132 @@ mod tests { .remove_modifier(Modifier::DIM) ); } + + #[test] + fn no_style() { + let text = Text::from("Item 1"); + let list = List::new([ListItem::new(text)]) + .highlight_symbol(">>") + .highlight_spacing(HighlightSpacing::Always); + let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 1)); + + list.render(buffer.area, &mut buffer, &mut ListState::default()); + + assert_eq!(buffer, Buffer::with_lines([" Item 1 "])); + } + + #[test] + fn styled_text() { + let text = Text::from("Item 1").bold(); + let list = List::new([ListItem::new(text)]) + .highlight_symbol(">>") + .highlight_spacing(HighlightSpacing::Always); + let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 1)); + + list.render(buffer.area, &mut buffer, &mut ListState::default()); + + assert_eq!( + buffer, + Buffer::with_lines([Line::from(vec![" ".to_span(), "Item 1 ".bold(),])]) + ); + } + + #[test] + fn styled_list_item() { + let text = Text::from("Item 1"); + // note this avoids using the `Stylize' methods as that gets then combines the style + // instead of setting it directly (which is not the same for some implementations) + let item = ListItem::new(text).style(Modifier::ITALIC); + let list = List::new([item]) + .highlight_symbol(">>") + .highlight_spacing(HighlightSpacing::Always); + let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 1)); + + list.render(buffer.area, &mut buffer, &mut ListState::default()); + + assert_eq!( + buffer, + Buffer::with_lines([Line::from_iter([" Item 1 ".italic()])]) + ); + } + + #[test] + fn styled_text_and_list_item() { + let text = Text::from("Item 1").bold(); + // note this avoids using the `Stylize' methods as that gets then combines the style + // instead of setting it directly (which is not the same for some implementations) + let item = ListItem::new(text).style(Modifier::ITALIC); + let list = List::new([item]) + .highlight_symbol(">>") + .highlight_spacing(HighlightSpacing::Always); + let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 1)); + + list.render(buffer.area, &mut buffer, &mut ListState::default()); + + assert_eq!( + buffer, + Buffer::with_lines([Line::from(vec![" ".italic(), "Item 1 ".bold().italic()])]) + ); + } + + #[test] + fn styled_highlight() { + let text = Text::from("Item 1").bold(); + // note this avoids using the `Stylize' methods as that gets then combines the style + // instead of setting it directly (which is not the same for some implementations) + let item = ListItem::new(text).style(Modifier::ITALIC); + let mut state = ListState::default().with_selected(Some(0)); + let list = List::new([item]) + .highlight_symbol(">>") + .highlight_style(Color::Red); + + let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 1)); + list.render(buffer.area, &mut buffer, &mut state); + + assert_eq!( + buffer, + Buffer::with_lines([Line::from(vec![ + ">>".italic().red(), + "Item 1 ".bold().italic().red(), + ])]) + ); + } + + #[test] + fn style_inheritance() { + let bold = Modifier::BOLD; + let italic = Modifier::ITALIC; + let items = [ + ListItem::new(Text::raw("Item 1")), // no style + ListItem::new(Text::styled("Item 2", bold)), // affects only the text + ListItem::new(Text::raw("Item 3")).style(italic), // affects the entire line + ListItem::new(Text::styled("Item 4", bold)).style(italic), // bold text, italic line + ListItem::new(Text::styled("Item 5", bold)).style(italic), // same but highlighted + ]; + let mut state = ListState::default().with_selected(Some(4)); + let list = List::new(items) + .highlight_symbol(">>") + .highlight_style(Color::Red) + .style(Style::new().on_blue()); + + let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 5)); + list.render(buffer.area, &mut buffer, &mut state); + + assert_eq!( + buffer, + Buffer::with_lines(vec![ + vec![" Item 1 ".on_blue()], + vec![" ".on_blue(), "Item 2 ".bold().on_blue()], + vec![" Item 3 ".italic().on_blue()], + vec![ + " ".italic().on_blue(), + "Item 4 ".bold().italic().on_blue(), + ], + vec![ + ">>".italic().red().on_blue(), + "Item 5 ".bold().italic().red().on_blue(), + ], + ]) + ); + } }