Skip to content

Commit

Permalink
test: Move DecodeError tests closer to the implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
caspermeijn committed Jan 8, 2025
1 parent 5684bd4 commit aae30b9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
30 changes: 30 additions & 0 deletions prost/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,33 @@ impl fmt::Display for UnknownEnumValue {

#[cfg(feature = "std")]
impl std::error::Error for UnknownEnumValue {}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_push() {
let mut decode_error = DecodeError::new("something failed");
decode_error.push("Foo bad", "bar.foo");
decode_error.push("Baz bad", "bar.baz");

assert_eq!(
decode_error.to_string(),
"failed to decode Protobuf message: Foo bad.bar.foo: Baz bad.bar.baz: something failed"
);
}

#[cfg(feature = "std")]
#[test]
fn test_into_std_io_error() {
let decode_error = DecodeError::new("something failed");
let std_io_error = std::io::Error::from(decode_error);

assert_eq!(std_io_error.kind(), std::io::ErrorKind::InvalidData);
assert_eq!(
std_io_error.to_string(),
"failed to decode Protobuf message: something failed"
);
}
}
25 changes: 0 additions & 25 deletions tests/src/decode_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,28 +148,3 @@ fn test_decode_error_any() {
"failed to decode Protobuf message: unexpected type URL.type_url: expected type URL: \"type.googleapis.com/google.protobuf.Timestamp\" (got: \"non-existing-url\")"
);
}

#[test]
fn test_push() {
let mut decode_error = prost::DecodeError::new("something failed");
decode_error.push("Foo bad", "bar.foo");
decode_error.push("Baz bad", "bar.baz");

assert_eq!(
decode_error.to_string(),
"failed to decode Protobuf message: Foo bad.bar.foo: Baz bad.bar.baz: something failed"
);
}

#[cfg(feature = "std")]
#[test]
fn test_into_std_io_error() {
let decode_error = prost::DecodeError::new("something failed");
let std_io_error = std::io::Error::from(decode_error);

assert_eq!(std_io_error.kind(), std::io::ErrorKind::InvalidData);
assert_eq!(
std_io_error.to_string(),
"failed to decode Protobuf message: something failed"
);
}

0 comments on commit aae30b9

Please sign in to comment.