From 4459c5ee3a5ae9a4172f06029da14d11d0bf9ac6 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sat, 30 Jan 2021 22:57:53 +0100 Subject: [PATCH] Change default udiff context radius to 3 --- src/text/udiff.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/text/udiff.rs b/src/text/udiff.rs index 6c28f6f..d8f12c7 100644 --- a/src/text/udiff.rs +++ b/src/text/udiff.rs @@ -89,12 +89,15 @@ impl<'diff, 'old, 'new, 'bufs> UnifiedDiff<'diff, 'old, 'new, 'bufs> { pub fn from_text_diff(diff: &'diff TextDiff<'old, 'new, 'bufs>) -> Self { UnifiedDiff { diff, - context_radius: 5, + context_radius: 3, header: None, } } - /// Changes the context radius. Defaults to `5`. + /// Changes the context radius. + /// + /// The context radius is the number of lines between changes that should + /// be emitted. This defaults to `3`. pub fn context_radius(&mut self, n: usize) -> &mut Self { self.context_radius = n; self @@ -208,6 +211,9 @@ impl<'diff, 'old, 'new, 'bufs> fmt::Display for UnifiedDiff<'diff, 'old, 'new, ' } /// Quick way to get a unified diff as string. +/// +/// `n` configures [`UnifiedDiff::context_radius`] and +/// `header` configures [`UnifiedDiff::header`] when not `None`. pub fn unified_diff<'old, 'new>( alg: Algorithm, old: &'old str, @@ -223,3 +229,12 @@ pub fn unified_diff<'old, 'new>( .header_opt(header) .to_string() } + +#[test] +fn test_unified_diff() { + let diff = TextDiff::from_lines( + "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nr\ns\nt\nu\nv\nw\nx\ny\nz\nA\nB\nC\nD\nE\nF\nG\nH\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nU\nV\nW\nX\nY\nZ", + "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nr\nS\nt\nu\nv\nw\nx\ny\nz\nA\nB\nC\nD\nE\nF\nG\nH\nI\nJ\nK\nL\nM\nN\no\nP\nQ\nR\nS\nT\nU\nV\nW\nX\nY\nZ", + ); + insta::assert_snapshot!(&diff.unified_diff().header("a.txt", "b.txt").to_string()); +}