1 //! Assorted testing utilities.
3 //! Most notable things are:
5 //! * Rich text comparison, which outputs a diff.
6 //! * Extracting markup (mainly, `$0` markers) out of fixture strings.
7 //! * marks (see the eponymous module).
14 collections::BTreeMap,
16 path::{Path, PathBuf},
19 use profile::StopWatch;
21 use text_size::{TextRange, TextSize};
23 pub use dissimilar::diff as __diff;
24 pub use rustc_hash::FxHashMap;
27 assert_linear::AssertLinear,
28 fixture::{Fixture, MiniCore},
31 pub const CURSOR_MARKER: &str = "$0";
32 pub const ESCAPED_CURSOR_MARKER: &str = "\\$0";
34 /// Asserts that two strings are equal, otherwise displays a rich diff between them.
36 /// The diff shows changes from the "original" left string to the "actual" right string.
38 /// All arguments starting from and including the 3rd one are passed to
39 /// `eprintln!()` macro in case of text inequality.
41 macro_rules! assert_eq_text {
42 ($left:expr, $right:expr) => {
43 assert_eq_text!($left, $right,)
45 ($left:expr, $right:expr, $($tt:tt)*) => {{
49 if left.trim() == right.trim() {
50 std::eprintln!("Left:\n{:?}\n\nRight:\n{:?}\n\nWhitespace difference\n", left, right);
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));
55 std::eprintln!($($tt)*);
56 panic!("text differs");
61 /// Infallible version of `try_extract_offset()`.
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,
69 /// Returns the offset of the first occurrence of `$0` marker and the copy of `text`
70 /// without the marker.
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))
80 /// Infallible version of `try_extract_range()`.
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,
88 /// Returns `TextRange` between the first two markers `$0...$0` and the copy
89 /// of `text` without both of these markers.
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))
96 #[derive(Clone, Copy)]
97 pub enum RangeOrOffset {
103 pub fn expect_offset(self) -> TextSize {
105 RangeOrOffset::Offset(it) => it,
106 RangeOrOffset::Range(_) => panic!("expected an offset but got a range instead"),
109 pub fn expect_range(self) -> TextRange {
111 RangeOrOffset::Range(it) => it,
112 RangeOrOffset::Offset(_) => panic!("expected a range but got an offset"),
115 pub fn range_or_empty(self) -> TextRange {
117 RangeOrOffset::Range(range) => range,
118 RangeOrOffset::Offset(offset) => TextRange::empty(offset),
123 impl From<RangeOrOffset> for TextRange {
124 fn from(selection: RangeOrOffset) -> Self {
126 RangeOrOffset::Range(it) => it,
127 RangeOrOffset::Offset(it) => TextRange::empty(it),
132 /// Extracts `TextRange` or `TextSize` depending on the amount of `$0` markers
136 /// Panics if no `$0` marker is present in the `text`.
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);
141 let (offset, text) = extract_offset(text);
142 (RangeOrOffset::Offset(offset), text)
145 /// Extracts ranges, marked with `<tag> </tag>` pairs from the `text`
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();
153 match text.find('<') {
159 res.push_str(&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()..];
171 stack.pop().unwrap_or_else(|| panic!("unmatched </{}>", tag));
172 let to = TextSize::of(&res);
173 ranges.push((TextRange::new(from, to), attr));
176 text = &text['<'.len_utf8()..];
181 assert!(stack.is_empty(), "unmatched <{}>", tag);
182 ranges.sort_by_key(|r| (r.0.start(), r.0.end()));
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),]);
192 /// Inserts `$0` marker into the `text` at `offset`.
193 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]);
198 res.push_str(&text[offset..]);
202 /// Extracts `//^^^ some text` annotations.
204 /// A run of `^^^` can be arbitrary long and points to the corresponding range
205 /// in the line above.
207 /// The `// ^file text` syntax can be used to attach `text` to the entirety of
210 /// Multiline string values are supported:
212 /// // ^^^ first line
215 /// Trailing whitespace is sometimes desired but usually stripped by the editor
216 /// if at the end of a line, or incorrectly sized if followed by another
217 /// annotation. In those cases the annotation can be explicitly ended with the
220 /// // ^^^ trailing-ws-wanted $
222 /// Annotations point to the last line that actually was long enough for the
223 /// range, not counting annotations themselves. So overlapping annotations are
226 /// // stuff other stuff
229 /// // ^^^^^^^^^^^ 'other stuff'
231 pub fn extract_annotations(text: &str) -> Vec<(TextRange, String)> {
232 let mut res = Vec::new();
233 // map from line length to beginning of last line that had that length
234 let mut line_start_map = BTreeMap::new();
235 let mut line_start: TextSize = 0.into();
236 let mut prev_line_annotations: Vec<(TextSize, usize)> = Vec::new();
237 for line in text.split_inclusive('\n') {
238 let mut this_line_annotations = Vec::new();
239 let line_length = if let Some((prefix, suffix)) = line.split_once("//") {
240 let ss_len = TextSize::of("//");
241 let annotation_offset = TextSize::of(prefix) + ss_len;
242 for annotation in extract_line_annotations(suffix.trim_end_matches('\n')) {
244 LineAnnotation::Annotation { mut range, content, file } => {
245 range += annotation_offset;
246 this_line_annotations.push((range.end(), res.len()));
247 let range = if file {
248 TextRange::up_to(TextSize::of(text))
250 let line_start = line_start_map.range(range.end()..).next().unwrap();
254 res.push((range, content));
256 LineAnnotation::Continuation { mut offset, content } => {
257 offset += annotation_offset;
258 let &(_, idx) = prev_line_annotations
260 .find(|&&(off, _idx)| off == offset)
262 res[idx].1.push('\n');
263 res[idx].1.push_str(&content);
264 res[idx].1.push('\n');
273 line_start_map = line_start_map.split_off(&line_length);
274 line_start_map.insert(line_length, line_start);
276 line_start += TextSize::of(line);
278 prev_line_annotations = this_line_annotations;
284 enum LineAnnotation {
285 Annotation { range: TextRange, content: String, file: bool },
286 Continuation { offset: TextSize, content: String },
289 fn extract_line_annotations(mut line: &str) -> Vec<LineAnnotation> {
290 let mut res = Vec::new();
291 let mut offset: TextSize = 0.into();
292 let marker: fn(char) -> bool = if line.contains('^') { |c| c == '^' } else { |c| c == '|' };
293 while let Some(idx) = line.find(marker) {
294 offset += TextSize::try_from(idx).unwrap();
297 let mut len = line.chars().take_while(|&it| it == '^').count();
298 let mut continuation = false;
300 assert!(line.starts_with('|'));
304 let range = TextRange::at(offset, len.try_into().unwrap());
305 let line_no_caret = &line[len..];
306 let end_marker = line_no_caret.find(|c| c == '$');
307 let next = line_no_caret.find(marker).map_or(line.len(), |it| it + len);
309 let cond = |end_marker| {
311 && (line_no_caret[end_marker + 1..].is_empty()
312 || line_no_caret[end_marker + 1..]
313 .strip_prefix(|c: char| c.is_whitespace() || c == '^')
316 let mut content = match end_marker {
317 Some(end_marker) if cond(end_marker) => &line_no_caret[..end_marker],
318 _ => line_no_caret[..next - len].trim_end(),
321 let mut file = false;
322 if !continuation && content.starts_with("file") {
324 content = &content["file".len()..];
327 let content = content.trim_start().to_string();
329 let annotation = if continuation {
330 LineAnnotation::Continuation { offset: range.end(), content }
332 LineAnnotation::Annotation { range, content, file }
334 res.push(annotation);
336 line = &line[next..];
337 offset += TextSize::try_from(next).unwrap();
344 fn test_extract_annotations_1() {
345 let text = stdx::trim_indent(
357 let res = extract_annotations(&text)
359 .map(|(range, ann)| (&text[range], ann))
360 .collect::<Vec<_>>();
364 [("x", "def".into()), ("y", "def".into()), ("zoo", "type:\ni32\n".into())]
366 assert_eq!(res[3].0.len(), 115);
370 fn test_extract_annotations_2() {
371 let text = stdx::trim_indent(
380 let res = extract_annotations(&text)
382 .map(|(range, ann)| (&text[range], ann))
383 .collect::<Vec<_>>();
385 assert_eq!(res, [("x", "a".into()), ("y", "b".into()), ("(x, y)", "c".into())]);
388 /// Returns `false` if slow tests should not run, otherwise returns `true` and
389 /// also creates a file at `./target/.slow_tests_cookie` which serves as a flag
390 /// that slow tests did run.
391 pub fn skip_slow_tests() -> bool {
392 let should_skip = std::env::var("CI").is_err() && std::env::var("RUN_SLOW_TESTS").is_err();
394 eprintln!("ignoring slow test");
396 let path = project_root().join("./target/.slow_tests_cookie");
397 fs::write(&path, ".").unwrap();
402 /// Returns the path to the root directory of `rust-analyzer` project.
403 pub fn project_root() -> PathBuf {
404 let dir = env!("CARGO_MANIFEST_DIR");
405 PathBuf::from(dir).parent().unwrap().parent().unwrap().to_owned()
408 pub fn format_diff(chunks: Vec<dissimilar::Chunk>) -> String {
409 let mut buf = String::new();
410 for chunk in chunks {
411 let formatted = match chunk {
412 dissimilar::Chunk::Equal(text) => text.into(),
413 dissimilar::Chunk::Delete(text) => format!("\x1b[41m{}\x1b[0m", text),
414 dissimilar::Chunk::Insert(text) => format!("\x1b[42m{}\x1b[0m", text),
416 buf.push_str(&formatted);
421 /// Utility for writing benchmark tests.
423 /// A benchmark test looks like this:
427 /// fn benchmark_foo() {
428 /// if skip_slow_tests() { return; }
430 /// let data = bench_fixture::some_fixture();
431 /// let analysis = some_setup();
434 /// let _b = bench("foo");
435 /// actual_work(analysis)
437 /// assert_eq!(hash, 92);
441 /// * We skip benchmarks by default, to save time.
442 /// Ideal benchmark time is 800 -- 1500 ms in debug.
443 /// * We don't count preparation as part of the benchmark
444 /// * The benchmark itself returns some kind of numeric hash.
445 /// The hash is used as a sanity check that some code is actually run.
446 /// Otherwise, it's too easy to win the benchmark by just doing nothing.
447 pub fn bench(label: &'static str) -> impl Drop {
453 impl Drop for Bencher {
455 eprintln!("{}: {}", self.label, self.sw.elapsed());
459 Bencher { sw: StopWatch::start(), label }
462 /// Checks that the `file` has the specified `contents`. If that is not the
463 /// case, updates the file and then fails the test.
465 pub fn ensure_file_contents(file: &Path, contents: &str) {
466 if let Err(()) = try_ensure_file_contents(file, contents) {
467 panic!("Some files were not up-to-date");
471 /// Checks that the `file` has the specified `contents`. If that is not the
472 /// case, updates the file and return an Error.
473 pub fn try_ensure_file_contents(file: &Path, contents: &str) -> Result<(), ()> {
474 match std::fs::read_to_string(file) {
475 Ok(old_contents) if normalize_newlines(&old_contents) == normalize_newlines(contents) => {
480 let display_path = file.strip_prefix(&project_root()).unwrap_or(file);
482 "\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n",
483 display_path.display()
486 eprintln!(" NOTE: run `cargo test` locally and commit the updated files\n");
488 if let Some(parent) = file.parent() {
489 let _ = std::fs::create_dir_all(parent);
491 std::fs::write(file, contents).unwrap();
495 fn normalize_newlines(s: &str) -> String {
496 s.replace("\r\n", "\n")