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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
#[cfg(feature = "integration")]
mod integration {
use std::path::PathBuf;
use helix_core::{syntax::AutoPairConfig, Position, Selection, Transaction};
use helix_term::{application::Application, args::Args, config::Config};
use helix_view::{doc, input::parse_macro};
use crossterm::event::{Event, KeyEvent};
use indoc::indoc;
pub struct TestCase {
pub in_text: String,
pub in_selection: Selection,
pub in_keys: String,
pub out_text: String,
pub out_selection: Selection,
}
fn test_key_sequence(
app: Option<Application>,
test_case: &TestCase,
test_fn: &dyn Fn(&mut Application),
) -> anyhow::Result<()> {
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,
);
let input_keys = parse_macro(&test_case.in_keys)?
.into_iter()
.map(|key_event| Event::Key(KeyEvent::from(key_event)));
for key in input_keys {
app.handle_terminal_events(Ok(key));
}
test_fn(&mut app);
Ok(())
}
/// 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.
fn test_key_sequence_text_result(
args: Args,
config: Config,
test_case: TestCase,
) -> anyhow::Result<()> {
let app = Application::new(args, config).unwrap();
test_key_sequence(Some(app), &test_case, &|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);
})?;
Ok(())
}
#[tokio::test]
async fn hello_world() -> anyhow::Result<()> {
test_key_sequence_text_result(
Args::default(),
Config::default(),
TestCase {
in_text: "\n".into(),
in_selection: Selection::single(0, 1),
// TODO: fix incorrect selection on new doc
in_keys: "ihello world<esc>".into(),
out_text: "hello world\n".into(),
out_selection: Selection::single(12, 11),
},
)?;
Ok(())
}
#[tokio::test]
async fn insert_mode_cursor_position() -> anyhow::Result<()> {
test_key_sequence_text_result(
Args::default(),
Config::default(),
TestCase {
in_text: String::new(),
in_selection: Selection::single(0, 0),
in_keys: "i".into(),
out_text: String::new(),
out_selection: Selection::single(0, 0),
},
)?;
test_key_sequence_text_result(
Args::default(),
Config::default(),
TestCase {
in_text: "\n".into(),
in_selection: Selection::single(0, 1),
in_keys: "i".into(),
out_text: "\n".into(),
out_selection: Selection::single(1, 0),
},
)?;
test_key_sequence_text_result(
Args::default(),
Config::default(),
TestCase {
in_text: "\n".into(),
in_selection: Selection::single(0, 1),
in_keys: "i<esc>i".into(),
out_text: "\n".into(),
out_selection: Selection::single(1, 0),
},
)?;
Ok(())
}
#[tokio::test]
async fn insert_to_normal_mode_cursor_position() -> anyhow::Result<()> {
test_key_sequence_text_result(
Args::default(),
Config::default(),
TestCase {
in_text: "\n".into(),
in_selection: Selection::single(0, 1),
in_keys: "i".into(),
out_text: "\n".into(),
out_selection: Selection::single(1, 0),
},
)?;
Ok(())
}
#[tokio::test]
async fn auto_pairs_basic() -> anyhow::Result<()> {
test_key_sequence_text_result(
Args::default(),
Config::default(),
TestCase {
in_text: "\n".into(),
in_selection: Selection::single(0, 1),
in_keys: "i(<esc>".into(),
out_text: "()\n".into(),
out_selection: Selection::single(2, 1),
},
)?;
test_key_sequence_text_result(
Args::default(),
Config {
editor: helix_view::editor::Config {
auto_pairs: AutoPairConfig::Enable(false),
..Default::default()
},
..Default::default()
},
TestCase {
in_text: "\n".into(),
in_selection: Selection::single(0, 1),
in_keys: "i(<esc>".into(),
out_text: "(\n".into(),
out_selection: Selection::single(2, 1),
},
)?;
Ok(())
}
#[tokio::test]
async fn auto_indent_rs() -> anyhow::Result<()> {
test_key_sequence_text_result(
Args {
files: vec![(PathBuf::from("foo.c"), Position::default())],
..Default::default()
},
Config::default(),
TestCase {
in_text: "void foo() {}\n".into(),
in_selection: Selection::single(13, 12),
in_keys: "i<ret><esc>".into(),
out_text: indoc! {r#"
void foo() {
}
"#}
.trim_start()
.into(),
out_selection: Selection::single(16, 15),
},
)?;
Ok(())
}
}
|