Skip to content

Commit

Permalink
tests: add insta for snapshot testing
Browse files Browse the repository at this point in the history
Insta makes writing new tests or performing changes which update the
expected value(s) easier. The new reference data can be reviewed
and inserted/updated automatically by `cargo insta review` (after
`cargo install cargo-insta`, but that tool is optional).

The snapshots are stored inline using the `@""` syntax and not in
separate files because the delta test output is small and designed to
be human readable.

See https://insta.rs/#hello-snapshot-testing
and https://docs.rs/insta/1.39.0/insta/
  • Loading branch information
th1000s committed Jul 6, 2024
1 parent f5b3717 commit f61b60a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,14 @@ version = "0.29.0"
default-features = false
features = []

[dev-dependencies]
insta = { version = "1.*", features = ["colors"] }

[profile.test]
opt-level = 2

[profile.dev.package.insta]
opt-level = 3

[profile.dev.package.similar]
opt-level = 3
17 changes: 10 additions & 7 deletions src/features/side_by_side.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,16 +594,19 @@ pub mod tests {
use crate::features::line_numbers::tests::*;
use crate::options::theme;
use crate::tests::integration_test_utils::{make_config_from_args, run_delta, DeltaTest};
use insta::assert_snapshot;

#[test]
fn test_two_minus_lines() {
DeltaTest::with_args(&["--side-by-side", "--width", "40"])
fn test_two_fitting_minus_lines() {
// rustfmt ignores the assert macro arguments, so do the setup outside
let result = DeltaTest::with_args(&["--side-by-side", "--width", "40"])
.with_input(TWO_MINUS_LINES_DIFF)
.expect_after_header(
r#"
│ 1 │a = 1 │ │
│ 2 │b = 23456 │ │"#,
);
.skip_header();
assert_snapshot!(result, @r###"
│ 1 │a = 1 │ │
│ 2 │b = 23456 │ │
"###
);
}

#[test]
Expand Down
6 changes: 5 additions & 1 deletion src/tests/integration_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::io::{BufReader, Write};
use std::path::Path;

use bytelines::ByteLines;
use itertools;
use itertools::Itertools;

use crate::ansi;
use crate::cli;
Expand Down Expand Up @@ -264,6 +264,10 @@ impl DeltaTestOutput {
self.expect_after_skip(crate::config::HEADER_LEN, expected)
}

pub fn skip_header(self) -> String {
self.output.lines().skip(config::HEADER_LEN).join("\n")
}

pub fn expect_contains(self, expected: &str) -> Self {
assert!(
self.output.contains(expected),
Expand Down

0 comments on commit f61b60a

Please sign in to comment.