From 88f02975a1064e91ffe3e28efe1b744a11a39209 Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Thu, 19 Oct 2017 20:49:33 +0100 Subject: [PATCH 1/5] Add a simple passing test for diffs. --- src/rustfmt_diff.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/rustfmt_diff.rs b/src/rustfmt_diff.rs index 3fb2286538515..6884e39141d86 100644 --- a/src/rustfmt_diff.rs +++ b/src/rustfmt_diff.rs @@ -162,3 +162,23 @@ where } } } + +#[cfg(test)] +mod test { + use super::{make_diff,Mismatch}; + use super::DiffLine::*; + + #[test] + fn simple_diff() { + let src = "one\ntwo\nthree\nfour\nfive\n"; + let dest= "one\ntwo\ntrois\nfour\nfive\n"; + let diff = make_diff(src, dest, 1); + assert_eq!(diff, vec![Mismatch { line_number: 2, + lines: vec![ + Context("two".into()), + Resulting("three".into()), + Expected("trois".into()), + Context("four".into()), + ] }]); + } +} \ No newline at end of file From 6282c970cf3dcd0e30c3e1751768cc2d18c7295d Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Thu, 19 Oct 2017 20:55:20 +0100 Subject: [PATCH 2/5] Add a failing test of zero context. --- src/rustfmt_diff.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/rustfmt_diff.rs b/src/rustfmt_diff.rs index 6884e39141d86..644ad7ceeacd4 100644 --- a/src/rustfmt_diff.rs +++ b/src/rustfmt_diff.rs @@ -169,7 +169,7 @@ mod test { use super::DiffLine::*; #[test] - fn simple_diff() { + fn diff_simple() { let src = "one\ntwo\nthree\nfour\nfive\n"; let dest= "one\ntwo\ntrois\nfour\nfive\n"; let diff = make_diff(src, dest, 1); @@ -181,4 +181,16 @@ mod test { Context("four".into()), ] }]); } + + #[test] + fn diff_zerocontext() { + let src = "one\ntwo\nthree\nfour\nfive\n"; + let dest= "one\ntwo\ntrois\nfour\nfive\n"; + let diff = make_diff(src, dest, 0); + assert_eq!(diff, vec![Mismatch { line_number: 3, + lines: vec![ + Resulting("three".into()), + Expected("trois".into()), + ] }]); + } } \ No newline at end of file From 6c1c81bbced8c6d98701ea70845f1626ac703c31 Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Thu, 19 Oct 2017 23:03:27 +0100 Subject: [PATCH 3/5] Add a test for two nearby chunks (with context). --- src/rustfmt_diff.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/rustfmt_diff.rs b/src/rustfmt_diff.rs index 644ad7ceeacd4..fa3c6305ed8bc 100644 --- a/src/rustfmt_diff.rs +++ b/src/rustfmt_diff.rs @@ -182,6 +182,26 @@ mod test { ] }]); } + #[test] + fn diff_simple2() { + let src = "one\ntwo\nthree\nfour\nfive\nsix\nseven\n"; + let dest= "one\ntwo\ntrois\nfour\ncinq\nsix\nseven\n"; + let diff = make_diff(src, dest, 1); + assert_eq!(diff, vec![Mismatch { line_number: 2, + lines: vec![ + Context("two".into()), + Resulting("three".into()), + Expected("trois".into()), + Context("four".into()), + ] }, + Mismatch { line_number: 5, + lines: vec![ + Resulting("five".into()), + Expected("cinq".into()), + Context("six".into()), + ] }]); + } + #[test] fn diff_zerocontext() { let src = "one\ntwo\nthree\nfour\nfive\n"; From f9bcb58eb3f3b6508cd7a24e7728f21fcd8f63b9 Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Thu, 19 Oct 2017 23:14:20 +0100 Subject: [PATCH 4/5] Add a couple of special cases which fix the zero-context diff case. --- src/rustfmt_diff.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rustfmt_diff.rs b/src/rustfmt_diff.rs index fa3c6305ed8bc..d6b021d42d4ee 100644 --- a/src/rustfmt_diff.rs +++ b/src/rustfmt_diff.rs @@ -47,7 +47,7 @@ pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec { - if lines_since_mismatch >= context_size { + if lines_since_mismatch >= context_size && lines_since_mismatch > 0 { results.push(mismatch); mismatch = Mismatch::new(line_number - context_queue.len() as u32); } @@ -60,7 +60,7 @@ pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec { - if lines_since_mismatch >= context_size { + if lines_since_mismatch >= context_size && lines_since_mismatch > 0 { results.push(mismatch); mismatch = Mismatch::new(line_number - context_queue.len() as u32); } @@ -80,7 +80,7 @@ pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec 0 { context_queue.push_back(str); } From 2a84352d1df09d04fc38311f49bb4ccb4ad83a91 Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Thu, 19 Oct 2017 23:32:27 +0100 Subject: [PATCH 5/5] Run rustfmt on the new changes. --- src/rustfmt_diff.rs | 80 ++++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 30 deletions(-) diff --git a/src/rustfmt_diff.rs b/src/rustfmt_diff.rs index d6b021d42d4ee..c0aca68fe2ffe 100644 --- a/src/rustfmt_diff.rs +++ b/src/rustfmt_diff.rs @@ -165,52 +165,72 @@ where #[cfg(test)] mod test { - use super::{make_diff,Mismatch}; + use super::{make_diff, Mismatch}; use super::DiffLine::*; #[test] fn diff_simple() { let src = "one\ntwo\nthree\nfour\nfive\n"; - let dest= "one\ntwo\ntrois\nfour\nfive\n"; + let dest = "one\ntwo\ntrois\nfour\nfive\n"; let diff = make_diff(src, dest, 1); - assert_eq!(diff, vec![Mismatch { line_number: 2, - lines: vec![ - Context("two".into()), - Resulting("three".into()), - Expected("trois".into()), - Context("four".into()), - ] }]); + assert_eq!( + diff, + vec![ + Mismatch { + line_number: 2, + lines: vec![ + Context("two".into()), + Resulting("three".into()), + Expected("trois".into()), + Context("four".into()), + ], + }, + ] + ); } #[test] fn diff_simple2() { let src = "one\ntwo\nthree\nfour\nfive\nsix\nseven\n"; - let dest= "one\ntwo\ntrois\nfour\ncinq\nsix\nseven\n"; + let dest = "one\ntwo\ntrois\nfour\ncinq\nsix\nseven\n"; let diff = make_diff(src, dest, 1); - assert_eq!(diff, vec![Mismatch { line_number: 2, - lines: vec![ - Context("two".into()), - Resulting("three".into()), - Expected("trois".into()), - Context("four".into()), - ] }, - Mismatch { line_number: 5, - lines: vec![ - Resulting("five".into()), - Expected("cinq".into()), - Context("six".into()), - ] }]); + assert_eq!( + diff, + vec![ + Mismatch { + line_number: 2, + lines: vec![ + Context("two".into()), + Resulting("three".into()), + Expected("trois".into()), + Context("four".into()), + ], + }, + Mismatch { + line_number: 5, + lines: vec![ + Resulting("five".into()), + Expected("cinq".into()), + Context("six".into()), + ], + }, + ] + ); } #[test] fn diff_zerocontext() { let src = "one\ntwo\nthree\nfour\nfive\n"; - let dest= "one\ntwo\ntrois\nfour\nfive\n"; + let dest = "one\ntwo\ntrois\nfour\nfive\n"; let diff = make_diff(src, dest, 0); - assert_eq!(diff, vec![Mismatch { line_number: 3, - lines: vec![ - Resulting("three".into()), - Expected("trois".into()), - ] }]); + assert_eq!( + diff, + vec![ + Mismatch { + line_number: 3, + lines: vec![Resulting("three".into()), Expected("trois".into())], + }, + ] + ); } -} \ No newline at end of file +}