Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TanmayPatil105 committed Mar 31, 2024
1 parent 42eb15b commit 9ff8f89
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ path = "src/main.rs"
[dependencies]
chrono = "0.4.35"
diff = "0.1.10"
regex = "1.10.4"
same-file = "1.0.6"

[dev-dependencies]
Expand Down
31 changes: 25 additions & 6 deletions src/context_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,12 @@ mod tests {
if f != 2 {
bet.write_all(b"l\n").unwrap();
}
let _ = File::create(&format!("{target}/aalef")).unwrap();
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff = diff(
&alef,
"a/alef",
&format!("{target}/aalef"),
&bet,
&format!("{target}/alef"),
2,
Expand Down Expand Up @@ -512,11 +513,12 @@ mod tests {
if f != 2 {
bet.write_all(b"l\n").unwrap();
}
let _ = File::create(&format!("{target}/aalef_")).unwrap();
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff = diff(
&alef,
"a/alef_",
&format!("{target}/aalef_"),
&bet,
&format!("{target}/alef_"),
2,
Expand Down Expand Up @@ -594,11 +596,12 @@ mod tests {
if alef.is_empty() && bet.is_empty() {
continue;
};
let _ = File::create(&format!("{target}/aalefx")).unwrap();
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff = diff(
&alef,
"a/alefx",
&format!("{target}/aalefx"),
&bet,
&format!("{target}/alefx"),
2,
Expand Down Expand Up @@ -679,11 +682,12 @@ mod tests {
if f != 2 {
bet.write_all(b"f\n").unwrap();
}
let _ = File::create(&format!("{target}/aalefr")).unwrap();
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff = diff(
&alef,
"a/alefr",
&format!("{target}/aalefr"),
&bet,
&format!("{target}/alefr"),
2,
Expand Down Expand Up @@ -720,9 +724,15 @@ mod tests {

#[test]
fn test_stop_early() {
use regex::Regex;
use std::fs::File;
use std::str;

let from_filename = "foo";
let _ = File::create(&format!("foo")).unwrap();
let from = vec!["a", "b", "c", ""].join("\n");
let to_filename = "bar";
let _ = File::create(&format!("bar")).unwrap();
let to = vec!["a", "d", "c", ""].join("\n");
let context_size: usize = 3;

Expand All @@ -734,6 +744,11 @@ mod tests {
context_size,
false,
);

let diff_full_text = str::from_utf8(&diff_full).unwrap();
let re = Regex::new(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [+-]\d{4}").unwrap();
let diff_full = re.replace_all(diff_full_text, "");

let expected_full = vec![
"*** foo\t",
"--- bar\t",
Expand All @@ -749,7 +764,7 @@ mod tests {
"",
]
.join("\n");
assert_eq!(diff_full, expected_full.as_bytes());
assert_eq!(diff_full, expected_full);

let diff_brief = diff(
from.as_bytes(),
Expand All @@ -759,8 +774,12 @@ mod tests {
context_size,
true,
);

let diff_brief_text = str::from_utf8(&diff_brief).unwrap();
let re = Regex::new(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [+-]\d{4}").unwrap();
let diff_brief = re.replace_all(diff_brief_text, "");
let expected_brief = vec!["*** foo\t", "--- bar\t", ""].join("\n");
assert_eq!(diff_brief, expected_brief.as_bytes());
assert_eq!(diff_brief, expected_brief);

let nodiff_full = diff(
from.as_bytes(),
Expand Down

0 comments on commit 9ff8f89

Please sign in to comment.