more tests

This commit is contained in:
Dylan Hurd
2025-12-04 14:15:39 -08:00
parent badd55aae3
commit 050382098a
2 changed files with 85 additions and 0 deletions

View File

@@ -827,6 +827,34 @@ mod tests {
assert_eq!(String::from_utf8(stderr).unwrap(), "");
}
#[test]
fn test_apply_patch_multiple_change_contexts_success() {
let dir = tempdir().unwrap();
let path = dir.path().join("example.py");
let original =
include_str!("../tests/fixtures/scenarios/019_multiple_context_lines/input/example.py");
fs::write(&path, original).unwrap();
let patch = wrap_patch(&format!(
r#"*** Update File: {}
@@ class BaseClass:
@@ def method():
- # to_remove
+ # to_add"#,
path.display()
));
let mut stdout = Vec::new();
let mut stderr = Vec::new();
apply_patch(&patch, &mut stdout, &mut stderr).unwrap();
let contents = fs::read_to_string(&path).unwrap();
let expected = include_str!(
"../tests/fixtures/scenarios/019_multiple_context_lines/expected/example.py"
);
assert_eq!(contents, expected);
}
#[test]
fn test_unified_diff() {
// Start with a file containing four lines.
@@ -1065,4 +1093,33 @@ g
let result = apply_patch(&patch, &mut stdout, &mut stderr);
assert!(result.is_err());
}
#[test]
fn test_apply_patch_multiple_change_contexts_missing_context() {
let dir = tempdir().unwrap();
let path = dir.path().join("example.py");
let original =
include_str!("../tests/fixtures/scenarios/019_multiple_context_lines/input/example.py");
fs::write(&path, original).unwrap();
let patch = wrap_patch(&format!(
r#"*** Update File: {}
@@ class BaseClass:
@@ def missing():
- # to_remove
+ # to_add"#,
path.display()
));
let mut stdout = Vec::new();
let mut stderr = Vec::new();
let result = apply_patch(&patch, &mut stdout, &mut stderr);
assert_matches!(
result,
Err(ApplyPatchError::IoError(IoError { context, .. }))
if context.contains("Failed to find context ' def missing():'")
&& context.contains(&path.display().to_string())
);
}
}

View File

@@ -776,3 +776,31 @@ fn test_update_file_chunk() {
))
);
}
#[test]
fn test_update_file_chunk_multiple_change_context_lines() {
assert_eq!(
parse_update_file_chunk(
&[
"@@ class BaseClass:",
"@@ def method():",
"- # to_remove",
"+ # to_add",
],
200,
false
),
Ok((
(UpdateFileChunk {
change_context: vec![
"class BaseClass:".to_string(),
" def method():".to_string()
],
old_lines: vec![" # to_remove".to_string()],
new_lines: vec![" # to_add".to_string()],
is_end_of_file: false
}),
4
))
);
}