mirror of
https://github.com/rust-lang/rust.git
synced 2026-03-23 16:25:14 +00:00
Merge pull request #2070 from jugglerchris/diff_zero_context
Fix make_diff with zero context requested
This commit is contained in:
@@ -47,7 +47,7 @@ pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Misma
|
||||
for result in diff::lines(expected, actual) {
|
||||
match result {
|
||||
diff::Result::Left(str) => {
|
||||
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<Misma
|
||||
lines_since_mismatch = 0;
|
||||
}
|
||||
diff::Result::Right(str) => {
|
||||
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<Misma
|
||||
|
||||
if lines_since_mismatch < context_size {
|
||||
mismatch.lines.push(DiffLine::Context(str.to_owned()));
|
||||
} else {
|
||||
} else if context_size > 0 {
|
||||
context_queue.push_back(str);
|
||||
}
|
||||
|
||||
@@ -162,3 +162,75 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
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 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()),
|
||||
],
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[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";
|
||||
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())],
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user