1
use super::*;
2

            
3
32
fn put(x: i32, y: i32, width: u16, height: u16) -> Place {
4
32
    Place {
5
32
        x,
6
32
        y,
7
32
        width,
8
32
        height,
9
32
    }
10
32
}
11

            
12
9
fn screen() -> Rect {
13
9
    Rect::new(0, 1, 80, 22)
14
9
}
15

            
16
#[test]
17
1
fn a_place_inside_the_area_is_all_visible() {
18
1
    let place = put(5, 3, 20, 8);
19
1
    assert_eq!(place.visible_in(screen()), Rect::new(5, 3, 20, 8));
20
1
}
21

            
22
#[test]
23
1
fn a_place_off_the_right_edge_shows_its_left_part() {
24
1
    let place = put(70, 3, 20, 8);
25
1
    assert_eq!(place.visible_in(screen()), Rect::new(70, 3, 10, 8));
26
1
}
27

            
28
#[test]
29
1
fn a_place_off_the_left_and_bottom_edges_shows_its_top_right_part() {
30
1
    let place = put(-5, 18, 20, 8);
31
1
    assert_eq!(place.visible_in(screen()), Rect::new(0, 18, 15, 5));
32
1
}
33

            
34
#[test]
35
1
fn a_place_wholly_outside_shows_nothing() {
36
1
    assert!(put(-30, 3, 20, 8).visible_in(screen()).is_empty());
37
1
    assert!(put(80, 3, 20, 8).visible_in(screen()).is_empty());
38
1
}
39

            
40
#[test]
41
1
fn a_rect_of_the_window_shows_where_the_screen_has_it() {
42
1
    let place = put(70, 3, 20, 8);
43
1
    let icon = Rect::new(15, 0, 3, 1);
44
1
    assert_eq!(
45
1
        place.shown(Rect::new(2, 0, 3, 1), screen()),
46
1
        Rect::new(72, 3, 3, 1)
47
    );
48
1
    assert!(place.shown(icon, screen()).is_empty());
49
1
    let left = put(-5, 3, 20, 8);
50
1
    assert_eq!(
51
1
        left.shown(Rect::new(2, 0, 6, 1), screen()),
52
1
        Rect::new(0, 3, 3, 1)
53
    );
54
1
}
55

            
56
#[test]
57
1
fn the_limits_keep_one_column_inside_on_each_side_and_the_top_on_the_desktop() {
58
1
    let area = screen();
59
1
    assert_eq!(put(-100, -100, 20, 8).limited(area), put(-19, 1, 20, 8));
60
1
    assert_eq!(put(100, 100, 20, 8).limited(area), put(79, 22, 20, 8));
61
1
    assert_eq!(put(3, 3, 200, 100).limited(area), put(3, 3, 80, 22));
62
1
}
63

            
64
#[test]
65
1
fn scaling_a_window_with_the_desktop_rounds_as_tv_does() {
66
1
    let old = Rect::new(0, 0, 80, 22);
67
1
    let new = Rect::new(0, 0, 40, 16);
68
1
    assert_eq!(put(10, 3, 60, 16).scaled(old, new), put(5, 2, 30, 12));
69
1
}
70

            
71
#[test]
72
1
fn a_shrink_keeps_a_window_usable_and_a_grow_scales_it_back() {
73
1
    let old = Rect::new(0, 0, 80, 24);
74
1
    let small = Rect::new(0, 0, 40, 12);
75
1
    let shrunk = put(50, 15, 30, 8).scaled(old, small);
76
1
    assert_eq!(shrunk, put(25, 8, 16, 6));
77
1
    assert_eq!(shrunk.visible_in(small), Rect::new(25, 8, 15, 4));
78
1
    assert_eq!(shrunk.scaled(small, old), put(50, 16, 32, 12));
79
1
}
80

            
81
#[test]
82
1
fn a_shrink_never_leaves_less_than_the_minimum_where_the_desktop_has_room() {
83
1
    let old = Rect::new(0, 0, 80, 24);
84
1
    let tiny = Rect::new(0, 0, 20, 8);
85
1
    let place = put(70, 20, 30, 8).scaled(old, tiny);
86
1
    assert!(place.width >= 16 && place.height >= 6, "{place:?}");
87
1
    assert!(place.visible_in(tiny).width >= 1);
88
1
}
89

            
90
#[test]
91
1
fn the_desktop_offset_does_not_scale() {
92
1
    let old = Rect::new(0, 1, 80, 22);
93
1
    let new = Rect::new(0, 1, 40, 22);
94
1
    assert_eq!(put(20, 5, 40, 10).scaled(old, new), put(10, 5, 20, 10));
95
1
}
96

            
97
5
fn numbered(width: u16) -> Buffer {
98
5
    let mut buf = Buffer::empty(Rect::new(0, 0, width, 2));
99
5
    for x in 0..width {
100
50
        let digit = char::from_digit(u32::from(x % 10), 10)
101
50
            .unwrap_or('?')
102
50
            .to_string();
103
50
        buf.set_string(x, 0, &digit, ratatui::style::Style::default());
104
50
        buf.set_string(x, 1, "-", ratatui::style::Style::default());
105
    }
106
5
    buf
107
5
}
108

            
109
7
fn row(buf: &Buffer, y: u16) -> String {
110
7
    (0..buf.area.width)
111
484
        .map(|x| buf[(x, y)].symbol().to_string())
112
        .collect()
113
7
}
114

            
115
#[test]
116
1
fn a_window_off_the_right_edge_copies_its_left_part_at_full_layout() {
117
1
    let window = numbered(10);
118
1
    let place = put(75, 0, 10, 2);
119
1
    let mut screen = Buffer::empty(Rect::new(0, 0, 80, 2));
120
1
    copy_visible(&window, place, place.visible_in(screen.area), &mut screen);
121
1
    assert_eq!(&row(&screen, 0)[75..], "01234");
122
1
    assert_eq!(&row(&screen, 0)[..75], " ".repeat(75));
123
1
}
124

            
125
#[test]
126
1
fn a_window_off_the_left_edge_copies_its_right_part() {
127
1
    let window = numbered(10);
128
1
    let place = put(-4, 0, 10, 2);
129
1
    let mut screen = Buffer::empty(Rect::new(0, 0, 80, 2));
130
1
    copy_visible(&window, place, place.visible_in(screen.area), &mut screen);
131
1
    assert_eq!(&row(&screen, 0)[..6], "456789");
132
1
    assert_eq!(&row(&screen, 1)[..6], "------");
133
1
}
134

            
135
#[test]
136
1
fn a_wide_glyph_cut_by_the_right_edge_is_blanked() {
137
1
    let mut window = numbered(10);
138
1
    window.set_string(4, 0, "\u{6f22}", ratatui::style::Style::default());
139
1
    let place = put(75, 0, 10, 2);
140
1
    let mut screen = Buffer::empty(Rect::new(0, 0, 80, 2));
141
1
    copy_visible(&window, place, place.visible_in(screen.area), &mut screen);
142
1
    assert_eq!(&row(&screen, 0)[75..], "0123 ");
143
1
    let mut whole = Buffer::empty(Rect::new(0, 0, 80, 2));
144
1
    let inside = put(70, 0, 10, 2);
145
1
    copy_visible(&window, inside, inside.visible_in(whole.area), &mut whole);
146
1
    assert_eq!(whole[(74, 0)].symbol(), "\u{6f22}");
147
1
}
148

            
149
#[test]
150
1
fn copying_ignores_cells_outside_the_screen_buffer() {
151
1
    let window = numbered(10);
152
1
    let place = put(0, 0, 10, 2);
153
1
    let mut screen = Buffer::empty(Rect::new(0, 0, 4, 1));
154
1
    copy_visible(&window, place, Rect::new(0, 0, 10, 2), &mut screen);
155
1
    assert_eq!(row(&screen, 0), "0123");
156
1
}
157

            
158
#[test]
159
1
fn a_window_over_the_second_half_of_a_wide_glyph_gets_its_first_cell_written() {
160
1
    let mut screen = Buffer::empty(Rect::new(0, 0, 10, 1));
161
1
    screen.set_string(3, 0, "\u{6f22}", ratatui::style::Style::default());
162
1
    let before = Buffer::empty(screen.area);
163
1
    let mut window = Buffer::empty(Rect::new(0, 0, 3, 1));
164
1
    window.set_string(0, 0, "A", ratatui::style::Style::default());
165
1
    let place = put(4, 0, 3, 1);
166
1
    copy_visible(&window, place, place.visible_in(screen.area), &mut screen);
167
1
    let written: Vec<_> = before
168
1
        .diff(&screen)
169
1
        .iter()
170
1
        .map(|(x, y, _)| (*x, *y))
171
        .collect();
172
1
    assert!(written.contains(&(4, 0)), "{written:?}");
173
1
    assert_eq!(screen[(3, 0)].symbol(), " ");
174
1
}
175

            
176
#[test]
177
1
fn a_wide_glyph_cut_by_the_left_edge_leaves_a_blank_and_no_half_glyph() {
178
1
    let mut window = numbered(10);
179
1
    window.set_string(4, 0, "\u{6f22}", ratatui::style::Style::default());
180
1
    let place = put(-5, 0, 10, 2);
181
1
    let mut screen = Buffer::empty(Rect::new(0, 0, 80, 2));
182
1
    copy_visible(&window, place, place.visible_in(screen.area), &mut screen);
183
1
    assert_eq!(&row(&screen, 0)[..5], " 6789");
184
80
    assert!((0..80).all(|x| screen[(x, 0)].symbol() != "\u{6f22}"));
185
1
}
186

            
187
#[test]
188
1
fn a_resize_that_changes_one_axis_keeps_the_other_exactly() {
189
1
    let old = Rect::new(0, 1, 80, 22);
190
1
    let taller = Rect::new(0, 1, 80, 30);
191
1
    let hung = put(-3, 4, 40, 10);
192
1
    let scaled = hung.scaled(old, taller);
193
1
    assert_eq!((scaled.x, scaled.width), (-3, 40));
194
1
    let wider = Rect::new(0, 1, 120, 22);
195
1
    let scaled = hung.scaled(old, wider);
196
1
    assert_eq!((scaled.y, scaled.height), (4, 10));
197
1
}
198

            
199
#[test]
200
1
fn repeated_resizes_of_a_hung_window_do_not_drift() {
201
1
    let tall = Rect::new(0, 1, 80, 30);
202
1
    let short = Rect::new(0, 1, 80, 22);
203
1
    let mut place = put(-3, 4, 40, 10);
204
1
    for _ in 0..8 {
205
8
        place = place.scaled(short, tall).scaled(tall, short);
206
    }
207
1
    assert_eq!((place.x, place.width), (-3, 40));
208
1
}
209

            
210
#[test]
211
1
fn a_round_trip_through_a_narrower_terminal_returns_the_window() {
212
1
    let wide = Rect::new(0, 1, 80, 22);
213
1
    let narrow = Rect::new(0, 1, 50, 22);
214
1
    for start in [put(10, 4, 40, 10), put(-3, 4, 40, 10)] {
215
2
        let back = start.scaled(wide, narrow).scaled(narrow, wide);
216
2
        assert_eq!(back, start);
217
    }
218
1
}