Skip to content

Commit

Permalink
Merge pull request #10 from breezy-team/more-tests
Browse files Browse the repository at this point in the history
Add tests for .as_bytes
  • Loading branch information
jelmer authored Jul 7, 2024
2 parents e393189 + 21a6b0a commit 769875d
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub enum HunkLine {
}

impl HunkLine {
pub fn get_str(&self, leadchar: u8) -> Vec<u8> {
fn get_str(&self, leadchar: u8) -> Vec<u8> {
match self {
Self::ContextLine(contents)
| Self::InsertLine(contents)
Expand Down Expand Up @@ -244,12 +244,12 @@ impl HunkLine {
pub fn parse_line(line: &[u8]) -> Result<Self, MalformedLine> {
if line.starts_with(b"\n") {
Ok(Self::ContextLine(line.to_vec()))
} else if line.starts_with(b" ") {
Ok(Self::ContextLine(line[1..].to_vec()))
} else if line.starts_with(b"+") {
Ok(Self::InsertLine(line[1..].to_vec()))
} else if line.starts_with(b"-") {
Ok(Self::RemoveLine(line[1..].to_vec()))
} else if let Some(line) = line.strip_prefix(b" ") {
Ok(Self::ContextLine(line.to_vec()))
} else if let Some(line) = line.strip_prefix(b"+") {
Ok(Self::InsertLine(line.to_vec()))
} else if let Some(line) = line.strip_prefix(b"-") {
Ok(Self::RemoveLine(line.to_vec()))
} else {
Err(MalformedLine(line.to_vec()))
}
Expand Down Expand Up @@ -295,6 +295,30 @@ mod hunkline_tests {
MalformedLine(b"aaaaa\n".to_vec())
);
}

#[test]
fn as_bytes() {
assert_eq!(
HunkLine::ContextLine(b"foo\n".to_vec()).as_bytes(),
b" foo\n"
);
assert_eq!(
HunkLine::InsertLine(b"foo\n".to_vec()).as_bytes(),
b"+foo\n"
);
assert_eq!(
HunkLine::RemoveLine(b"foo\n".to_vec()).as_bytes(),
b"-foo\n"
);
}

#[test]
fn as_bytes_no_nl() {
assert_eq!(
HunkLine::ContextLine(b"foo".to_vec()).as_bytes(),
b" foo\n\\ No newline at end of file\n"
);
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -521,4 +545,3 @@ mod parse_range_tests {
parse_range("foo").unwrap_err();
}
}

0 comments on commit 769875d

Please sign in to comment.