use ratatui::{
buffer::Buffer,
layout::{Position, Rect},
style::Color,
widgets::{Block, Borders, Widget},
};
use crate::ui::widgets::SplitGauge;
#[test]
fn test_ratio_clamped_above_one() {
let area = Rect::new(0, 0, 6, 1);
let mut buf = Buffer::empty(area);
SplitGauge::new(2.0, Color::Green, "").render(area, &mut buf);
for x in 0..6u16 {
let cell = buf.cell(Position { x, y: 0 }).unwrap();
assert_eq!(cell.bg, Color::Green, "x={x} should be filled (bg=Green)");
}
}
#[test]
fn test_ratio_clamped_below_zero() {
let area = Rect::new(0, 0, 6, 1);
let mut buf = Buffer::empty(area);
SplitGauge::new(-1.0, Color::Red, "").render(area, &mut buf);
for x in 0..6u16 {
let cell = buf.cell(Position { x, y: 0 }).unwrap();
assert_ne!(cell.bg, Color::Red, "x={x} should not be filled (bg≠Red)");
}
}
#[test]
fn test_full_fill_all_cells_inverted() {
let area = Rect::new(0, 0, 10, 1);
let mut buf = Buffer::empty(area);
SplitGauge::new(1.0, Color::Cyan, "").render(area, &mut buf);
for x in 0..10u16 {
let cell = buf.cell(Position { x, y: 0 }).unwrap();
assert_eq!(cell.bg, Color::Cyan, "x={x}: expected filled bg");
assert_eq!(cell.fg, Color::Black, "x={x}: expected filled fg");
}
}
#[test]
fn test_empty_fill_no_cells_inverted() {
let area = Rect::new(0, 0, 10, 1);
let mut buf = Buffer::empty(area);
SplitGauge::new(0.0, Color::Yellow, "").render(area, &mut buf);
for x in 0..10u16 {
let cell = buf.cell(Position { x, y: 0 }).unwrap();
assert_ne!(cell.bg, Color::Yellow, "x={x} should not be filled");
assert_eq!(cell.fg, Color::Yellow, "x={x} should use bar_color as fg");
}
}
#[test]
fn test_half_fill_correct_boundary() {
let area = Rect::new(0, 0, 10, 1);
let mut buf = Buffer::empty(area);
SplitGauge::new(0.5, Color::Magenta, "").render(area, &mut buf);
for x in 0..5u16 {
let cell = buf.cell(Position { x, y: 0 }).unwrap();
assert_eq!(cell.bg, Color::Magenta, "x={x} should be filled");
}
for x in 5..10u16 {
let cell = buf.cell(Position { x, y: 0 }).unwrap();
assert_ne!(cell.bg, Color::Magenta, "x={x} should be unfilled");
}
}
#[test]
fn test_label_is_centred() {
let label = "42%";
let area = Rect::new(0, 0, 10, 1);
let mut buf = Buffer::empty(area);
SplitGauge::new(0.0, Color::Green, label).render(area, &mut buf);
let expected_chars = ['4', '2', '%'];
for (i, &ch) in expected_chars.iter().enumerate() {
let x = 3 + i as u16;
let cell = buf.cell(Position { x, y: 0 }).unwrap();
assert_eq!(
cell.symbol(),
ch.to_string(),
"column {x} should contain '{ch}'"
);
}
}
#[test]
fn test_with_block_gauge_stays_inside_border() {
let area = Rect::new(0, 0, 12, 3);
let mut buf = Buffer::empty(area);
SplitGauge::new(1.0, Color::Blue, "")
.block(Block::default().borders(Borders::ALL))
.render(area, &mut buf);
for x in 1..=10u16 {
let cell = buf.cell(Position { x, y: 1 }).unwrap();
assert_eq!(cell.bg, Color::Blue, "inner cell ({x},1) should be filled");
}
for x in 0..12u16 {
for y in [0u16, 2u16] {
let cell = buf.cell(Position { x, y }).unwrap();
assert_ne!(
cell.bg,
Color::Blue,
"border cell ({x},{y}) must not be filled"
);
}
}
}
#[test]
fn test_zero_width_does_not_panic() {
let area = Rect::new(0, 0, 0, 1);
let mut buf = Buffer::empty(area);
SplitGauge::new(0.5, Color::Green, "50%").render(area, &mut buf);
}
#[test]
fn test_zero_height_does_not_panic() {
let area = Rect::new(0, 0, 10, 0);
let mut buf = Buffer::empty(area);
SplitGauge::new(0.5, Color::Green, "50%").render(area, &mut buf);
}