diff options
Diffstat (limited to 'vendor/pretty_assertions/src')
-rw-r--r-- | vendor/pretty_assertions/src/lib.rs | 475 | ||||
-rw-r--r-- | vendor/pretty_assertions/src/printer.rs | 474 |
2 files changed, 949 insertions, 0 deletions
diff --git a/vendor/pretty_assertions/src/lib.rs b/vendor/pretty_assertions/src/lib.rs new file mode 100644 index 000000000..5c1787533 --- /dev/null +++ b/vendor/pretty_assertions/src/lib.rs @@ -0,0 +1,475 @@ +//! # Pretty Assertions +//! +//! When writing tests in Rust, you'll probably use `assert_eq!(a, b)` _a lot_. +//! +//! If such a test fails, it will present all the details of `a` and `b`. +//! But you have to spot the differences yourself, which is not always straightforward, +//! like here: +//! +//! ![standard assertion](https://raw.githubusercontent.com/colin-kiegel/rust-pretty-assertions/2d2357ff56d22c51a86b2f1cfe6efcee9f5a8081/examples/standard_assertion.png) +//! +//! Wouldn't that task be _much_ easier with a colorful diff? +//! +//! ![pretty assertion](https://raw.githubusercontent.com/colin-kiegel/rust-pretty-assertions/2d2357ff56d22c51a86b2f1cfe6efcee9f5a8081/examples/pretty_assertion.png) +//! +//! Yep — and you only need **one line of code** to make it happen: +//! +//! ```rust +//! use pretty_assertions::{assert_eq, assert_ne}; +//! ``` +//! +//! <details> +//! <summary>Show the example behind the screenshots above.</summary> +//! +//! ```rust,should_panic +//! // 1. add the `pretty_assertions` dependency to `Cargo.toml`. +//! // 2. insert this line at the top of each module, as needed +//! use pretty_assertions::{assert_eq, assert_ne}; +//! +//! #[derive(Debug, PartialEq)] +//! struct Foo { +//! lorem: &'static str, +//! ipsum: u32, +//! dolor: Result<String, String>, +//! } +//! +//! let x = Some(Foo { lorem: "Hello World!", ipsum: 42, dolor: Ok("hey".to_string())}); +//! let y = Some(Foo { lorem: "Hello Wrold!", ipsum: 42, dolor: Ok("hey ho!".to_string())}); +//! +//! assert_eq!(x, y); +//! ``` +//! </details> +//! +//! ## Tip +//! +//! Specify it as [`[dev-dependencies]`](http://doc.crates.io/specifying-dependencies.html#development-dependencies) +//! and it will only be used for compiling tests, examples, and benchmarks. +//! This way the compile time of `cargo build` won't be affected! +//! +//! Also add `#[cfg(test)]` to your `use` statements, like this: +//! +//! ```rust +//! #[cfg(test)] +//! use pretty_assertions::{assert_eq, assert_ne}; +//! ``` +//! +//! ## Note +//! +//! * Since `Rust 2018` edition, you need to declare +//! `use pretty_assertions::{assert_eq, assert_ne};` per module. +//! Before you would write `#[macro_use] extern crate pretty_assertions;`. +//! * The replacement is only effective in your own crate, not in other libraries +//! you include. +//! * `assert_ne` is also switched to multi-line presentation, but does _not_ show +//! a diff. + +#![deny(clippy::all, missing_docs, unsafe_code)] + +pub use ansi_term::Style; +use std::fmt::{self, Debug, Display}; + +mod printer; + +#[cfg(windows)] +use ctor::*; +#[cfg(windows)] +#[ctor] +fn init() { + output_vt100::try_init().ok(); // Do not panic on fail +} + +/// A comparison of two values. +/// +/// Where both values implement `Debug`, the comparison can be displayed as a pretty diff. +/// +/// ``` +/// use pretty_assertions::Comparison; +/// +/// print!("{}", Comparison::new(&123, &134)); +/// ``` +/// +/// The values may have different types, although in practice they are usually the same. +pub struct Comparison<'a, TLeft, TRight> +where + TLeft: ?Sized, + TRight: ?Sized, +{ + left: &'a TLeft, + right: &'a TRight, +} + +impl<'a, TLeft, TRight> Comparison<'a, TLeft, TRight> +where + TLeft: ?Sized, + TRight: ?Sized, +{ + /// Store two values to be compared in future. + /// + /// Expensive diffing is deferred until calling `Debug::fmt`. + pub fn new(left: &'a TLeft, right: &'a TRight) -> Comparison<'a, TLeft, TRight> { + Comparison { left, right } + } +} + +impl<'a, TLeft, TRight> Display for Comparison<'a, TLeft, TRight> +where + TLeft: Debug + ?Sized, + TRight: Debug + ?Sized, +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + // To diff arbitary types, render them as debug strings + let left_debug = format!("{:#?}", self.left); + let right_debug = format!("{:#?}", self.right); + // And then diff the debug output + printer::write_header(f)?; + printer::write_lines(f, &left_debug, &right_debug) + } +} + +/// Asserts that two expressions are equal to each other (using [`PartialEq`]). +/// +/// On panic, this macro will print a diff derived from [`Debug`] representation of +/// each value. +/// +/// This is a drop in replacement for [`std::assert_eq!`]. +/// You can provide a custom panic message if desired. +/// +/// # Examples +/// +/// ``` +/// use pretty_assertions::assert_eq; +/// +/// let a = 3; +/// let b = 1 + 2; +/// assert_eq!(a, b); +/// +/// assert_eq!(a, b, "we are testing addition with {} and {}", a, b); +/// ``` +#[macro_export] +macro_rules! assert_eq { + ($left:expr , $right:expr,) => ({ + $crate::assert_eq!($left, $right) + }); + ($left:expr , $right:expr) => ({ + match (&($left), &($right)) { + (left_val, right_val) => { + if !(*left_val == *right_val) { + ::std::panic!("assertion failed: `(left == right)`\ + \n\ + \n{}\ + \n", + $crate::Comparison::new(left_val, right_val)) + } + } + } + }); + ($left:expr , $right:expr, $($arg:tt)*) => ({ + match (&($left), &($right)) { + (left_val, right_val) => { + if !(*left_val == *right_val) { + ::std::panic!("assertion failed: `(left == right)`: {}\ + \n\ + \n{}\ + \n", + format_args!($($arg)*), + $crate::Comparison::new(left_val, right_val)) + } + } + } + }); +} + +/// Asserts that two expressions are not equal to each other (using [`PartialEq`]). +/// +/// On panic, this macro will print the values of the expressions with their +/// [`Debug`] representations. +/// +/// This is a drop in replacement for [`std::assert_ne!`]. +/// You can provide a custom panic message if desired. +/// +/// # Examples +/// +/// ``` +/// use pretty_assertions::assert_ne; +/// +/// let a = 3; +/// let b = 2; +/// assert_ne!(a, b); +/// +/// assert_ne!(a, b, "we are testing that the values are not equal"); +/// ``` +#[macro_export] +macro_rules! assert_ne { + ($left:expr, $right:expr) => ({ + $crate::assert_ne!(@ $left, $right, "", ""); + }); + ($left:expr, $right:expr,) => ({ + $crate::assert_ne!(@ $left, $right, "", ""); + }); + ($left:expr, $right:expr, $($arg:tt)+) => ({ + $crate::assert_ne!(@ $left, $right, ": ", $($arg)+); + }); + (@ $left:expr, $right:expr, $maybe_semicolon:expr, $($arg:tt)+) => ({ + match (&($left), &($right)) { + (left_val, right_val) => { + if *left_val == *right_val { + let left_dbg = ::std::format!("{:?}", &*left_val); + let right_dbg = ::std::format!("{:?}", &*right_val); + if left_dbg != right_dbg { + + ::std::panic!("assertion failed: `(left != right)`{}{}\ + \n\ + \n{}\ + \n{}: According to the `PartialEq` implementation, both of the values \ + are partially equivalent, even if the `Debug` outputs differ.\ + \n\ + \n", + $maybe_semicolon, + format_args!($($arg)+), + $crate::Comparison::new(left_val, right_val), + $crate::Style::new() + .bold() + .underline() + .paint("Note")) + } + + ::std::panic!("assertion failed: `(left != right)`{}{}\ + \n\ + \n{}:\ + \n{:#?}\ + \n\ + \n", + $maybe_semicolon, + format_args!($($arg)+), + $crate::Style::new().bold().paint("Both sides"), + left_val) + } + } + } + }); +} + +#[cfg(test)] +#[allow(clippy::eq_op)] +#[no_implicit_prelude] +mod test { + mod assert_eq { + use ::std::string::{String, ToString}; + + #[test] + fn passes() { + let a = "some value"; + crate::assert_eq!(a, a); + } + + #[test] + fn passes_unsized() { + let a: &[u8] = b"e"; + crate::assert_eq!(*a, *a); + } + + #[test] + fn passes_comparable_types() { + let s0: &'static str = "foo"; + let s1: String = "foo".to_string(); + crate::assert_eq!(s0, s1); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left == right)` + +[1mDiff[0m [31m< left[0m / [32mright >[0m : +[31m<[0m[1;48;5;52;31m666[0m +[32m>[0m[1;48;5;22;32m999[0m + +"#)] + fn fails() { + crate::assert_eq!(666, 999); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left == right)` + +[1mDiff[0m [31m< left[0m / [32mright >[0m : +[31m<[0m[1;48;5;52;31m666[0m +[32m>[0m[1;48;5;22;32m999[0m + +"#)] + fn fails_trailing_comma() { + crate::assert_eq!(666, 999,); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left == right)` + +[1mDiff[0m [31m< left[0m / [32mright >[0m : + [ + 101, +[32m> 101,[0m + ] + +"#)] + fn fails_unsized() { + let a: &[u8] = b"e"; + let b: &[u8] = b"ee"; + crate::assert_eq!(*a, *b); + } + + #[test] + #[should_panic( + expected = r#"assertion failed: `(left == right)`: custom panic message + +[1mDiff[0m [31m< left[0m / [32mright >[0m : +[31m<[0m[1;48;5;52;31m666[0m +[32m>[0m[1;48;5;22;32m999[0m + +"# + )] + fn fails_custom() { + crate::assert_eq!(666, 999, "custom panic message"); + } + + #[test] + #[should_panic( + expected = r#"assertion failed: `(left == right)`: custom panic message + +[1mDiff[0m [31m< left[0m / [32mright >[0m : +[31m<[0m[1;48;5;52;31m666[0m +[32m>[0m[1;48;5;22;32m999[0m + +"# + )] + fn fails_custom_trailing_comma() { + crate::assert_eq!(666, 999, "custom panic message",); + } + } + + mod assert_ne { + use ::std::string::{String, ToString}; + + #[test] + fn passes() { + let a = "a"; + let b = "b"; + crate::assert_ne!(a, b); + } + + #[test] + fn passes_unsized() { + let a: &[u8] = b"e"; + let b: &[u8] = b"ee"; + crate::assert_ne!(*a, *b); + } + + #[test] + fn passes_comparable_types() { + let s0: &'static str = "foo"; + let s1: String = "bar".to_string(); + crate::assert_ne!(s0, s1); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left != right)` + +[1mBoth sides[0m: +666 +"#)] + fn fails() { + crate::assert_ne!(666, 666); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left != right)` + +[1mBoth sides[0m: +666 +"#)] + fn fails_trailing_comma() { + crate::assert_ne!(666, 666,); + } + + #[test] + #[should_panic(expected = r#"assertion failed: `(left != right)` + +[1mBoth sides[0m: +[ + 101, +] + +"#)] + fn fails_unsized() { + let a: &[u8] = b"e"; + crate::assert_ne!(*a, *a); + } + + #[test] + #[should_panic( + expected = r#"assertion failed: `(left != right)`: custom panic message + +[1mBoth sides[0m: +666 +"# + )] + fn fails_custom() { + crate::assert_ne!(666, 666, "custom panic message"); + } + + #[test] + #[should_panic( + expected = r#"assertion failed: `(left != right)`: custom panic message + +[1mBoth sides[0m: +666 +"# + )] + fn fails_custom_trailing_comma() { + crate::assert_ne!(666, 666, "custom panic message",); + } + + // If the values are equal but their debug outputs are not + // show a specific warning + + #[test] + #[should_panic(expected = r#"assertion failed: `(left != right)` + +[1mDiff[0m [31m< left[0m / [32mright >[0m : +[31m<[0m[1;48;5;52;31m-[0m[31m0.0[0m +[32m>0.0[0m + +[1;4mNote[0m: According to the `PartialEq` implementation, both of the values are partially equivalent, even if the `Debug` outputs differ. + +"#)] + fn assert_ne_partial() { + // Workaround for https://github.com/rust-lang/rust/issues/47619 + // can be removed, when we require rust 1.25 or higher + struct Foo(f32); + + use ::std::fmt; + impl fmt::Debug for Foo { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + ::std::write!(f, "{:.1?}", self.0) + } + } + + impl ::std::cmp::PartialEq for Foo { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 + } + } + + crate::assert_ne!(Foo(-0.0), Foo(0.0)); + } + + // Regression tests + + #[test] + #[should_panic] + fn assert_ne_non_empty_return() { + fn not_zero(x: u32) -> u32 { + crate::assert_ne!(x, 0); + x + } + not_zero(0); + } + } +} diff --git a/vendor/pretty_assertions/src/printer.rs b/vendor/pretty_assertions/src/printer.rs new file mode 100644 index 000000000..172b1ed6c --- /dev/null +++ b/vendor/pretty_assertions/src/printer.rs @@ -0,0 +1,474 @@ +use ansi_term::{ + Colour::{Fixed, Green, Red}, + Style, +}; +use std::fmt; + +macro_rules! paint { + ($f:expr, $colour:expr, $fmt:expr, $($args:tt)*) => ( + write!($f, "{}", $colour.paint(format!($fmt, $($args)*))) + ) +} + +const SIGN_RIGHT: char = '>'; // + > → +const SIGN_LEFT: char = '<'; // - < ← + +/// Present the diff output for two mutliline strings in a pretty, colorised manner. +pub(crate) fn write_header(f: &mut fmt::Formatter) -> fmt::Result { + writeln!( + f, + "{} {} / {} :", + Style::new().bold().paint("Diff"), + Red.paint(format!("{} left", SIGN_LEFT)), + Green.paint(format!("right {}", SIGN_RIGHT)) + ) +} + +/// Delay formatting this deleted chunk until later. +/// +/// It can be formatted as a whole chunk by calling `flush`, or the inner value +/// obtained with `take` for further processing. +#[derive(Default)] +struct LatentDeletion<'a> { + // The most recent deleted line we've seen + value: Option<&'a str>, + // The number of deleted lines we've seen, including the current value + count: usize, +} + +impl<'a> LatentDeletion<'a> { + /// Set the chunk value. + fn set(&mut self, value: &'a str) { + self.value = Some(value); + self.count += 1; + } + + /// Take the underlying chunk value, if it's suitable for inline diffing. + /// + /// If there is no value of we've seen more than one line, return `None`. + fn take(&mut self) -> Option<&'a str> { + if self.count == 1 { + self.value.take() + } else { + None + } + } + + /// If a value is set, print it as a whole chunk, using the given formatter. + /// + /// If a value is not set, reset the count to zero (as we've called `flush` twice, + /// without seeing another deletion. Therefore the line in the middle was something else). + fn flush<TWrite: fmt::Write>(&mut self, f: &mut TWrite) -> fmt::Result { + if let Some(value) = self.value { + paint!(f, Red, "{}{}", SIGN_LEFT, value)?; + writeln!(f)?; + self.value = None; + } else { + self.count = 0; + } + + Ok(()) + } +} + +// Adapted from: +// https://github.com/johannhof/difference.rs/blob/c5749ad7d82aa3d480c15cb61af9f6baa08f116f/examples/github-style.rs +// Credits johannhof (MIT License) + +/// Present the diff output for two mutliline strings in a pretty, colorised manner. +pub(crate) fn write_lines<TWrite: fmt::Write>( + f: &mut TWrite, + left: &str, + right: &str, +) -> fmt::Result { + let diff = ::diff::lines(left, right); + + let mut changes = diff.into_iter().peekable(); + let mut previous_deletion = LatentDeletion::default(); + + while let Some(change) = changes.next() { + match (change, changes.peek()) { + // If the text is unchanged, just print it plain + (::diff::Result::Both(value, _), _) => { + previous_deletion.flush(f)?; + writeln!(f, " {}", value)?; + } + // Defer any deletions to next loop + (::diff::Result::Left(deleted), _) => { + previous_deletion.flush(f)?; + previous_deletion.set(deleted); + } + // Underlying diff library should never return this sequence + (::diff::Result::Right(_), Some(::diff::Result::Left(_))) => { + panic!("insertion followed by deletion"); + } + // If we're being followed by more insertions, don't inline diff + (::diff::Result::Right(inserted), Some(::diff::Result::Right(_))) => { + previous_deletion.flush(f)?; + paint!(f, Green, "{}{}", SIGN_RIGHT, inserted)?; + writeln!(f)?; + } + // Otherwise, check if we need to inline diff with the previous line (if it was a deletion) + (::diff::Result::Right(inserted), _) => { + if let Some(deleted) = previous_deletion.take() { + write_inline_diff(f, deleted, inserted)?; + } else { + previous_deletion.flush(f)?; + paint!(f, Green, "{}{}", SIGN_RIGHT, inserted)?; + writeln!(f)?; + } + } + }; + } + + previous_deletion.flush(f)?; + Ok(()) +} + +/// Group character styling for an inline diff, to prevent wrapping each single +/// character in terminal styling codes. +/// +/// Styles are applied automatically each time a new style is given in `write_with_style`. +struct InlineWriter<'a, Writer> { + f: &'a mut Writer, + style: Style, +} + +impl<'a, Writer> InlineWriter<'a, Writer> +where + Writer: fmt::Write, +{ + fn new(f: &'a mut Writer) -> Self { + InlineWriter { + f, + style: Style::new(), + } + } + + /// Push a new character into the buffer, specifying the style it should be written in. + fn write_with_style(&mut self, c: &char, style: Style) -> fmt::Result { + // If the style is the same as previously, just write character + if style == self.style { + write!(self.f, "{}", c)?; + } else { + // Close out previous style + write!(self.f, "{}", self.style.suffix())?; + + // Store new style and start writing it + write!(self.f, "{}{}", style.prefix(), c)?; + self.style = style; + } + Ok(()) + } + + /// Finish any existing style and reset to default state. + fn finish(&mut self) -> fmt::Result { + // Close out previous style + writeln!(self.f, "{}", self.style.suffix())?; + self.style = Default::default(); + Ok(()) + } +} + +/// Format a single line to show an inline diff of the two strings given. +/// +/// The given strings should not have a trailing newline. +/// +/// The output of this function will be two lines, each with a trailing newline. +fn write_inline_diff<TWrite: fmt::Write>(f: &mut TWrite, left: &str, right: &str) -> fmt::Result { + let diff = ::diff::chars(left, right); + let mut writer = InlineWriter::new(f); + + // Print the left string on one line, with differences highlighted + let light = Red.into(); + let heavy = Red.on(Fixed(52)).bold(); + writer.write_with_style(&SIGN_LEFT, light)?; + for change in diff.iter() { + match change { + ::diff::Result::Both(value, _) => writer.write_with_style(value, light)?, + ::diff::Result::Left(value) => writer.write_with_style(value, heavy)?, + _ => (), + } + } + writer.finish()?; + + // Print the right string on one line, with differences highlighted + let light = Green.into(); + let heavy = Green.on(Fixed(22)).bold(); + writer.write_with_style(&SIGN_RIGHT, light)?; + for change in diff.iter() { + match change { + ::diff::Result::Both(value, _) => writer.write_with_style(value, light)?, + ::diff::Result::Right(value) => writer.write_with_style(value, heavy)?, + _ => (), + } + } + writer.finish() +} + +#[cfg(test)] +mod test { + use super::*; + + // ANSI terminal codes used in our outputs. + // + // Interpolate these into test strings to make expected values easier to read. + const RED_LIGHT: &str = "\u{1b}[31m"; + const GREEN_LIGHT: &str = "\u{1b}[32m"; + const RED_HEAVY: &str = "\u{1b}[1;48;5;52;31m"; + const GREEN_HEAVY: &str = "\u{1b}[1;48;5;22;32m"; + const RESET: &str = "\u{1b}[0m"; + + /// Given that both of our diff printing functions have the same + /// type signature, we can reuse the same test code for them. + /// + /// This could probably be nicer with traits! + fn check_printer<TPrint>(printer: TPrint, left: &str, right: &str, expected: &str) + where + TPrint: Fn(&mut String, &str, &str) -> fmt::Result, + { + let mut actual = String::new(); + printer(&mut actual, left, right).expect("printer function failed"); + + println!( + "## left ##\n\ + {}\n\ + ## right ##\n\ + {}\n\ + ## actual diff ##\n\ + {}\n\ + ## expected diff ##\n\ + {}", + left, right, actual, expected + ); + assert_eq!(actual, expected); + } + + #[test] + fn write_inline_diff_empty() { + let left = ""; + let right = ""; + let expected = format!( + "{red_light}<{reset}\n\ + {green_light}>{reset}\n", + red_light = RED_LIGHT, + green_light = GREEN_LIGHT, + reset = RESET, + ); + + check_printer(write_inline_diff, left, right, &expected); + } + + #[test] + fn write_inline_diff_added() { + let left = ""; + let right = "polymerase"; + let expected = format!( + "{red_light}<{reset}\n\ + {green_light}>{reset}{green_heavy}polymerase{reset}\n", + red_light = RED_LIGHT, + green_light = GREEN_LIGHT, + green_heavy = GREEN_HEAVY, + reset = RESET, + ); + + check_printer(write_inline_diff, left, right, &expected); + } + + #[test] + fn write_inline_diff_removed() { + let left = "polyacrylamide"; + let right = ""; + let expected = format!( + "{red_light}<{reset}{red_heavy}polyacrylamide{reset}\n\ + {green_light}>{reset}\n", + red_light = RED_LIGHT, + green_light = GREEN_LIGHT, + red_heavy = RED_HEAVY, + reset = RESET, + ); + + check_printer(write_inline_diff, left, right, &expected); + } + + #[test] + fn write_inline_diff_changed() { + let left = "polymerase"; + let right = "polyacrylamide"; + let expected = format!( + "{red_light}<poly{reset}{red_heavy}me{reset}{red_light}ra{reset}{red_heavy}s{reset}{red_light}e{reset}\n\ + {green_light}>poly{reset}{green_heavy}ac{reset}{green_light}r{reset}{green_heavy}yl{reset}{green_light}a{reset}{green_heavy}mid{reset}{green_light}e{reset}\n", + red_light = RED_LIGHT, + green_light = GREEN_LIGHT, + red_heavy = RED_HEAVY, + green_heavy = GREEN_HEAVY, + reset = RESET, + ); + + check_printer(write_inline_diff, left, right, &expected); + } + + /// If one of our strings is empty, it should not be shown at all in the output. + #[test] + fn write_lines_empty_string() { + let left = ""; + let right = "content"; + let expected = format!( + "{green_light}>content{reset}\n", + green_light = GREEN_LIGHT, + reset = RESET, + ); + + check_printer(write_lines, left, right, &expected); + } + + /// Realistic multiline struct diffing case. + #[test] + fn write_lines_struct() { + let left = r#"Some( + Foo { + lorem: "Hello World!", + ipsum: 42, + dolor: Ok( + "hey", + ), + }, +)"#; + let right = r#"Some( + Foo { + lorem: "Hello Wrold!", + ipsum: 42, + dolor: Ok( + "hey ho!", + ), + }, +)"#; + let expected = format!( + r#" Some( + Foo {{ +{red_light}< lorem: "Hello W{reset}{red_heavy}o{reset}{red_light}rld!",{reset} +{green_light}> lorem: "Hello Wr{reset}{green_heavy}o{reset}{green_light}ld!",{reset} + ipsum: 42, + dolor: Ok( +{red_light}< "hey",{reset} +{green_light}> "hey{reset}{green_heavy} ho!{reset}{green_light}",{reset} + ), + }}, + ) +"#, + red_light = RED_LIGHT, + red_heavy = RED_HEAVY, + green_light = GREEN_LIGHT, + green_heavy = GREEN_HEAVY, + reset = RESET, + ); + + check_printer(write_lines, left, right, &expected); + } + + /// Relistic multiple line chunks + /// + /// We can't support realistic line diffing in large blocks + /// (also, it's unclear how usefult this is) + /// + /// So if we have more than one line in a single removal chunk, disable inline diffing. + #[test] + fn write_lines_multiline_block() { + let left = r#"Proboscis +Cabbage"#; + let right = r#"Probed +Caravaggio"#; + let expected = format!( + r#"{red_light}<Proboscis{reset} +{red_light}<Cabbage{reset} +{green_light}>Probed{reset} +{green_light}>Caravaggio{reset} +"#, + red_light = RED_LIGHT, + green_light = GREEN_LIGHT, + reset = RESET, + ); + + check_printer(write_lines, left, right, &expected); + } + + /// Single deletion line, multiple insertions - no inline diffing. + #[test] + fn write_lines_multiline_insert() { + let left = r#"Cabbage"#; + let right = r#"Probed +Caravaggio"#; + let expected = format!( + r#"{red_light}<Cabbage{reset} +{green_light}>Probed{reset} +{green_light}>Caravaggio{reset} +"#, + red_light = RED_LIGHT, + green_light = GREEN_LIGHT, + reset = RESET, + ); + + check_printer(write_lines, left, right, &expected); + } + + /// Multiple deletion, single insertion - no inline diffing. + #[test] + fn write_lines_multiline_delete() { + let left = r#"Proboscis +Cabbage"#; + let right = r#"Probed"#; + let expected = format!( + r#"{red_light}<Proboscis{reset} +{red_light}<Cabbage{reset} +{green_light}>Probed{reset} +"#, + red_light = RED_LIGHT, + green_light = GREEN_LIGHT, + reset = RESET, + ); + + check_printer(write_lines, left, right, &expected); + } + + /// Regression test for multiline highlighting issue + #[test] + fn write_lines_issue12() { + let left = r#"[ + 0, + 0, + 0, + 128, + 10, + 191, + 5, + 64, +]"#; + let right = r#"[ + 84, + 248, + 45, + 64, +]"#; + let expected = format!( + r#" [ +{red_light}< 0,{reset} +{red_light}< 0,{reset} +{red_light}< 0,{reset} +{red_light}< 128,{reset} +{red_light}< 10,{reset} +{red_light}< 191,{reset} +{red_light}< 5,{reset} +{green_light}> 84,{reset} +{green_light}> 248,{reset} +{green_light}> 45,{reset} + 64, + ] +"#, + red_light = RED_LIGHT, + green_light = GREEN_LIGHT, + reset = RESET, + ); + + check_printer(write_lines, left, right, &expected); + } +} |