nandi/jolt-nativepublic Fork 0
a7f62025fc9a5a5db4edb9a6ba6808dc31f7596b
Commits
Clone
git clone https://git.rickub.com/nandi/jolt-native.git
git clone ssh://git@rickub.com/nandi/jolt-native.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Paint the same tree into a terminal, for the machines with no window a7f6202 · on a7f62025fc9a5a5db4edb9a6ba6808dc31f7596b · nandi · 17d ago
tests.rs · 371 lines · 11.4 KBRust Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//! The backend end to end, with no terminal.
//!
//! Every one of these mounts real nodes through the same calls the ABI makes,
//! paints a real frame, and asserts on the lines that came out — which is the
//! whole reason painting goes through a grid. Nothing here needs a TTY, a
//! display or raw mode, so it all runs in CI.

use crate::screen::attr;
use crate::tree::Value;
use crate::ui::Ui;

fn ui() -> Ui {
    Ui::new(24, 8)
}

/// Mount `tag` under `parent` with the string props given.
fn node(ui: &mut Ui, parent: u32, tag: &str, props: &[(&str, &str)]) -> u32 {
    let id = ui.tree.new_node(tag);
    for (key, value) in props {
        ui.tree.set(id, key, Value::Str((*value).to_owned()));
    }
    ui.tree.append(parent, id);
    id
}

fn events(ui: &mut Ui) -> Vec<(u32, String, String, f64)> {
    let mut out = Vec::new();
    while ui.tree.poll() {
        let event = ui.tree.current().unwrap();
        out.push((
            event.node,
            event.name.to_owned(),
            event.text.clone(),
            event.num,
        ));
    }
    out
}

#[test]
fn a_column_paints_its_children_down_the_page() {
    let mut ui = ui();
    let root = ui.tree.root();
    node(&mut ui, root, "label", &[("label", "first")]);
    node(&mut ui, root, "label", &[("label", "second")]);
    ui.frame();
    assert_eq!(ui.screen.line(0), "first");
    assert_eq!(ui.screen.line(1), "second");
}

#[test]
fn a_row_paints_its_children_across_with_its_spacing_between_them() {
    let mut ui = ui();
    let root = ui.tree.root();
    let row = node(&mut ui, root, "hbox", &[("orientation", "horizontal")]);
    ui.tree.set(row, "spacing", Value::Num(2.0));
    node(&mut ui, row, "label", &[("label", "aa")]);
    node(&mut ui, row, "label", &[("label", "bb")]);
    ui.frame();
    assert_eq!(ui.screen.line(0), "aa  bb");
}

#[test]
fn a_label_wraps_to_the_width_it_was_given() {
    let mut ui = Ui::new(10, 4);
    let root = ui.tree.root();
    node(&mut ui, root, "label", &[("label", "one two three four")]);
    ui.frame();
    assert_eq!(ui.screen.line(0), "one two");
    assert_eq!(ui.screen.line(1), "three four");
}

#[test]
fn a_frame_draws_a_border_with_its_label_in_the_top_edge() {
    let mut ui = Ui::new(12, 4);
    let root = ui.tree.root();
    let frame = node(&mut ui, root, "frame", &[("label", "Task")]);
    node(&mut ui, frame, "label", &[("label", "hi")]);
    ui.frame();
    assert_eq!(ui.screen.line(0), "┌ Task ────┐");
    assert_eq!(ui.screen.line(1), "│hi        │");
    assert_eq!(ui.screen.line(2), "└──────────┘");
}

#[test]
fn the_first_focusable_widget_takes_focus_and_tab_walks_the_ring() {
    let mut ui = ui();
    let root = ui.tree.root();
    let first = node(&mut ui, root, "button", &[("label", "one")]);
    let second = node(&mut ui, root, "button", &[("label", "two")]);
    ui.frame();
    assert_eq!(ui.focus(), first);
    ui.key("tab");
    assert_eq!(ui.focus(), second);
    ui.key("tab");
    assert_eq!(ui.focus(), first, "the ring wraps");
    ui.key("shift+tab");
    assert_eq!(ui.focus(), second);
}

#[test]
fn autofocus_beats_paint_order() {
    let mut ui = ui();
    let root = ui.tree.root();
    node(&mut ui, root, "button", &[("label", "one")]);
    let wanted = node(&mut ui, root, "entry", &[]);
    ui.tree.set(wanted, "autofocus", Value::Bool(true));
    ui.frame();
    assert_eq!(ui.focus(), wanted);
}

#[test]
fn a_focused_button_is_drawn_in_reverse_and_activates_on_enter() {
    let mut ui = ui();
    let root = ui.tree.root();
    let button = node(&mut ui, root, "button", &[("label", "go")]);
    ui.frame();
    assert_eq!(ui.screen.line(0), "[ go ]");
    assert!(ui.screen.cell(0, 0).unwrap().style.has(attr::REVERSE));
    ui.key("enter");
    assert_eq!(
        events(&mut ui),
        vec![(button, "click".into(), String::new(), 0.0)]
    );
}

#[test]
fn a_checkbutton_writes_its_new_state_back_as_well_as_reporting_it() {
    let mut ui = ui();
    let root = ui.tree.root();
    let check = node(&mut ui, root, "checkbutton", &[("label", "live")]);
    ui.frame();
    assert_eq!(ui.screen.line(0), "[ ] live");
    ui.key("space");
    assert_eq!(
        events(&mut ui),
        vec![(check, "toggled".into(), String::new(), 1.0)]
    );
    // A caller that ignores the event still has a working control.
    assert_eq!(ui.tree.props(check).bool("active", false), true);
    ui.frame();
    assert_eq!(ui.screen.line(0), "[x] live");
}

#[test]
fn typing_in_an_entry_edits_its_text_and_reports_every_change() {
    let mut ui = ui();
    let root = ui.tree.root();
    let entry = node(&mut ui, root, "entry", &[("placeholder", "name")]);
    ui.frame();
    assert_eq!(
        ui.screen.line(0),
        "name",
        "the placeholder shows until it is typed in"
    );
    for key in ["h", "i", "space", "there"] {
        for name in [key] {
            if name.chars().count() > 1 && name != "space" {
                for ch in name.chars() {
                    ui.key(&ch.to_string());
                }
            } else {
                ui.key(name);
            }
        }
    }
    assert_eq!(ui.tree.props(entry).str("text"), "hi there");
    let changes = events(&mut ui);
    assert_eq!(changes.len(), 8);
    assert_eq!(changes.last().unwrap().2, "hi there");
    ui.frame();
    assert_eq!(ui.screen.line(0), "hi there");
}

#[test]
fn readline_keys_edit_where_the_caret_is() {
    let mut ui = ui();
    let root = ui.tree.root();
    let entry = node(&mut ui, root, "entry", &[("text", "one two three")]);
    ui.frame();
    ui.key("ctrl+w");
    assert_eq!(ui.tree.props(entry).str("text"), "one two ");
    ui.key("ctrl+a");
    ui.key("delete");
    assert_eq!(ui.tree.props(entry).str("text"), "ne two ");
    ui.key("ctrl+e");
    ui.key("backspace");
    assert_eq!(ui.tree.props(entry).str("text"), "ne two");
    ui.key("ctrl+u");
    assert_eq!(ui.tree.props(entry).str("text"), "");
}

#[test]
fn enter_in_an_entry_activates_rather_than_typing() {
    let mut ui = ui();
    let root = ui.tree.root();
    let entry = node(&mut ui, root, "entry", &[("text", "search")]);
    ui.frame();
    ui.key("enter");
    assert_eq!(
        events(&mut ui),
        vec![(entry, "activate".into(), "search".into(), 0.0)]
    );
    assert_eq!(ui.tree.props(entry).str("text"), "search");
}

#[test]
fn a_listbox_moves_its_cursor_with_the_arrows_and_with_j_and_k() {
    let mut ui = ui();
    let root = ui.tree.root();
    let list = node(&mut ui, root, "listbox", &[]);
    for name in ["alpha", "beta", "gamma"] {
        node(&mut ui, list, "label", &[("label", name)]);
    }
    ui.frame();
    assert_eq!(ui.screen.line(0), "› alpha");
    ui.key("j");
    assert_eq!(
        events(&mut ui),
        vec![(list, "select".into(), "beta".into(), 1.0)]
    );
    ui.key("G");
    assert_eq!(ui.tree.props(list).num("selected", -1.0), 2.0);
    ui.key("enter");
    assert_eq!(
        events(&mut ui),
        vec![
            (list, "select".into(), "gamma".into(), 2.0),
            (list, "activate".into(), "gamma".into(), 2.0)
        ]
    );
    ui.frame();
    assert_eq!(ui.screen.line(2), "› gamma");
}

#[test]
fn a_key_nothing_wanted_reaches_the_caller_as_an_event() {
    let mut ui = ui();
    let root = ui.tree.root();
    let button = node(&mut ui, root, "button", &[("label", "go")]);
    ui.frame();
    assert!(!ui.key("f5"));
    assert_eq!(
        events(&mut ui),
        vec![(button, "key".into(), "f5".into(), 0.0)]
    );
}

#[test]
fn ctrl_c_asks_the_loop_to_stop() {
    let mut ui = ui();
    assert!(!ui.should_close());
    ui.key("ctrl+c");
    assert!(ui.should_close());
}

#[test]
fn an_insensitive_subtree_is_dimmed_and_out_of_the_focus_ring() {
    let mut ui = ui();
    let root = ui.tree.root();
    let column = node(&mut ui, root, "vbox", &[]);
    ui.tree.set(column, "sensitive", Value::Bool(false));
    node(&mut ui, column, "button", &[("label", "go")]);
    let live = node(&mut ui, root, "button", &[("label", "live")]);
    ui.frame();
    assert!(ui.screen.cell(0, 0).unwrap().style.has(attr::DIM));
    assert_eq!(ui.focus(), live);
}

#[test]
fn a_click_focuses_and_activates_what_is_under_it() {
    let mut ui = ui();
    let root = ui.tree.root();
    node(&mut ui, root, "button", &[("label", "one")]);
    let second = node(&mut ui, root, "button", &[("label", "two")]);
    ui.frame();
    assert!(ui.click(2, 1));
    assert_eq!(ui.focus(), second);
    assert_eq!(
        events(&mut ui),
        vec![(second, "click".into(), String::new(), 0.0)]
    );
    assert!(!ui.click(20, 7), "a click on nothing is not an event");
}

#[test]
fn a_scroll_shows_a_window_of_its_content_and_will_not_go_past_the_end() {
    let mut ui = Ui::new(10, 3);
    let root = ui.tree.root();
    let scroll = node(&mut ui, root, "scroll", &[]);
    for i in 0..6 {
        node(&mut ui, scroll, "label", &[("label", &format!("row {i}"))]);
    }
    ui.frame();
    assert_eq!(ui.screen.lines(), vec!["row 0", "row 1", "row 2"]);
    ui.wheel(1, 1, 2);
    ui.frame();
    assert_eq!(ui.screen.lines(), vec!["row 2", "row 3", "row 4"]);
    ui.wheel(1, 1, 40);
    ui.frame();
    assert_eq!(ui.screen.lines(), vec!["row 3", "row 4", "row 5"]);
    // The clamped viewport is written back, so the next scroll up starts from
    // where the reader actually is.
    assert_eq!(ui.tree.props(scroll).num("offset", -1.0), 3.0);
}

#[test]
fn an_overlay_floats_in_the_middle_over_whatever_was_under_it() {
    let mut ui = Ui::new(14, 5);
    let root = ui.tree.root();
    node(&mut ui, root, "label", &[("label", "beneath")]);
    let overlay = node(&mut ui, root, "overlay", &[("label", "Sure?")]);
    node(&mut ui, overlay, "label", &[("label", "yes")]);
    ui.frame();
    assert_eq!(ui.screen.line(1), "  ┌ Sure? ┐");
    assert_eq!(ui.screen.line(2), "  │yes    │");
    ui.key("esc");
    assert_eq!(
        events(&mut ui),
        vec![(overlay, "close".into(), String::new(), 0.0)]
    );
}

#[test]
fn a_progress_bar_fills_the_share_of_its_width_it_was_given() {
    let mut ui = Ui::new(10, 2);
    let root = ui.tree.root();
    let bar = node(&mut ui, root, "progress", &[]);
    ui.tree.set(bar, "value", Value::Num(0.5));
    ui.frame();
    assert_eq!(ui.screen.line(0), "█████░░░░░");
}

#[test]
fn colours_and_attributes_are_inherited_by_a_subtree() {
    let mut ui = ui();
    let root = ui.tree.root();
    let column = node(&mut ui, root, "vbox", &[("color", "red")]);
    ui.tree.set(column, "bold", Value::Bool(true));
    node(&mut ui, column, "label", &[("label", "hi")]);
    ui.frame();
    let cell = ui.screen.cell(0, 0).unwrap();
    assert_eq!(cell.style.fg, crate::screen::Color::Indexed(1));
    assert!(cell.style.has(attr::BOLD));
}

#[test]
fn an_unknown_tag_still_shows_its_children() {
    let mut ui = ui();
    let root = ui.tree.root();
    let odd = node(&mut ui, root, "sparkline", &[]);
    node(&mut ui, odd, "label", &[("label", "inside")]);
    ui.frame();
    assert_eq!(ui.screen.line(0), "inside");
}

#[test]
fn focus_survives_a_repaint_and_lands_somewhere_when_its_widget_is_unmounted() {
    let mut ui = ui();
    let root = ui.tree.root();
    let first = node(&mut ui, root, "button", &[("label", "one")]);
    let second = node(&mut ui, root, "button", &[("label", "two")]);
    ui.frame();
    ui.key("tab");
    assert_eq!(ui.focus(), second);
    ui.frame();
    assert_eq!(ui.focus(), second, "a repaint does not move the focus");
    ui.tree.remove(root, second);
    ui.frame();
    assert_eq!(ui.focus(), first);
}