aboutsummaryrefslogtreecommitdiff
path: root/crates/test_utils
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-06-30 17:04:25 +0100
committerAleksey Kladov <[email protected]>2020-06-30 17:04:25 +0100
commit442c13ba176a40491deb7f9d2a2e1e24eca29f63 (patch)
treeb505a45efca93c959720ae0aecca78eca0f2ad62 /crates/test_utils
parent0954d31beeb924510769fe8e201386a7cc3621f8 (diff)
Simplify most of the inlay hints tests
Diffstat (limited to 'crates/test_utils')
-rw-r--r--crates/test_utils/src/lib.rs30
1 files changed, 22 insertions, 8 deletions
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs
index f9d6c6c96..e32a0a0c3 100644
--- a/crates/test_utils/src/lib.rs
+++ b/crates/test_utils/src/lib.rs
@@ -11,7 +11,7 @@ pub mod mark;
11mod fixture; 11mod fixture;
12 12
13use std::{ 13use std::{
14 convert::TryInto, 14 convert::{TryFrom, TryInto},
15 env, fs, 15 env, fs,
16 path::{Path, PathBuf}, 16 path::{Path, PathBuf},
17}; 17};
@@ -169,10 +169,9 @@ pub fn extract_annotations(text: &str) -> Vec<(TextRange, String)> {
169 for line in lines_with_ends(text) { 169 for line in lines_with_ends(text) {
170 if let Some(idx) = line.find("//^") { 170 if let Some(idx) = line.find("//^") {
171 let offset = prev_line_start.unwrap() + TextSize::of(&line[..idx + "//".len()]); 171 let offset = prev_line_start.unwrap() + TextSize::of(&line[..idx + "//".len()]);
172 let marker_and_data = &line[idx + "//".len()..]; 172 for (line_range, text) in extract_line_annotations(&line[idx + "//".len()..]) {
173 let len = marker_and_data.chars().take_while(|&it| it == '^').count(); 173 res.push((line_range + offset, text))
174 let data = marker_and_data[len..].trim().to_string(); 174 }
175 res.push((TextRange::at(offset, len.try_into().unwrap()), data))
176 } 175 }
177 prev_line_start = Some(line_start); 176 prev_line_start = Some(line_start);
178 line_start += TextSize::of(line); 177 line_start += TextSize::of(line);
@@ -180,13 +179,28 @@ pub fn extract_annotations(text: &str) -> Vec<(TextRange, String)> {
180 res 179 res
181} 180}
182 181
182fn extract_line_annotations(mut line: &str) -> Vec<(TextRange, String)> {
183 let mut res = Vec::new();
184 let mut offset: TextSize = 0.into();
185 while !line.is_empty() {
186 let len = line.chars().take_while(|&it| it == '^').count();
187 assert!(len > 0);
188 let range = TextRange::at(offset, len.try_into().unwrap());
189 let next = line[len..].find('^').map_or(line.len(), |it| it + len);
190 res.push((range, line[len..][..next - len].trim().to_string()));
191 line = &line[next..];
192 offset += TextSize::try_from(next).unwrap();
193 }
194 res
195}
196
183#[test] 197#[test]
184fn test_extract_annotations() { 198fn test_extract_annotations() {
185 let text = stdx::trim_indent( 199 let text = stdx::trim_indent(
186 r#" 200 r#"
187fn main() { 201fn main() {
188 let x = 92; 202 let (x, y) = (9, 2);
189 //^ def 203 //^ def ^ def
190 zoo + 1 204 zoo + 1
191} //^^^ i32 205} //^^^ i32
192 "#, 206 "#,
@@ -195,7 +209,7 @@ fn main() {
195 .into_iter() 209 .into_iter()
196 .map(|(range, ann)| (&text[range], ann)) 210 .map(|(range, ann)| (&text[range], ann))
197 .collect::<Vec<_>>(); 211 .collect::<Vec<_>>();
198 assert_eq!(res, vec![("x", "def".into()), ("zoo", "i32".into()),]); 212 assert_eq!(res, vec![("x", "def".into()), ("y", "def".into()), ("zoo", "i32".into()),]);
199} 213}
200 214
201// Comparison functionality borrowed from cargo: 215// Comparison functionality borrowed from cargo: