Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/ed_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Result<Vec<u8>,
match (expected_count, actual_count) {
(0, 0) => unreachable!(),
(0, _) => writeln!(&mut output, "{}a", line_number_expected - 1).unwrap(),
(1, 0) => writeln!(&mut output, "{line_number_expected}d").unwrap(),
(_, 0) => writeln!(
&mut output,
"{},{}d",
Expand Down Expand Up @@ -180,6 +181,12 @@ mod tests {
assert_eq!(diff, expected.as_bytes());
}

#[test]
fn test_single_line_deleted() {
let diff = diff(b"a\nb\n", b"a\n", &Params::default()).unwrap();
assert_eq!(diff, b"2d\n");
}

#[test]
fn test_permutations() {
let target = "target/ed-diff/";
Expand Down
23 changes: 23 additions & 0 deletions src/normal_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
let actual_count = result.actual.len();
match (expected_count, actual_count) {
(0, 0) => unreachable!(),
(0, 1) => writeln!(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please run the upstream test suite on it?
TESTS=basic ./tests/run-upstream-testsuite.sh
the whole suite would be even better, it would tell us if it makes more tests pass

// exactly one line added
&mut output,
"{}a{line_number_actual}",
line_number_expected - 1
)
.unwrap(),
(0, _) => writeln!(
// 'a' stands for "Add lines"
&mut output,
Expand All @@ -142,6 +149,13 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
line_number_actual + actual_count - 1
)
.unwrap(),
(1, 0) => writeln!(
// exactly one line deleted
&mut output,
"{line_number_expected}d{}",
line_number_actual - 1
)
.unwrap(),
(_, 0) => writeln!(
// 'd' stands for "Delete lines"
&mut output,
Expand Down Expand Up @@ -228,6 +242,15 @@ mod tests {
assert_eq!(diff, expected);
}

#[test]
fn test_single_line_added_or_deleted() {
// A one-line range is written as a single number, as for c.
let added = diff(b"a\n", b"a\nb\n", &Params::default());
assert_eq!(added, b"1a2\n> b\n".to_vec());
let deleted = diff(b"a\nb\n", b"a\n", &Params::default());
assert_eq!(deleted, b"2d1\n< b\n".to_vec());
}

#[test]
fn test_permutations() {
let target = "target/normal-diff/";
Expand Down
Loading