From 39cd313b3b70eb2dcf4ea465079fa8a4043b8e9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Sat, 1 Nov 2025 00:07:15 +0300 Subject: [PATCH] test(layout): add visual buffer tests for Rect methods (#2124) --- ratatui-core/tests/rect.rs | 148 +++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 ratatui-core/tests/rect.rs diff --git a/ratatui-core/tests/rect.rs b/ratatui-core/tests/rect.rs new file mode 100644 index 00000000..a181879d --- /dev/null +++ b/ratatui-core/tests/rect.rs @@ -0,0 +1,148 @@ +//! Integration tests for Rect operations visualized with buffers. + +use ratatui_core::buffer::Buffer; +use ratatui_core::layout::{Margin, Offset, Rect}; +use ratatui_core::widgets::Widget; + +/// A minimal widget that fills its entire area with the given symbol. +struct Filled<'a> { + symbol: &'a str, +} + +impl Widget for Filled<'_> { + fn render(self, area: Rect, buf: &mut Buffer) { + for y in area.top()..area.bottom() { + for x in area.left()..area.right() { + if let Some(cell) = buf.cell_mut((x, y)) { + cell.set_symbol(self.symbol); + } + } + } + } +} + +#[test] +fn inner() { + let base = Rect::new(2, 2, 10, 6); + let inner = base.inner(Margin::new(2, 1)); + + let mut buf = Buffer::empty(Rect::new(0, 0, 15, 10)); + Filled { symbol: "█" }.render(base, &mut buf); + Filled { symbol: "░" }.render(inner, &mut buf); + + let expected = Buffer::with_lines([ + " ", + " ", + " ██████████ ", + " ██░░░░░░██ ", + " ██░░░░░░██ ", + " ██░░░░░░██ ", + " ██░░░░░░██ ", + " ██████████ ", + " ", + " ", + ]); + assert_eq!(buf, expected); +} + +#[test] +fn outer() { + let base = Rect::new(4, 3, 6, 4); + let outer = base.outer(Margin::new(2, 1)); + + let mut buf = Buffer::empty(Rect::new(0, 0, 15, 10)); + Filled { symbol: "░" }.render(outer, &mut buf); + Filled { symbol: "█" }.render(base, &mut buf); + + let expected = Buffer::with_lines([ + " ", + " ", + " ░░░░░░░░░░ ", + " ░░██████░░ ", + " ░░██████░░ ", + " ░░██████░░ ", + " ░░██████░░ ", + " ░░░░░░░░░░ ", + " ", + " ", + ]); + assert_eq!(buf, expected); +} + +#[test] +fn offset() { + let base = Rect::new(2, 2, 5, 3); + let moved = base.offset(Offset { x: 4, y: 2 }); + + let mut buf = Buffer::empty(Rect::new(0, 0, 15, 10)); + Filled { symbol: "░" }.render(base, &mut buf); + Filled { symbol: "█" }.render(moved, &mut buf); + + let expected = Buffer::with_lines([ + " ", + " ", + " ░░░░░ ", + " ░░░░░ ", + " ░░░░█████ ", + " █████ ", + " █████ ", + " ", + " ", + " ", + ]); + assert_eq!(buf, expected); +} + +#[test] +fn intersection() { + let a = Rect::new(2, 2, 6, 4); + let b = Rect::new(5, 3, 6, 4); + let inter = a.intersection(b); + + let mut buf = Buffer::empty(Rect::new(0, 0, 15, 10)); + Filled { symbol: "░" }.render(a, &mut buf); + Filled { symbol: "▒" }.render(b, &mut buf); + Filled { symbol: "█" }.render(inter, &mut buf); + + let expected = Buffer::with_lines([ + " ", + " ", + " ░░░░░░ ", + " ░░░███▒▒▒ ", + " ░░░███▒▒▒ ", + " ░░░███▒▒▒ ", + " ▒▒▒▒▒▒ ", + " ", + " ", + " ", + ]); + assert_eq!(buf, expected); +} + +#[test] +fn clamp() { + let area = Rect::new(2, 2, 10, 6); + let rect = Rect::new(8, 5, 8, 4); + let clamped = rect.clamp(area); + + let mut buf = Buffer::empty(Rect::new(0, 0, 20, 12)); + Filled { symbol: "█" }.render(area, &mut buf); + Filled { symbol: "▒" }.render(rect, &mut buf); + Filled { symbol: "░" }.render(clamped, &mut buf); + + let expected = Buffer::with_lines([ + " ", + " ", + " ██████████ ", + " ██████████ ", + " ██░░░░░░░░ ", + " ██░░░░░░░░▒▒▒▒ ", + " ██░░░░░░░░▒▒▒▒ ", + " ██░░░░░░░░▒▒▒▒ ", + " ▒▒▒▒▒▒▒▒ ", + " ", + " ", + " ", + ]); + assert_eq!(buf, expected); +}