1 //! Assorted testing utilities.
2 //!
3 //! Most notable things are:
4 //!
5 //! * Rich text comparison, which outputs a diff.
6 //! * Extracting markup (mainly, `$0` markers) out of fixture strings.
7 //! * marks (see the eponymous module).
8
9 pub mod bench_fixture;
10 mod fixture;
11 mod assert_linear;
12
13 use std::{
14 collections::BTreeMap,
15 env, fs,
16 path::{Path, PathBuf},
17 };
18
19 use profile::StopWatch;
20 use stdx::is_ci;
21 use text_size::{TextRange, TextSize};
22
23 pub use dissimilar::diff as __diff;
24 pub use rustc_hash::FxHashMap;
25
26 pub use crate::{
27 assert_linear::AssertLinear,
28 fixture::{Fixture, MiniCore},
29 };
30
31 pub const CURSOR_MARKER: &str = "$0";
32 pub const ESCAPED_CURSOR_MARKER: &str = "\\$0";
33
34 /// Asserts that two strings are equal, otherwise displays a rich diff between them.
35 ///
36 /// The diff shows changes from the "original" left string to the "actual" right string.
37 ///
38 /// All arguments starting from and including the 3rd one are passed to
39 /// `eprintln!()` macro in case of text inequality.
40 #[macro_export]
41 macro_rules! assert_eq_text {
42 ($left:expr, $right:expr) => {
43 assert_eq_text!($left, $right,)
44 };
45 ($left:expr, $right:expr, $($tt:tt)*) => {{
46 let left = $left;
47 let right = $right;
48 if left != right {
49 if left.trim() == right.trim() {
50 std::eprintln!("Left:\n{:?}\n\nRight:\n{:?}\n\nWhitespace difference\n", left, right);
51 } else {
52 let diff = $crate::__diff(left, right);
53 std::eprintln!("Left:\n{}\n\nRight:\n{}\n\nDiff:\n{}\n", left, right, $crate::format_diff(diff));
54 }
55 std::eprintln!($($tt)*);
56 panic!("text differs");
57 }
58 }};
59 }
60
61 /// Infallible version of `try_extract_offset()`.
extract_offset(text: &str) -> (TextSize, String)62 pub fn extract_offset(text: &str) -> (TextSize, String) {
63 match try_extract_offset(text) {
64 None => panic!("text should contain cursor marker"),
65 Some(result) => result,
66 }
67 }
68
69 /// Returns the offset of the first occurrence of `$0` marker and the copy of `text`
70 /// without the marker.
try_extract_offset(text: &str) -> Option<(TextSize, String)>71 fn try_extract_offset(text: &str) -> Option<(TextSize, String)> {
72 let cursor_pos = text.find(CURSOR_MARKER)?;
73 let mut new_text = String::with_capacity(text.len() - CURSOR_MARKER.len());
74 new_text.push_str(&text[..cursor_pos]);
75 new_text.push_str(&text[cursor_pos + CURSOR_MARKER.len()..]);
76 let cursor_pos = TextSize::from(cursor_pos as u32);
77 Some((cursor_pos, new_text))
78 }
79
80 /// Infallible version of `try_extract_range()`.
extract_range(text: &str) -> (TextRange, String)81 pub fn extract_range(text: &str) -> (TextRange, String) {
82 match try_extract_range(text) {
83 None => panic!("text should contain cursor marker"),
84 Some(result) => result,
85 }
86 }
87
88 /// Returns `TextRange` between the first two markers `$0...$0` and the copy
89 /// of `text` without both of these markers.
try_extract_range(text: &str) -> Option<(TextRange, String)>90 fn try_extract_range(text: &str) -> Option<(TextRange, String)> {
91 let (start, text) = try_extract_offset(text)?;
92 let (end, text) = try_extract_offset(&text)?;
93 Some((TextRange::new(start, end), text))
94 }
95
96 #[derive(Clone, Copy)]
97 pub enum RangeOrOffset {
98 Range(TextRange),
99 Offset(TextSize),
100 }
101
102 impl RangeOrOffset {
expect_offset(self) -> TextSize103 pub fn expect_offset(self) -> TextSize {
104 match self {
105 RangeOrOffset::Offset(it) => it,
106 RangeOrOffset::Range(_) => panic!("expected an offset but got a range instead"),
107 }
108 }
expect_range(self) -> TextRange109 pub fn expect_range(self) -> TextRange {
110 match self {
111 RangeOrOffset::Range(it) => it,
112 RangeOrOffset::Offset(_) => panic!("expected a range but got an offset"),
113 }
114 }
range_or_empty(self) -> TextRange115 pub fn range_or_empty(self) -> TextRange {
116 match self {
117 RangeOrOffset::Range(range) => range,
118 RangeOrOffset::Offset(offset) => TextRange::empty(offset),
119 }
120 }
121 }
122
123 impl From<RangeOrOffset> for TextRange {
from(selection: RangeOrOffset) -> Self124 fn from(selection: RangeOrOffset) -> Self {
125 match selection {
126 RangeOrOffset::Range(it) => it,
127 RangeOrOffset::Offset(it) => TextRange::empty(it),
128 }
129 }
130 }
131
132 /// Extracts `TextRange` or `TextSize` depending on the amount of `$0` markers
133 /// found in `text`.
134 ///
135 /// # Panics
136 /// Panics if no `$0` marker is present in the `text`.
extract_range_or_offset(text: &str) -> (RangeOrOffset, String)137 pub fn extract_range_or_offset(text: &str) -> (RangeOrOffset, String) {
138 if let Some((range, text)) = try_extract_range(text) {
139 return (RangeOrOffset::Range(range), text);
140 }
141 let (offset, text) = extract_offset(text);
142 (RangeOrOffset::Offset(offset), text)
143 }
144
145 /// Extracts ranges, marked with `<tag> </tag>` pairs from the `text`
extract_tags(mut text: &str, tag: &str) -> (Vec<(TextRange, Option<String>)>, String)146 pub fn extract_tags(mut text: &str, tag: &str) -> (Vec<(TextRange, Option<String>)>, String) {
147 let open = format!("<{}", tag);
148 let close = format!("</{}>", tag);
149 let mut ranges = Vec::new();
150 let mut res = String::new();
151 let mut stack = Vec::new();
152 loop {
153 match text.find('<') {
154 None => {
155 res.push_str(text);
156 break;
157 }
158 Some(i) => {
159 res.push_str(&text[..i]);
160 text = &text[i..];
161 if text.starts_with(&open) {
162 let close_open = text.find('>').unwrap();
163 let attr = text[open.len()..close_open].trim();
164 let attr = if attr.is_empty() { None } else { Some(attr.to_string()) };
165 text = &text[close_open + '>'.len_utf8()..];
166 let from = TextSize::of(&res);
167 stack.push((from, attr));
168 } else if text.starts_with(&close) {
169 text = &text[close.len()..];
170 let (from, attr) =
171 stack.pop().unwrap_or_else(|| panic!("unmatched </{}>", tag));
172 let to = TextSize::of(&res);
173 ranges.push((TextRange::new(from, to), attr));
174 } else {
175 res.push('<');
176 text = &text['<'.len_utf8()..];
177 }
178 }
179 }
180 }
181 assert!(stack.is_empty(), "unmatched <{}>", tag);
182 ranges.sort_by_key(|r| (r.0.start(), r.0.end()));
183 (ranges, res)
184 }
185 #[test]
test_extract_tags()186 fn test_extract_tags() {
187 let (tags, text) = extract_tags(r#"<tag fn>fn <tag>main</tag>() {}</tag>"#, "tag");
188 let actual = tags.into_iter().map(|(range, attr)| (&text[range], attr)).collect::<Vec<_>>();
189 assert_eq!(actual, vec![("fn main() {}", Some("fn".into())), ("main", None),]);
190 }
191
192 /// Inserts `$0` marker into the `text` at `offset`.
add_cursor(text: &str, offset: TextSize) -> String193 pub fn add_cursor(text: &str, offset: TextSize) -> String {
194 let offset: usize = offset.into();
195 let mut res = String::new();
196 res.push_str(&text[..offset]);
197 res.push_str("$0");
198 res.push_str(&text[offset..]);
199 res
200 }
201
202 /// Extracts `//^^^ some text` annotations.
203 ///
204 /// A run of `^^^` can be arbitrary long and points to the corresponding range
205 /// in the line above.
206 ///
207 /// The `// ^file text` syntax can be used to attach `text` to the entirety of
208 /// the file.
209 ///
210 /// Multiline string values are supported:
211 ///
212 /// // ^^^ first line
213 /// // | second line
214 ///
215 /// Annotations point to the last line that actually was long enough for the
216 /// range, not counting annotations themselves. So overlapping annotations are
217 /// possible:
218 /// ```no_run
219 /// // stuff other stuff
220 /// // ^^ 'st'
221 /// // ^^^^^ 'stuff'
222 /// // ^^^^^^^^^^^ 'other stuff'
223 /// ```
extract_annotations(text: &str) -> Vec<(TextRange, String)>224 pub fn extract_annotations(text: &str) -> Vec<(TextRange, String)> {
225 let mut res = Vec::new();
226 // map from line length to beginning of last line that had that length
227 let mut line_start_map = BTreeMap::new();
228 let mut line_start: TextSize = 0.into();
229 let mut prev_line_annotations: Vec<(TextSize, usize)> = Vec::new();
230 for line in text.split_inclusive('\n') {
231 let mut this_line_annotations = Vec::new();
232 let line_length = if let Some(idx) = line.find("//") {
233 let annotation_offset = TextSize::of(&line[..idx + "//".len()]);
234 for annotation in extract_line_annotations(&line[idx + "//".len()..]) {
235 match annotation {
236 LineAnnotation::Annotation { mut range, content, file } => {
237 range += annotation_offset;
238 this_line_annotations.push((range.end(), res.len()));
239 let range = if file {
240 TextRange::up_to(TextSize::of(text))
241 } else {
242 let line_start = line_start_map.range(range.end()..).next().unwrap();
243
244 range + line_start.1
245 };
246 res.push((range, content));
247 }
248 LineAnnotation::Continuation { mut offset, content } => {
249 offset += annotation_offset;
250 let &(_, idx) = prev_line_annotations
251 .iter()
252 .find(|&&(off, _idx)| off == offset)
253 .unwrap();
254 res[idx].1.push('\n');
255 res[idx].1.push_str(&content);
256 res[idx].1.push('\n');
257 }
258 }
259 }
260 idx.try_into().unwrap()
261 } else {
262 TextSize::of(line)
263 };
264
265 line_start_map = line_start_map.split_off(&line_length);
266 line_start_map.insert(line_length, line_start);
267
268 line_start += TextSize::of(line);
269
270 prev_line_annotations = this_line_annotations;
271 }
272
273 res
274 }
275
276 enum LineAnnotation {
277 Annotation { range: TextRange, content: String, file: bool },
278 Continuation { offset: TextSize, content: String },
279 }
280
extract_line_annotations(mut line: &str) -> Vec<LineAnnotation>281 fn extract_line_annotations(mut line: &str) -> Vec<LineAnnotation> {
282 let mut res = Vec::new();
283 let mut offset: TextSize = 0.into();
284 let marker: fn(char) -> bool = if line.contains('^') { |c| c == '^' } else { |c| c == '|' };
285 while let Some(idx) = line.find(marker) {
286 offset += TextSize::try_from(idx).unwrap();
287 line = &line[idx..];
288
289 let mut len = line.chars().take_while(|&it| it == '^').count();
290 let mut continuation = false;
291 if len == 0 {
292 assert!(line.starts_with('|'));
293 continuation = true;
294 len = 1;
295 }
296 let range = TextRange::at(offset, len.try_into().unwrap());
297 let next = line[len..].find(marker).map_or(line.len(), |it| it + len);
298 let mut content = &line[len..][..next - len];
299
300 let mut file = false;
301 if !continuation && content.starts_with("file") {
302 file = true;
303 content = &content["file".len()..];
304 }
305
306 let content = content.trim().to_string();
307
308 let annotation = if continuation {
309 LineAnnotation::Continuation { offset: range.end(), content }
310 } else {
311 LineAnnotation::Annotation { range, content, file }
312 };
313 res.push(annotation);
314
315 line = &line[next..];
316 offset += TextSize::try_from(next).unwrap();
317 }
318
319 res
320 }
321
322 #[test]
test_extract_annotations_1()323 fn test_extract_annotations_1() {
324 let text = stdx::trim_indent(
325 r#"
326 fn main() {
327 let (x, y) = (9, 2);
328 //^ def ^ def
329 zoo + 1
330 } //^^^ type:
331 // | i32
332
333 // ^file
334 "#,
335 );
336 let res = extract_annotations(&text)
337 .into_iter()
338 .map(|(range, ann)| (&text[range], ann))
339 .collect::<Vec<_>>();
340
341 assert_eq!(
342 res[..3],
343 [("x", "def".into()), ("y", "def".into()), ("zoo", "type:\ni32\n".into())]
344 );
345 assert_eq!(res[3].0.len(), 115);
346 }
347
348 #[test]
test_extract_annotations_2()349 fn test_extract_annotations_2() {
350 let text = stdx::trim_indent(
351 r#"
352 fn main() {
353 (x, y);
354 //^ a
355 // ^ b
356 //^^^^^^^^ c
357 }"#,
358 );
359 let res = extract_annotations(&text)
360 .into_iter()
361 .map(|(range, ann)| (&text[range], ann))
362 .collect::<Vec<_>>();
363
364 assert_eq!(res, [("x", "a".into()), ("y", "b".into()), ("(x, y)", "c".into())]);
365 }
366
367 /// Returns `false` if slow tests should not run, otherwise returns `true` and
368 /// also creates a file at `./target/.slow_tests_cookie` which serves as a flag
369 /// that slow tests did run.
skip_slow_tests() -> bool370 pub fn skip_slow_tests() -> bool {
371 let should_skip = std::env::var("CI").is_err() && std::env::var("RUN_SLOW_TESTS").is_err();
372 if should_skip {
373 eprintln!("ignoring slow test");
374 } else {
375 let path = project_root().join("./target/.slow_tests_cookie");
376 fs::write(&path, ".").unwrap();
377 }
378 should_skip
379 }
380
381 /// Returns the path to the root directory of `rust-analyzer` project.
project_root() -> PathBuf382 pub fn project_root() -> PathBuf {
383 let dir = env!("CARGO_MANIFEST_DIR");
384 PathBuf::from(dir).parent().unwrap().parent().unwrap().to_owned()
385 }
386
format_diff(chunks: Vec<dissimilar::Chunk>) -> String387 pub fn format_diff(chunks: Vec<dissimilar::Chunk>) -> String {
388 let mut buf = String::new();
389 for chunk in chunks {
390 let formatted = match chunk {
391 dissimilar::Chunk::Equal(text) => text.into(),
392 dissimilar::Chunk::Delete(text) => format!("\x1b[41m{}\x1b[0m", text),
393 dissimilar::Chunk::Insert(text) => format!("\x1b[42m{}\x1b[0m", text),
394 };
395 buf.push_str(&formatted);
396 }
397 buf
398 }
399
400 /// Utility for writing benchmark tests.
401 ///
402 /// A benchmark test looks like this:
403 ///
404 /// ```
405 /// #[test]
406 /// fn benchmark_foo() {
407 /// if skip_slow_tests() { return; }
408 ///
409 /// let data = bench_fixture::some_fixture();
410 /// let analysis = some_setup();
411 ///
412 /// let hash = {
413 /// let _b = bench("foo");
414 /// actual_work(analysis)
415 /// };
416 /// assert_eq!(hash, 92);
417 /// }
418 /// ```
419 ///
420 /// * We skip benchmarks by default, to save time.
421 /// Ideal benchmark time is 800 -- 1500 ms in debug.
422 /// * We don't count preparation as part of the benchmark
423 /// * The benchmark itself returns some kind of numeric hash.
424 /// The hash is used as a sanity check that some code is actually run.
425 /// Otherwise, it's too easy to win the benchmark by just doing nothing.
bench(label: &'static str) -> impl Drop426 pub fn bench(label: &'static str) -> impl Drop {
427 struct Bencher {
428 sw: StopWatch,
429 label: &'static str,
430 }
431
432 impl Drop for Bencher {
433 fn drop(&mut self) {
434 eprintln!("{}: {}", self.label, self.sw.elapsed());
435 }
436 }
437
438 Bencher { sw: StopWatch::start(), label }
439 }
440
441 /// Checks that the `file` has the specified `contents`. If that is not the
442 /// case, updates the file and then fails the test.
ensure_file_contents(file: &Path, contents: &str)443 pub fn ensure_file_contents(file: &Path, contents: &str) {
444 if let Err(()) = try_ensure_file_contents(file, contents) {
445 panic!("Some files were not up-to-date");
446 }
447 }
448
449 /// Checks that the `file` has the specified `contents`. If that is not the
450 /// case, updates the file and return an Error.
try_ensure_file_contents(file: &Path, contents: &str) -> Result<(), ()>451 pub fn try_ensure_file_contents(file: &Path, contents: &str) -> Result<(), ()> {
452 match std::fs::read_to_string(file) {
453 Ok(old_contents) if normalize_newlines(&old_contents) == normalize_newlines(contents) => {
454 return Ok(())
455 }
456 _ => (),
457 }
458 let display_path = file.strip_prefix(&project_root()).unwrap_or(file);
459 eprintln!(
460 "\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n",
461 display_path.display()
462 );
463 if is_ci() {
464 eprintln!(" NOTE: run `cargo test` locally and commit the updated files\n");
465 }
466 if let Some(parent) = file.parent() {
467 let _ = std::fs::create_dir_all(parent);
468 }
469 std::fs::write(file, contents).unwrap();
470 Err(())
471 }
472
normalize_newlines(s: &str) -> String473 fn normalize_newlines(s: &str) -> String {
474 s.replace("\r\n", "\n")
475 }
476