1
use super::*;
2

            
3
9
fn theme() -> Theme {
4
9
    Theme::preset("dn").expect("the dn preset parses")
5
9
}
6

            
7
7
fn drawn(items: &[(String, String)], width: u16) -> Buffer {
8
7
    let area = Rect::new(0, 0, width, 1);
9
7
    let mut buf = Buffer::empty(area);
10
7
    StatusBar::new(&theme(), items).render(area, &mut buf);
11
7
    buf
12
7
}
13

            
14
5
fn row(buf: &Buffer) -> String {
15
5
    (0..buf.area.width)
16
79
        .map(|x| buf[(x, 0)].symbol().to_string())
17
        .collect()
18
5
}
19

            
20
8
fn items() -> Vec<(String, String)> {
21
8
    Vec::from([
22
8
        ("F1".to_string(), "Help".to_string()),
23
8
        ("M-x".to_string(), "Exit".to_string()),
24
8
    ])
25
8
}
26

            
27
#[test]
28
1
fn items_are_key_then_label_two_spaces_apart() {
29
1
    assert_eq!(row(&drawn(&items(), 24)), " F1 Help  M-x Exit      ");
30
1
}
31

            
32
#[test]
33
1
fn the_key_has_its_own_role_and_the_rest_the_status_one() {
34
1
    let theme = theme();
35
1
    let buf = drawn(&items(), 24);
36
1
    assert_eq!(buf[(1, 0)].style().fg, theme.style(Role::StatusHotkey).fg);
37
1
    assert_eq!(buf[(2, 0)].style().fg, theme.style(Role::StatusHotkey).fg);
38
1
    assert_eq!(buf[(4, 0)].style().fg, theme.style(Role::Status).fg);
39
1
    assert_eq!(buf[(23, 0)].style().bg, theme.style(Role::Status).bg);
40
1
}
41

            
42
#[test]
43
1
fn an_item_that_does_not_fit_is_left_out_whole() {
44
1
    assert_eq!(row(&drawn(&items(), 12)), " F1 Help    ");
45
1
    assert_eq!(row(&drawn(&items(), 18)), " F1 Help  M-x Exit");
46
1
}
47

            
48
#[test]
49
1
fn a_later_shorter_item_shows_when_one_before_it_does_not_fit() {
50
1
    let mut items = items();
51
1
    items.insert(1, ("C-o".to_string(), "Suggest".to_string()));
52
1
    assert_eq!(row(&drawn(&items, 20)), " F1 Help  M-x Exit  ");
53
1
}
54

            
55
#[test]
56
1
fn no_items_leave_the_filled_row() {
57
1
    let buf = drawn(&[], 5);
58
1
    assert_eq!(row(&buf), "     ");
59
1
    assert_eq!(buf[(0, 0)].style().bg, theme().style(Role::Status).bg);
60
1
}
61

            
62
3
fn areas(items: &[(String, String)], width: u16) -> Vec<(usize, Rect)> {
63
3
    StatusBar::item_areas(Rect::new(3, 2, width, 1), items)
64
3
}
65

            
66
#[test]
67
1
fn item_areas_cover_the_drawn_key_and_label() {
68
1
    let found = areas(&items(), 24);
69
1
    assert_eq!(
70
1
        found,
71
1
        Vec::from([(0, Rect::new(4, 2, 7, 1)), (1, Rect::new(13, 2, 8, 1))])
72
    );
73
1
    let buf = drawn(&items(), 24);
74
2
    let text = |rect: Rect| -> String {
75
2
        (rect.x - 3..rect.right() - 3)
76
15
            .map(|x| buf[(x, 0)].symbol().to_string())
77
            .collect()
78
2
    };
79
1
    assert_eq!(text(found[0].1), "F1 Help");
80
1
    assert_eq!(text(found[1].1), "M-x Exit");
81
1
}
82

            
83
#[test]
84
1
fn item_areas_skip_what_the_bar_leaves_out() {
85
1
    let mut items = items();
86
1
    items.insert(1, ("C-o".to_string(), "Suggest".to_string()));
87
2
    let indexes: Vec<usize> = areas(&items, 20).iter().map(|(index, _)| *index).collect();
88
1
    assert_eq!(indexes, Vec::from([0, 2]));
89
1
    assert!(areas(&[], 5).is_empty());
90
1
}