Skip to content

Add option to specify context size #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//!
//! ```
//! use imara_diff::intern::InternedInput;
//! use imara_diff::{diff, Algorithm, UnifiedDiffBuilder};
//! use imara_diff::{diff, Algorithm, UnifiedDiffBuilder, unified_diff};
//!
//! let before = r#"fn foo() -> Bar {
//! let mut foo = 2;
Expand All @@ -49,7 +49,7 @@
//! "#;
//!
//! let input = InternedInput::new(before, after);
//! let diff = diff(Algorithm::Histogram, &input, UnifiedDiffBuilder::new(&input));
//! let diff = diff(Algorithm::Histogram, &input, UnifiedDiffBuilder::new(&input, unified_diff::ContextSize::symmetrical(3)));
//! assert_eq!(
//! diff,
//! r#"@@ -1,5 +1,8 @@
Expand Down Expand Up @@ -150,7 +150,7 @@
//! ```

#[cfg(feature = "unified_diff")]
pub use unified_diff::UnifiedDiffBuilder;
pub use unified_diff::_impl::UnifiedDiffBuilder;

use crate::intern::{InternedInput, Token, TokenSource};
pub use crate::sink::Sink;
Expand All @@ -160,7 +160,7 @@ mod myers;
pub mod sink;
pub mod sources;
#[cfg(feature = "unified_diff")]
mod unified_diff;
pub mod unified_diff;
mod util;

#[cfg(test)]
Expand Down
42 changes: 35 additions & 7 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use expect_test::{expect, expect_file};

use crate::intern::InternedInput;
use crate::sink::Counter;
use crate::{diff, Algorithm, UnifiedDiffBuilder};
use crate::{diff, unified_diff::ContextSize, Algorithm, UnifiedDiffBuilder};

#[test]
fn replace() {
Expand All @@ -28,7 +28,11 @@ fn foo() -> Bar{
let input = InternedInput::new(before, after);
for algorithm in Algorithm::ALL {
println!("{algorithm:?}");
let diff = diff(algorithm, &input, UnifiedDiffBuilder::new(&input));
let diff = diff(
algorithm,
&input,
UnifiedDiffBuilder::new(&input, ContextSize::default()),
);
expect![[r#"
@@ -1,5 +1,8 @@
+const TEST: i32 = 0;
Expand All @@ -55,7 +59,11 @@ fn identical_files() {
for algorithm in Algorithm::ALL {
println!("{algorithm:?}");
let input = InternedInput::new(file, file);
let diff = diff(algorithm, &input, UnifiedDiffBuilder::new(&input));
let diff = diff(
algorithm,
&input,
UnifiedDiffBuilder::new(&input, ContextSize::default()),
);
assert_eq!(diff, "");
}
}
Expand All @@ -76,7 +84,11 @@ fn simple_insert() {
let mut input = InternedInput::new(before, after);
for algorithm in Algorithm::ALL {
println!("{algorithm:?}");
let res = diff(algorithm, &input, UnifiedDiffBuilder::new(&input));
let res = diff(
algorithm,
&input,
UnifiedDiffBuilder::new(&input, ContextSize::default()),
);
expect![[r#"
@@ -1,4 +1,5 @@
fn foo() -> Bar{
Expand All @@ -89,7 +101,11 @@ fn simple_insert() {

swap(&mut input.before, &mut input.after);

let res = diff(algorithm, &input, UnifiedDiffBuilder::new(&input));
let res = diff(
algorithm,
&input,
UnifiedDiffBuilder::new(&input, ContextSize::default()),
);
expect![[r#"
@@ -1,5 +1,4 @@
fn foo() -> Bar{
Expand Down Expand Up @@ -129,8 +145,20 @@ fn hand_checked_udiffs() {
let before = read_to_string(path_before).unwrap();
let after = read_to_string(path_after).unwrap();
let input = InternedInput::new(&*before, &*after);
let diff = diff(algorithm, &input, UnifiedDiffBuilder::new(&input));
expect_file![path_diff].assert_eq(&diff);
let diff_res = diff(
algorithm,
&input,
UnifiedDiffBuilder::new(&input, ContextSize::default()),
);
expect_file![path_diff].assert_eq(&diff_res);
// test with a context of 5 lines
let path_diff = test_dir.join(format!("{file}.{algorithm:?}_ctx5.diff"));
let diff_res = diff(
algorithm,
&input,
UnifiedDiffBuilder::new(&input, ContextSize::symmetrical(5)),
);
expect_file![path_diff].assert_eq(&diff_res);
}
}

Expand Down
Loading
Loading