Skip to content

Commit

Permalink
Raise an explicit panic when context_lines is 0. Fixes #37
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Jul 28, 2022
1 parent 3c784d6 commit 3a938ae
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ pub fn get_diff_ratio(ops: &[DiffOp], old_len: usize, new_len: usize) -> f32 {
/// This will leave holes behind in long periods of equal ranges so that
/// you can build things like unified diffs.
pub fn group_diff_ops(mut ops: Vec<DiffOp>, n: usize) -> Vec<Vec<DiffOp>> {
assert!(n > 0, "n must be 1 or larger");

if ops.is_empty() {
return vec![];
}
Expand Down
9 changes: 9 additions & 0 deletions src/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,3 +758,12 @@ fn test_serde_ops() {
let json = serde_json::to_string_pretty(&changes).unwrap();
insta::assert_snapshot!(&json);
}

#[test]
#[should_panic = "n must be 1 or larger"]
fn test_regression_issue_37() {
let config = TextDiffConfig::default();
let diff = config.diff_lines("", "");
let mut output = diff.unified_diff();
output.context_radius(0).to_string();
}
3 changes: 2 additions & 1 deletion src/udiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ impl<'diff, 'old, 'new, 'bufs, T: DiffableStr + ?Sized> UnifiedDiff<'diff, 'old,
/// Changes the context radius.
///
/// The context radius is the number of lines between changes that should
/// be emitted. This defaults to `3`.
/// be emitted. This defaults to `3`. This value must be one or larger or
/// creating the diff will panic.
pub fn context_radius(&mut self, n: usize) -> &mut Self {
self.context_radius = n;
self
Expand Down

0 comments on commit 3a938ae

Please sign in to comment.