Skip to content

Commit 902cd93

Browse files
committed
Add insta-based snapshot tests that cover side-by-side format
I'm going to fix inline diff issue #704, and found that the inline format isn't covered by CLI tests. I could duplicate compare_all.sh to test various formats, but it's uneasy to review that a new formatting is better than the previous version. I think snapshot testing will help guarantee the output quality without increasing maintenance burden too much. If we like the idea, maybe we can migrate some of compare_all.sh tests to the insta-based ones. Note that this will add ~10sec to "cargo test" runs.
1 parent 3649807 commit 902cd93

File tree

86 files changed

+1982
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+1982
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ flamegraph.svg
55
.idea
66

77
sample_files/compare.result
8+
tests/snapshots/*.snap.new
89

910
notes.md

manual/src/adding_a_parser.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ $ ./sample_files/compare_all.sh
139139
$ cp sample_files/compare.result sample_files/compare.expected
140140
```
141141

142+
Run the CLI test and [update the snapshots][insta] as well.
143+
144+
```
145+
$ cargo insta test
146+
$ cargo insta accept
147+
```
148+
149+
[insta]: https://insta.rs/docs/quickstart/#reviewing-snapshots
150+
142151
## Maintenance
143152

144153
To update a parser that is already imported, use `git subtree pull`.

tests/cli.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::process::Command;
2+
use std::str;
23

34
use assert_cmd::prelude::*;
45
use predicates::prelude::*;
@@ -259,3 +260,39 @@ fn walk_hidden_items() {
259260
.and(predicate::str::contains("after"));
260261
cmd.assert().stdout(predicate_fn);
261262
}
263+
264+
const MAX_SAMPLE_FILE_SIZE: u64 = 10_000;
265+
266+
#[test]
267+
fn samples_side_by_side() {
268+
insta::glob!("../sample_files", "*_1.*", |left_file| {
269+
let base_dir = left_file.parent().unwrap().parent().unwrap();
270+
let file_name = left_file.file_name().unwrap().to_str().unwrap();
271+
let right_file = left_file.with_file_name(file_name.replace("_1.", "_2."));
272+
// Large sample files are excluded because it's slow to diff with debug
273+
// binary, and the snapshot results wouldn't help review changes.
274+
if left_file.metadata().unwrap().len() > MAX_SAMPLE_FILE_SIZE
275+
|| right_file.metadata().unwrap().len() > MAX_SAMPLE_FILE_SIZE
276+
{
277+
eprintln!("Skipping large sample: {file_name}");
278+
return;
279+
}
280+
281+
let to_path_arg = |path: &Path| {
282+
let short_path = path.strip_prefix(base_dir).unwrap();
283+
let short_str = short_path.to_str().unwrap();
284+
short_str.replace(std::path::MAIN_SEPARATOR, "/")
285+
};
286+
let mut cmd = get_base_command();
287+
let assert = cmd
288+
.arg("--color=always")
289+
.arg("--display=side-by-side")
290+
.arg("--width=160")
291+
.arg(to_path_arg(left_file))
292+
.arg(to_path_arg(&right_file))
293+
.assert()
294+
.success();
295+
let stdout = str::from_utf8(&assert.get_output().stdout).unwrap();
296+
insta::assert_snapshot!(stdout);
297+
});
298+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
source: tests/cli.rs
3+
expression: stdout
4+
input_file: sample_files/Session_1.kt
5+
---
6+
sample_files/Session_2.kt --- 1/2 --- Kotlin
7+
 96  /**  96  /**
8+
 97  * The session's photo URL.  97  * The session's photo URL.
9+
 98  */  98  */
10+
 99  val photoUrl: String?,  99  val photoUrl: String,
11+
100  100 
12+
101  /** 101  /**
13+
102  * IDs of the sessions related to this session. 102  * IDs of the sessions related to this session.
14+
15+
sample_files/Session_2.kt --- 2/2 --- Kotlin
16+
114  return startTime <= now && endTime >= now 114  return startTime <= now && endTime >= now
17+
115  } 115  }
18+
...  116 
19+
...  117  val hasPhoto inline get() = photoUrl.isNotEmpty()
20+
116  118 
21+
117  /** 119  /**
22+
118  * Returns whether the session has a video or not. A session could be l 120  * Returns whether the session has a video or not. A session could be l
23+
... ive streaming or have a ... ive streaming or have a
24+
119  * recorded session. Both live stream and recorded videos are stored in 121  * recorded session. Both live stream and recorded videos are stored in
25+
...  [Session.youTubeUrl]. ...  [Session.youTubeUrl].
26+
120  */ 122  */
27+
121  fun hasVideo() = youTubeUrl.isNotEmpty() 123  val hasVideo inline get() = youTubeUrl.isNotEmpty()
28+
...  124 
29+
...  125  val hasPhotoOrVideo inline get() = hasPhoto || hasVideo
30+
122  126 
31+
123  /** 127  /**
32+
124  * The year the session was held. 128  * The year the session was held.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
source: tests/cli.rs
3+
expression: stdout
4+
input_file: sample_files/ada_1.adb
5+
---
6+
sample_files/ada_2.adb --- Ada
7+
1  with Ada.Text_IO; use Ada.Text_IO; procedure Hello is begin Put_Line ("Hello 1 with Ada.Text_IO;
8+
.  WORLD!"); end Hello; . 
9+
.  2 
10+
.  3 procedure Hello is
11+
.  4  package TIO renames Ada.Text_IO;
12+
.  5 begin
13+
.  6  TIO.Put_Line ("Hello World!");
14+
.  7 end Hello;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: tests/cli.rs
3+
expression: stdout
4+
input_file: sample_files/added_line_1.txt
5+
---
6+
sample_files/added_line_2.txt --- Text
7+
1 1 potato
8+
2 2 tomato
9+
 3 legato
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: tests/cli.rs
3+
expression: stdout
4+
input_file: sample_files/align_footer_1.txt
5+
---
6+
sample_files/align_footer_2.txt --- Text
7+
1 before 1 before
8+
2  foo x 2  x
9+
3 y . 
10+
4 after 3 after
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: tests/cli.rs
3+
expression: stdout
4+
input_file: sample_files/apex_1.cls
5+
---
6+
sample_files/apex_2.cls --- Apex
7+
No syntactic changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
source: tests/cli.rs
3+
expression: stdout
4+
input_file: sample_files/bad_combine_1.rs
5+
---
6+
sample_files/bad_combine_2.rs --- 1/2 --- Rust
7+
 1  . 
8+
 2 fn column_widths( . 
9+
 3  hunks: &[Hunk], . 
10+
 4  lhs_mps: &[MatchedPos], . 
11+
 5  rhs_mps: &[MatchedPos], . 
12+
 6  max_lhs_src_line: LineNumber, . 
13+
 7  max_rhs_src_line: LineNumber, . 
14+
 8 ) { . 
15+
 9 } . 
16+
10  . 
17+
11 fn display_line_nums( 1 fn display_line_nums(
18+
12 ) -> (String, String) { 2 ) -> (String, String) {
19+
13  let display_rhs_line_num: String = match rhs_line_num { 3  let display_rhs_line_num: String = match rhs_line_num {
20+
14  Some(line_num) => { 4  Some(line_num) => {
21+
15  let s = format_line_num_padded(line_num, rhs_column_width); 5  let s = format_line_num_padded(line_num, widths.rhs_line_nums);
22+
16  if rhs_lines_with_novel.contains(&line_num) { 6  if rhs_lines_with_novel.contains(&line_num) {
23+
17  s.bright_green().to_string() 7  s.bright_green().to_string()
24+
18  } else { 8  } else {
25+
26+
sample_files/bad_combine_2.rs --- 2/2 --- Rust
27+
21  } 11  }
28+
22  None => format_missing_line_num( 12  None => format_missing_line_num(
29+
23  prev_rhs_line_num.unwrap_or_else(|| 10.into()), 13  prev_rhs_line_num.unwrap_or_else(|| 10.into()),
30+
24  rhs_column_width, 14  widths.rhs_line_nums,
31+
25  ), 15  ),
32+
26  }; 16  };
33+
27  17 
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: tests/cli.rs
3+
expression: stdout
4+
input_file: sample_files/change_outer_1.el
5+
---
6+
sample_files/change_outer_2.el --- Emacs Lisp
7+
1 (lhs comma rhs) 1 [(lhs) comma rhs]

0 commit comments

Comments
 (0)