Lines
98.69 %
Functions
100 %
Branches
//! The multi-line text area of the tag form's description, through the real key dispatch.
use crate::test_support::{key, press};
use forms::{FormValues, TAG_EDIT, write_payload};
use super::edit::{top_form, wire_of};
use crate::app::App;
use crate::tabs::fetch::Fetch;
use crate::view::Tab;
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
const TAG: &str = "aaaaaaaa-0000-0000-0000-000000000001";
fn type_text(app: &mut App, text: &str) {
text.chars().for_each(|c| press(app, key(KeyCode::Char(c))));
}
fn description(app: &App) -> Option<String> {
top_form(app)
.values()
.text("description")
.map(str::to_string)
fn create_form_on_description() -> anyhow::Result<App> {
let mut app = crate::test_support::emacs_app();
app.attach_echo();
app.switch_tab(Tab::Tags);
let Fetch::Loading { id } = app.tags().state else {
anyhow::bail!("tags not loading");
};
app.route_reply(id, &format!("(:id {id} :value ())"));
press(&mut app, key(KeyCode::Char('c')));
type_text(&mut app, "area");
press(&mut app, key(KeyCode::Tab));
type_text(&mut app, "home");
Ok(app)
fn source(separator: &str) -> String {
(0..50)
.map(|i| format!("(line {i})"))
.collect::<Vec<_>>()
.join(separator)
fn edit_form_on_description(text: &str) -> anyhow::Result<App> {
let entry = format!(r#"(:tag :id "{TAG}" :name "kind" :value "fiat" :description "")"#);
app.route_reply(id, &format!("(:id {id} :value ({entry}))"));
press(&mut app, key(KeyCode::Char('e')));
let mut values = FormValues::default();
values.set("id", TAG);
values.set("revision", "rev-1");
values.set("name", "kind");
values.set("value", "fiat");
values.set("description", text);
app.deliver_tag_edit_reply(
Some(app.window_of(crate::view::ViewId::Tags)),
&wire_of(&write_payload(&TAG_EDIT, &values)),
);
#[tokio::test]
async fn enter_types_a_newline_and_ctrl_s_submits() -> anyhow::Result<()> {
let mut app = create_form_on_description()?;
type_text(&mut app, "a");
press(&mut app, key(KeyCode::Enter));
type_text(&mut app, "b");
assert_eq!(description(&app), Some("a\nb".to_string()));
assert!(app.pending_form.is_none());
press(
&mut app,
KeyEvent::new(KeyCode::Char('s'), KeyModifiers::CONTROL),
assert!(app.pending_form.is_some());
Ok(())
async fn alt_enter_submits_instead_of_typing_a_newline() -> anyhow::Result<()> {
press(&mut app, KeyEvent::new(KeyCode::Enter, KeyModifiers::ALT));
async fn enter_still_submits_from_a_single_line_field() -> anyhow::Result<()> {
press(&mut app, key(KeyCode::BackTab));
async fn a_single_line_field_takes_horizontal_cursor_keys_only() -> anyhow::Result<()> {
let cursor = |app: &mut App| {
app.overlays
.form_mut()
.and_then(|f| f.focused_editor_mut())
.map(|e| e.cursor())
let before = cursor(&mut app);
for code in [KeyCode::Up, KeyCode::PageDown] {
press(&mut app, key(code));
assert_eq!(cursor(&mut app), before, "{code:?}");
press(&mut app, key(KeyCode::Left));
assert_eq!(cursor(&mut app), before.map(|c| c - 1));
press(&mut app, key(KeyCode::Home));
assert_eq!(cursor(&mut app), Some(0));
async fn a_loaded_source_survives_moving_around() -> anyhow::Result<()> {
for separator in ["\n", "\r\n"] {
let text = source(separator);
let mut app = edit_form_on_description(&text)?;
for code in [
KeyCode::PageUp,
KeyCode::Up,
KeyCode::Down,
KeyCode::Home,
KeyCode::End,
KeyCode::Left,
KeyCode::Right,
KeyCode::PageDown,
] {
assert_eq!(description(&app), Some(text.clone()));
async fn a_typed_newline_in_a_crlf_source_is_lf() -> anyhow::Result<()> {
let mut app = edit_form_on_description("a\r\nb")?;
press(&mut app, key(KeyCode::PageUp));
press(&mut app, key(KeyCode::End));
assert_eq!(description(&app), Some("a\n\r\nb".to_string()));
async fn ctrl_k_kills_only_the_rest_of_the_line() -> anyhow::Result<()> {
let mut app = edit_form_on_description("abcd\nef")?;
press(&mut app, key(KeyCode::Right));
KeyEvent::new(KeyCode::Char('k'), KeyModifiers::CONTROL),
assert_eq!(description(&app), Some("a\nef".to_string()));
assert_eq!(description(&app), Some("aef".to_string()));