DeltaTest improvements (#876)

* DeltaTest improvements

- Added .expect_contains(), .expect_raw_contains(), and
  .expect_contains_once() member functions to DeltaTestOutput.
  These functions also output some helpful debug info when the
  assert fails.
- Separated .with_config_and_input() into .with_config() and (the
  tweaked) .with_input().
- Made .with_config() create a DeltaTest object (like .with() does).
- Renamed .with() as .with_args().
- Moved .explain_ansi() from DeltaTestOutput to DeltaTest, which must
  now be called prior to .with_input() since the latter stashes off both
  the raw_output & the processed output in the object.
- Changed .expect() and .expect_skip() to return Self so that they can
  continue a chain of multiple expect calls (e.g. a series of partial
  matches with different skip values).
- The processed output text can be accessed via `test_obj.output` (see
  also `test_obj.raw_output`).
- Renamed .expect_skip() to .expect_after_skip().
- Changed .expect() to start at the first line.
- Added .expect_after_header() that works like the old .expect().
- Renamed lines_match() to assert_lines_match() and made it match all
  lines (no skip number).
- Added assert_lines_match_after_skip() to work like the old
  lines_match() function, but with the .expect_after_skip() arg order.
- Converted some old-style tests into DeltaTest style tests.
- Renamed set_cfg as set_config
This commit is contained in:
Wayne Davison 2022-01-04 00:39:03 -08:00 committed by GitHub
parent cd47b21176
commit 1d6f18a663
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 313 additions and 296 deletions

View File

@ -630,7 +630,7 @@ pub mod tests {
#[test] #[test]
fn test_two_minus_lines() { fn test_two_minus_lines() {
DeltaTest::with(&[ DeltaTest::with_args(&[
"--line-numbers", "--line-numbers",
"--line-numbers-left-format", "--line-numbers-left-format",
"{nm:^4}⋮", "{nm:^4}⋮",
@ -646,7 +646,7 @@ pub mod tests {
"0 4", "0 4",
]) ])
.with_input(TWO_MINUS_LINES_DIFF) .with_input(TWO_MINUS_LINES_DIFF)
.expect( .expect_after_header(
r#" r#"
#indent_mark #indent_mark
1 a = 1 1 a = 1
@ -656,7 +656,7 @@ pub mod tests {
#[test] #[test]
fn test_two_plus_lines() { fn test_two_plus_lines() {
DeltaTest::with(&[ DeltaTest::with_args(&[
"--line-numbers", "--line-numbers",
"--line-numbers-left-format", "--line-numbers-left-format",
"{nm:^4}⋮", "{nm:^4}⋮",
@ -672,7 +672,7 @@ pub mod tests {
"0 4", "0 4",
]) ])
.with_input(TWO_PLUS_LINES_DIFF) .with_input(TWO_PLUS_LINES_DIFF)
.expect( .expect_after_header(
r#" r#"
#indent_mark #indent_mark
1 a = 1 1 a = 1
@ -779,9 +779,9 @@ pub mod tests {
#[test] #[test]
fn test_line_numbers_continue_correctly() { fn test_line_numbers_continue_correctly() {
DeltaTest::with(&["--side-by-side", "--width", "44", "--line-fill-method=ansi"]) DeltaTest::with_args(&["--side-by-side", "--width", "44", "--line-fill-method=ansi"])
.with_input(DIFF_PLUS_MINUS_WITH_1_CONTEXT_DIFF) .with_input(DIFF_PLUS_MINUS_WITH_1_CONTEXT_DIFF)
.expect( .expect_after_header(
r#" r#"
1 abc 1 abc 1 abc 1 abc
2 a = left side 2 a = right side 2 a = left side 2 a = right side
@ -791,7 +791,7 @@ pub mod tests {
#[test] #[test]
fn test_line_numbers_continue_correctly_after_wrapping() { fn test_line_numbers_continue_correctly_after_wrapping() {
DeltaTest::with(&[ DeltaTest::with_args(&[
"--side-by-side", "--side-by-side",
"--width", "--width",
"32", "32",
@ -802,7 +802,7 @@ pub mod tests {
"@", "@",
]) ])
.with_input(DIFF_PLUS_MINUS_WITH_1_CONTEXT_DIFF) .with_input(DIFF_PLUS_MINUS_WITH_1_CONTEXT_DIFF)
.expect( .expect_after_header(
r#" r#"
1 abc 1 abc 1 abc 1 abc
2 a = left @ 2 a = right@ 2 a = left @ 2 a = right@
@ -821,9 +821,9 @@ pub mod tests {
"@", "@",
]; ];
DeltaTest::with(cfg) DeltaTest::with_args(cfg)
.with_input(DIFF_WITH_LONGER_MINUS_1_CONTEXT) .with_input(DIFF_WITH_LONGER_MINUS_1_CONTEXT)
.expect( .expect_after_header(
r#" r#"
1 abc 1 abc 1 abc 1 abc
2 a = one side 2 a = one longer@ 2 a = one side 2 a = one longer@
@ -831,9 +831,9 @@ pub mod tests {
3 xyz 3 xyz"#, 3 xyz 3 xyz"#,
); );
DeltaTest::with(cfg) DeltaTest::with_args(cfg)
.with_input(DIFF_WITH_LONGER_PLUS_1_CONTEXT) .with_input(DIFF_WITH_LONGER_PLUS_1_CONTEXT)
.expect( .expect_after_header(
r#" r#"
1 abc 1 abc 1 abc 1 abc
2 a = one longer@ 2 a = one side 2 a = one longer@ 2 a = one side
@ -841,9 +841,9 @@ pub mod tests {
3 xyz 3 xyz"#, 3 xyz 3 xyz"#,
); );
DeltaTest::with(cfg) DeltaTest::with_args(cfg)
.with_input(DIFF_MISMATCH_LONGER_MINUS_1_CONTEXT) .with_input(DIFF_MISMATCH_LONGER_MINUS_1_CONTEXT)
.expect( .expect_after_header(
r#" r#"
1 abc 1 abc 1 abc 1 abc
2 a = left side @ 2 a = left side @
@ -852,9 +852,9 @@ pub mod tests {
3 xyz 3 xyz"#, 3 xyz 3 xyz"#,
); );
DeltaTest::with(cfg) DeltaTest::with_args(cfg)
.with_input(DIFF_MISMATCH_LONGER_PLUS_1_CONTEXT) .with_input(DIFF_MISMATCH_LONGER_PLUS_1_CONTEXT)
.expect( .expect_after_header(
r#" r#"
1 abc 1 abc 1 abc 1 abc
2 a = other one 2 a = other one

View File

@ -597,9 +597,9 @@ pub mod tests {
#[test] #[test]
fn test_two_minus_lines() { fn test_two_minus_lines() {
DeltaTest::with(&["--side-by-side", "--width", "40"]) DeltaTest::with_args(&["--side-by-side", "--width", "40"])
.with_input(TWO_MINUS_LINES_DIFF) .with_input(TWO_MINUS_LINES_DIFF)
.expect( .expect_after_header(
r#" r#"
1 a = 1 1 a = 1
2 b = 23456 "#, 2 b = 23456 "#,
@ -608,7 +608,7 @@ pub mod tests {
#[test] #[test]
fn test_two_minus_lines_truncated() { fn test_two_minus_lines_truncated() {
DeltaTest::with(&[ DeltaTest::with_args(&[
"--side-by-side", "--side-by-side",
"--wrap-max-lines", "--wrap-max-lines",
"0", "0",
@ -616,9 +616,9 @@ pub mod tests {
"28", "28",
"--line-fill-method=spaces", "--line-fill-method=spaces",
]) ])
.set_cfg(|cfg| cfg.truncation_symbol = ">".into()) .set_config(|cfg| cfg.truncation_symbol = ">".into())
.with_input(TWO_MINUS_LINES_DIFF) .with_input(TWO_MINUS_LINES_DIFF)
.expect( .expect_after_header(
r#" r#"
1 a = 1 1 a = 1
2 b = 234> "#, 2 b = 234> "#,
@ -627,14 +627,14 @@ pub mod tests {
#[test] #[test]
fn test_two_plus_lines() { fn test_two_plus_lines() {
DeltaTest::with(&[ DeltaTest::with_args(&[
"--side-by-side", "--side-by-side",
"--width", "--width",
"41", "41",
"--line-fill-method=spaces", "--line-fill-method=spaces",
]) ])
.with_input(TWO_PLUS_LINES_DIFF) .with_input(TWO_PLUS_LINES_DIFF)
.expect( .expect_after_header(
r#" r#"
1 a = 1 1 a = 1
2 b = 234567 "#, 2 b = 234567 "#,
@ -643,27 +643,27 @@ pub mod tests {
#[test] #[test]
fn test_two_plus_lines_spaces_and_ansi() { fn test_two_plus_lines_spaces_and_ansi() {
DeltaTest::with(&[ DeltaTest::with_args(&[
"--side-by-side", "--side-by-side",
"--width", "--width",
"41", "41",
"--line-fill-method=spaces", "--line-fill-method=spaces",
]) ])
.with_input(TWO_PLUS_LINES_DIFF)
.explain_ansi() .explain_ansi()
.expect(r#" .with_input(TWO_PLUS_LINES_DIFF)
.expect_after_header(r#"
(blue)(88) (blue)(normal) (blue)(28) 1 (blue)(231 22)a (203)=(231) (141)1(normal 22) (normal) (blue)(88) (blue)(normal) (blue)(28) 1 (blue)(231 22)a (203)=(231) (141)1(normal 22) (normal)
(blue)(88) (blue)(normal) (blue)(28) 2 (blue)(231 22)b (203)=(231) (141)234567(normal 22) (normal)"#); (blue)(88) (blue)(normal) (blue)(28) 2 (blue)(231 22)b (203)=(231) (141)234567(normal 22) (normal)"#);
DeltaTest::with(&[ DeltaTest::with_args(&[
"--side-by-side", "--side-by-side",
"--width", "--width",
"41", "41",
"--line-fill-method=ansi", "--line-fill-method=ansi",
]) ])
.with_input(TWO_PLUS_LINES_DIFF)
.explain_ansi() .explain_ansi()
.expect(r#" .with_input(TWO_PLUS_LINES_DIFF)
.expect_after_header(r#"
(blue)(88) (blue)(normal) (blue) (28) 1 (blue)(231 22)a (203)=(231) (141)1(normal) (blue)(88) (blue)(normal) (blue) (28) 1 (blue)(231 22)a (203)=(231) (141)1(normal)
(blue)(88) (blue)(normal) (blue) (28) 2 (blue)(231 22)b (203)=(231) (141)234567(normal)"#); (blue)(88) (blue)(normal) (blue) (28) 2 (blue)(231 22)b (203)=(231) (141)234567(normal)"#);
} }
@ -701,14 +701,14 @@ pub mod tests {
#[test] #[test]
fn test_one_minus_one_plus_line() { fn test_one_minus_one_plus_line() {
DeltaTest::with(&[ DeltaTest::with_args(&[
"--side-by-side", "--side-by-side",
"--width", "--width",
"40", "40",
"--line-fill-method=spaces", "--line-fill-method=spaces",
]) ])
.with_input(ONE_MINUS_ONE_PLUS_LINE_DIFF) .with_input(ONE_MINUS_ONE_PLUS_LINE_DIFF)
.expect( .expect_after_header(
r#" r#"
1 a = 1 1 a = 1 1 a = 1 1 a = 1
2 b = 2 2 bb = 2 "#, 2 b = 2 2 bb = 2 "#,

View File

@ -281,11 +281,11 @@ mod tests {
#[test] #[test]
fn test_word_diff() { fn test_word_diff() {
DeltaTest::with(&[]) DeltaTest::with_args(&[])
.with_calling_process("git diff --word-diff") .with_calling_process("git diff --word-diff")
.with_input(GIT_DIFF_WORD_DIFF)
.explain_ansi() .explain_ansi()
.expect_skip( .with_input(GIT_DIFF_WORD_DIFF)
.expect_after_skip(
11, 11,
" "
#indent_mark #indent_mark
@ -299,11 +299,11 @@ mod tests {
#[test] #[test]
fn test_color_words() { fn test_color_words() {
DeltaTest::with(&[]) DeltaTest::with_args(&[])
.with_calling_process("git diff --color-words") .with_calling_process("git diff --color-words")
.with_input(GIT_DIFF_COLOR_WORDS)
.explain_ansi() .explain_ansi()
.expect_skip( .with_input(GIT_DIFF_COLOR_WORDS)
.expect_after_skip(
11, 11,
" "
#indent_mark #indent_mark
@ -318,15 +318,15 @@ mod tests {
#[test] #[test]
#[ignore] // FIXME #[ignore] // FIXME
fn test_color_words_map_styles() { fn test_color_words_map_styles() {
DeltaTest::with(&[ DeltaTest::with_args(&[
"--map-styles", "--map-styles",
"red => bold yellow #dddddd, green => bold blue #dddddd", "red => bold yellow #dddddd, green => bold blue #dddddd",
]) ])
.with_calling_process("git diff --color-words") .with_calling_process("git diff --color-words")
.with_input(GIT_DIFF_COLOR_WORDS)
.explain_ansi() .explain_ansi()
.with_input(GIT_DIFF_COLOR_WORDS)
.inspect() .inspect()
.expect_skip( .expect_after_skip(
11, 11,
r##" r##"
#indent_mark #indent_mark
@ -340,10 +340,10 @@ mod tests {
#[test] #[test]
fn test_hunk_line_style_raw() { fn test_hunk_line_style_raw() {
DeltaTest::with(&["--minus-style", "raw", "--plus-style", "raw"]) DeltaTest::with_args(&["--minus-style", "raw", "--plus-style", "raw"])
.with_input(GIT_DIFF_WITH_COLOR)
.explain_ansi() .explain_ansi()
.expect_skip( .with_input(GIT_DIFF_WITH_COLOR)
.expect_after_skip(
14, 14,
" "
(red)aaa(normal) (red)aaa(normal)
@ -354,7 +354,7 @@ mod tests {
#[test] #[test]
fn test_hunk_line_style_raw_map_styles() { fn test_hunk_line_style_raw_map_styles() {
DeltaTest::with(&[ DeltaTest::with_args(&[
"--minus-style", "--minus-style",
"raw", "raw",
"--plus-style", "--plus-style",
@ -362,9 +362,9 @@ mod tests {
"--map-styles", "--map-styles",
"red => bold blue, green => dim yellow", "red => bold blue, green => dim yellow",
]) ])
.with_input(GIT_DIFF_WITH_COLOR)
.explain_ansi() .explain_ansi()
.expect_skip( .with_input(GIT_DIFF_WITH_COLOR)
.expect_after_skip(
14, 14,
" "
(bold blue)aaa(normal) (bold blue)aaa(normal)

View File

@ -12,6 +12,7 @@ use crate::cli;
use crate::config; use crate::config;
use crate::delta::delta; use crate::delta::delta;
use crate::git_config::GitConfig; use crate::git_config::GitConfig;
use crate::tests::test_utils;
use crate::utils::process::tests::FakeParentArgs; use crate::utils::process::tests::FakeParentArgs;
pub fn make_options_from_args_and_git_config( pub fn make_options_from_args_and_git_config(
@ -94,19 +95,25 @@ pub fn get_line_of_code_from_delta(
// line1"#;` // line 2 etc. // line1"#;` // line 2 etc.
// ignore the first newline and compare the following `lines()` to those produced // ignore the first newline and compare the following `lines()` to those produced
// by `have`, `skip`-ping the first few. The leading spaces of the first line // by `have`, `skip`-ping the first few. The leading spaces of the first line
// to indicate the last line in the list). The leading spaces of the first line
// are stripped from every following line (and verified), unless the first line // are stripped from every following line (and verified), unless the first line
// marks the indentation level with `#indent_mark`. // marks the indentation level with `#indent_mark`.
pub fn lines_match(expected: &str, have: &str, skip: Option<usize>) { pub fn assert_lines_match_after_skip(skip: usize, expected: &str, have: &str) {
let mut exp = expected.lines().peekable(); let mut exp = expected.lines().peekable();
assert!(exp.next() == Some(""), "first line must be empty"); let mut line1 = exp.next().unwrap();
let line1 = exp.peek().unwrap(); let allow_partial = line1 == "#partial";
assert!(
allow_partial || line1.is_empty(),
"first line must be empty or \"#partial\""
);
line1 = exp.peek().unwrap();
let indentation = line1.find(|c| c != ' ').unwrap_or(0); let indentation = line1.find(|c| c != ' ').unwrap_or(0);
let ignore_indent = &line1[indentation..] == "#indent_mark"; let ignore_indent = &line1[indentation..] == "#indent_mark";
if ignore_indent { if ignore_indent {
let _indent_mark = exp.next(); let _indent_mark = exp.next();
} }
let mut it = have.lines().skip(skip.unwrap_or(0)); let mut it = have.lines().skip(skip);
for (i, expected) in exp.enumerate() { for (i, expected) in exp.enumerate() {
if !ignore_indent { if !ignore_indent {
@ -119,27 +126,51 @@ pub fn lines_match(expected: &str, have: &str, skip: Option<usize>) {
assert_eq!( assert_eq!(
&expected[indentation..], &expected[indentation..],
it.next().unwrap(), it.next().unwrap(),
"on line {} of input", "on line {} of input:\n{}",
i + 1 i + 1,
delineated_string(have),
); );
} }
assert_eq!(it.next(), None, "more input than expected"); if !allow_partial {
assert_eq!(it.next(), None, "more input than expected");
}
}
pub fn assert_lines_match(expected: &str, have: &str) {
assert_lines_match_after_skip(0, expected, have)
}
pub fn delineated_string(txt: &str) -> String {
let top = "".repeat(100);
let btm = "".repeat(100);
let nl = "\n";
top + &nl + txt + &nl + &btm
} }
pub struct DeltaTest { pub struct DeltaTest {
config: config::Config, config: config::Config,
calling_process: Option<String>, calling_process: Option<String>,
explain_ansi_: bool,
} }
impl DeltaTest { impl DeltaTest {
pub fn with(args: &[&str]) -> Self { pub fn with_args(args: &[&str]) -> Self {
Self { Self {
config: make_config_from_args(args), config: make_config_from_args(args),
calling_process: None, calling_process: None,
explain_ansi_: false,
} }
} }
pub fn set_cfg<F>(mut self, f: F) -> Self pub fn with_config(config: config::Config) -> Self {
Self {
config: config,
calling_process: None,
explain_ansi_: false,
}
}
pub fn set_config<F>(mut self, f: F) -> Self
where where
F: Fn(&mut config::Config), F: Fn(&mut config::Config),
{ {
@ -152,22 +183,30 @@ impl DeltaTest {
self self
} }
pub fn with_config_and_input(config: &config::Config, input: &str) -> DeltaTestOutput { pub fn explain_ansi(mut self) -> Self {
DeltaTestOutput { self.explain_ansi_ = true;
output: run_delta(input, &config), self
explain_ansi_: false,
}
} }
pub fn with_input(&self, input: &str) -> DeltaTestOutput { pub fn with_input(&self, input: &str) -> DeltaTestOutput {
let _args = FakeParentArgs::for_scope(self.calling_process.as_deref().unwrap_or("")); let _args = FakeParentArgs::for_scope(self.calling_process.as_deref().unwrap_or(""));
DeltaTest::with_config_and_input(&self.config, input) let raw = run_delta(input, &self.config);
let cooked = if self.explain_ansi_ {
ansi::explain_ansi(&raw, false)
} else {
ansi::strip_ansi_codes(&raw)
};
DeltaTestOutput {
raw_output: raw,
output: cooked,
}
} }
} }
pub struct DeltaTestOutput { pub struct DeltaTestOutput {
output: String, pub raw_output: String,
explain_ansi_: bool, pub output: String,
} }
impl DeltaTestOutput { impl DeltaTestOutput {
@ -175,42 +214,58 @@ impl DeltaTestOutput {
/// with ASCII explanation of ANSI escape sequences. /// with ASCII explanation of ANSI escape sequences.
#[allow(unused)] #[allow(unused)]
pub fn inspect(self) -> Self { pub fn inspect(self) -> Self {
eprintln!("{}", "".repeat(100)); eprintln!("{}", delineated_string(&self.output.as_str()));
eprintln!("{}", self.format_output());
eprintln!("{}", "".repeat(100));
self self
} }
/// Print raw output, with any ANSI escape sequences. /// Print raw output, with any ANSI escape sequences.
#[allow(unused)] #[allow(unused)]
pub fn inspect_raw(self) -> Self { pub fn inspect_raw(self) -> Self {
eprintln!("{}", "".repeat(100)); eprintln!("{}", delineated_string(&self.raw_output));
eprintln!("{}", self.output);
eprintln!("{}", "".repeat(100));
self self
} }
pub fn explain_ansi(mut self) -> Self { pub fn expect_after_skip(self, skip: usize, expected: &str) -> Self {
self.explain_ansi_ = true; assert_lines_match_after_skip(skip, expected, &self.output);
self self
} }
pub fn expect_skip(self, skip: usize, expected: &str) -> String { pub fn expect(self, expected: &str) -> Self {
let processed = self.format_output(); self.expect_after_skip(0, expected)
lines_match(expected, &processed, Some(skip));
processed
} }
pub fn expect(self, expected: &str) -> String { pub fn expect_after_header(self, expected: &str) -> Self {
self.expect_skip(crate::config::HEADER_LEN, expected) self.expect_after_skip(crate::config::HEADER_LEN, expected)
} }
fn format_output(&self) -> String { pub fn expect_contains(self, expected: &str) -> Self {
if self.explain_ansi_ { assert!(
ansi::explain_ansi(&self.output, false) self.output.contains(expected),
} else { "Output does not contain \"{}\":\n{}",
ansi::strip_ansi_codes(&self.output) expected,
} delineated_string(&self.output.as_str())
);
self
}
pub fn expect_raw_contains(self, expected: &str) -> Self {
assert!(
self.raw_output.contains(expected),
"Raw output does not contain \"{}\":\n{}",
expected,
delineated_string(&self.raw_output.as_str())
);
self
}
pub fn expect_contains_once(self, expected: &str) -> Self {
assert!(
test_utils::contains_once(&self.output, expected),
"Output does not contain \"{}\" exactly once:\n{}",
expected,
delineated_string(&self.output.as_str())
);
self
} }
} }
@ -235,30 +290,30 @@ pub mod tests {
one one
two two
three"#; three"#;
lines_match(expected, "one\ntwo\nthree", None); assert_lines_match(expected, "one\ntwo\nthree");
let expected = r#" let expected = r#"
#indent_mark #indent_mark
one one
2 2
three"#; three"#;
lines_match(expected, "one\n 2\nthree", None); assert_lines_match(expected, "one\n 2\nthree");
let expected = r#" let expected = r#"
#indent_mark #indent_mark
1 1
2 2
3"#; 3"#;
lines_match(expected, " 1 \n 2 \n 3", None); assert_lines_match(expected, " 1 \n 2 \n 3");
let expected = r#" let expected = r#"
#indent_mark #indent_mark
1 1
ignored! 2 ignored! 2
3"#; 3"#;
lines_match(expected, " 1 \n 2 \n 3", None); assert_lines_match(expected, " 1 \n 2 \n 3");
let expected = "\none\ntwo\nthree"; let expected = "\none\ntwo\nthree";
lines_match(expected, "one\ntwo\nthree", None); assert_lines_match(expected, "one\ntwo\nthree");
} }
#[test] #[test]
@ -266,7 +321,7 @@ ignored! 2
fn test_lines_match_no_nl() { fn test_lines_match_no_nl() {
let expected = r#"bad let expected = r#"bad
lines"#; lines"#;
lines_match(expected, "bad\nlines", None); assert_lines_match(expected, "bad\nlines");
} }
#[test] #[test]
@ -276,7 +331,7 @@ ignored! 2
one one
two two
three"#; three"#;
lines_match(expected, "one\ntwo\nthree\nFOUR", None); assert_lines_match(expected, "one\ntwo\nthree\nFOUR");
} }
#[test] #[test]
@ -286,7 +341,7 @@ ignored! 2
ok ok
wrong_indent wrong_indent
"#; "#;
lines_match(expected, "ok", None); assert_lines_match(expected, "ok");
} }
#[test] #[test]
@ -296,18 +351,17 @@ ignored! 2
ok ok
wrong_indent wrong_indent
"#; "#;
lines_match(expected, "ok", None); assert_lines_match(expected, "ok");
} }
#[test] #[test]
fn test_delta_test() { fn test_delta_test() {
let input = "@@ -1,1 +1,1 @@ fn foo() {\n-1\n+2\n"; let input = "@@ -1,1 +1,1 @@ fn foo() {\n-1\n+2\n";
DeltaTest::with(&["--raw"]) DeltaTest::with_args(&["--raw"])
.set_cfg(|c| c.pager = None) .set_config(|c| c.pager = None)
.set_cfg(|c| c.line_numbers = true) .set_config(|c| c.line_numbers = true)
.with_input(input) .with_input(input)
.expect_skip( .expect(
0,
r#" r#"
#indent_mark #indent_mark
@@ -1,1 +1,1 @@ fn foo() { @@ -1,1 +1,1 @@ fn foo() {
@ -315,18 +369,19 @@ ignored! 2
1 +2"#, 1 +2"#,
); );
DeltaTest::with(&[]).with_input(input).expect_skip( DeltaTest::with_args(&[])
4, .with_input(input)
r#" .expect_after_skip(
4,
r#"
1 1
2"#, 2"#,
); );
DeltaTest::with(&["--raw"]) DeltaTest::with_args(&["--raw"])
.with_input(input)
.explain_ansi() .explain_ansi()
.expect_skip( .with_input(input)
0, .expect(
"\n\ "\n\
(normal)@@ -1,1 +1,1 @@ fn foo() {\n\ (normal)@@ -1,1 +1,1 @@ fn foo() {\n\
(red)-1(normal)\n\ (red)-1(normal)\n\

View File

@ -7,67 +7,53 @@ mod tests {
use crate::style; use crate::style;
use crate::tests::ansi_test_utils::ansi_test_utils; use crate::tests::ansi_test_utils::ansi_test_utils;
use crate::tests::integration_test_utils; use crate::tests::integration_test_utils;
use crate::tests::test_utils; use crate::tests::integration_test_utils::DeltaTest;
use regex::Regex;
#[test] #[test]
fn test_added_file() { fn test_added_file() {
let config = integration_test_utils::make_config_from_args(&[]); DeltaTest::with_args(&[])
let output = integration_test_utils::run_delta(ADDED_FILE_INPUT, &config); .with_input(ADDED_FILE_INPUT)
let output = strip_ansi_codes(&output); .expect_contains("\nadded: a.py\n");
assert!(output.contains("\nadded: a.py\n"));
} }
#[test] #[test]
#[ignore] // #128 #[ignore] // #128
fn test_added_empty_file() { fn test_added_empty_file() {
let config = integration_test_utils::make_config_from_args(&[]); DeltaTest::with_args(&[])
let output = integration_test_utils::run_delta(ADDED_EMPTY_FILE, &config); .with_input(ADDED_EMPTY_FILE)
let output = strip_ansi_codes(&output); .expect_contains("\nadded: file\n");
assert!(output.contains("\nadded: file\n"));
} }
#[test] #[test]
fn test_added_file_directory_path_containing_space() { fn test_added_file_directory_path_containing_space() {
let config = integration_test_utils::make_config_from_args(&[]); DeltaTest::with_args(&[])
let output = .with_input(ADDED_FILES_DIRECTORY_PATH_CONTAINING_SPACE)
integration_test_utils::run_delta(ADDED_FILES_DIRECTORY_PATH_CONTAINING_SPACE, &config); .expect_contains("\nadded: with space/file1\n")
let output = strip_ansi_codes(&output); .expect_contains("\nadded: nospace/file2\n");
assert!(output.contains("\nadded: with space/file1\n"));
assert!(output.contains("\nadded: nospace/file2\n"));
} }
#[test] #[test]
fn test_renamed_file() { fn test_renamed_file() {
let config = integration_test_utils::make_config_from_args(&[]); DeltaTest::with_args(&[])
let output = integration_test_utils::run_delta(RENAMED_FILE_INPUT, &config); .with_input(RENAMED_FILE_INPUT)
let output = strip_ansi_codes(&output); .expect_contains_once("\nrenamed: a.py ⟶ b.py\n");
assert!(test_utils::contains_once(
&output,
"\nrenamed: a.py ⟶ b.py\n"
));
} }
#[test] #[test]
fn test_copied_file() { fn test_copied_file() {
let config = integration_test_utils::make_config_from_args(&[]); DeltaTest::with_args(&[])
let output = integration_test_utils::run_delta(GIT_DIFF_WITH_COPIED_FILE, &config); .with_input(GIT_DIFF_WITH_COPIED_FILE)
let output = strip_ansi_codes(&output); .expect_contains_once("\ncopied: first_file ⟶ copied_file\n");
assert!(test_utils::contains_once(
&output,
"\ncopied: first_file ⟶ copied_file\n"
));
} }
#[test] #[test]
fn test_renamed_file_with_changes() { fn test_renamed_file_with_changes() {
let config = integration_test_utils::make_config_from_args(&[]); let t = DeltaTest::with_args(&[])
let output = integration_test_utils::run_delta(RENAMED_FILE_WITH_CHANGES_INPUT, &config); .with_input(RENAMED_FILE_WITH_CHANGES_INPUT)
let output = strip_ansi_codes(&output); .expect_contains_once(
println!("{}", output); "\nrenamed: Casks/font-dejavusansmono-nerd-font.rb ⟶ Casks/font-dejavu-sans-mono-nerd-font.rb\n"
assert!(test_utils::contains_once( );
&output, println!("{}", t.output);
"\nrenamed: Casks/font-dejavusansmono-nerd-font.rb ⟶ Casks/font-dejavu-sans-mono-nerd-font.rb\n"));
} }
#[test] #[test]
@ -1587,84 +1573,65 @@ src/align.rs:71: impl<'a> Alignment<'a> { │
#[test] #[test]
fn test_file_mode_change_gain_executable_bit() { fn test_file_mode_change_gain_executable_bit() {
let config = integration_test_utils::make_config_from_args(&[]); DeltaTest::with_args(&[])
let output = integration_test_utils::run_delta( .with_input(GIT_DIFF_FILE_MODE_CHANGE_GAIN_EXECUTABLE_BIT)
GIT_DIFF_FILE_MODE_CHANGE_GAIN_EXECUTABLE_BIT, .expect_contains(r"src/delta.rs: mode +x");
&config,
);
let output = strip_ansi_codes(&output);
assert!(output.contains(r"src/delta.rs: mode +x"));
} }
#[test] #[test]
fn test_file_mode_change_lose_executable_bit() { fn test_file_mode_change_lose_executable_bit() {
let config = integration_test_utils::make_config_from_args(&[]); DeltaTest::with_args(&[])
let output = integration_test_utils::run_delta( .with_input(GIT_DIFF_FILE_MODE_CHANGE_LOSE_EXECUTABLE_BIT)
GIT_DIFF_FILE_MODE_CHANGE_LOSE_EXECUTABLE_BIT, .expect_contains(r"src/delta.rs: mode -x");
&config,
);
let output = strip_ansi_codes(&output);
assert!(output.contains(r"src/delta.rs: mode -x"));
} }
#[test] #[test]
fn test_file_mode_change_unexpected_bits() { fn test_file_mode_change_unexpected_bits() {
let config = DeltaTest::with_args(&["--navigate", "--right-arrow=->"])
integration_test_utils::make_config_from_args(&["--navigate", "--right-arrow=->"]); .with_input(GIT_DIFF_FILE_MODE_CHANGE_UNEXPECTED_BITS)
let output = .expect_contains(r"Δ src/delta.rs: 100700 -> 100644");
integration_test_utils::run_delta(GIT_DIFF_FILE_MODE_CHANGE_UNEXPECTED_BITS, &config);
let output = strip_ansi_codes(&output);
assert!(output.contains(r"Δ src/delta.rs: 100700 -> 100644"));
} }
#[test] #[test]
fn test_file_mode_change_with_diff() { fn test_file_mode_change_with_diff() {
let config = integration_test_utils::make_config_from_args(&[ DeltaTest::with_args(&["--navigate", "--keep-plus-minus-markers"])
"--navigate", .with_input(GIT_DIFF_FILE_MODE_CHANGE_WITH_DIFF)
"--keep-plus-minus-markers", .expect_contains("Δ src/script: mode +x")
]); .expect_after_skip(
let output = 5,
integration_test_utils::run_delta(GIT_DIFF_FILE_MODE_CHANGE_WITH_DIFF, &config); "
let output = strip_ansi_codes(&output);
let re = Regex::new(r"\n─+\n").unwrap(); 1:
let output = re.replace(&output, "\n-----\n");
assert!(output.contains( -#!/bin/sh
"Δ src/script: mode +x +#!/bin/bash",
----- );
1:
-#!/bin/sh
+#!/bin/bash
"
));
} }
#[test] #[test]
fn test_hyperlinks_commit_link_format() { fn test_hyperlinks_commit_link_format() {
let config = integration_test_utils::make_config_from_args(&[ // If commit-style is not set then the commit line is handled in raw
// If commit-style is not set then the commit line is handled in raw // mode, in which case we only format hyperlinks if output is a tty;
// mode, in which case we only format hyperlinks if output is a tty; // this causes the test to fail on Github Actions, but pass locally
// this causes the test to fail on Github Actions, but pass locally // if output is left going to the screen.
// if output is left going to the screen. DeltaTest::with_args(&[
"--commit-style", "--commit-style",
"blue", "blue",
"--hyperlinks", "--hyperlinks",
"--hyperlinks-commit-link-format", "--hyperlinks-commit-link-format",
"https://invent.kde.org/utilities/konsole/-/commit/{commit}", "https://invent.kde.org/utilities/konsole/-/commit/{commit}",
]); ])
let output = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, &config); .with_input(GIT_DIFF_SINGLE_HUNK)
assert!(output.contains(r"https://invent.kde.org/utilities/konsole/-/commit/94907c0f136f46dc46ffae2dc92dca9af7eb7c2e")); .expect_raw_contains(
r"https://invent.kde.org/utilities/konsole/-/commit/94907c0f136f46dc46ffae2dc92dca9af7eb7c2e"
);
} }
#[test] #[test]
fn test_filenames_with_spaces() { fn test_filenames_with_spaces() {
let config = integration_test_utils::make_config_from_args(&[]); DeltaTest::with_args(&[])
let output = .with_input(GIT_DIFF_NO_INDEX_FILENAMES_WITH_SPACES)
integration_test_utils::run_delta(GIT_DIFF_NO_INDEX_FILENAMES_WITH_SPACES, &config); .expect_contains("a b ⟶ c d\n");
let output = strip_ansi_codes(&output);
assert!(output.contains("a b ⟶ c d\n"));
} }
const GIT_DIFF_SINGLE_HUNK: &str = "\ const GIT_DIFF_SINGLE_HUNK: &str = "\

View File

@ -903,7 +903,7 @@ index 223ca50..e69de29 100644
#[test] #[test]
fn test_wrap_with_unequal_hunk_zero_width() { fn test_wrap_with_unequal_hunk_zero_width() {
DeltaTest::with(&default_wrap_cfg_plus(&[ DeltaTest::with_args(&default_wrap_cfg_plus(&[
"--side-by-side", "--side-by-side",
"--line-numbers-left-format", "--line-numbers-left-format",
"│L│", "│L│",
@ -914,9 +914,9 @@ index 223ca50..e69de29 100644
"--line-fill-method", "--line-fill-method",
"spaces", "spaces",
])) ]))
.set_cfg(|cfg| cfg.truncation_symbol = ">".into()) .set_config(|cfg| cfg.truncation_symbol = ">".into())
.with_input(HUNK_ZERO_DIFF) .with_input(HUNK_ZERO_DIFF)
.expect( .expect_after_header(
r#" r#"
Labcdefghijklm+ RRRRabcdefghijklm+ Labcdefghijklm+ RRRRabcdefghijklm+
Lnopqrstuvwxzy+ RRRRnopqrstuvwxzy+ Lnopqrstuvwxzy+ RRRRnopqrstuvwxzy+
@ -929,7 +929,7 @@ index 223ca50..e69de29 100644
#[test] #[test]
fn test_wrap_with_large_hunk_zero_line_numbers() { fn test_wrap_with_large_hunk_zero_line_numbers() {
DeltaTest::with(&default_wrap_cfg_plus(&[ DeltaTest::with_args(&default_wrap_cfg_plus(&[
"--side-by-side", "--side-by-side",
"--line-numbers-left-format", "--line-numbers-left-format",
"│LLL│", "│LLL│",
@ -940,9 +940,9 @@ index 223ca50..e69de29 100644
"--line-fill-method", "--line-fill-method",
"ansi", "ansi",
])) ]))
.set_cfg(|cfg| cfg.truncation_symbol = ">".into()) .set_config(|cfg| cfg.truncation_symbol = ">".into())
.with_input(HUNK_ZERO_LARGE_LINENUMBERS_DIFF) .with_input(HUNK_ZERO_LARGE_LINENUMBERS_DIFF)
.expect( .expect_after_header(
r#" r#"
LLLabcde+ WW 10 +- 101999 WWabcde+ LLLabcde+ WW 10 +- 101999 WWabcde+
LLLfghij+ WW +- WWfghij+ LLLfghij+ WW +- WWfghij+
@ -956,15 +956,15 @@ index 223ca50..e69de29 100644
#[test] #[test]
fn test_wrap_with_keep_markers() { fn test_wrap_with_keep_markers() {
use crate::features::side_by_side::ansifill::ODD_PAD_CHAR; use crate::features::side_by_side::ansifill::ODD_PAD_CHAR;
let t = DeltaTest::with(&default_wrap_cfg_plus(&[ let t = DeltaTest::with_args(&default_wrap_cfg_plus(&[
"--side-by-side", "--side-by-side",
"--keep-plus-minus-markers", "--keep-plus-minus-markers",
"--width", "--width",
"45", "45",
])) ]))
.set_cfg(|cfg| cfg.truncation_symbol = ">".into()) .set_config(|cfg| cfg.truncation_symbol = ">".into())
.with_input(HUNK_MP_DIFF); .with_input(HUNK_MP_DIFF)
let output = t.expect( .expect_after_header(
r#" r#"
4 abcdefghijklmn+ 15 abcdefghijklmn+ 4 abcdefghijklmn+ 15 abcdefghijklmn+
opqrstuvwxzy 0+ opqrstuvwxzy 0+ opqrstuvwxzy 0+ opqrstuvwxzy 0+
@ -979,138 +979,133 @@ index 223ca50..e69de29 100644
// this column here is^ where ODD_PAD_CHAR is inserted due to the odd 45 width // this column here is^ where ODD_PAD_CHAR is inserted due to the odd 45 width
); );
assert!(!output.is_empty()); assert!(!t.output.is_empty());
for line in output.lines().skip(crate::config::HEADER_LEN) { for line in t.output.lines().skip(crate::config::HEADER_LEN) {
assert_eq!(line.chars().nth(22), Some(ODD_PAD_CHAR)); assert_eq!(line.chars().nth(22), Some(ODD_PAD_CHAR));
} }
} }
#[test] #[test]
fn test_alignment_2_lines_vs_3_lines() { fn test_alignment_2_lines_vs_3_lines() {
let config = let config_1 =
make_config_from_args(&default_wrap_cfg_plus(&["--side-by-side", "--width", "55"])); || make_config_from_args(&default_wrap_cfg_plus(&["--side-by-side", "--width", "55"]));
{ {
DeltaTest::with_config_and_input( DeltaTest::with_config(config_1())
&config, .with_input(&format!(
&format!(
"{}-{}+{}", "{}-{}+{}",
HUNK_ALIGN_DIFF_HEADER, HUNK_ALIGN_DIFF_SHORT, HUNK_ALIGN_DIFF_LONG HUNK_ALIGN_DIFF_HEADER, HUNK_ALIGN_DIFF_SHORT, HUNK_ALIGN_DIFF_LONG
), ))
) .expect_after_header(
.expect( r#"
r#" 1 .........1.........2< 1 .........1.........2+
1 .........1.........2< 1 .........1.........2+ >.... .........3.........4+
>.... .........3.........4+ .........5.........6"#,
.........5.........6"#, );
);
// the place where ODD_PAD_CHAR^ is inserted due to the odd 55 width // the place where ODD_PAD_CHAR^ is inserted due to the odd 55 width
} }
{ {
DeltaTest::with_config_and_input( DeltaTest::with_config(config_1())
&config, .with_input(&format!(
&format!(
"{}-{}+{}", "{}-{}+{}",
HUNK_ALIGN_DIFF_HEADER, HUNK_ALIGN_DIFF_LONG, HUNK_ALIGN_DIFF_SHORT HUNK_ALIGN_DIFF_HEADER, HUNK_ALIGN_DIFF_LONG, HUNK_ALIGN_DIFF_SHORT
), ))
) .expect_after_header(
.expect( r#"
r#" 1 .........1.........2+ 1 .........1.........2<
1 .........1.........2+ 1 .........1.........2< .........3.........4+ >....
.........3.........4+ >.... .........5.........6 "#,
.........5.........6 "#, );
);
} }
} }
#[test] #[test]
fn test_alignment_1_line_vs_3_lines() { fn test_alignment_1_line_vs_3_lines() {
let config = make_config_from_args(&default_wrap_cfg_plus(&[ let config_2 = || {
"--side-by-side", make_config_from_args(&default_wrap_cfg_plus(&[
"--width", "--side-by-side",
"61", "--width",
"--line-fill-method", "61",
"spaces", "--line-fill-method",
])); "spaces",
]))
};
{ {
DeltaTest::with_config_and_input( DeltaTest::with_config(config_2())
&config, .with_input(&format!(
&format!(
"{}-{}+{}", "{}-{}+{}",
HUNK_ALIGN_DIFF_HEADER, HUNK_ALIGN_DIFF_SHORT, HUNK_ALIGN_DIFF_LONG HUNK_ALIGN_DIFF_HEADER, HUNK_ALIGN_DIFF_SHORT, HUNK_ALIGN_DIFF_LONG
), ))
) .expect_after_header(
.expect( r#"
r#" 1 .........1.........2.... 1 .........1.........2...+
1 .........1.........2.... 1 .........1.........2...+ ......3.........4......+
......3.........4......+ ...5.........6 "#,
...5.........6 "#, );
);
} }
{ {
DeltaTest::with_config_and_input( DeltaTest::with_config(config_2())
&config, .with_input(&format!(
&format!(
"{}-{}+{}", "{}-{}+{}",
HUNK_ALIGN_DIFF_HEADER, HUNK_ALIGN_DIFF_LONG, HUNK_ALIGN_DIFF_SHORT HUNK_ALIGN_DIFF_HEADER, HUNK_ALIGN_DIFF_LONG, HUNK_ALIGN_DIFF_SHORT
), ))
) .expect_after_header(
.expect( r#"
r#" 1 .........1.........2...+ 1 .........1.........2....
1 .........1.........2...+ 1 .........1.........2.... ......3.........4......+
......3.........4......+ ...5.........6 "#,
...5.........6 "#, );
);
} }
} }
#[test] #[test]
fn test_wrap_max_lines_2() { fn test_wrap_max_lines_2() {
// TODO overriding is not possible, need to change config directly // TODO overriding is not possible, need to change config directly
let mut config = make_config_from_args(&default_wrap_cfg_plus(&[ let config_3 = || {
// "--wrap-max-lines", let mut config = make_config_from_args(&default_wrap_cfg_plus(&[
// "2", // "--wrap-max-lines",
"--side-by-side", // "2",
"--width", "--side-by-side",
"72", "--width",
"--line-fill-method", "72",
"spaces", "--line-fill-method",
])); "spaces",
config.truncation_symbol = ">".into(); ]));
config.truncation_symbol = ">".into();
config
};
{ {
DeltaTest::with_config_and_input( DeltaTest::with_config(config_3())
&config, .with_input(&format!(
&format!(
"{}-{}+{}", "{}-{}+{}",
HUNK_ALIGN_DIFF_HEADER, HUNK_ALIGN_DIFF_SHORT, HUNK_ALIGN_DIFF_LONG HUNK_ALIGN_DIFF_HEADER, HUNK_ALIGN_DIFF_SHORT, HUNK_ALIGN_DIFF_LONG
), ))
) .expect_after_header(
.expect( r#"
r#" 1 .........1.........2.... 1 .........1.........2.........+
1 .........1.........2.... 1 .........1.........2.........+ 3.........4.........5........+
3.........4.........5........+ .6 "#,
.6 "#, );
);
} }
{ {
let mut config = config_3();
config.wrap_config.max_lines = 2; config.wrap_config.max_lines = 2;
DeltaTest::with_config_and_input( DeltaTest::with_config(config)
&config, .with_input(&format!(
&format!(
"{}-{}+{}", "{}-{}+{}",
HUNK_ALIGN_DIFF_HEADER, HUNK_ALIGN_DIFF_SHORT, HUNK_ALIGN_DIFF_LONG HUNK_ALIGN_DIFF_HEADER, HUNK_ALIGN_DIFF_SHORT, HUNK_ALIGN_DIFF_LONG
), ))
) .expect_after_header(
.expect( r#"
r#" 1 .........1.........2.... 1 .........1.........2.........+
1 .........1.........2.... 1 .........1.........2.........+ 3.........4.........5........>"#,
3.........4.........5........>"#, );
);
} }
} }
} }