From afa51f7bfac01b9859834dba53936e807dbdde02 Mon Sep 17 00:00:00 2001 From: "Jamil Lambert, PhD" Date: Tue, 15 Apr 2025 11:15:18 +0100 Subject: [PATCH 1/4] Add missing empty line, and remove extra one Make all tests functions have a single blank line between them --- primitives/src/script/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/src/script/mod.rs b/primitives/src/script/mod.rs index 3a0244c59..f299a1baa 100644 --- a/primitives/src/script/mod.rs +++ b/primitives/src/script/mod.rs @@ -725,6 +725,7 @@ mod tests { let script = ScriptBuf::from(vec![0x51; 521]); assert!(ScriptHash::try_from(&script).is_err()); } + #[test] fn try_from_script_for_scripthash() { let script = Script::from_bytes(&[0x51; 520]); @@ -752,7 +753,6 @@ mod tests { assert!(WScriptHash::try_from(&script).is_err()); } - #[test] fn try_from_script_for_wscript_hash() { let script = Script::from_bytes(&[0x51; 10_000]); From 8a096b77d50e9379824f5d1af70949b63f7e3c07 Mon Sep 17 00:00:00 2001 From: "Jamil Lambert, PhD" Date: Tue, 15 Apr 2025 11:41:08 +0100 Subject: [PATCH 2/4] 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] From 6bcc6b703dec2d25e32234f28455b290da141f93 Mon Sep 17 00:00:00 2001 From: "Jamil Lambert, PhD" Date: Tue, 15 Apr 2025 12:03:45 +0100 Subject: [PATCH 3/4] Expand display test to cover debug and OP_0 Add to the test to cover the `fmt::Debug` impl without checking the actual text. Change one of the opcodes so that the display of the special case OP_0 is checked. --- primitives/src/script/mod.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/primitives/src/script/mod.rs b/primitives/src/script/mod.rs index ff55b2c66..a9c436e68 100644 --- a/primitives/src/script/mod.rs +++ b/primitives/src/script/mod.rs @@ -782,26 +782,28 @@ mod tests { #[test] fn script_display() { - let script = Script::from_bytes(&[0xa1, 0xb2, 0xc3]); - assert_eq!(format!("{}", script), "OP_LESSTHANOREQUAL OP_CSV OP_RETURN_195"); + let script = Script::from_bytes(&[0x00, 0xa1, 0xb2]); + assert_eq!(format!("{}", script), "OP_0 OP_LESSTHANOREQUAL OP_CSV"); #[cfg(feature = "hex")] { - assert_eq!(format!("{:x}", script), "a1b2c3"); - assert_eq!(format!("{:X}", script), "A1B2C3"); + assert_eq!(format!("{:x}", script), "00a1b2"); + assert_eq!(format!("{:X}", script), "00A1B2"); } + assert!(!format!("{:?}", script).is_empty()); } #[test] fn scriptbuf_display() { - let script_buf = ScriptBuf::from(vec![0xa1, 0xb2, 0xc3]); - assert_eq!(format!("{}", script_buf), "OP_LESSTHANOREQUAL OP_CSV OP_RETURN_195"); + let script_buf = ScriptBuf::from(vec![0x00, 0xa1, 0xb2]); + assert_eq!(format!("{}", script_buf), "OP_0 OP_LESSTHANOREQUAL OP_CSV"); #[cfg(feature = "hex")] { - assert_eq!(format!("{:x}", script_buf), "a1b2c3"); - assert_eq!(format!("{:X}", script_buf), "A1B2C3"); + assert_eq!(format!("{:x}", script_buf), "00a1b2"); + assert_eq!(format!("{:X}", script_buf), "00A1B2"); } + assert!(!format!("{:?}", script_buf).is_empty()); } #[test] From b4b61cb8849d5d536532474691c5d9f10c146672 Mon Sep 17 00:00:00 2001 From: "Jamil Lambert, PhD" Date: Tue, 15 Apr 2025 13:08:12 +0100 Subject: [PATCH 4/4] Add tests to increase coverage There are missed lines in coverage when running `cargo tarpaulin` on `Script`. Add tests to cover all lines missed except for `serde`. --- primitives/src/script/mod.rs | 95 ++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/primitives/src/script/mod.rs b/primitives/src/script/mod.rs index a9c436e68..619a948ec 100644 --- a/primitives/src/script/mod.rs +++ b/primitives/src/script/mod.rs @@ -650,6 +650,15 @@ mod tests { assert_eq!(script_buf.as_bytes(), &[0x50, 0x51, 0x53]); } + #[test] + fn scriptbuf_borrow_mut() { + let mut script_buf = ScriptBuf::from(vec![0x51, 0x52, 0x53]); + let script_mut: &mut Script = script_buf.borrow_mut(); + script_mut.as_mut_bytes()[0] = 0x50; + + assert_eq!(script_buf.as_bytes(), &[0x50, 0x52, 0x53]); + } + #[test] #[allow(clippy::useless_asref)] fn script_as_ref() { @@ -831,6 +840,14 @@ mod tests { assert_eq!(script_buf2, script_buf); } + #[test] + fn cow_owned_to_scriptbuf() { + let script_buf = ScriptBuf::from(vec![0x51, 0x52, 0x53]); + let cow_owned: Cow