1
use super::*;
2
use ratatui::style::Modifier;
3
use ratatui::widgets::Widget;
4

            
5
5
fn theme() -> Theme {
6
5
    Theme::preset("dn").expect("the dn preset parses")
7
5
}
8

            
9
3
fn drawn(block: Block, area: Rect) -> Buffer {
10
3
    let mut buf = Buffer::empty(area);
11
3
    block.render(area, &mut buf);
12
3
    buf
13
3
}
14

            
15
3
fn row(buf: &Buffer, y: u16) -> String {
16
3
    (0..buf.area.width)
17
28
        .map(|x| buf[(x, y)].symbol().to_string())
18
        .collect()
19
3
}
20

            
21
#[test]
22
1
fn a_dialog_is_double_with_its_own_title_role() {
23
1
    let theme = theme();
24
1
    let buf = drawn(
25
1
        dialog(&theme, "Help", FrameMode::Active),
26
1
        Rect::new(0, 0, 12, 3),
27
1
    );
28
1
    assert_eq!(row(&buf, 0), "╔══ Help ══╗");
29
1
    assert_eq!(buf[(0, 0)].style().fg, theme.style(Role::DialogFrame).fg);
30
1
    assert_eq!(buf[(5, 0)].style().fg, theme.style(Role::DialogTitle).fg);
31
1
    assert_eq!(buf[(1, 1)].style().bg, theme.style(Role::Dialog).bg);
32
1
}
33

            
34
#[test]
35
1
fn a_menu_box_is_single_without_a_title_in_the_menu_colours() {
36
1
    let theme = theme();
37
1
    let buf = drawn(menu_box(&theme), Rect::new(0, 0, 8, 3));
38
1
    assert_eq!(row(&buf, 0), "┌──────┐");
39
1
    assert_eq!(row(&buf, 2), "└──────┘");
40
1
    assert_eq!(buf[(0, 0)].style().fg, theme.style(Role::Menu).fg);
41
1
    assert_eq!(buf[(1, 1)].style().bg, theme.style(Role::Menu).bg);
42
1
}
43

            
44
3
fn lettered(width: u16, height: u16) -> Buffer {
45
3
    let mut buf = Buffer::empty(Rect::new(0, 0, width, height));
46
3
    for y in 0..height {
47
20
        for x in 0..width {
48
188
            buf[(x, y)].set_char('x');
49
        }
50
    }
51
3
    buf
52
3
}
53

            
54
2
fn shadowed(buf: &Buffer, theme: &Theme) -> Vec<(u16, u16)> {
55
2
    let style = theme.style(Role::Shadow);
56
2
    buf.area
57
2
        .positions()
58
108
        .filter(|position| buf[*position].style().bg == style.bg)
59
10
        .map(|position| (position.x, position.y))
60
        .collect()
61
2
}
62

            
63
#[test]
64
1
fn the_shadow_is_two_columns_right_and_one_row_below() {
65
1
    let theme = theme();
66
1
    let mut buf = lettered(10, 8);
67
1
    buf[(5, 2)].modifier = Modifier::BOLD;
68
1
    shadow(&mut buf, Rect::new(1, 1, 4, 3), &theme);
69
1
    assert_eq!(buf[(5, 2)].modifier, Modifier::empty());
70
1
    let mut expected = Vec::from([(5, 2), (6, 2), (5, 3), (6, 3), (5, 4), (6, 4)]);
71
4
    expected.extend((3..=6).map(|x| (x, 4)));
72
28
    expected.sort_by_key(|&(x, y)| (y, x));
73
1
    expected.dedup();
74
1
    let mut found = shadowed(&buf, &theme);
75
14
    found.sort_by_key(|&(x, y)| (y, x));
76
1
    assert_eq!(found, expected);
77
1
    assert_eq!(buf[(5, 2)].symbol(), "x");
78
1
    assert_eq!(buf[(5, 2)].style().fg, theme.style(Role::Shadow).fg);
79
1
}
80

            
81
#[test]
82
1
fn the_shadow_is_clipped_at_the_buffer_edge() {
83
1
    let theme = theme();
84
1
    let mut buf = lettered(7, 4);
85
1
    shadow(&mut buf, Rect::new(1, 1, 5, 3), &theme);
86
1
    assert_eq!(shadowed(&buf, &theme), [(6, 2), (6, 3)]);
87
1
}
88

            
89
#[test]
90
1
fn the_shadow_leaves_kitty_placeholder_cells() {
91
1
    let theme = theme();
92
1
    let mut buf = lettered(10, 8);
93
1
    buf[(5, 2)].set_symbol("\u{10EEEE}\u{305}");
94
1
    let before = buf[(5, 2)].clone();
95
1
    shadow(&mut buf, Rect::new(1, 1, 4, 3), &theme);
96
1
    assert_eq!(buf[(5, 2)], before);
97
1
    assert_eq!(buf[(6, 2)].style().bg, theme.style(Role::Shadow).bg);
98
1
}
99

            
100
#[test]
101
1
fn a_dragging_dialog_frame_is_single_line_in_the_icon_role() {
102
1
    let theme = Theme::preset("dn").expect("preset");
103
1
    let buf = drawn(
104
1
        dialog(&theme, "Help", FrameMode::Dragging),
105
1
        Rect::new(0, 0, 12, 3),
106
1
    );
107
1
    assert_eq!(buf[(0, 0)].symbol(), "┌");
108
1
    assert_eq!(buf[(0, 0)].style().fg, theme.style(Role::DialogIcon).fg);
109
1
    assert_eq!(buf[(5, 0)].style().fg, theme.style(Role::DialogIcon).fg);
110
1
}