2024-07-09 01:10:55 +03:00
|
|
|
use gitbutler_branch::hunk::Hunk;
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn to_from_string() {
|
|
|
|
let hunk = "1-2".parse::<Hunk>().unwrap();
|
|
|
|
assert_eq!("1-2", hunk.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_invalid() {
|
|
|
|
"3-2".parse::<Hunk>().unwrap_err();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_with_hash() {
|
2024-05-06 21:22:40 +03:00
|
|
|
let hash = Hunk::hash("hash");
|
2024-03-29 12:04:26 +03:00
|
|
|
assert_eq!(
|
2024-04-20 16:18:11 +03:00
|
|
|
format!("2-3-{hash:x}").parse::<Hunk>().unwrap(),
|
2024-06-25 13:04:26 +03:00
|
|
|
Hunk::new(2, 3, Some(hash)).unwrap()
|
2024-03-29 12:04:26 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_with_timestamp() {
|
|
|
|
assert_eq!(
|
|
|
|
"2-3--123".parse::<Hunk>().unwrap(),
|
2024-06-25 13:04:26 +03:00
|
|
|
Hunk::new(2, 3, None).unwrap()
|
2024-03-29 12:04:26 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_invalid_2() {
|
|
|
|
"3-2".parse::<Hunk>().unwrap_err();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn to_string_no_hash() {
|
2024-06-25 13:04:26 +03:00
|
|
|
assert_eq!("1-2", Hunk::new(1, 2, None).unwrap().to_string());
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|
|
|
|
|
2024-04-20 16:18:11 +03:00
|
|
|
#[test]
|
2024-04-20 18:46:41 +03:00
|
|
|
fn hash_diff_no_diff_header_is_normal_hash() {
|
2024-05-06 21:22:40 +03:00
|
|
|
let actual = Hunk::hash_diff("a");
|
|
|
|
let expected = Hunk::hash("a");
|
2024-04-20 18:46:41 +03:00
|
|
|
assert_eq!(actual, expected)
|
2024-04-20 16:47:29 +03:00
|
|
|
}
|
2024-04-20 16:18:11 +03:00
|
|
|
|
2024-04-20 16:47:29 +03:00
|
|
|
#[test]
|
2024-04-20 18:46:41 +03:00
|
|
|
fn hash_diff_empty_is_fine() {
|
2024-05-06 21:22:40 +03:00
|
|
|
let actual = Hunk::hash_diff("");
|
|
|
|
let expected = Hunk::hash("");
|
2024-04-20 18:46:41 +03:00
|
|
|
assert_eq!(
|
|
|
|
actual, expected,
|
|
|
|
"The special hash is the same as a normal one in case of empty input.\
|
|
|
|
Don't yet know why that should be except that more works then"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hash_diff_content_hash() {
|
2024-05-06 21:22:40 +03:00
|
|
|
let a_hash = Hunk::hash_diff("@@x\na");
|
|
|
|
let b_hash = Hunk::hash_diff("@@y\na");
|
2024-04-20 16:18:11 +03:00
|
|
|
assert_eq!(
|
|
|
|
a_hash, b_hash,
|
|
|
|
"it skips the first line which is assumed to be a diff-header.\
|
|
|
|
That way, the content is hashed instead"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-03-29 12:04:26 +03:00
|
|
|
#[test]
|
|
|
|
fn eq() {
|
2024-05-06 21:22:40 +03:00
|
|
|
let a_hash = Hunk::hash("a");
|
|
|
|
let b_hash = Hunk::hash("b");
|
2024-04-20 16:18:11 +03:00
|
|
|
assert_ne!(a_hash, b_hash);
|
2024-03-29 12:04:26 +03:00
|
|
|
for (a, b, expected) in vec![
|
|
|
|
(
|
|
|
|
"1-2".parse::<Hunk>().unwrap(),
|
|
|
|
"1-2".parse::<Hunk>().unwrap(),
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"1-2".parse::<Hunk>().unwrap(),
|
|
|
|
"2-3".parse::<Hunk>().unwrap(),
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
(
|
2024-04-20 16:18:11 +03:00
|
|
|
format!("1-2-{a_hash:x}").parse::<Hunk>().unwrap(),
|
|
|
|
format!("1-2-{a_hash:x}").parse::<Hunk>().unwrap(),
|
2024-03-29 12:04:26 +03:00
|
|
|
true,
|
|
|
|
),
|
|
|
|
(
|
2024-04-20 16:18:11 +03:00
|
|
|
format!("1-2-{a_hash:x}").parse::<Hunk>().unwrap(),
|
|
|
|
format!("2-3-{a_hash:x}").parse::<Hunk>().unwrap(),
|
2024-03-29 12:04:26 +03:00
|
|
|
false,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"1-2".parse::<Hunk>().unwrap(),
|
2024-04-20 16:18:11 +03:00
|
|
|
format!("1-2-{a_hash:x}").parse::<Hunk>().unwrap(),
|
2024-03-29 12:04:26 +03:00
|
|
|
true,
|
|
|
|
),
|
|
|
|
(
|
2024-04-20 16:18:11 +03:00
|
|
|
format!("1-2-{a_hash:x}").parse::<Hunk>().unwrap(),
|
2024-03-29 12:04:26 +03:00
|
|
|
"1-2".parse::<Hunk>().unwrap(),
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
(
|
2024-04-20 16:18:11 +03:00
|
|
|
format!("1-2-{a_hash:x}").parse::<Hunk>().unwrap(),
|
|
|
|
format!("1-2-{b_hash:x}").parse::<Hunk>().unwrap(),
|
2024-03-29 12:04:26 +03:00
|
|
|
false,
|
|
|
|
),
|
|
|
|
(
|
2024-04-20 16:18:11 +03:00
|
|
|
format!("1-2-{a_hash:x}").parse::<Hunk>().unwrap(),
|
|
|
|
format!("2-3-{b_hash:x}").parse::<Hunk>().unwrap(),
|
2024-03-29 12:04:26 +03:00
|
|
|
false,
|
|
|
|
),
|
2024-06-25 13:04:26 +03:00
|
|
|
(
|
|
|
|
// Ensures unknown data is ignored
|
|
|
|
format!("1-2-{a_hash:x}-unknown").parse::<Hunk>().unwrap(),
|
|
|
|
"1-2".parse::<Hunk>().unwrap(),
|
|
|
|
true,
|
|
|
|
),
|
2024-03-29 12:04:26 +03:00
|
|
|
] {
|
2024-04-20 16:18:11 +03:00
|
|
|
assert_eq!(a == b, expected, "comparing {} and {}", a, b);
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|
|
|
|
}
|