diff options
Diffstat (limited to 'crates/expect/src')
-rw-r--r-- | crates/expect/src/lib.rs | 356 |
1 files changed, 356 insertions, 0 deletions
diff --git a/crates/expect/src/lib.rs b/crates/expect/src/lib.rs new file mode 100644 index 000000000..21a458d47 --- /dev/null +++ b/crates/expect/src/lib.rs | |||
@@ -0,0 +1,356 @@ | |||
1 | //! Snapshot testing library, see | ||
2 | //! https://github.com/rust-analyzer/rust-analyzer/pull/5101 | ||
3 | use std::{ | ||
4 | collections::HashMap, | ||
5 | env, fmt, fs, mem, | ||
6 | ops::Range, | ||
7 | panic, | ||
8 | path::{Path, PathBuf}, | ||
9 | sync::Mutex, | ||
10 | }; | ||
11 | |||
12 | use difference::Changeset; | ||
13 | use once_cell::sync::Lazy; | ||
14 | use stdx::{lines_with_ends, trim_indent}; | ||
15 | |||
16 | const HELP: &str = " | ||
17 | You can update all `expect![[]]` tests by running: | ||
18 | |||
19 | env UPDATE_EXPECT=1 cargo test | ||
20 | |||
21 | To update a single test, place the cursor on `expect` token and use `run` feature of rust-analyzer. | ||
22 | "; | ||
23 | |||
24 | fn update_expect() -> bool { | ||
25 | env::var("UPDATE_EXPECT").is_ok() | ||
26 | } | ||
27 | |||
28 | /// expect![[r#"inline snapshot"#]] | ||
29 | #[macro_export] | ||
30 | macro_rules! expect { | ||
31 | [[$data:literal]] => {$crate::Expect { | ||
32 | position: $crate::Position { | ||
33 | file: file!(), | ||
34 | line: line!(), | ||
35 | column: column!(), | ||
36 | }, | ||
37 | data: $data, | ||
38 | }}; | ||
39 | [[]] => { $crate::expect![[""]] }; | ||
40 | } | ||
41 | |||
42 | /// expect_file!["/crates/foo/test_data/bar.html"] | ||
43 | #[macro_export] | ||
44 | macro_rules! expect_file { | ||
45 | [$path:expr] => {$crate::ExpectFile { | ||
46 | path: std::path::PathBuf::from($path) | ||
47 | }}; | ||
48 | } | ||
49 | |||
50 | #[derive(Debug)] | ||
51 | pub struct Expect { | ||
52 | pub position: Position, | ||
53 | pub data: &'static str, | ||
54 | } | ||
55 | |||
56 | #[derive(Debug)] | ||
57 | pub struct ExpectFile { | ||
58 | pub path: PathBuf, | ||
59 | } | ||
60 | |||
61 | #[derive(Debug)] | ||
62 | pub struct Position { | ||
63 | pub file: &'static str, | ||
64 | pub line: u32, | ||
65 | pub column: u32, | ||
66 | } | ||
67 | |||
68 | impl fmt::Display for Position { | ||
69 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
70 | write!(f, "{}:{}:{}", self.file, self.line, self.column) | ||
71 | } | ||
72 | } | ||
73 | |||
74 | impl Expect { | ||
75 | pub fn assert_eq(&self, actual: &str) { | ||
76 | let trimmed = self.trimmed(); | ||
77 | if &trimmed == actual { | ||
78 | return; | ||
79 | } | ||
80 | Runtime::fail_expect(self, &trimmed, actual); | ||
81 | } | ||
82 | pub fn assert_debug_eq(&self, actual: &impl fmt::Debug) { | ||
83 | let actual = format!("{:#?}\n", actual); | ||
84 | self.assert_eq(&actual) | ||
85 | } | ||
86 | |||
87 | fn trimmed(&self) -> String { | ||
88 | if !self.data.contains('\n') { | ||
89 | return self.data.to_string(); | ||
90 | } | ||
91 | trim_indent(self.data) | ||
92 | } | ||
93 | |||
94 | fn locate(&self, file: &str) -> Location { | ||
95 | let mut target_line = None; | ||
96 | let mut line_start = 0; | ||
97 | for (i, line) in lines_with_ends(file).enumerate() { | ||
98 | if i == self.position.line as usize - 1 { | ||
99 | let pat = "expect![["; | ||
100 | let offset = line.find(pat).unwrap(); | ||
101 | let literal_start = line_start + offset + pat.len(); | ||
102 | let indent = line.chars().take_while(|&it| it == ' ').count(); | ||
103 | target_line = Some((literal_start, indent)); | ||
104 | break; | ||
105 | } | ||
106 | line_start += line.len(); | ||
107 | } | ||
108 | let (literal_start, line_indent) = target_line.unwrap(); | ||
109 | let literal_length = | ||
110 | file[literal_start..].find("]]").expect("Couldn't find matching `]]` for `expect![[`."); | ||
111 | let literal_range = literal_start..literal_start + literal_length; | ||
112 | Location { line_indent, literal_range } | ||
113 | } | ||
114 | } | ||
115 | |||
116 | impl ExpectFile { | ||
117 | pub fn assert_eq(&self, actual: &str) { | ||
118 | let expected = self.read(); | ||
119 | if actual == expected { | ||
120 | return; | ||
121 | } | ||
122 | Runtime::fail_file(self, &expected, actual); | ||
123 | } | ||
124 | pub fn assert_debug_eq(&self, actual: &impl fmt::Debug) { | ||
125 | let actual = format!("{:#?}\n", actual); | ||
126 | self.assert_eq(&actual) | ||
127 | } | ||
128 | fn read(&self) -> String { | ||
129 | fs::read_to_string(self.abs_path()).unwrap_or_default().replace("\r\n", "\n") | ||
130 | } | ||
131 | fn write(&self, contents: &str) { | ||
132 | fs::write(self.abs_path(), contents).unwrap() | ||
133 | } | ||
134 | fn abs_path(&self) -> PathBuf { | ||
135 | WORKSPACE_ROOT.join(&self.path) | ||
136 | } | ||
137 | } | ||
138 | |||
139 | #[derive(Default)] | ||
140 | struct Runtime { | ||
141 | help_printed: bool, | ||
142 | per_file: HashMap<&'static str, FileRuntime>, | ||
143 | } | ||
144 | static RT: Lazy<Mutex<Runtime>> = Lazy::new(Default::default); | ||
145 | |||
146 | impl Runtime { | ||
147 | fn fail_expect(expect: &Expect, expected: &str, actual: &str) { | ||
148 | let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); | ||
149 | if update_expect() { | ||
150 | println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.position); | ||
151 | rt.per_file | ||
152 | .entry(expect.position.file) | ||
153 | .or_insert_with(|| FileRuntime::new(expect)) | ||
154 | .update(expect, actual); | ||
155 | return; | ||
156 | } | ||
157 | rt.panic(expect.position.to_string(), expected, actual); | ||
158 | } | ||
159 | |||
160 | fn fail_file(expect: &ExpectFile, expected: &str, actual: &str) { | ||
161 | let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); | ||
162 | if update_expect() { | ||
163 | println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.path.display()); | ||
164 | expect.write(actual); | ||
165 | return; | ||
166 | } | ||
167 | rt.panic(expect.path.display().to_string(), expected, actual); | ||
168 | } | ||
169 | |||
170 | fn panic(&mut self, position: String, expected: &str, actual: &str) { | ||
171 | let print_help = !mem::replace(&mut self.help_printed, true); | ||
172 | let help = if print_help { HELP } else { "" }; | ||
173 | |||
174 | let diff = Changeset::new(actual, expected, "\n"); | ||
175 | |||
176 | println!( | ||
177 | "\n | ||
178 | \x1b[1m\x1b[91merror\x1b[97m: expect test failed\x1b[0m | ||
179 | \x1b[1m\x1b[34m-->\x1b[0m {} | ||
180 | {} | ||
181 | \x1b[1mExpect\x1b[0m: | ||
182 | ---- | ||
183 | {} | ||
184 | ---- | ||
185 | |||
186 | \x1b[1mActual\x1b[0m: | ||
187 | ---- | ||
188 | {} | ||
189 | ---- | ||
190 | |||
191 | \x1b[1mDiff\x1b[0m: | ||
192 | ---- | ||
193 | {} | ||
194 | ---- | ||
195 | ", | ||
196 | position, help, expected, actual, diff | ||
197 | ); | ||
198 | // Use resume_unwind instead of panic!() to prevent a backtrace, which is unnecessary noise. | ||
199 | panic::resume_unwind(Box::new(())); | ||
200 | } | ||
201 | } | ||
202 | |||
203 | struct FileRuntime { | ||
204 | path: PathBuf, | ||
205 | original_text: String, | ||
206 | patchwork: Patchwork, | ||
207 | } | ||
208 | |||
209 | impl FileRuntime { | ||
210 | fn new(expect: &Expect) -> FileRuntime { | ||
211 | let path = WORKSPACE_ROOT.join(expect.position.file); | ||
212 | let original_text = fs::read_to_string(&path).unwrap(); | ||
213 | let patchwork = Patchwork::new(original_text.clone()); | ||
214 | FileRuntime { path, original_text, patchwork } | ||
215 | } | ||
216 | fn update(&mut self, expect: &Expect, actual: &str) { | ||
217 | let loc = expect.locate(&self.original_text); | ||
218 | let patch = format_patch(loc.line_indent.clone(), actual); | ||
219 | self.patchwork.patch(loc.literal_range, &patch); | ||
220 | fs::write(&self.path, &self.patchwork.text).unwrap() | ||
221 | } | ||
222 | } | ||
223 | |||
224 | #[derive(Debug)] | ||
225 | struct Location { | ||
226 | line_indent: usize, | ||
227 | literal_range: Range<usize>, | ||
228 | } | ||
229 | |||
230 | #[derive(Debug)] | ||
231 | struct Patchwork { | ||
232 | text: String, | ||
233 | indels: Vec<(Range<usize>, usize)>, | ||
234 | } | ||
235 | |||
236 | impl Patchwork { | ||
237 | fn new(text: String) -> Patchwork { | ||
238 | Patchwork { text, indels: Vec::new() } | ||
239 | } | ||
240 | fn patch(&mut self, mut range: Range<usize>, patch: &str) { | ||
241 | self.indels.push((range.clone(), patch.len())); | ||
242 | self.indels.sort_by_key(|(delete, _insert)| delete.start); | ||
243 | |||
244 | let (delete, insert) = self | ||
245 | .indels | ||
246 | .iter() | ||
247 | .take_while(|(delete, _)| delete.start < range.start) | ||
248 | .map(|(delete, insert)| (delete.end - delete.start, insert)) | ||
249 | .fold((0usize, 0usize), |(x1, y1), (x2, y2)| (x1 + x2, y1 + y2)); | ||
250 | |||
251 | for pos in &mut [&mut range.start, &mut range.end] { | ||
252 | **pos -= delete; | ||
253 | **pos += insert; | ||
254 | } | ||
255 | |||
256 | self.text.replace_range(range, &patch); | ||
257 | } | ||
258 | } | ||
259 | |||
260 | fn format_patch(line_indent: usize, patch: &str) -> String { | ||
261 | let mut max_hashes = 0; | ||
262 | let mut cur_hashes = 0; | ||
263 | for byte in patch.bytes() { | ||
264 | if byte != b'#' { | ||
265 | cur_hashes = 0; | ||
266 | continue; | ||
267 | } | ||
268 | cur_hashes += 1; | ||
269 | max_hashes = max_hashes.max(cur_hashes); | ||
270 | } | ||
271 | let hashes = &"#".repeat(max_hashes + 1); | ||
272 | let indent = &" ".repeat(line_indent); | ||
273 | let is_multiline = patch.contains('\n'); | ||
274 | |||
275 | let mut buf = String::new(); | ||
276 | buf.push('r'); | ||
277 | buf.push_str(hashes); | ||
278 | buf.push('"'); | ||
279 | if is_multiline { | ||
280 | buf.push('\n'); | ||
281 | } | ||
282 | let mut final_newline = false; | ||
283 | for line in lines_with_ends(patch) { | ||
284 | if is_multiline && !line.trim().is_empty() { | ||
285 | buf.push_str(indent); | ||
286 | buf.push_str(" "); | ||
287 | } | ||
288 | buf.push_str(line); | ||
289 | final_newline = line.ends_with('\n'); | ||
290 | } | ||
291 | if final_newline { | ||
292 | buf.push_str(indent); | ||
293 | } | ||
294 | buf.push('"'); | ||
295 | buf.push_str(hashes); | ||
296 | buf | ||
297 | } | ||
298 | |||
299 | static WORKSPACE_ROOT: Lazy<PathBuf> = Lazy::new(|| { | ||
300 | let my_manifest = | ||
301 | env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()); | ||
302 | // Heuristic, see https://github.com/rust-lang/cargo/issues/3946 | ||
303 | Path::new(&my_manifest) | ||
304 | .ancestors() | ||
305 | .filter(|it| it.join("Cargo.toml").exists()) | ||
306 | .last() | ||
307 | .unwrap() | ||
308 | .to_path_buf() | ||
309 | }); | ||
310 | |||
311 | #[cfg(test)] | ||
312 | mod tests { | ||
313 | use super::*; | ||
314 | |||
315 | #[test] | ||
316 | fn test_format_patch() { | ||
317 | let patch = format_patch(0, "hello\nworld\n"); | ||
318 | expect![[r##" | ||
319 | r#" | ||
320 | hello | ||
321 | world | ||
322 | "#"##]] | ||
323 | .assert_eq(&patch); | ||
324 | |||
325 | let patch = format_patch(4, "single line"); | ||
326 | expect![[r##"r#"single line"#"##]].assert_eq(&patch); | ||
327 | } | ||
328 | |||
329 | #[test] | ||
330 | fn test_patchwork() { | ||
331 | let mut patchwork = Patchwork::new("one two three".to_string()); | ||
332 | patchwork.patch(4..7, "zwei"); | ||
333 | patchwork.patch(0..3, "один"); | ||
334 | patchwork.patch(8..13, "3"); | ||
335 | expect![[r#" | ||
336 | Patchwork { | ||
337 | text: "один zwei 3", | ||
338 | indels: [ | ||
339 | ( | ||
340 | 0..3, | ||
341 | 8, | ||
342 | ), | ||
343 | ( | ||
344 | 4..7, | ||
345 | 4, | ||
346 | ), | ||
347 | ( | ||
348 | 8..13, | ||
349 | 1, | ||
350 | ), | ||
351 | ], | ||
352 | } | ||
353 | "#]] | ||
354 | .assert_debug_eq(&patchwork); | ||
355 | } | ||
356 | } | ||