summaryrefslogtreecommitdiff
path: root/helix-term/tests/integration/helpers.rs
blob: df662f07d463dd433b3943db3310c19f67e4ef6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use std::{io::Write, time::Duration};

use anyhow::bail;
use crossterm::event::{Event, KeyEvent};
use helix_core::{test, Selection, Transaction};
use helix_term::{application::Application, args::Args, config::Config};
use helix_view::{doc, input::parse_macro};
use tokio::time::timeout;
use tokio_stream::wrappers::UnboundedReceiverStream;

#[derive(Clone, Debug)]
pub struct TestCase {
    pub in_text: String,
    pub in_selection: Selection,
    pub in_keys: String,
    pub out_text: String,
    pub out_selection: Selection,
}

impl<S: Into<String>> From<(S, S, S)> for TestCase {
    fn from((input, keys, output): (S, S, S)) -> Self {
        let (in_text, in_selection) = test::print(&input.into());
        let (out_text, out_selection) = test::print(&output.into());

        TestCase {
            in_text,
            in_selection,
            in_keys: keys.into(),
            out_text,
            out_selection,
        }
    }
}

pub async fn test_key_sequence(
    app: &mut Application,
    in_keys: &str,
    test_fn: Option<&dyn Fn(&Application)>,
) -> anyhow::Result<()> {
    let (tx, rx) = tokio::sync::mpsc::unbounded_channel();

    for key_event in parse_macro(&in_keys)?.into_iter() {
        tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
    }

    let mut rx_stream = UnboundedReceiverStream::new(rx);
    let event_loop = app.event_loop(&mut rx_stream);
    let result = timeout(Duration::from_millis(500), event_loop).await;

    if result.is_ok() {
        bail!("application exited before test function could run");
    }

    if let Some(test) = test_fn {
        test(app);
    };

    for key_event in parse_macro("<esc>:q!<ret>")?.into_iter() {
        tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
    }

    let event_loop = app.event_loop(&mut rx_stream);
    timeout(Duration::from_millis(5000), event_loop).await?;
    app.close().await?;

    Ok(())
}

pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
    app: Option<Application>,
    test_case: T,
    test_fn: &dyn Fn(&Application),
) -> anyhow::Result<()> {
    let test_case = test_case.into();
    let mut app =
        app.unwrap_or_else(|| Application::new(Args::default(), Config::default()).unwrap());

    let (view, doc) = helix_view::current!(app.editor);
    let sel = doc.selection(view.id).clone();

    // replace the initial text with the input text
    doc.apply(
        &Transaction::change_by_selection(&doc.text(), &sel, |_| {
            (0, doc.text().len_chars(), Some((&test_case.in_text).into()))
        })
        .with_selection(test_case.in_selection.clone()),
        view.id,
    );

    test_key_sequence(&mut app, &test_case.in_keys, Some(test_fn)).await
}

/// Use this for very simple test cases where there is one input
/// document, selection, and sequence of key presses, and you just
/// want to verify the resulting document and selection.
pub async fn test_key_sequence_text_result<T: Into<TestCase>>(
    args: Args,
    config: Config,
    test_case: T,
) -> anyhow::Result<()> {
    let test_case = test_case.into();
    let app = Application::new(args, config).unwrap();

    test_key_sequence_with_input_text(Some(app), test_case.clone(), &|app| {
        let doc = doc!(app.editor);
        assert_eq!(&test_case.out_text, doc.text());

        let mut selections: Vec<_> = doc.selections().values().cloned().collect();
        assert_eq!(1, selections.len());

        let sel = selections.pop().unwrap();
        assert_eq!(test_case.out_selection, sel);
    })
    .await
}

pub fn temp_file_with_contents<S: AsRef<str>>(content: S) -> tempfile::NamedTempFile {
    let mut temp_file = tempfile::NamedTempFile::new().unwrap();
    temp_file
        .as_file_mut()
        .write_all(content.as_ref().as_bytes())
        .unwrap();
    temp_file.flush().unwrap();
    temp_file.as_file_mut().sync_all().unwrap();
    temp_file
}