on 2158366 help sign in
helix/helix-tui/tests/widgets_paragraph.rs
220 lines 8.5 kB history raw
1// use helix_tui::{
2// backend::TestBackend,
3// buffer::Buffer,
4// layout::Alignment,
5// text::{Span, Spans, Text},
6// widgets::{Block, Borders, Paragraph, Wrap},
7// Terminal,
8// };
10// const SAMPLE_STRING: &str = "The library is based on the principle of immediate rendering with \
11// intermediate buffers. This means that at each new frame you should build all widgets that are \
12// supposed to be part of the UI. While providing a great flexibility for rich and \
13// interactive UI, this may introduce overhead for highly dynamic content.";
15// #[test]
16// fn widgets_paragraph_can_wrap_its_content() {
17// let test_case = |alignment, expected| {
18// let backend = TestBackend::new(20, 10);
19// let mut terminal = Terminal::new(backend).unwrap();
21// terminal
22// .draw(|f| {
23// let size = f.size();
24// let text = vec![Spans::from(SAMPLE_STRING)];
25// let paragraph = Paragraph::new(text)
26// .block(Block::default().borders(Borders::ALL))
27// .alignment(alignment)
28// .wrap(Wrap { trim: true });
29// f.render_widget(paragraph, size);
30// })
31// .unwrap();
32// terminal.backend().assert_buffer(&expected);
33// };
35// test_case(
36// Alignment::Left,
37// Buffer::with_lines(vec![
38// "┌──────────────────┐",
39// "│The library is │",
40// "│based on the │",
41// "│principle of │",
42// "│immediate │",
43// "│rendering with │",
44// "│intermediate │",
45// "│buffers. This │",
46// "│means that at each│",
47// "└──────────────────┘",
48// ]),
49// );
50// test_case(
51// Alignment::Right,
52// Buffer::with_lines(vec![
53// "┌──────────────────┐",
54// "│ The library is│",
55// "│ based on the│",
56// "│ principle of│",
57// "│ immediate│",
58// "│ rendering with│",
59// "│ intermediate│",
60// "│ buffers. This│",
61// "│means that at each│",
62// "└──────────────────┘",
63// ]),
64// );
65// test_case(
66// Alignment::Center,
67// Buffer::with_lines(vec![
68// "┌──────────────────┐",
69// "│ The library is │",
70// "│ based on the │",
71// "│ principle of │",
72// "│ immediate │",
73// "│ rendering with │",
74// "│ intermediate │",
75// "│ buffers. This │",
76// "│means that at each│",
77// "└──────────────────┘",
78// ]),
79// );
80// }
82// #[test]
83// fn widgets_paragraph_renders_double_width_graphemes() {
84// let backend = TestBackend::new(10, 10);
85// let mut terminal = Terminal::new(backend).unwrap();
87// let s = "コンピュータ上で文字を扱う場合、典型的には文字による通信を行う場合にその両端点では、";
88// terminal
89// .draw(|f| {
90// let size = f.size();
91// let text = vec![Spans::from(s)];
92// let paragraph = Paragraph::new(text)
93// .block(Block::default().borders(Borders::ALL))
94// .wrap(Wrap { trim: true });
95// f.render_widget(paragraph, size);
96// })
97// .unwrap();
99// let expected = Buffer::with_lines(vec![
100// "┌────────┐",
101// "│コンピュ│",
102// "│ータ上で│",
103// "│文字を扱│",
104// "│う場合、│",
105// "│典型的に│",
106// "│は文字に│",
107// "│よる通信│",
108// "│を行う場│",
109// "└────────┘",
110// ]);
111// terminal.backend().assert_buffer(&expected);
112// }
114// #[test]
115// fn widgets_paragraph_renders_mixed_width_graphemes() {
116// let backend = TestBackend::new(10, 7);
117// let mut terminal = Terminal::new(backend).unwrap();
119// let s = "aコンピュータ上で文字を扱う場合、";
120// terminal
121// .draw(|f| {
122// let size = f.size();
123// let text = vec![Spans::from(s)];
124// let paragraph = Paragraph::new(text)
125// .block(Block::default().borders(Borders::ALL))
126// .wrap(Wrap { trim: true });
127// f.render_widget(paragraph, size);
128// })
129// .unwrap();
131// let expected = Buffer::with_lines(vec![
132// // The internal width is 8 so only 4 slots for double-width characters.
133// "┌────────┐",
134// "│aコンピ │", // Here we have 1 latin character so only 3 double-width ones can fit.
135// "│ュータ上│",
136// "│で文字を│",
137// "│扱う場合│",
138// "│、 │",
139// "└────────┘",
140// ]);
141// terminal.backend().assert_buffer(&expected);
142// }
144// #[test]
145// fn widgets_paragraph_can_wrap_with_a_trailing_nbsp() {
146// let nbsp: &str = "\u{00a0}";
147// let line = Spans::from(vec![Span::raw("NBSP"), Span::raw(nbsp)]);
148// let backend = TestBackend::new(20, 3);
149// let mut terminal = Terminal::new(backend).unwrap();
150// let expected = Buffer::with_lines(vec![
151// "┌──────────────────┐",
152// "│NBSP\u{00a0} │",
153// "└──────────────────┘",
154// ]);
155// terminal
156// .draw(|f| {
157// let size = f.size();
159// let paragraph = Paragraph::new(line).block(Block::default().borders(Borders::ALL));
160// f.render_widget(paragraph, size);
161// })
162// .unwrap();
163// terminal.backend().assert_buffer(&expected);
164// }
165// #[test]
166// fn widgets_paragraph_can_scroll_horizontally() {
167// let test_case = |alignment, scroll, expected| {
168// let backend = TestBackend::new(20, 10);
169// let mut terminal = Terminal::new(backend).unwrap();
171// terminal
172// .draw(|f| {
173// let size = f.size();
174// let text = Text::from(
175// "段落现在可以水平滚动了!\nParagraph can scroll horizontally!\nShort line",
176// );
177// let paragraph = Paragraph::new(text)
178// .block(Block::default().borders(Borders::ALL))
179// .alignment(alignment)
180// .scroll(scroll);
181// f.render_widget(paragraph, size);
182// })
183// .unwrap();
184// terminal.backend().assert_buffer(&expected);
185// };
187// test_case(
188// Alignment::Left,
189// (0, 7),
190// Buffer::with_lines(vec![
191// "┌──────────────────┐",
192// "│在可以水平滚动了!│",
193// "│ph can scroll hori│",
194// "│ine │",
195// "│ │",
196// "│ │",
197// "│ │",
198// "│ │",
199// "│ │",
200// "└──────────────────┘",
201// ]),
202// );
203// // only support Alignment::Left
204// test_case(
205// Alignment::Right,
206// (0, 7),
207// Buffer::with_lines(vec![
208// "┌──────────────────┐",
209// "│段落现在可以水平滚│",
210// "│Paragraph can scro│",
211// "│ Short line│",
212// "│ │",
213// "│ │",
214// "│ │",
215// "│ │",
216// "│ │",
217// "└──────────────────┘",
218// ]),
219// );
220// }