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