Lines
100 %
Functions
Branches
use super::*;
fn put(x: i32, y: i32, width: u16, height: u16) -> Place {
Place {
x,
y,
width,
height,
}
fn screen() -> Rect {
Rect::new(0, 1, 80, 22)
#[test]
fn a_place_inside_the_area_is_all_visible() {
let place = put(5, 3, 20, 8);
assert_eq!(place.visible_in(screen()), Rect::new(5, 3, 20, 8));
fn a_place_off_the_right_edge_shows_its_left_part() {
let place = put(70, 3, 20, 8);
assert_eq!(place.visible_in(screen()), Rect::new(70, 3, 10, 8));
fn a_place_off_the_left_and_bottom_edges_shows_its_top_right_part() {
let place = put(-5, 18, 20, 8);
assert_eq!(place.visible_in(screen()), Rect::new(0, 18, 15, 5));
fn a_place_wholly_outside_shows_nothing() {
assert!(put(-30, 3, 20, 8).visible_in(screen()).is_empty());
assert!(put(80, 3, 20, 8).visible_in(screen()).is_empty());
fn a_rect_of_the_window_shows_where_the_screen_has_it() {
let icon = Rect::new(15, 0, 3, 1);
assert_eq!(
place.shown(Rect::new(2, 0, 3, 1), screen()),
Rect::new(72, 3, 3, 1)
);
assert!(place.shown(icon, screen()).is_empty());
let left = put(-5, 3, 20, 8);
left.shown(Rect::new(2, 0, 6, 1), screen()),
Rect::new(0, 3, 3, 1)
fn the_limits_keep_one_column_inside_on_each_side_and_the_top_on_the_desktop() {
let area = screen();
assert_eq!(put(-100, -100, 20, 8).limited(area), put(-19, 1, 20, 8));
assert_eq!(put(100, 100, 20, 8).limited(area), put(79, 22, 20, 8));
assert_eq!(put(3, 3, 200, 100).limited(area), put(3, 3, 80, 22));
fn scaling_a_window_with_the_desktop_rounds_as_tv_does() {
let old = Rect::new(0, 0, 80, 22);
let new = Rect::new(0, 0, 40, 16);
assert_eq!(put(10, 3, 60, 16).scaled(old, new), put(5, 2, 30, 12));
fn a_shrink_keeps_a_window_usable_and_a_grow_scales_it_back() {
let old = Rect::new(0, 0, 80, 24);
let small = Rect::new(0, 0, 40, 12);
let shrunk = put(50, 15, 30, 8).scaled(old, small);
assert_eq!(shrunk, put(25, 8, 16, 6));
assert_eq!(shrunk.visible_in(small), Rect::new(25, 8, 15, 4));
assert_eq!(shrunk.scaled(small, old), put(50, 16, 32, 12));
fn a_shrink_never_leaves_less_than_the_minimum_where_the_desktop_has_room() {
let tiny = Rect::new(0, 0, 20, 8);
let place = put(70, 20, 30, 8).scaled(old, tiny);
assert!(place.width >= 16 && place.height >= 6, "{place:?}");
assert!(place.visible_in(tiny).width >= 1);
fn the_desktop_offset_does_not_scale() {
let old = Rect::new(0, 1, 80, 22);
let new = Rect::new(0, 1, 40, 22);
assert_eq!(put(20, 5, 40, 10).scaled(old, new), put(10, 5, 20, 10));
fn numbered(width: u16) -> Buffer {
let mut buf = Buffer::empty(Rect::new(0, 0, width, 2));
for x in 0..width {
let digit = char::from_digit(u32::from(x % 10), 10)
.unwrap_or('?')
.to_string();
buf.set_string(x, 0, &digit, ratatui::style::Style::default());
buf.set_string(x, 1, "-", ratatui::style::Style::default());
buf
fn row(buf: &Buffer, y: u16) -> String {
(0..buf.area.width)
.map(|x| buf[(x, y)].symbol().to_string())
.collect()
fn a_window_off_the_right_edge_copies_its_left_part_at_full_layout() {
let window = numbered(10);
let place = put(75, 0, 10, 2);
let mut screen = Buffer::empty(Rect::new(0, 0, 80, 2));
copy_visible(&window, place, place.visible_in(screen.area), &mut screen);
assert_eq!(&row(&screen, 0)[75..], "01234");
assert_eq!(&row(&screen, 0)[..75], " ".repeat(75));
fn a_window_off_the_left_edge_copies_its_right_part() {
let place = put(-4, 0, 10, 2);
assert_eq!(&row(&screen, 0)[..6], "456789");
assert_eq!(&row(&screen, 1)[..6], "------");
fn a_wide_glyph_cut_by_the_right_edge_is_blanked() {
let mut window = numbered(10);
window.set_string(4, 0, "\u{6f22}", ratatui::style::Style::default());
assert_eq!(&row(&screen, 0)[75..], "0123 ");
let mut whole = Buffer::empty(Rect::new(0, 0, 80, 2));
let inside = put(70, 0, 10, 2);
copy_visible(&window, inside, inside.visible_in(whole.area), &mut whole);
assert_eq!(whole[(74, 0)].symbol(), "\u{6f22}");
fn copying_ignores_cells_outside_the_screen_buffer() {
let place = put(0, 0, 10, 2);
let mut screen = Buffer::empty(Rect::new(0, 0, 4, 1));
copy_visible(&window, place, Rect::new(0, 0, 10, 2), &mut screen);
assert_eq!(row(&screen, 0), "0123");
fn a_window_over_the_second_half_of_a_wide_glyph_gets_its_first_cell_written() {
let mut screen = Buffer::empty(Rect::new(0, 0, 10, 1));
screen.set_string(3, 0, "\u{6f22}", ratatui::style::Style::default());
let before = Buffer::empty(screen.area);
let mut window = Buffer::empty(Rect::new(0, 0, 3, 1));
window.set_string(0, 0, "A", ratatui::style::Style::default());
let place = put(4, 0, 3, 1);
let written: Vec<_> = before
.diff(&screen)
.iter()
.map(|(x, y, _)| (*x, *y))
.collect();
assert!(written.contains(&(4, 0)), "{written:?}");
assert_eq!(screen[(3, 0)].symbol(), " ");
fn a_wide_glyph_cut_by_the_left_edge_leaves_a_blank_and_no_half_glyph() {
let place = put(-5, 0, 10, 2);
assert_eq!(&row(&screen, 0)[..5], " 6789");
assert!((0..80).all(|x| screen[(x, 0)].symbol() != "\u{6f22}"));
fn a_resize_that_changes_one_axis_keeps_the_other_exactly() {
let taller = Rect::new(0, 1, 80, 30);
let hung = put(-3, 4, 40, 10);
let scaled = hung.scaled(old, taller);
assert_eq!((scaled.x, scaled.width), (-3, 40));
let wider = Rect::new(0, 1, 120, 22);
let scaled = hung.scaled(old, wider);
assert_eq!((scaled.y, scaled.height), (4, 10));
fn repeated_resizes_of_a_hung_window_do_not_drift() {
let tall = Rect::new(0, 1, 80, 30);
let short = Rect::new(0, 1, 80, 22);
let mut place = put(-3, 4, 40, 10);
for _ in 0..8 {
place = place.scaled(short, tall).scaled(tall, short);
assert_eq!((place.x, place.width), (-3, 40));
fn a_round_trip_through_a_narrower_terminal_returns_the_window() {
let wide = Rect::new(0, 1, 80, 22);
let narrow = Rect::new(0, 1, 50, 22);
for start in [put(10, 4, 40, 10), put(-3, 4, 40, 10)] {
let back = start.scaled(wide, narrow).scaled(narrow, wide);
assert_eq!(back, start);