aboutsummaryrefslogtreecommitdiff
path: root/crates/test_utils/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/test_utils/src/lib.rs')
-rw-r--r--crates/test_utils/src/lib.rs36
1 files changed, 25 insertions, 11 deletions
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs
index 05940a546..e19d2ad61 100644
--- a/crates/test_utils/src/lib.rs
+++ b/crates/test_utils/src/lib.rs
@@ -3,7 +3,7 @@
3//! Most notable things are: 3//! Most notable things are:
4//! 4//!
5//! * Rich text comparison, which outputs a diff. 5//! * Rich text comparison, which outputs a diff.
6//! * Extracting markup (mainly, `<|>` markers) out of fixture strings. 6//! * Extracting markup (mainly, `$0` markers) out of fixture strings.
7//! * marks (see the eponymous module). 7//! * marks (see the eponymous module).
8 8
9#[macro_use] 9#[macro_use]
@@ -20,12 +20,13 @@ use serde_json::Value;
20use stdx::lines_with_ends; 20use stdx::lines_with_ends;
21use text_size::{TextRange, TextSize}; 21use text_size::{TextRange, TextSize};
22 22
23pub use difference::Changeset as __Changeset; 23pub use dissimilar::diff as __diff;
24pub use rustc_hash::FxHashMap; 24pub use rustc_hash::FxHashMap;
25 25
26pub use crate::fixture::Fixture; 26pub use crate::fixture::Fixture;
27 27
28pub const CURSOR_MARKER: &str = "<|>"; 28pub const CURSOR_MARKER: &str = "$0";
29pub const ESCAPED_CURSOR_MARKER: &str = "\\$0";
29 30
30/// Asserts that two strings are equal, otherwise displays a rich diff between them. 31/// Asserts that two strings are equal, otherwise displays a rich diff between them.
31/// 32///
@@ -45,8 +46,8 @@ macro_rules! assert_eq_text {
45 if left.trim() == right.trim() { 46 if left.trim() == right.trim() {
46 std::eprintln!("Left:\n{:?}\n\nRight:\n{:?}\n\nWhitespace difference\n", left, right); 47 std::eprintln!("Left:\n{:?}\n\nRight:\n{:?}\n\nWhitespace difference\n", left, right);
47 } else { 48 } else {
48 let changeset = $crate::__Changeset::new(left, right, "\n"); 49 let diff = $crate::__diff(left, right);
49 std::eprintln!("Left:\n{}\n\nRight:\n{}\n\nDiff:\n{}\n", left, right, changeset); 50 std::eprintln!("Left:\n{}\n\nRight:\n{}\n\nDiff:\n{}\n", left, right, $crate::format_diff(diff));
50 } 51 }
51 std::eprintln!($($tt)*); 52 std::eprintln!($($tt)*);
52 panic!("text differs"); 53 panic!("text differs");
@@ -62,7 +63,7 @@ pub fn extract_offset(text: &str) -> (TextSize, String) {
62 } 63 }
63} 64}
64 65
65/// Returns the offset of the first occurence of `<|>` marker and the copy of `text` 66/// Returns the offset of the first occurrence of `$0` marker and the copy of `text`
66/// without the marker. 67/// without the marker.
67fn try_extract_offset(text: &str) -> Option<(TextSize, String)> { 68fn try_extract_offset(text: &str) -> Option<(TextSize, String)> {
68 let cursor_pos = text.find(CURSOR_MARKER)?; 69 let cursor_pos = text.find(CURSOR_MARKER)?;
@@ -81,7 +82,7 @@ pub fn extract_range(text: &str) -> (TextRange, String) {
81 } 82 }
82} 83}
83 84
84/// Returns `TextRange` between the first two markers `<|>...<|>` and the copy 85/// Returns `TextRange` between the first two markers `$0...$0` and the copy
85/// of `text` without both of these markers. 86/// of `text` without both of these markers.
86fn try_extract_range(text: &str) -> Option<(TextRange, String)> { 87fn try_extract_range(text: &str) -> Option<(TextRange, String)> {
87 let (start, text) = try_extract_offset(text)?; 88 let (start, text) = try_extract_offset(text)?;
@@ -104,11 +105,11 @@ impl From<RangeOrOffset> for TextRange {
104 } 105 }
105} 106}
106 107
107/// Extracts `TextRange` or `TextSize` depending on the amount of `<|>` markers 108/// Extracts `TextRange` or `TextSize` depending on the amount of `$0` markers
108/// found in `text`. 109/// found in `text`.
109/// 110///
110/// # Panics 111/// # Panics
111/// Panics if no `<|>` marker is present in the `text`. 112/// Panics if no `$0` marker is present in the `text`.
112pub fn extract_range_or_offset(text: &str) -> (RangeOrOffset, String) { 113pub fn extract_range_or_offset(text: &str) -> (RangeOrOffset, String) {
113 if let Some((range, text)) = try_extract_range(text) { 114 if let Some((range, text)) = try_extract_range(text) {
114 return (RangeOrOffset::Range(range), text); 115 return (RangeOrOffset::Range(range), text);
@@ -164,12 +165,12 @@ fn test_extract_tags() {
164 assert_eq!(actual, vec![("fn main() {}", Some("fn".into())), ("main", None),]); 165 assert_eq!(actual, vec![("fn main() {}", Some("fn".into())), ("main", None),]);
165} 166}
166 167
167/// Inserts `<|>` marker into the `text` at `offset`. 168/// Inserts `$0` marker into the `text` at `offset`.
168pub fn add_cursor(text: &str, offset: TextSize) -> String { 169pub fn add_cursor(text: &str, offset: TextSize) -> String {
169 let offset: usize = offset.into(); 170 let offset: usize = offset.into();
170 let mut res = String::new(); 171 let mut res = String::new();
171 res.push_str(&text[..offset]); 172 res.push_str(&text[..offset]);
172 res.push_str("<|>"); 173 res.push_str("$0");
173 res.push_str(&text[offset..]); 174 res.push_str(&text[offset..]);
174 res 175 res
175} 176}
@@ -392,3 +393,16 @@ pub fn project_dir() -> PathBuf {
392 let dir = env!("CARGO_MANIFEST_DIR"); 393 let dir = env!("CARGO_MANIFEST_DIR");
393 PathBuf::from(dir).parent().unwrap().parent().unwrap().to_owned() 394 PathBuf::from(dir).parent().unwrap().parent().unwrap().to_owned()
394} 395}
396
397pub fn format_diff(chunks: Vec<dissimilar::Chunk>) -> String {
398 let mut buf = String::new();
399 for chunk in chunks {
400 let formatted = match chunk {
401 dissimilar::Chunk::Equal(text) => text.into(),
402 dissimilar::Chunk::Delete(text) => format!("\x1b[41m{}\x1b[0m", text),
403 dissimilar::Chunk::Insert(text) => format!("\x1b[42m{}\x1b[0m", text),
404 };
405 buf.push_str(&formatted);
406 }
407 buf
408}