Lines
98.47 %
Functions
100 %
Branches
use super::*;
use crate::app::App;
use crate::hits::Hits;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::layout::Rect;
use tvui::Dialog;
/// The rows of the body of `form` drawn in a dialog on a roomy screen.
pub(in crate::draw) fn form_rows(form: &Form, pending: bool, app: &App) -> Vec<String> {
let body = form_body(form, (pending, true), app);
let screen = Rect::new(0, 0, 100, 60);
let mut terminal =
Terminal::new(TestBackend::new(screen.width, screen.height)).expect("a test terminal");
terminal
.draw(|frame| {
let at = tvui::Place::from(screen);
let mut hits = Hits::default();
crate::draw::canvas::draw_placed(frame, at, screen, &mut hits, |canvas, area, hits| {
draw_form(canvas, area, &body, &[], &app.theme, false, hits);
});
})
.expect("the form draws");
let buffer = terminal.backend().buffer();
let inner = Dialog::body_area(screen);
(inner.y..inner.y + u16::try_from(body.height()).unwrap_or(0))
.map(|y| {
let mut row = String::new();
let mut x = inner.x;
while x < inner.right() {
let symbol = buffer[(x, y)].symbol();
row.push_str(symbol);
x += u16::try_from(Line::from(symbol).width().max(1)).unwrap_or(1);
}
row.trim_end().to_string()
.collect()
fn texts(app: &App, form: &Form) -> Vec<String> {
form_rows(form, false, app)
#[test]
fn the_transaction_footer_is_one_note_wherever_the_focus_is() {
let mut form = Form::transaction_create();
for focus in 0..=form.fields.len() {
form.focus = focus;
let texts = texts(&crate::test_support::app(), &form);
let notes: Vec<&String> = texts.iter().filter(|l| l.starts_with("Date:")).collect();
assert_eq!(notes, ["Date: YYYY-MM-DDTHH:MM"], "{texts:?}");
assert!(!texts.iter().any(|l| l.contains("C-t")), "{texts:?}");
fn a_form_whose_keys_are_all_on_the_bar_has_no_footer() {
let texts = texts(&crate::test_support::app(), &Form::config_set());
assert_eq!(texts, [" Key:", " Value:"]);
fn footer_separated_from_fields_by_single_blank_line() {
let form = Form::ssh_key_add();
let texts = form_rows(&form, false, &crate::test_support::app());
let footer = texts
.iter()
.position(|l| l.contains("Paste one public key"))
.expect("footer line present");
assert!(
footer >= 1 && texts[footer - 1].trim().is_empty(),
"exactly one blank line must precede the footer: {texts:?}"
);
footer < 2 || !texts[footer - 2].trim().is_empty(),
"no double blank line before the footer: {texts:?}"
fn pending_form_shows_submitting_indicator_instead_of_the_key_hints() {
let form = Form::config_set();
let texts = form_rows(&form, true, &crate::test_support::app());
assert!(texts.iter().any(|l| l.contains("Submitting")), "{texts:?}");
!texts.iter().any(|l| l.contains("Paste one public key")),
"{texts:?}"
fn unlocked_form_shows_its_error_messages() {
let mut form = Form::config_set();
form.errors = vec!["value: too long".to_string()];
assert!(texts.iter().any(|l| l == "! value: too long"), "{texts:?}");
fn a_multiline_note_shows_its_lines_on_separate_rows() {
let mut values = forms::FormValues::default();
values.set("note", "first\nsecond");
let form = Form::transaction_edit(&values);
assert!(texts.iter().any(|l| l.contains("first")), "{texts:?}");
assert!(texts.iter().any(|l| l.contains("second")), "{texts:?}");
assert!(!texts.iter().any(|l| l.contains('\u{23ce}')), "{texts:?}");
assert_eq!(form.values().text("note"), Some("first\nsecond"));
fn transaction_rows(app: &App) -> Vec<String> {
texts(app, &Form::transaction_create())
fn tags_block(rows: &[String]) -> Vec<&String> {
let at = rows
.position(|l| l.trim_start().starts_with("Tags:"))
.expect("the Tags field is drawn");
rows[at..].iter().collect()
fn an_empty_tags_field_says_how_to_add_a_tag() {
let rows = transaction_rows(&crate::test_support::app());
let block = tags_block(&rows);
block.iter().any(|l| l.contains("(no tags: C-n adds one)")),
"{rows:?}"
fn the_empty_tags_note_names_the_key_of_the_keymap() {
let mut app = crate::test_support::app();
app.keys = app
.keys
.overridden("widget-cells: C-n = none; widget-cells: F9 = add-row")
.expect("the override parses");
let rows = transaction_rows(&app);
block.iter().any(|l| l.contains("(no tags: F9 adds one)")),
assert!(!rows.iter().any(|l| l.contains("C-n adds one")), "{rows:?}");
fn open_split_tags(form: &mut Form, focus: usize) {
form.focus = 3;
let Some(Widget::Splits(splits)) = form.fields.get_mut(3).map(|f| &mut f.widget) else {
panic!("splits field");
};
splits.open_tags();
fn an_empty_open_split_tag_list_names_the_add_key_with_or_without_focus() {
let app = crate::test_support::app();
for focus in [3, 0] {
open_split_tags(&mut form, focus);
let rows = texts(&app, &form);
let Some(from) = rows
.position(|l| l.trim_start().starts_with("Splits:"))
else {
panic!("the splits field is drawn: {rows:?}");
let note = rows[from..]
.any(|l| l.contains("(no tags: C-n adds one)"));
assert!(note, "focus {focus}: {rows:?}");
fn a_split_line_shows_its_tag_count_even_when_zero() {
assert!(rows.iter().any(|l| l.contains("#0")), "{rows:?}");