1
//! Tests for the overlay stack.
2

            
3
use super::*;
4
use crate::form::Form;
5
use crate::modal::{Draft, Modal};
6
use ratatui::layout::Rect;
7
use tvui::Place;
8

            
9
3
fn a_config_set_form() -> Modal {
10
3
    Modal::Form(Form::config_set())
11
3
}
12

            
13
#[test]
14
1
fn empty_stack_has_no_top() {
15
1
    let stack = OverlayStack::new();
16
1
    assert!(stack.is_empty());
17
1
    assert!(stack.top().is_none());
18
1
}
19

            
20
#[test]
21
1
fn push_then_pop_yields_last_in_first_out() {
22
1
    let mut stack = OverlayStack::new();
23
1
    stack.push(Modal::Draft(Draft::new(Vec::new())));
24
1
    stack.push(a_config_set_form());
25
1
    let popped = stack.pop().expect("expected a modal");
26
1
    assert!(matches!(popped, Modal::Form(_)));
27
1
    assert!(matches!(stack.top(), Some(Modal::Draft(_))));
28
1
    assert!(!stack.is_empty());
29
1
}
30

            
31
#[test]
32
1
fn pop_on_empty_returns_none() {
33
1
    let mut stack = OverlayStack::new();
34
1
    assert!(stack.pop().is_none());
35
1
}
36

            
37
#[test]
38
1
fn top_mut_allows_mutating_focus_field() {
39
1
    let mut stack = OverlayStack::new();
40
1
    stack.push(a_config_set_form());
41
1
    if let Some(Modal::Form(f)) = stack.top_mut() {
42
1
        f.focus = 1;
43
    }
44
1
    match stack.top() {
45
1
        Some(Modal::Form(f)) => assert_eq!(f.focus, 1),
46
        _ => panic!("expected Form modal"),
47
    }
48
1
}
49

            
50
5
fn moved(dx: i32, dy: i32, size: Option<(u16, u16)>) -> Placement {
51
5
    let auto = Rect::new(10, 5, 40, 10);
52
5
    let (width, height) = size.unwrap_or((40, 10));
53
5
    let place = Place {
54
5
        x: 10 + dx,
55
5
        y: 5 + dy,
56
5
        width,
57
5
        height,
58
5
    };
59
5
    Placement::of(place, auto)
60
5
}
61

            
62
#[test]
63
1
fn a_placement_belongs_to_its_layer() {
64
1
    let mut stack = OverlayStack::new();
65
1
    stack.push(Modal::Draft(Draft::new(Vec::new())));
66
1
    let placed = moved(3, 2, None);
67
1
    stack.set_placement(placed);
68
1
    stack.push(a_config_set_form());
69
1
    assert_eq!(stack.placement(), Placement::default());
70
1
    stack.pop();
71
1
    assert_eq!(stack.placement(), placed);
72
1
    stack.pop();
73
1
    assert_eq!(stack.placement(), Placement::default());
74
1
}
75

            
76
#[test]
77
1
fn a_dialog_with_no_placement_opens_where_it_is_centred() {
78
1
    let auto = Rect::new(10, 5, 40, 10);
79
1
    let host = Rect::new(0, 1, 80, 22);
80
1
    assert_eq!(
81
1
        Placement::default().place(auto, (16, 6), host),
82
1
        Place::from(auto)
83
    );
84
1
}
85

            
86
#[test]
87
1
fn a_placement_gives_back_the_place_it_was_made_from() {
88
1
    let auto = Rect::new(10, 5, 40, 10);
89
1
    let host = Rect::new(0, 1, 80, 22);
90
1
    let placement = moved(-20, 3, Some((30, 8)));
91
1
    let place = placement.place(auto, (16, 6), host);
92
1
    assert_eq!((place.x, place.y), (-10, 8));
93
1
    assert_eq!((place.width, place.height), (30, 8));
94
1
}
95

            
96
#[test]
97
1
fn a_placement_is_limited_to_the_host_as_a_window_is() {
98
1
    let auto = Rect::new(10, 5, 40, 10);
99
1
    let host = Rect::new(0, 1, 80, 22);
100
1
    let far = moved(500, -500, None).place(auto, (16, 6), host);
101
1
    assert_eq!((far.x, far.y), (79, 1));
102
1
    let left = moved(-500, 500, None).place(auto, (16, 6), host);
103
1
    assert_eq!((left.x, left.y), (-39, 22));
104
1
}
105

            
106
#[test]
107
1
fn a_placement_never_sizes_a_dialog_below_its_least() {
108
1
    let auto = Rect::new(10, 5, 40, 10);
109
1
    let host = Rect::new(0, 1, 80, 22);
110
1
    let small = moved(0, 0, Some((4, 2))).place(auto, (24, 6), host);
111
1
    assert_eq!((small.width, small.height), (24, 6));
112
1
}