summaryrefslogtreecommitdiffstats
path: root/src/view/tests.rs
blob: c55e81e92d03613b60c4c677d53c6c2804368637 (plain)
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
use super::*;
use crate::{
	config::Theme,
	display::{
		testutil::{assert_output, CrossTerm},
		Size,
	},
};

fn assert_render(width: usize, height: usize, view_data: &ViewData, expected: &[&str]) {
	let theme = Theme::new();
	let mut crossterm = CrossTerm::new();
	crossterm.set_size(Size::new(width, height));
	let display = Display::new(crossterm, &theme);
	let mut view = View::new(display, "~", "?");

	let mut render_slice = RenderSlice::new();
	render_slice.record_resize(width, height);
	render_slice.sync_view_data(view_data);
	view.render(&render_slice).unwrap();

	assert_output(&view.display, expected);
}

#[test]
fn render_empty() {
	assert_render(20, 10, &ViewData::new(|_| {}), &["~"; 10]);
}

#[test]
fn render_title_full_width() {
	let mut expected = vec!["Git Interactive Rebase Tool        "];
	expected.extend(vec!["~"; 9]);
	assert_render(
		35,
		10,
		&ViewData::new(|updater| updater.set_show_title(true)),
		&expected,
	);
}

#[test]
fn render_title_short_title() {
	let mut expected = vec!["Git Rebase                "];
	expected.extend(vec!["~"; 9]);
	assert_render(
		26,
		10,
		&ViewData::new(|updater| updater.set_show_title(true)),
		&expected,
	);
}

#[test]
fn render_title_full_width_with_help() {
	let mut expected = vec!["Git Interactive Rebase Tool Help: ?"];
	expected.extend(vec!["~"; 9]);
	assert_render(
		35,
		10,
		&ViewData::new(|updater| {
			updater.set_show_title(true);
			updater.set_show_help(true);
		}),
		&expected,
	);
}

#[test]
fn render_title_full_width_with_help_enabled_but_not_enough_length() {
	let mut expected = vec!["Git Interactive Rebase Tool       "];
	expected.extend(vec!["~"; 9]);
	assert_render(
		34,
		10,
		&ViewData::new(|updater| {
			updater.set_show_title(true);
			updater.set_show_help(true);
		}),
		&expected,
	);
}

#[test]
fn render_leading_lines() {
	let mut expected = vec!["This is a leading line"];
	expected.extend(vec!["~"; 9]);
	assert_render(
		30,
		10,
		&ViewData::new(|updater| {
			updater.push_leading_line(ViewLine::from("This is a leading line"));
		}),
		&expected,
	);
}

#[test]
fn render_normal_lines() {
	let mut expected = vec!["This is a line"];
	expected.extend(vec!["~"; 9]);
	assert_render(
		30,
		10,
		&ViewData::new(|updater| {
			updater.push_line(ViewLine::from("This is a line"));
		}),
		&expected,
	);
}

#[test]
fn render_tailing_lines() {
	let mut expected = vec!["~"; 9];
	expected.push("This is a trailing line");
	assert_render(
		30,
		10,
		&ViewData::new(|updater| {
			updater.push_trailing_line(ViewLine::from("This is a trailing line"));
		}),
		&expected,
	);
}

#[test]
fn render_all_lines() {
	let mut expected = vec!["This is a leading line", "This is a line"];
	expected.extend(vec!["~"; 7]);
	expected.push("This is a trailing line");
	assert_render(
		30,
		10,
		&ViewData::new(|updater| {
			updater.push_leading_line(ViewLine::from("This is a leading line"));
			updater.push_line(ViewLine::from("This is a line"));
			updater.push_trailing_line(ViewLine::from("This is a trailing line"));
		}),
		&expected,
	);
}

#[test]
fn render_with_full_screen_data() {
	assert_render(
		30,
		6,
		&ViewData::new(|updater| {
			updater.push_leading_line(ViewLine::from("This is a leading line"));
			updater.push_line(ViewLine::from("This is line 1"));
			updater.push_line(ViewLine::from("This is line 2"));
			updater.push_line(ViewLine::from("This is line 3"));
			updater.push_line(ViewLine::from("This is line 4"));
			updater.push_trailing_line(ViewLine::from("This is a trailing line"));
		}),
		&[
			"This is a leading line",
			"This is line 1",
			"This is line 2",
			"This is line 3",
			"This is line 4",
			"This is a trailing line",
		],
	);
}

#[test]
fn render_with_scroll_bar() {
	assert_render(
		30,
		6,
		&ViewData::new(|updater| {
			updater.push_leading_line(ViewLine::from("This is a leading line"));
			updater.push_line(ViewLine::from("This is line 1"));
			updater.push_line(ViewLine::from("This is line 2"));
			updater.push_line(ViewLine::from("This is line 3"));
			updater.push_line(ViewLine::from("This is line 4"));
			updater.push_line(ViewLine::from("This is line 5"));
			updater.push_trailing_line(ViewLine::from("This is a trailing line"));
		}),
		&[
			"This is a leading line",
			"This is line 1█",
			"This is line 2 ",
			"This is line 3 ",
			"This is line 4 ",
			"This is a trailing line",
		],
	);
}