Skip to content

Commit 927a544

Browse files
committed
test(panes): add unit tests for EditorPane render and highlight
1 parent 9341abf commit 927a544

1 file changed

Lines changed: 130 additions & 0 deletions

File tree

‎src/panes/editor.rs‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,133 @@ impl EditorPane {
270270
.unwrap_or(theme.syntax_variable) // Use theme color instead of Color::White
271271
}
272272
}
273+
274+
#[cfg(test)]
275+
mod tests {
276+
use super::*;
277+
use crate::syntax::{HighlightSpan, TokenType};
278+
use ratatui::{backend::TestBackend, buffer::Buffer, style::Modifier, Terminal};
279+
280+
fn highlight(start: usize, end: usize, token_type: TokenType) -> HighlightSpan {
281+
HighlightSpan {
282+
start,
283+
end,
284+
token_type,
285+
}
286+
}
287+
288+
fn render_buffer(engine: &AnimationEngine, theme: &Theme, width: u16, height: u16) -> Buffer {
289+
let backend = TestBackend::new(width, height);
290+
let mut terminal = Terminal::new(backend).unwrap();
291+
terminal
292+
.draw(|f| EditorPane.render(f, Rect::new(0, 0, width, height), engine, theme))
293+
.unwrap();
294+
terminal.backend().buffer().clone()
295+
}
296+
297+
fn row_symbols(buffer: &Buffer, y: u16) -> String {
298+
(0..buffer.area.width)
299+
.map(|x| buffer[(x, y)].symbol())
300+
.collect::<Vec<_>>()
301+
.join("")
302+
}
303+
304+
#[test]
305+
fn render_shows_scrolled_highlighted_lines_and_selected_row_background() {
306+
let theme = Theme::default();
307+
let mut engine = AnimationEngine::new(16);
308+
engine.buffer.lines = vec!["skip".to_string(), "let".to_string(), "tail".to_string()];
309+
engine.buffer.scroll_offset = 1;
310+
engine.buffer.cursor_line = 2;
311+
engine.buffer.cursor_col = 4;
312+
engine.cursor_visible = true;
313+
engine.active_pane = ActivePane::Editor;
314+
engine.buffer.new_highlights = vec![
315+
highlight(5, 8, TokenType::Keyword),
316+
highlight(9, 13, TokenType::String),
317+
];
318+
engine.buffer.new_content_line_offsets = vec![0, 5, 9];
319+
320+
let buffer = render_buffer(&engine, &theme, 20, 4);
321+
322+
assert!(!row_symbols(&buffer, 1).contains("skip"));
323+
assert!(row_symbols(&buffer, 1).contains("let"));
324+
assert!(row_symbols(&buffer, 2).contains("tail"));
325+
assert_eq!(buffer[(4, 2)].fg, theme.editor_line_number_cursor);
326+
assert_eq!(buffer[(8, 2)].fg, theme.syntax_string);
327+
assert_eq!(buffer[(8, 2)].bg, theme.editor_cursor_line_bg);
328+
329+
let cursor_x = 8 + "tail".chars().count() as u16;
330+
assert_eq!(buffer[(cursor_x, 2)].symbol(), " ");
331+
assert_eq!(buffer[(cursor_x, 2)].bg, theme.editor_cursor_char_bg);
332+
assert_eq!(buffer[(cursor_x, 2)].fg, theme.editor_cursor_char_fg);
333+
}
334+
335+
#[test]
336+
fn highlight_line_uses_old_offsets_below_cursor_and_defaults_to_variable_color() {
337+
let theme = Theme::default();
338+
let pane = EditorPane;
339+
let old_highlights = vec![highlight(4, 5, TokenType::Number)];
340+
let new_highlights = Vec::new();
341+
let old_offsets = vec![0, 4];
342+
let new_offsets = Vec::new();
343+
344+
let spans = pane.highlight_line(HighlightContext {
345+
line_content: "xyz",
346+
line_num: 2,
347+
show_cursor: false,
348+
cursor_col: 0,
349+
cursor_line: 0,
350+
old_highlights: &old_highlights,
351+
new_highlights: &new_highlights,
352+
old_line_offsets: &old_offsets,
353+
new_line_offsets: &new_offsets,
354+
line_offset: 1,
355+
theme: &theme,
356+
});
357+
358+
assert_eq!(spans.len(), 3);
359+
assert_eq!(spans[0].content.as_ref(), "x");
360+
assert_eq!(spans[0].style.fg, Some(theme.syntax_number));
361+
assert_eq!(spans[1].style.fg, Some(theme.syntax_variable));
362+
assert_eq!(spans[2].style.fg, Some(theme.syntax_variable));
363+
}
364+
365+
#[test]
366+
fn calculate_byte_offset_falls_back_to_last_known_offset_or_zero() {
367+
let pane = EditorPane;
368+
369+
assert_eq!(pane.calculate_byte_offset(5, 0, 0, &[0, 4]), 4);
370+
assert_eq!(pane.calculate_byte_offset(5, 0, 0, &[]), 0);
371+
}
372+
373+
#[test]
374+
fn build_line_overlays_cursor_character_when_editor_is_active() {
375+
let theme = Theme::default();
376+
let pane = EditorPane;
377+
let mut engine = AnimationEngine::new(16);
378+
engine.buffer.lines = vec!["ab".to_string()];
379+
engine.buffer.cursor_line = 0;
380+
engine.buffer.cursor_col = 1;
381+
engine.cursor_visible = true;
382+
engine.active_pane = ActivePane::Editor;
383+
engine.buffer.new_highlights = vec![highlight(0, 2, TokenType::Keyword)];
384+
engine.buffer.new_content_line_offsets = vec![0];
385+
386+
let line = pane.build_line("ab", 0, 3, &engine, &theme);
387+
388+
assert_eq!(line.spans[0].content.as_ref(), " 1 ");
389+
assert_eq!(
390+
line.spans[0].style.fg,
391+
Some(theme.editor_line_number_cursor)
392+
);
393+
assert!(line.spans[0].style.add_modifier.contains(Modifier::BOLD));
394+
assert_eq!(line.spans[1].content.as_ref(), " ");
395+
assert_eq!(line.spans[1].style.fg, Some(theme.editor_separator));
396+
assert_eq!(line.spans[2].style.fg, Some(theme.syntax_keyword));
397+
assert_eq!(line.spans[3].content.as_ref(), "b");
398+
assert_eq!(line.spans[3].style.fg, Some(theme.editor_cursor_char_fg));
399+
assert_eq!(line.spans[3].style.bg, Some(theme.editor_cursor_char_bg));
400+
assert!(line.spans[3].style.add_modifier.contains(Modifier::BOLD));
401+
}
402+
}

0 commit comments

Comments
 (0)