1
use super::*;
2

            
3
const MINIMAL: &str = "
4
# a comment
5
menu = black on lightgray
6
menu-selected = black on cyan
7
menu-hotkey = red on lightgray
8
menu-selected-hotkey = red on cyan
9
menu-disabled = darkgray on lightgray
10
menu-selected-disabled = darkgray on cyan
11
panel = lightcyan on darkgray
12
panel-frame = lightcyan on darkgray
13
panel-frame-passive = lightgray on darkgray
14
panel-title = black on cyan
15
panel-header = yellow on darkgray bold underline
16
panel-cursor = black on cyan
17
panel-dim = lightgray on darkgray
18
frame-icon = lightgreen on darkgray
19
desktop = darkgray on black
20
scrollbar = lightcyan on darkgray
21
scrollbar-thumb = black on cyan
22
status = black on lightgray
23
status-hotkey = red on lightgray
24
dialog = black on lightgray
25
dialog-frame = black on lightgray
26
dialog-title = white on lightgray
27
dialog-icon = lightgreen on lightgray
28
dialog-focus = white on lightblue reverse
29
label = black on lightgray
30
label-lit = white on lightgray
31
button = lightcyan on magenta
32
button-default = white on brown
33
button-hotkey = lightmagenta on magenta
34
button-focus = white on lightred
35
button-shadow = black on lightgray
36
input-cursor = black on cyan
37
input = white on black
38
input-focused = white on lightblue
39
input-arrow = lightgreen on lightblue
40
history = green on lightgray
41
history-arrow = black on green
42
prompt = white on darkgray bold
43
list = black on cyan
44
list-selected = white on black
45
shadow = darkgray on black
46
";
47

            
48
2
fn without(role: &str) -> String {
49
2
    MINIMAL
50
2
        .lines()
51
86
        .filter(|line| !line.starts_with(&format!("{role} =")))
52
        .collect::<Vec<_>>()
53
2
        .join("\n")
54
2
}
55

            
56
10
fn error_of(text: &str) -> ParseError {
57
10
    Theme::parse(text).expect_err("the text must not parse")
58
10
}
59

            
60
#[test]
61
1
fn cga_names_map_to_the_exact_rgb_values() {
62
1
    let table = [
63
1
        ("black", 0x000000),
64
1
        ("blue", 0x0000AA),
65
1
        ("green", 0x00AA00),
66
1
        ("cyan", 0x00AAAA),
67
1
        ("red", 0xAA0000),
68
1
        ("magenta", 0xAA00AA),
69
1
        ("brown", 0xAA5500),
70
1
        ("lightgray", 0xAAAAAA),
71
1
        ("darkgray", 0x555555),
72
1
        ("lightblue", 0x5555FF),
73
1
        ("lightgreen", 0x55FF55),
74
1
        ("lightcyan", 0x55FFFF),
75
1
        ("lightred", 0xFF5555),
76
1
        ("lightmagenta", 0xFF55FF),
77
1
        ("yellow", 0xFFFF55),
78
1
        ("white", 0xFFFFFF),
79
1
    ];
80
1
    for (name, rgb) in table {
81
16
        let cga = Cga::from_name(name).expect(name);
82
16
        assert_eq!(cga.color(ColorDepth::Rgb), Color::from_u32(rgb), "{name}");
83
    }
84
1
    assert_eq!(Cga::from_name("grey"), None);
85
1
}
86

            
87
#[test]
88
1
fn roles_are_listed_in_declaration_order() {
89
1
    for (index, role) in Role::ALL.iter().enumerate() {
90
41
        assert_eq!(role.index(), index, "{role:?}");
91
41
        assert_eq!(Role::from_name(role.name()), Some(*role));
92
    }
93
1
}
94

            
95
#[test]
96
1
fn a_valid_file_gives_each_role_its_style() {
97
1
    let theme = Theme::parse(MINIMAL).expect("valid");
98
1
    let header = theme.style(Role::PanelHeader);
99
1
    assert_eq!(header.fg, Some(Cga::Yellow.color(ColorDepth::Rgb)));
100
1
    assert_eq!(header.bg, Some(Cga::DarkGray.color(ColorDepth::Rgb)));
101
1
    assert_eq!(header.add_modifier, Modifier::BOLD | Modifier::UNDERLINED);
102
1
    assert_eq!(
103
1
        theme.style(Role::DialogFocus).add_modifier,
104
1
        Modifier::REVERSED
105
    );
106
1
    assert_eq!(theme.style(Role::Menu).add_modifier, Modifier::empty());
107
1
    assert_eq!(theme.bg(Role::Panel), Cga::DarkGray.color(ColorDepth::Rgb));
108
1
}
109

            
110
#[test]
111
1
fn the_dn_preset_maps_every_role_to_the_dos_navigator_colours() {
112
    use Cga::{
113
        Black, Brown, Cyan, DarkGray, Green, LightBlue, LightCyan, LightGray, LightGreen,
114
        LightMagenta, LightRed, Magenta, White, Yellow,
115
    };
116
1
    let bold = Modifier::BOLD;
117
1
    let none = Modifier::empty();
118
1
    let expected = [
119
1
        (Role::Menu, Black, LightGray, none),
120
1
        (Role::MenuSelected, Black, Cyan, none),
121
1
        (Role::MenuHotkey, Magenta, LightGray, none),
122
1
        (Role::MenuSelectedHotkey, Magenta, Cyan, none),
123
1
        (Role::MenuDisabled, DarkGray, LightGray, none),
124
1
        (Role::MenuSelectedDisabled, DarkGray, Cyan, none),
125
1
        (Role::Panel, LightCyan, DarkGray, none),
126
1
        (Role::PanelFrame, LightCyan, DarkGray, none),
127
1
        (Role::PanelFramePassive, LightGray, DarkGray, none),
128
1
        (Role::PanelTitle, Black, Cyan, none),
129
1
        (Role::PanelHeader, Yellow, DarkGray, bold),
130
1
        (Role::PanelCursor, Black, Cyan, none),
131
1
        (Role::PanelDim, LightGray, DarkGray, none),
132
1
        (Role::FrameIcon, LightGreen, DarkGray, none),
133
1
        (Role::Desktop, DarkGray, Black, none),
134
1
        (Role::Scrollbar, LightCyan, DarkGray, none),
135
1
        (Role::ScrollbarThumb, Black, Cyan, none),
136
1
        (Role::Status, Black, LightGray, none),
137
1
        (Role::StatusHotkey, Magenta, LightGray, none),
138
1
        (Role::Dialog, Black, LightGray, none),
139
1
        (Role::DialogFrame, White, LightGray, none),
140
1
        (Role::DialogTitle, White, LightGray, none),
141
1
        (Role::DialogIcon, LightGreen, LightGray, none),
142
1
        (Role::DialogFocus, White, LightBlue, none),
143
1
        (Role::Label, Black, LightGray, none),
144
1
        (Role::LabelLit, White, LightGray, none),
145
1
        (Role::Button, LightCyan, Magenta, none),
146
1
        (Role::ButtonDefault, White, Brown, none),
147
1
        (Role::ButtonHotkey, LightMagenta, Magenta, none),
148
1
        (Role::ButtonFocus, White, LightRed, none),
149
1
        (Role::ButtonShadow, Black, LightGray, none),
150
1
        (Role::InputCursor, Black, Cyan, none),
151
1
        (Role::Input, White, Black, none),
152
1
        (Role::InputFocused, White, LightBlue, none),
153
1
        (Role::InputArrow, LightGreen, LightBlue, none),
154
1
        (Role::History, Green, LightGray, none),
155
1
        (Role::HistoryArrow, Black, Green, none),
156
1
        (Role::Prompt, White, DarkGray, bold),
157
1
        (Role::List, Black, Cyan, none),
158
1
        (Role::ListSelected, White, Black, none),
159
1
        (Role::Shadow, DarkGray, Black, none),
160
1
    ];
161
1
    assert_eq!(expected.len(), Role::ALL.len());
162
1
    let theme = Theme::preset("dn").expect("the preset parses");
163
1
    for (role, fg, bg, modifiers) in expected {
164
41
        let style = theme.style(role);
165
41
        assert_eq!(style.fg, Some(fg.color(ColorDepth::Rgb)), "{role:?} fg");
166
41
        assert_eq!(style.bg, Some(bg.color(ColorDepth::Rgb)), "{role:?} bg");
167
41
        assert_eq!(style.add_modifier, modifiers, "{role:?} modifiers");
168
41
        assert_eq!(theme.bg(role), bg.color(ColorDepth::Rgb), "{role:?} bg()");
169
    }
170
1
}
171

            
172
#[test]
173
1
fn errors_carry_the_line_number() {
174
1
    let err = error_of(&format!("{MINIMAL}\nmenu-selected = black on cyan"));
175
1
    assert_eq!(err.line, Some(45));
176
1
    assert!(
177
1
        err.message.contains("duplicate role `menu-selected`"),
178
        "{err}"
179
    );
180

            
181
1
    let err = error_of("# one\nmystery = black on cyan");
182
1
    assert_eq!(err.line, Some(2));
183
1
    assert!(err.message.contains("unknown role `mystery`"), "{err}");
184

            
185
1
    let err = error_of("menu = black on pink");
186
1
    assert_eq!(err.line, Some(1));
187
1
    assert!(err.message.contains("unknown colour `pink`"), "{err}");
188

            
189
1
    let err = error_of("\n\nmenu black on cyan");
190
1
    assert_eq!(err.line, Some(3));
191

            
192
1
    let err = error_of("menu = black cyan");
193
1
    assert!(err.message.contains("`on`"), "{err}");
194

            
195
1
    let err = error_of("menu = black on");
196
1
    assert_eq!(err.line, Some(1));
197

            
198
1
    let err = error_of("menu = black on cyan blink");
199
1
    assert!(err.message.contains("unknown modifier `blink`"), "{err}");
200
1
}
201

            
202
#[test]
203
1
fn a_missing_role_is_named_without_a_line() {
204
1
    let err = error_of(&without("status"));
205
1
    assert_eq!(err.line, None);
206
1
    assert!(err.message.contains("missing role `status`"), "{err}");
207
1
}
208

            
209
#[test]
210
1
fn the_error_display_names_the_line() {
211
1
    assert_eq!(
212
1
        error_of("menu = black on pink").to_string(),
213
1
        "line 1: unknown colour `pink`"
214
    );
215
1
    assert_eq!(
216
1
        error_of(&without("menu")).to_string(),
217
1
        "missing role `menu`"
218
    );
219
1
}
220

            
221
#[test]
222
1
fn detect_reads_colorterm_and_term() {
223
    use ColorDepth::{Ansi16, Indexed256, Rgb};
224
1
    let cases = [
225
1
        (Some("xterm"), Some("truecolor"), Rgb),
226
1
        (Some("xterm"), Some("24BIT"), Rgb),
227
1
        (None, Some("TrueColor"), Rgb),
228
1
        (Some("xterm-kitty"), None, Rgb),
229
1
        (Some("ghostty"), None, Rgb),
230
1
        (Some("wezterm"), None, Rgb),
231
1
        (Some("foot"), None, Rgb),
232
1
        (Some("alacritty"), None, Rgb),
233
1
        (Some("ALACRITTY"), None, Rgb),
234
1
        (Some("eat-truecolor"), None, Rgb),
235
1
        (Some("xterm-truecolor"), None, Rgb),
236
1
        (Some("xterm-direct"), None, Rgb),
237
1
        (Some("xterm-256color"), None, Indexed256),
238
1
        (Some("XTERM-256COLOR"), Some("other"), Indexed256),
239
1
        (Some("screen"), None, Ansi16),
240
1
        (Some("linux"), None, Ansi16),
241
1
        (Some("vt100"), None, Ansi16),
242
1
        (None, None, Ansi16),
243
1
    ];
244
1
    for (term, colorterm, depth) in cases {
245
18
        assert_eq!(
246
18
            ColorDepth::detect(term, colorterm),
247
18
            depth,
248
            "{term:?} {colorterm:?}"
249
        );
250
    }
251
1
}
252

            
253
3922
fn xterm_rgb(index: u8) -> (i32, i32, i32) {
254
    const LEVELS: [i32; 6] = [0, 95, 135, 175, 215, 255];
255
3922
    let n = i32::from(index);
256
3922
    if n >= 232 {
257
420
        let grey = 8 + 10 * (n - 232);
258
420
        return (grey, grey, grey);
259
3502
    }
260
3502
    let cube = n - 16;
261
3502
    (
262
3502
        LEVELS[(cube / 36) as usize],
263
3502
        LEVELS[(cube / 6 % 6) as usize],
264
3502
        LEVELS[(cube % 6) as usize],
265
3502
    )
266
3922
}
267

            
268
#[test]
269
1
fn each_cga_colour_maps_to_its_nearest_cube_or_grey_index() {
270
1
    for cga in Cga::ALL {
271
16
        let Color::Rgb(r, g, b) = cga.color(ColorDepth::Rgb) else {
272
            panic!("{cga:?} is not RGB");
273
        };
274
3840
        let distance = |index: u8| {
275
3840
            let (pr, pg, pb) = xterm_rgb(index);
276
3840
            (pr - i32::from(r)).pow(2) + (pg - i32::from(g)).pow(2) + (pb - i32::from(b)).pow(2)
277
3840
        };
278
16
        let nearest = (16..=255u8)
279
3840
            .min_by_key(|index| distance(*index))
280
16
            .expect("indices");
281
16
        assert_eq!(
282
16
            cga.color(ColorDepth::Indexed256),
283
16
            Color::Indexed(nearest),
284
            "{cga:?}"
285
        );
286
    }
287
1
}
288

            
289
#[test]
290
1
fn ansi16_follows_the_ansi_order_not_the_cga_order() {
291
1
    let expected = [
292
1
        Color::Black,
293
1
        Color::Blue,
294
1
        Color::Green,
295
1
        Color::Cyan,
296
1
        Color::Red,
297
1
        Color::Magenta,
298
1
        Color::Yellow,
299
1
        Color::Gray,
300
1
        Color::DarkGray,
301
1
        Color::LightBlue,
302
1
        Color::LightGreen,
303
1
        Color::LightCyan,
304
1
        Color::LightRed,
305
1
        Color::LightMagenta,
306
1
        Color::LightYellow,
307
1
        Color::White,
308
1
    ];
309
1
    for (cga, color) in Cga::ALL.into_iter().zip(expected) {
310
16
        assert_eq!(cga.color(ColorDepth::Ansi16), color, "{cga:?}");
311
    }
312
1
}
313

            
314
#[test]
315
1
fn ansi16_backgrounds_fold_bright_colours_to_dark() {
316
1
    for cga in Cga::ALL {
317
16
        let dark = match cga {
318
1
            Cga::DarkGray => Cga::Black,
319
1
            Cga::LightBlue => Cga::Blue,
320
1
            Cga::LightGreen => Cga::Green,
321
1
            Cga::LightCyan => Cga::Cyan,
322
1
            Cga::LightRed => Cga::Red,
323
1
            Cga::LightMagenta => Cga::Magenta,
324
1
            Cga::Yellow => Cga::Brown,
325
1
            Cga::White => Cga::LightGray,
326
8
            other => other,
327
        };
328
16
        assert_eq!(
329
16
            cga.background(ColorDepth::Ansi16),
330
16
            dark.color(ColorDepth::Ansi16),
331
            "{cga:?}"
332
        );
333
    }
334
1
    assert_eq!(
335
1
        Cga::White.background(ColorDepth::Rgb),
336
1
        Cga::White.color(ColorDepth::Rgb)
337
    );
338
1
}
339

            
340
#[test]
341
1
fn a_theme_renders_each_role_at_its_depth() {
342
1
    let theme = Theme::preset("dn").expect("the preset parses");
343
1
    let focus = Role::DialogFocus;
344
1
    let rgb = theme.style(focus);
345
1
    assert_eq!(rgb.fg, Some(Color::Rgb(255, 255, 255)));
346
1
    assert_eq!(rgb.bg, Some(Color::Rgb(0x55, 0x55, 0xFF)));
347
1
    let indexed = theme.clone().with_depth(ColorDepth::Indexed256);
348
1
    assert_eq!(indexed.style(focus).fg, Some(Color::Indexed(231)));
349
1
    assert_eq!(indexed.style(focus).bg, Some(Color::Indexed(63)));
350
1
    assert_eq!(indexed.bg(focus), Color::Indexed(63));
351
1
    let ansi = theme.with_depth(ColorDepth::Ansi16);
352
1
    assert_eq!(ansi.style(focus).fg, Some(Color::White));
353
1
    assert_eq!(ansi.style(focus).bg, Some(Color::Blue));
354
1
    assert_eq!(ansi.style(Role::Panel).bg, Some(Color::Black));
355
1
    assert_eq!(ansi.bg(Role::Panel), Color::Black);
356
1
}
357

            
358
246
fn shown_rgb(color: Color) -> [u8; 3] {
359
246
    let channel = |v: i32| u8::try_from(v).expect("a channel");
360
246
    match color {
361
82
        Color::Rgb(r, g, b) => [r, g, b],
362
        Color::Indexed(index) => {
363
82
            let (r, g, b) = xterm_rgb(index);
364
82
            [channel(r), channel(g), channel(b)]
365
        }
366
82
        named => Cga::ALL
367
82
            .into_iter()
368
369
            .find(|cga| cga.color(ColorDepth::Ansi16) == named)
369
82
            .expect("an ANSI colour")
370
            .rgb(),
371
    }
372
246
}
373

            
374
const DEPTHS: [ColorDepth; 3] = [ColorDepth::Rgb, ColorDepth::Indexed256, ColorDepth::Ansi16];
375

            
376
#[test]
377
1
fn bg_rgb_is_the_colour_the_cells_show_at_every_depth() {
378
1
    for name in ["dn", "turbo"] {
379
2
        for depth in DEPTHS {
380
6
            let theme = Theme::preset(name)
381
6
                .expect("the preset parses")
382
6
                .with_depth(depth);
383
6
            for role in Role::ALL {
384
246
                assert_eq!(
385
246
                    theme.bg_rgb(role),
386
246
                    shown_rgb(theme.bg(role)),
387
                    "{name} {depth:?} {role:?}"
388
                );
389
            }
390
        }
391
    }
392
1
}
393

            
394
#[test]
395
1
fn the_dn_panel_is_black_in_the_picture_at_ansi16() {
396
1
    let dn = Theme::preset("dn").expect("the preset parses");
397
1
    assert_eq!(dn.bg_rgb(Role::Panel), [0x55; 3]);
398
1
    assert_eq!(
399
1
        dn.with_depth(ColorDepth::Ansi16).bg_rgb(Role::Panel),
400
1
        [0, 0, 0]
401
    );
402
1
}
403

            
404
#[test]
405
1
fn preset_names_are_the_presets_that_parse() {
406
1
    let names: Vec<_> = Theme::preset_names().collect();
407
1
    assert_eq!(names, ["dn", "turbo"]);
408
1
    for name in names {
409
2
        assert!(Theme::preset(name).is_ok(), "{name}");
410
    }
411
1
}
412

            
413
#[test]
414
1
fn a_depth_name_parses_back_to_its_depth() {
415
1
    for depth in [ColorDepth::Rgb, ColorDepth::Indexed256, ColorDepth::Ansi16] {
416
3
        assert_eq!(depth.name().parse(), Ok(depth));
417
    }
418
1
}