297 lines
10 KiB
Rust
297 lines
10 KiB
Rust
use std::collections::BTreeMap;
|
|
|
|
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
|
use pretty_assertions::assert_eq;
|
|
use rcom::app::{
|
|
CollisionChoice, Command, HELP_INVENTORY, HelpContext, HelpViewport, QuitDecision,
|
|
};
|
|
use rcom::input::{InputOutcome, InputState};
|
|
|
|
// This fixture intentionally does not derive data from the renderer or input dispatcher. Adding a
|
|
// handler or changing its public aliases requires a conscious update of this independent contract.
|
|
const WORKSPACE: &[(&str, &str)] = &[
|
|
("filter-open", "/"),
|
|
("filter-next-match", "n"),
|
|
("filter-previous-match", "N"),
|
|
("filter-clear-applied", "Esc"),
|
|
("workspace-next-entry", "j / Down"),
|
|
("workspace-previous-entry", "k / Up"),
|
|
("workspace-parent", "h / Left"),
|
|
("workspace-open-directory", "l / Right / Enter"),
|
|
("workspace-first-entry", "gg / Home"),
|
|
("workspace-last-entry", "G / End"),
|
|
("workspace-page-up", "PageUp / Ctrl-b"),
|
|
("workspace-page-down", "PageDown / Ctrl-f"),
|
|
("workspace-half-page-up", "Ctrl-u"),
|
|
("workspace-half-page-down", "Ctrl-d"),
|
|
("workspace-local-home", "~"),
|
|
("workspace-toggle-selection", "Space / Insert"),
|
|
("workspace-visual-selection", "v"),
|
|
("workspace-select-all", "Ctrl-a"),
|
|
("workspace-invert-selection", "I"),
|
|
("workspace-clear-selection", "U"),
|
|
("workspace-yank", "y"),
|
|
("workspace-cut", "x"),
|
|
("workspace-paste", "p"),
|
|
("workspace-copy", "F5"),
|
|
("workspace-move", "F6"),
|
|
("workspace-create-file", "a"),
|
|
("workspace-create-directory", "A / F7"),
|
|
("workspace-rename", "r / F2"),
|
|
("workspace-trash", "d"),
|
|
("workspace-permanent-delete", "D / F8"),
|
|
("workspace-undo", "u / Ctrl-z"),
|
|
("workspace-edit", "e / F4"),
|
|
("workspace-open-tasks", "t"),
|
|
("workspace-open-host-picker", "s"),
|
|
("workspace-switch-pane", "Tab / Shift-Tab"),
|
|
("workspace-toggle-pane-mode", "\\"),
|
|
("workspace-toggle-hidden", "."),
|
|
("workspace-refresh", "Ctrl-r"),
|
|
("workspace-open-preview", "F3"),
|
|
("workspace-exit-visual", "Esc"),
|
|
("workspace-error-details", "m"),
|
|
("workspace-open-theme-picker", "T"),
|
|
("workspace-open-help", "? / F1"),
|
|
("workspace-quit", "q"),
|
|
];
|
|
const HOST_PICKER: &[(&str, &str)] = &[
|
|
("host-picker-input", "Printable"),
|
|
("host-picker-backspace", "Backspace"),
|
|
("host-picker-select", "Up / Down"),
|
|
("host-picker-connect", "Enter"),
|
|
("host-picker-close", "Esc"),
|
|
];
|
|
const THEME_PICKER: &[(&str, &str)] = &[
|
|
("theme-next", "j / Down"),
|
|
("theme-previous", "k / Up"),
|
|
("theme-save", "Enter"),
|
|
("theme-cancel", "Esc"),
|
|
];
|
|
const TASKS: &[(&str, &str)] = &[
|
|
("tasks-next", "j / Down"),
|
|
("tasks-previous", "k / Up"),
|
|
("tasks-page-up", "PageUp / Ctrl-b"),
|
|
("tasks-page-down", "PageDown / Ctrl-f"),
|
|
("tasks-half-page-up", "Ctrl-u"),
|
|
("tasks-half-page-down", "Ctrl-d"),
|
|
("tasks-open-details", "m / Enter"),
|
|
("tasks-cancel", "c"),
|
|
("tasks-close", "Esc / q / t"),
|
|
];
|
|
const TASK_DETAILS: &[(&str, &str)] = &[
|
|
("details-line-down", "j / Down"),
|
|
("details-line-up", "k / Up"),
|
|
("details-page-up", "PageUp / Ctrl-b"),
|
|
("details-page-down", "PageDown / Ctrl-f"),
|
|
("details-half-page-up", "Ctrl-u"),
|
|
("details-half-page-down", "Ctrl-d"),
|
|
("details-cancel", "c"),
|
|
("details-close", "m / Enter / Esc / q / t"),
|
|
];
|
|
const PREVIEW: &[(&str, &str)] = &[("preview-close", "F3 / Esc / q")];
|
|
const FILTER: &[(&str, &str)] = &[
|
|
("filter-input", "Printable"),
|
|
("filter-backspace", "Backspace"),
|
|
("filter-clear-draft", "Ctrl-u"),
|
|
("filter-apply", "Enter"),
|
|
("filter-cancel", "Esc"),
|
|
("filter-quit", "F10"),
|
|
];
|
|
const HELP: &[(&str, &str)] = &[
|
|
("help-previous-category", "h / Left / Shift-Tab"),
|
|
("help-next-category", "l / Right / Tab"),
|
|
("help-line-down", "j / Down"),
|
|
("help-line-up", "k / Up"),
|
|
("help-page-up", "PageUp / Ctrl-b"),
|
|
("help-page-down", "PageDown / Ctrl-f"),
|
|
("help-half-page-up", "Ctrl-u"),
|
|
("help-half-page-down", "Ctrl-d"),
|
|
("help-close", "Esc / q / ? / F1"),
|
|
("help-enter-inert", "Enter"),
|
|
];
|
|
const DESTINATION: &[(&str, &str)] = &[
|
|
("destination-input", "Printable"),
|
|
("destination-backspace", "Backspace"),
|
|
("destination-submit", "Enter"),
|
|
("destination-cancel", "Esc"),
|
|
];
|
|
const CONFIRMATION: &[(&str, &str)] = &[
|
|
("operation-confirm", "Enter"),
|
|
("operation-cancel", "Esc / q"),
|
|
];
|
|
const COLLISION: &[(&str, &str)] = &[
|
|
("collision-next", "j / Down"),
|
|
("collision-previous", "k / Up"),
|
|
("collision-skip", "s / S"),
|
|
("collision-overwrite", "o / O"),
|
|
("collision-cancel", "Esc / Enter / q"),
|
|
];
|
|
const ENTRY_PROMPT: &[(&str, &str)] = &[
|
|
("entry-input", "Printable"),
|
|
("entry-backspace", "Backspace"),
|
|
("entry-submit", "Enter"),
|
|
("entry-cancel", "Esc"),
|
|
];
|
|
const ENTRY_CONFIRMATION: &[(&str, &str)] = &[
|
|
("entry-confirm", "Enter"),
|
|
("entry-confirmation-cancel", "Esc / q"),
|
|
];
|
|
const UNDO: &[(&str, &str)] = &[
|
|
("undo-confirm", "Enter"),
|
|
("undo-cancel", "Esc / q"),
|
|
("undo-dismiss-empty", "Enter"),
|
|
];
|
|
const EDITOR: &[(&str, &str)] = &[
|
|
("editor-confirm-upload", "Enter"),
|
|
("editor-cancel", "Esc / q"),
|
|
("editor-dismiss-notice", "Enter"),
|
|
];
|
|
const ERROR_DETAILS: &[(&str, &str)] = &[
|
|
("message-line-down", "j / Down"),
|
|
("message-line-up", "k / Up"),
|
|
("message-page-up", "PageUp / Ctrl-b"),
|
|
("message-page-down", "PageDown / Ctrl-f"),
|
|
("message-half-page-up", "Ctrl-u"),
|
|
("message-half-page-down", "Ctrl-d"),
|
|
("message-close", "Esc / q"),
|
|
];
|
|
const QUIT_CONFIRMATION: &[(&str, &str)] = &[
|
|
("quit-stay", "Esc / Enter"),
|
|
("quit-cancel-and-wait", "c"),
|
|
("quit-exit-with-warning", "x"),
|
|
];
|
|
const GLOBAL: &[(&str, &str)] = &[("global-quit", "F10")];
|
|
|
|
#[test]
|
|
fn independently_enumerated_input_contexts_match_every_inventory_action_and_alias() {
|
|
let expected = [
|
|
(HelpContext::Workspace, WORKSPACE),
|
|
(HelpContext::HostPicker, HOST_PICKER),
|
|
(HelpContext::ThemePicker, THEME_PICKER),
|
|
(HelpContext::Tasks, TASKS),
|
|
(HelpContext::TaskDetails, TASK_DETAILS),
|
|
(HelpContext::Preview, PREVIEW),
|
|
(HelpContext::Filter, FILTER),
|
|
(HelpContext::Help, HELP),
|
|
(HelpContext::DestinationPrompt, DESTINATION),
|
|
(HelpContext::Confirmation, CONFIRMATION),
|
|
(HelpContext::Collision, COLLISION),
|
|
(HelpContext::EntryPrompt, ENTRY_PROMPT),
|
|
(HelpContext::EntryConfirmation, ENTRY_CONFIRMATION),
|
|
(HelpContext::Undo, UNDO),
|
|
(HelpContext::Editor, EDITOR),
|
|
(HelpContext::ErrorDetails, ERROR_DETAILS),
|
|
(HelpContext::QuitConfirmation, QUIT_CONFIRMATION),
|
|
(HelpContext::Global, GLOBAL),
|
|
];
|
|
let mut actual = BTreeMap::new();
|
|
for entry in HELP_INVENTORY {
|
|
actual
|
|
.entry(entry.context.label())
|
|
.or_insert_with(Vec::new)
|
|
.push((entry.id, entry.aliases));
|
|
}
|
|
let expected = expected
|
|
.into_iter()
|
|
.map(|(context, entries)| {
|
|
let mut entries = entries.to_vec();
|
|
entries.sort_unstable();
|
|
(context.label(), entries)
|
|
})
|
|
.collect::<BTreeMap<_, _>>();
|
|
for bindings in actual.values_mut() {
|
|
bindings.sort_unstable();
|
|
}
|
|
assert_eq!(actual, expected);
|
|
assert_eq!(
|
|
HELP_INVENTORY.len(),
|
|
WORKSPACE.len()
|
|
+ HOST_PICKER.len()
|
|
+ THEME_PICKER.len()
|
|
+ TASKS.len()
|
|
+ TASK_DETAILS.len()
|
|
+ PREVIEW.len()
|
|
+ FILTER.len()
|
|
+ HELP.len()
|
|
+ DESTINATION.len()
|
|
+ CONFIRMATION.len()
|
|
+ COLLISION.len()
|
|
+ ENTRY_PROMPT.len()
|
|
+ ENTRY_CONFIRMATION.len()
|
|
+ UNDO.len()
|
|
+ EDITOR.len()
|
|
+ ERROR_DETAILS.len()
|
|
+ QUIT_CONFIRMATION.len()
|
|
+ GLOBAL.len()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn context_specific_bindings_execute_only_their_documented_handler() {
|
|
let press = |code| KeyEvent::new(code, KeyModifiers::NONE);
|
|
let shifted = |character| KeyEvent::new(KeyCode::Char(character), KeyModifiers::SHIFT);
|
|
assert_eq!(
|
|
InputState::new().handle_key(press(KeyCode::Char('T')), 10),
|
|
InputOutcome::Command(Command::OpenThemePicker)
|
|
);
|
|
assert_eq!(
|
|
InputState::new().handle_confirmation_key(press(KeyCode::Enter)),
|
|
InputOutcome::Command(Command::ConfirmOperation)
|
|
);
|
|
assert_eq!(
|
|
InputState::new().handle_entry_confirmation_key(press(KeyCode::Enter)),
|
|
InputOutcome::Command(Command::ConfirmEntryAction)
|
|
);
|
|
assert_eq!(
|
|
InputState::new().handle_key(press(KeyCode::Char('/')), 10),
|
|
InputOutcome::Command(Command::OpenFilter)
|
|
);
|
|
assert_eq!(
|
|
InputState::new().handle_key(press(KeyCode::Char('n')), 10),
|
|
InputOutcome::Command(Command::MoveMatch(1))
|
|
);
|
|
assert_eq!(
|
|
InputState::new().handle_key(shifted('N'), 10),
|
|
InputOutcome::Command(Command::MoveMatch(-1))
|
|
);
|
|
assert_eq!(
|
|
InputState::new().handle_filter_key(press(KeyCode::Enter)),
|
|
InputOutcome::Command(Command::ApplyFilter)
|
|
);
|
|
for (key, choice, apply_to_all) in [
|
|
(press(KeyCode::Char('s')), CollisionChoice::Skip, false),
|
|
(shifted('S'), CollisionChoice::Skip, true),
|
|
(press(KeyCode::Char('o')), CollisionChoice::Overwrite, false),
|
|
(shifted('O'), CollisionChoice::Overwrite, true),
|
|
] {
|
|
assert_eq!(
|
|
InputState::new().handle_collision_key(key),
|
|
InputOutcome::Command(Command::ResolveCollision {
|
|
choice,
|
|
apply_to_all,
|
|
})
|
|
);
|
|
}
|
|
for (key, decision) in [
|
|
(press(KeyCode::Enter), QuitDecision::Stay),
|
|
(press(KeyCode::Esc), QuitDecision::Stay),
|
|
(press(KeyCode::Char('c')), QuitDecision::CancelAndWait),
|
|
(press(KeyCode::Char('x')), QuitDecision::ExitWithWarning),
|
|
] {
|
|
assert_eq!(
|
|
InputState::new().handle_quit_key(key),
|
|
InputOutcome::Command(Command::DecideQuit(decision))
|
|
);
|
|
}
|
|
assert_eq!(
|
|
InputState::new().handle_help_key(
|
|
press(KeyCode::Enter),
|
|
HelpViewport {
|
|
page_size: 4,
|
|
max_scroll: 10,
|
|
},
|
|
),
|
|
InputOutcome::Ignored
|
|
);
|
|
}
|