Lines
98.78 %
Functions
100 %
Branches
use super::*;
use crate::modal::Modal;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::buffer::Buffer;
use ratatui::layout::Position;
use tvui::Role;
fn note_form(note: &str, focused: bool) -> Form {
let mut values = forms::FormValues::default();
values.set("note", note);
let mut form = Form::transaction_edit(&values);
let note = form.fields.iter().position(|f| f.key == Some("note"));
form.focus = if focused { note.unwrap_or(0) } else { 0 };
form
}
/// The form of [`note_form`] with the cursor moved to the end of the note, as after typing it.
fn typed_note_form(note: &str, focused: bool) -> Form {
let mut form = note_form(note, focused);
for field in &mut form.fields {
if let Some(ed) = field.widget.as_text_mut().filter(|ed| ed.is_multiline()) {
ed.replace_buffer(note);
fn numbered(count: usize) -> String {
(0..count)
.map(|i| format!("row{i:02}"))
.collect::<Vec<_>>()
.join("\n")
struct Screen {
buffer: Buffer,
cursor: Option<Position>,
impl Screen {
fn rows(&self) -> Vec<String> {
let area = self.buffer.area;
(0..area.height)
.map(|y| {
(0..area.width)
.map(|x| self.buffer[(x, y)].symbol().to_string())
.collect()
})
fn find(&self, text: &str) -> Option<Position> {
let rows = self.rows();
let (y, row) = rows
.iter()
.enumerate()
.find(|(_, row)| row.contains(text))?;
let x = row[..row.find(text)?].chars().count();
Some(Position::new(
u16::try_from(x).ok()?,
u16::try_from(y).ok()?,
))
fn draw(form: Form, size: (u16, u16)) -> (Screen, crate::app::App) {
let mut app = crate::test_support::app();
app.overlays.push(Modal::Form(form));
let screen = draw_app(&app, size);
(screen, app)
fn draw_app(app: &crate::app::App, size: (u16, u16)) -> Screen {
let mut terminal = Terminal::new(TestBackend::new(size.0, size.1)).expect("test terminal");
terminal
.draw(|frame| crate::draw::draw(frame, app, &mut crate::hits::Hits::default()))
.expect("the frame draws");
let backend = terminal.backend();
Screen {
buffer: backend.buffer().clone(),
cursor: backend.cursor_visible().then(|| backend.cursor_position()),
fn note_editor(app: &mut crate::app::App) -> &mut crate::widgets::Editor {
let Some(Modal::Form(form)) = app.overlays.top_mut() else {
panic!("the form is open");
};
let focus = form.focus;
form.fields[focus]
.widget
.as_text_mut()
.expect("a text field")
#[test]
fn a_focused_text_area_draws_its_lines_in_the_focused_input_colours_with_the_cursor_in_it() {
let (screen, _) = draw(typed_note_form("first\nsecond", true), (80, 24));
let first = screen.find("first").expect("the first line is drawn");
let theme = crate::test_support::dn_theme();
let want = theme.style(Role::InputFocused);
let style = screen.buffer[(first.x, first.y)].style();
assert_eq!((style.fg, style.bg), (want.fg, want.bg));
let second = screen.find("second").expect("the second line is drawn");
assert_eq!((second.x, second.y), (first.x, first.y + 1));
assert_eq!(screen.cursor, Some(Position::new(first.x + 6, first.y + 1)));
fn an_unfocused_text_area_draws_its_lines_in_the_input_colours_without_a_cursor() {
let (screen, _) = draw(note_form("first\nsecond", false), (80, 24));
assert!(screen.find("second").is_some());
assert!(!screen.rows().iter().any(|row| row.contains('\u{23ce}')));
let want = crate::test_support::dn_theme().style(Role::Input);
assert_eq!(screen.cursor.map(|at| at.y == first.y), Some(false));
fn dialog(screen: &Screen) -> (Position, Position) {
let corner = |mark| screen.find(mark).expect("the dialog frame is drawn");
(corner("\u{2554}"), corner("\u{255d}"))
fn the_dialog_keeps_its_rect_when_the_focus_moves_in_and_out_of_a_text_area() {
let none = forms::FormValues::default();
for size in [(80, 24), (120, 36)] {
for form in [Form::tag_create(), Form::transaction_edit(&none)] {
let count = form.fields.len();
let mut seen = Vec::new();
for focus in 0..=count {
form.focus = focus;
seen.push(dialog(&draw_app(&app, size)));
assert!(seen.windows(2).all(|pair| pair[0] == pair[1]), "{seen:?}");
fn a_loaded_text_shows_its_first_lines_from_the_first_column_when_unfocused() {
let lines: Vec<String> = (0..9)
.map(|i| format!("row{i} {}", "x".repeat(60)))
.collect();
let (screen, _) = draw(note_form(&lines.join("\n"), false), (80, 24));
let first = screen.find("row0").expect("the first line is shown");
let fifth = screen.find("row4").expect("five lines are shown");
assert_eq!(fifth, Position::new(first.x, first.y + 4));
assert!(screen.find("row5").is_none());
fn no_marker_and_no_scroll_lines_remain() {
let (screen, _) = draw(typed_note_form(&numbered(20), true), (80, 24));
let rows = screen.rows().join("\n");
assert!(!rows.contains('\u{25b8}'), "{rows}");
assert!(!rows.contains("more above"), "{rows}");
assert!(!rows.contains("more)"), "{rows}");
fn a_long_text_scrolls_to_keep_the_cursor_visible() {
assert!(screen.find("row14").is_none());
assert!(screen.find("row00").is_none());
let first = screen.find("row15").expect("the window starts at row15");
let last = screen.find("row19").expect("the last line is shown");
assert_eq!(last.y, first.y + 4);
assert_eq!(screen.cursor, Some(Position::new(last.x + 5, last.y)));
fn a_long_line_scrolls_sideways_to_keep_the_cursor_visible() {
let text = "x".repeat(60) + "END";
let (screen, _) = draw(typed_note_form(&text, true), (80, 24));
let end = screen.find("END").expect("the line end is shown");
assert_eq!(screen.cursor, Some(Position::new(end.x + 3, end.y)));
fn the_view_is_stable_while_the_cursor_moves_inside_it() {
let (before, mut app) = draw(typed_note_form(&numbered(20), true), (80, 24));
let at = before.find("row19").expect("the last line is shown");
note_editor(&mut app).move_up();
let after = draw_app(&app, (80, 24));
assert_eq!(after.find("row19"), Some(at));
assert_eq!(after.cursor, Some(Position::new(at.x + 5, at.y - 1)));
fn the_text_area_fits_a_narrow_screen() {
let (screen, _) = draw(typed_note_form(&numbered(20), true), (60, 24));
assert!(screen.find("row19").is_some());
assert!(screen.cursor.is_some());
fn joining_the_last_line_while_scrolled_down_leaves_no_empty_row() {
let top = before.find("row15").expect("the window starts at row15");
let editor = note_editor(&mut app);
editor.move_home();
editor.delete_backward();
let first = after
.find("row14")
.expect("the window moved up by the lost line");
assert_eq!(first.y, top.y);
let last = after
.find("row18row19")
.expect("the joined line is the last");
assert_eq!(last.y, top.y + 4);