2019-10-26 11:58:18 -05:00
|
|
|
//! Assorted testing utilities.
|
|
|
|
//!
|
|
|
|
//! Most notable things are:
|
|
|
|
//!
|
|
|
|
//! * Rich text comparison, which outputs a diff.
|
|
|
|
//! * Extracting markup (mainly, `<|>` markers) out of fixture strings.
|
|
|
|
//! * marks (see the eponymous module).
|
2019-09-30 03:58:53 -05:00
|
|
|
|
2020-05-19 17:19:59 -05:00
|
|
|
#[macro_use]
|
|
|
|
pub mod mark;
|
2020-06-23 10:59:56 -05:00
|
|
|
mod fixture;
|
2019-01-23 06:36:29 -06:00
|
|
|
|
|
|
|
use std::{
|
2020-06-30 11:04:25 -05:00
|
|
|
convert::{TryFrom, TryInto},
|
2020-06-15 04:02:17 -05:00
|
|
|
env, fs,
|
2020-07-01 05:30:17 -05:00
|
|
|
path::PathBuf,
|
2019-01-23 06:36:29 -06:00
|
|
|
};
|
2018-08-17 08:04:34 -05:00
|
|
|
|
2020-06-15 04:02:17 -05:00
|
|
|
use serde_json::Value;
|
2020-06-29 07:21:57 -05:00
|
|
|
use stdx::lines_with_ends;
|
2020-06-15 04:02:17 -05:00
|
|
|
use text_size::{TextRange, TextSize};
|
2020-05-16 03:16:32 -05:00
|
|
|
|
2020-06-23 10:59:56 -05:00
|
|
|
pub use difference::Changeset as __Changeset;
|
2020-05-16 03:16:32 -05:00
|
|
|
pub use rustc_hash::FxHashMap;
|
2018-08-17 08:04:34 -05:00
|
|
|
|
2020-06-23 11:46:56 -05:00
|
|
|
pub use crate::fixture::Fixture;
|
2018-08-12 10:50:16 -05:00
|
|
|
|
2018-10-31 14:34:31 -05:00
|
|
|
pub const CURSOR_MARKER: &str = "<|>";
|
|
|
|
|
2020-01-28 19:52:13 -06:00
|
|
|
/// Asserts that two strings are equal, otherwise displays a rich diff between them.
|
|
|
|
///
|
|
|
|
/// The diff shows changes from the "original" left string to the "actual" right string.
|
|
|
|
///
|
|
|
|
/// All arguments starting from and including the 3rd one are passed to
|
|
|
|
/// `eprintln!()` macro in case of text inequality.
|
2018-08-12 10:50:16 -05:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! assert_eq_text {
|
2019-01-13 09:21:23 -06:00
|
|
|
($left:expr, $right:expr) => {
|
|
|
|
assert_eq_text!($left, $right,)
|
2018-12-21 09:13:21 -06:00
|
|
|
};
|
2019-01-13 09:21:23 -06:00
|
|
|
($left:expr, $right:expr, $($tt:tt)*) => {{
|
|
|
|
let left = $left;
|
|
|
|
let right = $right;
|
|
|
|
if left != right {
|
|
|
|
if left.trim() == right.trim() {
|
|
|
|
eprintln!("Left:\n{:?}\n\nRight:\n{:?}\n\nWhitespace difference\n", left, right);
|
2018-12-21 09:13:21 -06:00
|
|
|
} else {
|
2020-06-23 15:27:24 -05:00
|
|
|
let changeset = $crate::__Changeset::new(left, right, "\n");
|
2019-01-13 09:21:23 -06:00
|
|
|
eprintln!("Left:\n{}\n\nRight:\n{}\n\nDiff:\n{}\n", left, right, changeset);
|
2018-12-21 09:13:21 -06:00
|
|
|
}
|
|
|
|
eprintln!($($tt)*);
|
2018-08-12 10:50:16 -05:00
|
|
|
panic!("text differs");
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
2018-08-17 08:04:34 -05:00
|
|
|
|
2020-01-28 19:52:13 -06:00
|
|
|
/// Infallible version of `try_extract_offset()`.
|
2020-04-24 16:40:41 -05:00
|
|
|
pub fn extract_offset(text: &str) -> (TextSize, String) {
|
2018-10-13 14:33:15 -05:00
|
|
|
match try_extract_offset(text) {
|
2018-08-25 06:30:54 -05:00
|
|
|
None => panic!("text should contain cursor marker"),
|
2018-10-13 14:33:15 -05:00
|
|
|
Some(result) => result,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-28 19:52:13 -06:00
|
|
|
/// Returns the offset of the first occurence of `<|>` marker and the copy of `text`
|
|
|
|
/// without the marker.
|
2020-04-24 16:40:41 -05:00
|
|
|
fn try_extract_offset(text: &str) -> Option<(TextSize, String)> {
|
2018-10-31 14:34:31 -05:00
|
|
|
let cursor_pos = text.find(CURSOR_MARKER)?;
|
|
|
|
let mut new_text = String::with_capacity(text.len() - CURSOR_MARKER.len());
|
2018-08-25 06:30:54 -05:00
|
|
|
new_text.push_str(&text[..cursor_pos]);
|
2018-10-31 14:34:31 -05:00
|
|
|
new_text.push_str(&text[cursor_pos + CURSOR_MARKER.len()..]);
|
2020-04-24 16:40:41 -05:00
|
|
|
let cursor_pos = TextSize::from(cursor_pos as u32);
|
2018-10-13 14:33:15 -05:00
|
|
|
Some((cursor_pos, new_text))
|
2018-08-25 06:30:54 -05:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:52:13 -06:00
|
|
|
/// Infallible version of `try_extract_range()`.
|
2018-08-25 06:30:54 -05:00
|
|
|
pub fn extract_range(text: &str) -> (TextRange, String) {
|
2018-10-13 14:33:15 -05:00
|
|
|
match try_extract_range(text) {
|
|
|
|
None => panic!("text should contain cursor marker"),
|
|
|
|
Some(result) => result,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-28 19:52:13 -06:00
|
|
|
/// Returns `TextRange` between the first two markers `<|>...<|>` and the copy
|
|
|
|
/// of `text` without both of these markers.
|
2019-10-26 11:58:18 -05:00
|
|
|
fn try_extract_range(text: &str) -> Option<(TextRange, String)> {
|
2018-10-13 14:33:15 -05:00
|
|
|
let (start, text) = try_extract_offset(text)?;
|
|
|
|
let (end, text) = try_extract_offset(&text)?;
|
2020-04-24 16:40:41 -05:00
|
|
|
Some((TextRange::new(start, end), text))
|
2018-10-13 14:33:15 -05:00
|
|
|
}
|
|
|
|
|
2020-02-25 11:57:47 -06:00
|
|
|
#[derive(Clone, Copy)]
|
2019-10-26 11:58:18 -05:00
|
|
|
pub enum RangeOrOffset {
|
|
|
|
Range(TextRange),
|
2020-04-24 16:40:41 -05:00
|
|
|
Offset(TextSize),
|
2019-10-26 11:58:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<RangeOrOffset> for TextRange {
|
|
|
|
fn from(selection: RangeOrOffset) -> Self {
|
|
|
|
match selection {
|
|
|
|
RangeOrOffset::Range(it) => it,
|
2020-06-23 10:59:56 -05:00
|
|
|
RangeOrOffset::Offset(it) => TextRange::empty(it),
|
2019-10-26 11:58:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 16:40:41 -05:00
|
|
|
/// Extracts `TextRange` or `TextSize` depending on the amount of `<|>` markers
|
2020-01-28 19:52:13 -06:00
|
|
|
/// found in `text`.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
/// Panics if no `<|>` marker is present in the `text`.
|
2019-10-26 11:58:18 -05:00
|
|
|
pub fn extract_range_or_offset(text: &str) -> (RangeOrOffset, String) {
|
|
|
|
if let Some((range, text)) = try_extract_range(text) {
|
|
|
|
return (RangeOrOffset::Range(range), text);
|
|
|
|
}
|
|
|
|
let (offset, text) = extract_offset(text);
|
|
|
|
(RangeOrOffset::Offset(offset), text)
|
|
|
|
}
|
|
|
|
|
2020-01-28 19:52:13 -06:00
|
|
|
/// Extracts ranges, marked with `<tag> </tag>` pairs from the `text`
|
2020-07-01 11:17:08 -05:00
|
|
|
pub fn extract_tags(mut text: &str, tag: &str) -> (Vec<(TextRange, Option<String>)>, String) {
|
|
|
|
let open = format!("<{}", tag);
|
2018-12-20 13:30:30 -06:00
|
|
|
let close = format!("</{}>", tag);
|
2018-10-13 14:33:15 -05:00
|
|
|
let mut ranges = Vec::new();
|
2018-12-20 13:30:30 -06:00
|
|
|
let mut res = String::new();
|
|
|
|
let mut stack = Vec::new();
|
|
|
|
loop {
|
|
|
|
match text.find('<') {
|
|
|
|
None => {
|
|
|
|
res.push_str(text);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Some(i) => {
|
|
|
|
res.push_str(&text[..i]);
|
|
|
|
text = &text[i..];
|
|
|
|
if text.starts_with(&open) {
|
2020-07-01 11:17:08 -05:00
|
|
|
let close_open = text.find('>').unwrap();
|
|
|
|
let attr = text[open.len()..close_open].trim();
|
|
|
|
let attr = if attr.is_empty() { None } else { Some(attr.to_string()) };
|
|
|
|
text = &text[close_open + '>'.len_utf8()..];
|
2020-04-24 16:40:41 -05:00
|
|
|
let from = TextSize::of(&res);
|
2020-07-01 11:17:08 -05:00
|
|
|
stack.push((from, attr));
|
2018-12-20 13:30:30 -06:00
|
|
|
} else if text.starts_with(&close) {
|
|
|
|
text = &text[close.len()..];
|
2020-07-01 11:17:08 -05:00
|
|
|
let (from, attr) =
|
|
|
|
stack.pop().unwrap_or_else(|| panic!("unmatched </{}>", tag));
|
2020-04-24 16:40:41 -05:00
|
|
|
let to = TextSize::of(&res);
|
2020-07-01 11:17:08 -05:00
|
|
|
ranges.push((TextRange::new(from, to), attr));
|
|
|
|
} else {
|
|
|
|
res.push('<');
|
|
|
|
text = &text['<'.len_utf8()..];
|
2018-12-20 13:30:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-13 14:33:15 -05:00
|
|
|
}
|
2018-12-20 13:30:30 -06:00
|
|
|
assert!(stack.is_empty(), "unmatched <{}>", tag);
|
2020-07-01 11:17:08 -05:00
|
|
|
ranges.sort_by_key(|r| (r.0.start(), r.0.end()));
|
2018-12-20 13:30:30 -06:00
|
|
|
(ranges, res)
|
2018-08-25 06:30:54 -05:00
|
|
|
}
|
2020-07-01 11:17:08 -05:00
|
|
|
#[test]
|
|
|
|
fn test_extract_tags() {
|
|
|
|
let (tags, text) = extract_tags(r#"<tag fn>fn <tag>main</tag>() {}</tag>"#, "tag");
|
|
|
|
let actual = tags.into_iter().map(|(range, attr)| (&text[range], attr)).collect::<Vec<_>>();
|
|
|
|
assert_eq!(actual, vec![("fn main() {}", Some("fn".into())), ("main", None),]);
|
|
|
|
}
|
2018-08-25 06:30:54 -05:00
|
|
|
|
2020-01-28 19:52:13 -06:00
|
|
|
/// Inserts `<|>` marker into the `text` at `offset`.
|
2020-04-24 16:40:41 -05:00
|
|
|
pub fn add_cursor(text: &str, offset: TextSize) -> String {
|
|
|
|
let offset: usize = offset.into();
|
2018-08-25 06:30:54 -05:00
|
|
|
let mut res = String::new();
|
|
|
|
res.push_str(&text[..offset]);
|
|
|
|
res.push_str("<|>");
|
|
|
|
res.push_str(&text[offset..]);
|
|
|
|
res
|
|
|
|
}
|
2018-10-31 13:37:32 -05:00
|
|
|
|
2020-06-29 07:21:57 -05:00
|
|
|
/// Extracts `//^ some text` annotations
|
2020-06-29 10:22:47 -05:00
|
|
|
pub fn extract_annotations(text: &str) -> Vec<(TextRange, String)> {
|
2020-06-29 07:21:57 -05:00
|
|
|
let mut res = Vec::new();
|
|
|
|
let mut prev_line_start: Option<TextSize> = None;
|
|
|
|
let mut line_start: TextSize = 0.into();
|
|
|
|
for line in lines_with_ends(text) {
|
|
|
|
if let Some(idx) = line.find("//^") {
|
|
|
|
let offset = prev_line_start.unwrap() + TextSize::of(&line[..idx + "//".len()]);
|
2020-06-30 11:04:25 -05:00
|
|
|
for (line_range, text) in extract_line_annotations(&line[idx + "//".len()..]) {
|
|
|
|
res.push((line_range + offset, text))
|
|
|
|
}
|
2020-06-29 07:21:57 -05:00
|
|
|
}
|
|
|
|
prev_line_start = Some(line_start);
|
|
|
|
line_start += TextSize::of(line);
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2020-06-30 11:04:25 -05:00
|
|
|
fn extract_line_annotations(mut line: &str) -> Vec<(TextRange, String)> {
|
|
|
|
let mut res = Vec::new();
|
|
|
|
let mut offset: TextSize = 0.into();
|
|
|
|
while !line.is_empty() {
|
|
|
|
let len = line.chars().take_while(|&it| it == '^').count();
|
|
|
|
assert!(len > 0);
|
|
|
|
let range = TextRange::at(offset, len.try_into().unwrap());
|
|
|
|
let next = line[len..].find('^').map_or(line.len(), |it| it + len);
|
|
|
|
res.push((range, line[len..][..next - len].trim().to_string()));
|
|
|
|
line = &line[next..];
|
|
|
|
offset += TextSize::try_from(next).unwrap();
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2020-06-29 07:21:57 -05:00
|
|
|
#[test]
|
|
|
|
fn test_extract_annotations() {
|
2020-06-29 10:22:47 -05:00
|
|
|
let text = stdx::trim_indent(
|
2020-06-29 07:21:57 -05:00
|
|
|
r#"
|
|
|
|
fn main() {
|
2020-06-30 11:04:25 -05:00
|
|
|
let (x, y) = (9, 2);
|
|
|
|
//^ def ^ def
|
2020-06-30 05:13:08 -05:00
|
|
|
zoo + 1
|
|
|
|
} //^^^ i32
|
2020-06-29 07:21:57 -05:00
|
|
|
"#,
|
2020-06-29 10:22:47 -05:00
|
|
|
);
|
|
|
|
let res = extract_annotations(&text)
|
|
|
|
.into_iter()
|
|
|
|
.map(|(range, ann)| (&text[range], ann))
|
|
|
|
.collect::<Vec<_>>();
|
2020-06-30 11:04:25 -05:00
|
|
|
assert_eq!(res, vec![("x", "def".into()), ("y", "def".into()), ("zoo", "i32".into()),]);
|
2020-06-29 07:21:57 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 14:07:31 -06:00
|
|
|
// Comparison functionality borrowed from cargo:
|
|
|
|
|
|
|
|
/// Compare a line with an expected pattern.
|
|
|
|
/// - Use `[..]` as a wildcard to match 0 or more characters on the same line
|
|
|
|
/// (similar to `.*` in a regex).
|
|
|
|
pub fn lines_match(expected: &str, actual: &str) -> bool {
|
|
|
|
// Let's not deal with / vs \ (windows...)
|
|
|
|
// First replace backslash-escaped backslashes with forward slashes
|
|
|
|
// which can occur in, for example, JSON output
|
2019-12-18 08:33:36 -06:00
|
|
|
let expected = expected.replace(r"\\", "/").replace(r"\", "/");
|
|
|
|
let mut actual: &str = &actual.replace(r"\\", "/").replace(r"\", "/");
|
2018-12-06 14:07:31 -06:00
|
|
|
for (i, part) in expected.split("[..]").enumerate() {
|
|
|
|
match actual.find(part) {
|
|
|
|
Some(j) => {
|
|
|
|
if i == 0 && j != 0 {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
actual = &actual[j + part.len()..];
|
|
|
|
}
|
|
|
|
None => return false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
actual.is_empty() || expected.ends_with("[..]")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lines_match_works() {
|
|
|
|
assert!(lines_match("a b", "a b"));
|
|
|
|
assert!(lines_match("a[..]b", "a b"));
|
|
|
|
assert!(lines_match("a[..]", "a b"));
|
|
|
|
assert!(lines_match("[..]", "a b"));
|
|
|
|
assert!(lines_match("[..]b", "a b"));
|
|
|
|
|
|
|
|
assert!(!lines_match("[..]b", "c"));
|
|
|
|
assert!(!lines_match("b", "c"));
|
|
|
|
assert!(!lines_match("b", "cb"));
|
|
|
|
}
|
|
|
|
|
2020-01-28 19:52:13 -06:00
|
|
|
/// Compares JSON object for approximate equality.
|
|
|
|
/// You can use `[..]` wildcard in strings (useful for OS dependent things such
|
|
|
|
/// as paths). You can use a `"{...}"` string literal as a wildcard for
|
|
|
|
/// arbitrary nested JSON. Arrays are sorted before comparison.
|
2018-12-06 14:07:31 -06:00
|
|
|
pub fn find_mismatch<'a>(expected: &'a Value, actual: &'a Value) -> Option<(&'a Value, &'a Value)> {
|
|
|
|
use serde_json::Value::*;
|
|
|
|
match (expected, actual) {
|
|
|
|
(&Number(ref l), &Number(ref r)) if l == r => None,
|
|
|
|
(&Bool(l), &Bool(r)) if l == r => None,
|
|
|
|
(&String(ref l), &String(ref r)) if lines_match(l, r) => None,
|
|
|
|
(&Array(ref l), &Array(ref r)) => {
|
|
|
|
if l.len() != r.len() {
|
|
|
|
return Some((expected, actual));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut l = l.iter().collect::<Vec<_>>();
|
|
|
|
let mut r = r.iter().collect::<Vec<_>>();
|
|
|
|
|
2019-02-08 05:49:43 -06:00
|
|
|
l.retain(|l| match r.iter().position(|r| find_mismatch(l, r).is_none()) {
|
|
|
|
Some(i) => {
|
|
|
|
r.remove(i);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
None => true,
|
|
|
|
});
|
2018-12-06 14:07:31 -06:00
|
|
|
|
|
|
|
if !l.is_empty() {
|
|
|
|
assert!(!r.is_empty());
|
|
|
|
Some((&l[0], &r[0]))
|
|
|
|
} else {
|
|
|
|
assert_eq!(r.len(), 0);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(&Object(ref l), &Object(ref r)) => {
|
|
|
|
let same_keys = l.len() == r.len() && l.keys().all(|k| r.contains_key(k));
|
|
|
|
if !same_keys {
|
|
|
|
return Some((expected, actual));
|
|
|
|
}
|
|
|
|
|
2020-02-18 06:53:02 -06:00
|
|
|
l.values().zip(r.values()).filter_map(|(l, r)| find_mismatch(l, r)).next()
|
2018-12-06 14:07:31 -06:00
|
|
|
}
|
|
|
|
(&Null, &Null) => None,
|
|
|
|
// magic string literal "{...}" acts as wildcard for any sub-JSON
|
|
|
|
(&String(ref l), _) if l == "{...}" => None,
|
|
|
|
_ => Some((expected, actual)),
|
|
|
|
}
|
|
|
|
}
|
2018-12-23 05:05:54 -06:00
|
|
|
|
2020-01-28 19:52:13 -06:00
|
|
|
/// Returns `false` if slow tests should not run, otherwise returns `true` and
|
|
|
|
/// also creates a file at `./target/.slow_tests_cookie` which serves as a flag
|
|
|
|
/// that slow tests did run.
|
2019-12-07 05:46:36 -06:00
|
|
|
pub fn skip_slow_tests() -> bool {
|
|
|
|
let should_skip = std::env::var("CI").is_err() && std::env::var("RUN_SLOW_TESTS").is_err();
|
|
|
|
if should_skip {
|
|
|
|
eprintln!("ignoring slow test")
|
|
|
|
} else {
|
|
|
|
let path = project_dir().join("./target/.slow_tests_cookie");
|
|
|
|
fs::write(&path, ".").unwrap();
|
|
|
|
}
|
|
|
|
should_skip
|
|
|
|
}
|
|
|
|
|
2020-07-01 05:30:17 -05:00
|
|
|
/// Returns the path to the root directory of `rust-analyzer` project.
|
|
|
|
pub fn project_dir() -> PathBuf {
|
|
|
|
let dir = env!("CARGO_MANIFEST_DIR");
|
|
|
|
PathBuf::from(dir).parent().unwrap().parent().unwrap().to_owned()
|
2018-12-23 05:05:54 -06:00
|
|
|
}
|