1
use ratatui::style::Style;
2

            
3
use super::*;
4

            
5
const PRESETS: [&str; 2] = ["dn", "turbo"];
6

            
7
9
fn theme(preset: &str) -> Theme {
8
9
    Theme::preset(preset).expect("the preset parses")
9
9
}
10

            
11
const OK: Button<'static> = Button {
12
    label: "O~K~",
13
    default: false,
14
};
15

            
16
16
fn drawn(button: &Button<'_>, theme: &Theme, look: Look, width: u16) -> Buffer {
17
16
    let mut buf = Buffer::empty(Rect::new(0, 0, width, HEIGHT));
18
16
    button.render(Rect::new(0, 0, width, HEIGHT), &mut buf, theme, look);
19
16
    buf
20
16
}
21

            
22
13
fn row(buf: &Buffer, y: u16) -> String {
23
13
    (0..buf.area.width)
24
122
        .map(|x| buf[(x, y)].symbol().to_string())
25
        .collect()
26
13
}
27

            
28
94
fn style_at(
29
94
    buf: &Buffer,
30
94
    x: u16,
31
94
    y: u16,
32
94
) -> (Option<ratatui::style::Color>, Option<ratatui::style::Color>) {
33
94
    let style = buf[(x, y)].style();
34
94
    (style.fg, style.bg)
35
94
}
36

            
37
24
fn pair(style: Style) -> (Option<ratatui::style::Color>, Option<ratatui::style::Color>) {
38
24
    (style.fg, style.bg)
39
24
}
40

            
41
#[test]
42
1
fn the_width_is_ten_or_the_label_and_four() {
43
1
    assert_eq!(OK.width(), 10);
44
1
    let cancel = Button {
45
1
        label: "~C~ancel",
46
1
        ..OK
47
1
    };
48
1
    assert_eq!(cancel.width(), 10);
49
1
    let long = Button {
50
1
        label: "~S~ave and close",
51
1
        ..OK
52
1
    };
53
1
    assert_eq!(long.width(), 18);
54
1
}
55

            
56
#[test]
57
1
fn the_up_button_has_the_face_the_right_shadow_and_the_lower_shadow() {
58
1
    for preset in PRESETS {
59
2
        let theme = theme(preset);
60
2
        let buf = drawn(&OK, &theme, Look::default(), 10);
61
2
        assert_eq!(row(&buf, 0), "    OK   \u{2584}", "{preset}");
62
2
        assert_eq!(
63
2
            row(&buf, 1),
64
2
            format!("  {}", "\u{2580}".repeat(8)),
65
            "{preset}"
66
        );
67
2
        let shadow = pair(theme.style(Role::ButtonShadow));
68
2
        let face = pair(theme.style(Role::Button));
69
2
        assert_eq!(style_at(&buf, 0, 0), shadow, "{preset} blank column");
70
2
        assert_eq!(style_at(&buf, 9, 0), shadow, "{preset} right shadow");
71
2
        for x in 1..9 {
72
16
            assert_ne!(style_at(&buf, x, 0), shadow, "{preset} face {x}");
73
        }
74
2
        assert_eq!(style_at(&buf, 1, 0), face, "{preset} face");
75
2
        for x in 0..10 {
76
20
            assert_eq!(style_at(&buf, x, 1), shadow, "{preset} lower {x}");
77
        }
78
    }
79
1
}
80

            
81
#[test]
82
1
fn the_pressed_button_loses_its_shadow_and_moves_one_column_right() {
83
1
    for preset in PRESETS {
84
2
        let theme = theme(preset);
85
2
        let look = Look {
86
2
            pressed: true,
87
2
            ..Look::default()
88
2
        };
89
2
        let buf = drawn(&OK, &theme, look, 10);
90
2
        assert_eq!(row(&buf, 0), "     OK   ", "{preset}");
91
2
        assert_eq!(row(&buf, 1), " ".repeat(10), "{preset}");
92
2
        let shadow = pair(theme.style(Role::ButtonShadow));
93
2
        let face = pair(theme.style(Role::Button));
94
2
        assert_eq!(style_at(&buf, 0, 0), shadow, "{preset}");
95
2
        assert_eq!(style_at(&buf, 1, 0), shadow, "{preset}");
96
2
        assert_eq!(style_at(&buf, 2, 0), face, "{preset}");
97
2
        assert_eq!(style_at(&buf, 9, 0), face, "{preset}");
98
2
        for x in 0..10 {
99
20
            assert_eq!(style_at(&buf, x, 1), shadow, "{preset} lower {x}");
100
        }
101
    }
102
1
}
103

            
104
#[test]
105
1
fn the_label_is_centred_on_the_face() {
106
1
    let cancel = Button {
107
1
        label: "~C~ancel",
108
1
        ..OK
109
1
    };
110
1
    let buf = drawn(&cancel, &theme("dn"), Look::default(), 10);
111
1
    assert_eq!(row(&buf, 0), "  Cancel \u{2584}");
112
1
    let wide = drawn(&OK, &theme("dn"), Look::default(), 20);
113
1
    assert_eq!(
114
1
        row(&wide, 0),
115
1
        format!("{}OK{}\u{2584}", " ".repeat(9), " ".repeat(8))
116
    );
117
1
}
118

            
119
#[test]
120
1
fn the_face_takes_its_role_from_the_state_and_the_hotkey_is_lit() {
121
1
    for preset in PRESETS {
122
2
        let theme = theme(preset);
123
2
        let default = Button {
124
2
            default: true,
125
2
            ..OK
126
2
        };
127
2
        let states = [
128
2
            (OK, false, Role::Button),
129
2
            (default, false, Role::ButtonDefault),
130
2
            (default, true, Role::ButtonFocus),
131
2
            (OK, true, Role::ButtonFocus),
132
2
        ];
133
2
        for (button, focused, role) in states {
134
8
            let look = Look {
135
8
                focused,
136
8
                pressed: false,
137
8
            };
138
8
            let buf = drawn(&button, &theme, look, 10);
139
8
            let face = theme.style(role);
140
8
            assert_eq!(style_at(&buf, 1, 0), pair(face), "{preset} {role:?}");
141
8
            assert_eq!(style_at(&buf, 4, 0), pair(face), "{preset} {role:?} O");
142
8
            let hot = (theme.style(Role::ButtonHotkey).fg, face.bg);
143
8
            assert_eq!(style_at(&buf, 5, 0), hot, "{preset} {role:?} K");
144
        }
145
    }
146
1
}
147

            
148
#[test]
149
1
fn a_button_in_a_narrow_or_off_screen_area_does_not_panic() {
150
1
    let theme = theme("dn");
151
1
    let narrow = drawn(&OK, &theme, Look::default(), 4);
152
1
    assert_eq!(row(&narrow, 0), "  O\u{2584}");
153
1
    let mut small = Buffer::empty(Rect::new(0, 0, 6, 1));
154
1
    OK.render(Rect::new(0, 0, 10, 2), &mut small, &theme, Look::default());
155
1
    assert_eq!(row(&small, 0), "    OK");
156
1
    let tiny = drawn(&OK, &theme, Look::default(), 2);
157
1
    assert_eq!(row(&tiny, 0), "  ");
158
1
}