diff options
Diffstat (limited to 'crates/libeditor/tests')
-rw-r--r-- | crates/libeditor/tests/test.rs | 59 |
1 files changed, 40 insertions, 19 deletions
diff --git a/crates/libeditor/tests/test.rs b/crates/libeditor/tests/test.rs index dd2c99c3f..7063425ce 100644 --- a/crates/libeditor/tests/test.rs +++ b/crates/libeditor/tests/test.rs | |||
@@ -6,10 +6,10 @@ extern crate assert_eq_text; | |||
6 | 6 | ||
7 | use std::fmt; | 7 | use std::fmt; |
8 | use itertools::Itertools; | 8 | use itertools::Itertools; |
9 | use libsyntax2::AstNode; | ||
10 | use libeditor::{ | 9 | use libeditor::{ |
11 | File, TextUnit, TextRange, | 10 | File, TextUnit, TextRange, ActionResult, CursorPosition, |
12 | highlight, runnables, extend_selection, file_structure, flip_comma, | 11 | highlight, runnables, extend_selection, file_structure, |
12 | flip_comma, add_derive, | ||
13 | }; | 13 | }; |
14 | 14 | ||
15 | #[test] | 15 | #[test] |
@@ -103,13 +103,19 @@ impl fmt::Debug for E {} | |||
103 | 103 | ||
104 | #[test] | 104 | #[test] |
105 | fn test_swap_comma() { | 105 | fn test_swap_comma() { |
106 | check_modification( | 106 | check_action( |
107 | "fn foo(x: i32,<|> y: Result<(), ()>) {}", | 107 | "fn foo(x: i32,<|> y: Result<(), ()>) {}", |
108 | "fn foo(y: Result<(), ()>, x: i32) {}", | 108 | "fn foo(y: Result<(), ()>,<|> x: i32) {}", |
109 | &|file, offset| { | 109 | |file, off| flip_comma(file, off).map(|f| f()), |
110 | let edit = flip_comma(file, offset).unwrap()(); | 110 | ) |
111 | edit.apply(&file.syntax().text()) | 111 | } |
112 | }, | 112 | |
113 | #[test] | ||
114 | fn test_add_derive() { | ||
115 | check_action( | ||
116 | "struct Foo { a: i32, <|>}", | ||
117 | "#[derive(<|>)]\nstruct Foo { a: i32, }", | ||
118 | |file, off| add_derive(file, off).map(|f| f()), | ||
113 | ) | 119 | ) |
114 | } | 120 | } |
115 | 121 | ||
@@ -123,21 +129,36 @@ fn dbg_eq(expected: &str, actual: &impl fmt::Debug) { | |||
123 | assert_eq!(expected, actual); | 129 | assert_eq!(expected, actual); |
124 | } | 130 | } |
125 | 131 | ||
126 | fn check_modification( | 132 | fn check_action<F: Fn(&File, TextUnit) -> Option<ActionResult>>( |
127 | before: &str, | 133 | before: &str, |
128 | after: &str, | 134 | after: &str, |
129 | f: &impl Fn(&File, TextUnit) -> String, | 135 | f: F, |
130 | ) { | 136 | ) { |
137 | let (before_cursor_pos, before) = extract_cursor(before); | ||
138 | let file = file(&before); | ||
139 | let result = f(&file, before_cursor_pos).expect("code action is not applicable"); | ||
140 | let actual = result.edit.apply(&before); | ||
141 | let actual_cursor_pos: u32 = match result.cursor_position { | ||
142 | CursorPosition::Same => result.edit.apply_to_offset(before_cursor_pos).unwrap(), | ||
143 | CursorPosition::Offset(off) => off, | ||
144 | }.into(); | ||
145 | let actual_cursor_pos = actual_cursor_pos as usize; | ||
146 | let mut actual_with_cursor = String::new(); | ||
147 | actual_with_cursor.push_str(&actual[..actual_cursor_pos]); | ||
148 | actual_with_cursor.push_str("<|>"); | ||
149 | actual_with_cursor.push_str(&actual[actual_cursor_pos..]); | ||
150 | assert_eq_text!(after, &actual_with_cursor); | ||
151 | } | ||
152 | |||
153 | fn extract_cursor(text: &str) -> (TextUnit, String) { | ||
131 | let cursor = "<|>"; | 154 | let cursor = "<|>"; |
132 | let cursor_pos = match before.find(cursor) { | 155 | let cursor_pos = match text.find(cursor) { |
133 | None => panic!("before text should contain cursor marker"), | 156 | None => panic!("text should contain cursor marker"), |
134 | Some(pos) => pos, | 157 | Some(pos) => pos, |
135 | }; | 158 | }; |
136 | let mut text = String::with_capacity(before.len() - cursor.len()); | 159 | let mut new_text = String::with_capacity(text.len() - cursor.len()); |
137 | text.push_str(&before[..cursor_pos]); | 160 | new_text.push_str(&text[..cursor_pos]); |
138 | text.push_str(&before[cursor_pos + cursor.len()..]); | 161 | new_text.push_str(&text[cursor_pos + cursor.len()..]); |
139 | let cursor_pos = TextUnit::from(cursor_pos as u32); | 162 | let cursor_pos = TextUnit::from(cursor_pos as u32); |
140 | let file = file(&text); | 163 | (cursor_pos, new_text) |
141 | let actual = f(&file, cursor_pos); | ||
142 | assert_eq_text!(after, &actual); | ||
143 | } | 164 | } |