1
//! A field and its label as Turbo Vision draws them, in a real form under both presets.
2

            
3
use ratatui::buffer::Buffer;
4
use ratatui::layout::Position;
5
use ratatui::style::Color;
6
use tvui::{Role, Theme};
7

            
8
use crate::form::Form;
9
use crate::modal::Modal;
10

            
11
const PRESETS: [&str; 2] = ["dn", "turbo"];
12
const LEFT_ARROW: &str = "\u{25c4}";
13
const RIGHT_ARROW: &str = "\u{25ba}";
14

            
15
10
fn drawn(preset: &str, form: Form) -> (Buffer, Theme) {
16
10
    let mut app = crate::test_support::app();
17
10
    let Ok(theme) = Theme::preset(preset) else {
18
        panic!("the {preset} preset parses");
19
    };
20
10
    app.theme = theme.clone();
21
10
    app.overlays.push(Modal::Form(form));
22
10
    (crate::test_support::draw_frame(&app, 100, 30).0, theme)
23
10
}
24

            
25
11
fn named(focus: usize, name: &str) -> Form {
26
11
    let mut form = Form::account_create();
27
11
    form.focus = focus;
28
11
    if let Some(editor) = form.fields[0].widget.as_text_mut() {
29
308
        name.chars().for_each(|c| editor.insert_char(c));
30
    }
31
11
    form
32
11
}
33

            
34
15
fn find(buf: &Buffer, text: &str) -> Position {
35
15
    for y in 0..buf.area.height {
36
19900
        let row: String = (0..buf.area.width).map(|x| buf[(x, y)].symbol()).collect();
37
199
        if let Some(byte) = row.find(text) {
38
15
            let x = u16::try_from(row[..byte].chars().count()).unwrap_or(0);
39
15
            return Position::new(x, y);
40
184
        }
41
    }
42
    panic!("{text:?} is not on the screen");
43
15
}
44

            
45
12
fn style_at(buf: &Buffer, at: Position) -> (Option<Color>, Option<Color>) {
46
12
    let style = buf[at].style();
47
12
    (style.fg, style.bg)
48
12
}
49

            
50
12
fn want(theme: &Theme, role: Role) -> (Option<Color>, Option<Color>) {
51
12
    let style = theme.style(role);
52
12
    (style.fg, style.bg)
53
12
}
54

            
55
#[test]
56
1
fn the_label_of_the_focused_field_is_lit_and_the_others_are_normal() {
57
1
    for preset in PRESETS {
58
2
        let (buf, theme) = drawn(preset, named(0, ""));
59
2
        let name = find(&buf, "Name:");
60
2
        let parent = find(&buf, "Parent:");
61
2
        assert_eq!(
62
2
            style_at(&buf, name),
63
2
            want(&theme, Role::LabelLit),
64
            "{preset}"
65
        );
66
2
        assert_eq!(
67
2
            style_at(&buf, parent),
68
2
            want(&theme, Role::Label),
69
            "{preset}"
70
        );
71
2
        let (buf, theme) = drawn(preset, named(1, ""));
72
2
        let (name, parent) = (find(&buf, "Name:"), find(&buf, "Parent:"));
73
2
        assert_eq!(style_at(&buf, name), want(&theme, Role::Label), "{preset}");
74
2
        assert_eq!(
75
2
            style_at(&buf, parent),
76
2
            want(&theme, Role::LabelLit),
77
            "{preset}"
78
        );
79
    }
80
1
}
81

            
82
#[test]
83
1
fn a_field_starts_its_text_one_cell_in_on_the_field_colour() {
84
1
    for preset in PRESETS {
85
2
        let (buf, theme) = drawn(preset, named(0, "Cash"));
86
2
        let text = find(&buf, "Cash");
87
2
        let margin = Position::new(text.x - 1, text.y);
88
2
        assert_eq!(buf[margin].symbol(), " ", "{preset}");
89
2
        assert_eq!(
90
2
            style_at(&buf, margin),
91
2
            want(&theme, Role::InputFocused),
92
            "{preset}"
93
        );
94
2
        assert_eq!(
95
2
            style_at(&buf, text),
96
2
            want(&theme, Role::InputFocused),
97
            "{preset}"
98
        );
99
    }
100
1
}
101

            
102
5
fn name_row(buf: &Buffer) -> String {
103
5
    let at = find(buf, "Name:");
104
5
    (0..buf.area.width)
105
500
        .map(|x| buf[(x, at.y)].symbol())
106
        .collect()
107
5
}
108

            
109
#[test]
110
1
fn a_long_name_shows_the_arrow_of_the_side_that_has_more_text() {
111
1
    let long = "x".repeat(60);
112
1
    for preset in PRESETS {
113
2
        let (buf, _) = drawn(preset, named(0, &long));
114
2
        let row = name_row(&buf);
115
2
        assert!(row.contains(LEFT_ARROW), "{preset}: {row}");
116
2
        assert!(!row.contains(RIGHT_ARROW), "{preset}: {row}");
117
2
        let mut form = named(0, &long);
118
2
        if let Some(editor) = form.fields[0].widget.as_text_mut() {
119
2
            editor.set_cursor(0);
120
        }
121
2
        let row = name_row(&drawn(preset, form).0);
122
2
        assert!(row.contains(RIGHT_ARROW), "{preset}: {row}");
123
2
        assert!(!row.contains(LEFT_ARROW), "{preset}: {row}");
124
    }
125
1
}
126

            
127
#[test]
128
1
fn erasing_a_long_name_scrolls_the_field_back_to_fill_it() {
129
1
    let mut app = crate::test_support::app();
130
1
    app.overlays.push(Modal::Form(named(0, &"x".repeat(60))));
131
1
    crate::test_support::draw_into(&mut app, 100, 30);
132
1
    let Some(Modal::Form(form)) = app.overlays.top_mut() else {
133
        panic!("the form is open");
134
    };
135
1
    if let Some(editor) = form.fields[0].widget.as_text_mut() {
136
50
        (0..50).for_each(|_| editor.delete_backward());
137
    }
138
1
    let (buf, _) = crate::test_support::draw_frame(&app, 100, 30);
139
1
    let row = name_row(&buf);
140
1
    assert!(row.contains(&format!("{} ", "x".repeat(10))), "{row}");
141
1
    assert!(!row.contains(LEFT_ARROW), "{row}");
142
1
}