Skip to content

Commit

Permalink
test: patch index parsing
Browse files Browse the repository at this point in the history
We add some tests for patch_index_range parsing. Making sure that
obvious edge cases are handled correctly.

Signed-off-by: Ali Caglayan <alizter@gmail.com>
Signed-off-by: Drew De Ponte <cyphactor@gmail.com>

<!-- ps-id: 53c6099d-d32a-4c9c-88cd-0d53bc9e7027 -->
  • Loading branch information
Alizter authored and drewdeponte committed Oct 19, 2023
1 parent e117ac5 commit 8db2fa8
Showing 1 changed file with 154 additions and 0 deletions.
154 changes: 154 additions & 0 deletions src/commands/patch_index_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ pub struct PatchIndexRange {
pub end_index: Option<usize>,
}

impl PartialEq for PatchIndexRange {
fn eq(&self, other: &Self) -> bool {
self.start_index == other.start_index && self.end_index == other.end_index
}
}

#[derive(Debug)]
pub enum ParsePatchIndexOrRangeError {
InvalidIndexRange(String),
Expand Down Expand Up @@ -61,3 +67,151 @@ impl std::str::FromStr for PatchIndexRange {
}
}
}

// Tests

// Parsing of single patch indexes

#[test]
fn single1() {
let patch_index_range = "1".parse::<PatchIndexRange>().unwrap();
assert_eq!(
patch_index_range,
PatchIndexRange {
start_index: 1,
end_index: None,
},
);
}

#[test]
fn single2() {
let patch_index_or_range = "12".parse::<PatchIndexRange>().unwrap();
assert_eq!(
patch_index_or_range,
PatchIndexRange {
start_index: 12,
end_index: None,
},
);
}

#[test]
fn single3() {
let patch_index_or_range = "12341234123412341234".parse::<PatchIndexRange>().unwrap();
assert_eq!(
patch_index_or_range,
PatchIndexRange {
start_index: 12341234123412341234,
end_index: None,
},
);
}

#[test]
fn single4() {
let patch_index_or_range = "0".parse::<PatchIndexRange>().unwrap();
assert_eq!(
patch_index_or_range,
PatchIndexRange {
start_index: 0,
end_index: None,
},
);
}

#[test]
fn single5() {
let patch_index_or_range = "-1".parse::<PatchIndexRange>();
assert!(patch_index_or_range.is_err());
}

#[test]
fn single6() {
let patch_index_or_range = u32::MAX.to_string().parse::<PatchIndexRange>().unwrap();
assert_eq!(
patch_index_or_range,
PatchIndexRange {
start_index: u32::MAX as usize,
end_index: None,
},
);
}

// Parsing of ranges

#[test]
fn range1() {
let patch_index_or_range = "2-4".parse::<PatchIndexRange>().unwrap();
assert_eq!(
patch_index_or_range,
PatchIndexRange {
start_index: 2,
end_index: Some(4),
},
);
}

#[test]
fn range2() {
let patch_index_or_range = "2-333".parse::<PatchIndexRange>().unwrap();
assert_eq!(
patch_index_or_range,
PatchIndexRange {
start_index: 2,
end_index: Some(333),
},
);
}

// CR-someday alizter: We don't accept reflexive ranges, maybe we should?
// It seems simple enought to parse this as a single patch index.
#[test]
fn range3() {
let patch_index_or_range = "2-2".parse::<PatchIndexRange>();
assert!(patch_index_or_range.is_err());
}

#[test]
fn range4() {
let patch_index_or_range = "4-2".parse::<PatchIndexRange>();
assert!(patch_index_or_range.is_err());
}

#[test]
fn range5() {
let patch_index_range = "0--1".parse::<PatchIndexRange>();
assert!(patch_index_range.is_err());
}

#[test]
fn range6() {
let patch_index_range = "-1-2".parse::<PatchIndexRange>();
assert!(patch_index_range.is_err());
}

// Invalid syntax

#[test]
fn malformed1() {
let patch_index_or_range = "2-4-6".parse::<PatchIndexRange>();
assert!(patch_index_or_range.is_err());
}

#[test]
fn malformed2() {
let patch_index_range = "2-".parse::<PatchIndexRange>();
assert!(patch_index_range.is_err());
}

#[test]
fn malformed3() {
let patch_index_range = "".parse::<PatchIndexRange>();
assert!(patch_index_range.is_err());
}

#[test]
fn malformed4() {
let patch_index_range = "-0".parse::<PatchIndexRange>();
assert!(patch_index_range.is_err());
}

0 comments on commit 8db2fa8

Please sign in to comment.