Improve test coverage of ParsePrimitiveError
What changed, and why it matters
This commit only adds new unit tests to verify how an existing error type is displayed and how it reports its underlying cause. It does not change any production code, fix a bug, or alter behavior that could affect security.
No security action required. Treat as routine test-coverage improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds a #[cfg(test)] mod tests block to primitives/src/lib.rs with three test functions covering ParsePrimitiveError’s Display, Error::source, and HexPrimitive iterator/debug implementations. No library logic is modified; only test assertions are introduced.
Changed components
primitives/src/lib.rs (tests only)Inspect captured patch +52 / −0
diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs
index 61381a33..d59ce80b 100644
--- a/primitives/src/lib.rs
+++ b/primitives/src/lib.rs
@@ -257,3 +257,55 @@ pub(crate) mod hex_codec {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ #[cfg(feature = "alloc")]
+ use alloc::{format, string::ToString};
+
+ #[cfg(feature = "alloc")]
+ use super::*;
+
+ #[test]
+ #[cfg(all(feature = "alloc", feature = "hex"))]
+ fn parse_primitive_error_display() {
+ let odd: ParsePrimitiveError<block::Header> =
+ hex_codec::HexPrimitive::from_str("0").unwrap_err();
+ let invalid: ParsePrimitiveError<block::Header> =
+ hex_codec::HexPrimitive::from_str("zz").unwrap_err();
+ let decode: ParsePrimitiveError<block::Header> =
+ hex_codec::HexPrimitive::from_str("00").unwrap_err();
+
+ assert!(!odd.to_string().is_empty());
+ assert!(!invalid.to_string().is_empty());
+ assert!(!decode.to_string().is_empty());
+ }
+
+ #[test]
+ #[cfg(all(feature = "hex", feature = "std"))]
+ fn parse_primitive_error_source() {
+ use std::error::Error as _;
+
+ let odd: ParsePrimitiveError<block::Header> =
+ hex_codec::HexPrimitive::from_str("0").unwrap_err();
+ let invalid: ParsePrimitiveError<block::Header> =
+ hex_codec::HexPrimitive::from_str("zz").unwrap_err();
+ let decode: ParsePrimitiveError<block::Header> =
+ hex_codec::HexPrimitive::from_str("00").unwrap_err();
+
+ assert!(odd.source().is_some());
+ assert!(invalid.source().is_some());
+ assert!(decode.source().is_none());
+ }
+
+ #[test]
+ #[cfg(all(feature = "alloc", feature = "hex"))]
+ fn hex_primitive_iter_and_debug() {
+ let header: block::Header =
+ encoding::decode_from_slice(&[0u8; block::Header::SIZE]).expect("valid header");
+ let hex = hex_codec::HexPrimitive(&header);
+
+ assert_eq!((&hex).into_iter().next(), Some(0u8));
+ assert!(!format!("{hex:?}").is_empty());
+ }
+}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.