Skip to content

Commit

Permalink
feat!: add ContextSize struct.
Browse files Browse the repository at this point in the history
That way it's possible to update it in future, for instance to add assymetric context options.

Co-authored-by: Sebastian Thiel <[email protected]> 1733756156 +0100
  • Loading branch information
gcanat authored and Byron committed Jan 20, 2025
1 parent 6f81859 commit 8517f10
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 135 deletions.
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, None));
//! 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
38 changes: 31 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, None));
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, None));
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, None));
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, None));
let res = diff(
algorithm,
&input,
UnifiedDiffBuilder::new(&input, ContextSize::default()),
);
expect![[r#"
@@ -1,5 +1,4 @@
fn foo() -> Bar{
Expand Down Expand Up @@ -129,11 +145,19 @@ 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_res = diff(algorithm, &input, UnifiedDiffBuilder::new(&input, None));
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, Some(5)));
let diff_res = diff(
algorithm,
&input,
UnifiedDiffBuilder::new(&input, ContextSize::symmetrical(5)),
);
expect_file![path_diff].assert_eq(&diff_res);
}
}
Expand Down
Loading

0 comments on commit 8517f10

Please sign in to comment.