Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for .as_bytes #10

Merged
merged 1 commit into from
Jul 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}