From 8a096b77d50e9379824f5d1af70949b63f7e3c07 Mon Sep 17 00:00:00 2001 From: "Jamil Lambert, PhD" Date: Tue, 15 Apr 2025 11:41:08 +0100 Subject: [PATCH] Expand as_ref and as_mut tests The tests for both `Script` and `ScriptBuf` `as_ref` and `as_mut` only covered the `u8` impl. Expand the tests to cover the `Self` impl as well. The allow useless_asref is required to remove a clippy lint. Removing the "useless" as_ref also removes the test coverage of the as_ref or as_mut impl. --- primitives/src/script/mod.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/primitives/src/script/mod.rs b/primitives/src/script/mod.rs index f299a1baa..ff55b2c66 100644 --- a/primitives/src/script/mod.rs +++ b/primitives/src/script/mod.rs @@ -632,30 +632,48 @@ mod tests { let script_buf = ScriptBuf::from(vec![0x51, 0x52, 0x53]); let script_ref: &[u8] = script_buf.as_ref(); assert_eq!(script_ref, &[0x51, 0x52, 0x53]); + + let script_ref: &Script = script_buf.as_ref(); + assert_eq!(script_ref.as_bytes(), &[0x51, 0x52, 0x53]); } #[test] fn scriptbuf_as_mut() { let mut script_buf = ScriptBuf::from(vec![0x51, 0x52, 0x53]); + let script_mut: &mut [u8] = script_buf.as_mut(); script_mut[0] = 0x50; assert_eq!(script_mut, [0x50, 0x52, 0x53]); + + let script_mut: &mut Script = script_buf.as_mut(); + script_mut.as_mut_bytes()[1] = 0x51; + assert_eq!(script_buf.as_bytes(), &[0x50, 0x51, 0x53]); } #[test] + #[allow(clippy::useless_asref)] fn script_as_ref() { let script = Script::from_bytes(&[0x51, 0x52, 0x53]); let script_ref: &[u8] = script.as_ref(); assert_eq!(script_ref, &[0x51, 0x52, 0x53]); + + let script_ref: &Script = script.as_ref(); + assert_eq!(script_ref.as_bytes(), &[0x51, 0x52, 0x53]); } #[test] + #[allow(clippy::useless_asref)] fn script_as_mut() { let bytes = &mut [0x51, 0x52, 0x53]; let script = Script::from_bytes_mut(bytes); + let script_mut: &mut [u8] = script.as_mut(); script_mut[0] = 0x50; assert_eq!(script_mut, [0x50, 0x52, 0x53]); + + let script_mut: &mut Script = script.as_mut(); + script_mut.as_mut_bytes()[1] = 0x51; + assert_eq!(script.as_bytes(), &[0x50, 0x51, 0x53]); } #[test]