use crate::tui::app::App;
use crossterm::event::{
Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers, MouseButton, MouseEvent,
MouseEventKind,
};
pub(crate) fn synth_press(code: KeyCode, modifiers: KeyModifiers) -> Event {
Event::Key(KeyEvent {
code,
modifiers,
kind: KeyEventKind::Press,
state: KeyEventState::NONE,
})
}
#[allow(dead_code)]
pub(crate) fn synth_mouse(
kind: MouseEventKind,
column: u16,
row: u16,
modifiers: KeyModifiers,
) -> Event {
Event::Mouse(MouseEvent {
kind,
column,
row,
modifiers,
})
}
#[allow(dead_code)]
pub(crate) fn synth_click(column: u16, row: u16) -> Event {
synth_mouse(
MouseEventKind::Down(MouseButton::Left),
column,
row,
KeyModifiers::NONE,
)
}
pub(crate) fn apply_event(app: &mut App, event: Event) {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("test runtime");
rt.block_on(crate::tui::terminal::handle_event(app, event));
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::Config;
use crate::tui::app::{App, Mode, View};
use std::sync::Arc;
fn cfg() -> Arc<Config> {
Arc::new(
crate::config::parse_str(
r#"
[runtime]
backend = "none"
[command.greet]
run = "echo hi"
[command.build]
run = "cargo build"
"#,
)
.unwrap(),
)
}
#[test]
fn capital_t_switches_to_terminals() {
let mut app = App::new(cfg());
assert_eq!(app.view(), View::ControlCenter);
apply_event(
&mut app,
synth_press(KeyCode::Char('T'), KeyModifiers::NONE),
);
assert_eq!(app.view(), View::Terminals);
}
#[test]
fn capital_g_switches_to_diff() {
let mut app = App::new(cfg());
apply_event(
&mut app,
synth_press(KeyCode::Char('G'), KeyModifiers::NONE),
);
assert_eq!(app.view(), View::Diff);
}
#[test]
fn slash_opens_palette() {
let mut app = App::new(cfg());
apply_event(
&mut app,
synth_press(KeyCode::Char('/'), KeyModifiers::NONE),
);
assert_eq!(app.mode(), Mode::Palette);
assert!(app.palette().is_some());
}
#[test]
fn esc_closes_palette() {
let mut app = App::new(cfg());
apply_event(
&mut app,
synth_press(KeyCode::Char('/'), KeyModifiers::NONE),
);
assert_eq!(app.mode(), Mode::Palette);
apply_event(&mut app, synth_press(KeyCode::Esc, KeyModifiers::NONE));
assert_eq!(app.mode(), Mode::Normal);
assert!(app.palette().is_none());
}
}