Skip to content

Commit 13c5366

Browse files
committed
Use hex_lit::hex in tests
The tests defined custom `hex!` macros (yes, two actually) that evaluated to `Vec<u8>`. While the performance didn't matter it made it harder to use with interfaces that require arrays and all current uses were passing it as slices anyway. So, in preparation for upcoming changes, this commit introduces `hex_lit` dev-dependency which evaluates to array allowing better interaction with type checker.
1 parent c4c029f commit 13c5366

File tree

5 files changed

+24
-23
lines changed

5 files changed

+24
-23
lines changed

Cargo-minimal.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ version = "0.1.1"
110110
source = "registry+https://github.com/rust-lang/crates.io-index"
111111
checksum = "30ed443af458ccb6d81c1e7e661545f94d3176752fb1df2f543b902a1e0f51e2"
112112

113+
[[package]]
114+
name = "hex_lit"
115+
version = "0.1.1"
116+
source = "registry+https://github.com/rust-lang/crates.io-index"
117+
checksum = "3011d1213f159867b13cfd6ac92d2cd5f1345762c63be3554e84092d85a50bbd"
118+
113119
[[package]]
114120
name = "itoa"
115121
version = "0.3.0"
@@ -262,6 +268,7 @@ dependencies = [
262268
"bincode",
263269
"bitcoin_hashes",
264270
"getrandom",
271+
"hex_lit",
265272
"rand",
266273
"rand_core",
267274
"secp256k1-sys",

Cargo-recent.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ version = "0.1.1"
8686
source = "registry+https://github.com/rust-lang/crates.io-index"
8787
checksum = "30ed443af458ccb6d81c1e7e661545f94d3176752fb1df2f543b902a1e0f51e2"
8888

89+
[[package]]
90+
name = "hex_lit"
91+
version = "0.1.1"
92+
source = "registry+https://github.com/rust-lang/crates.io-index"
93+
checksum = "3011d1213f159867b13cfd6ac92d2cd5f1345762c63be3554e84092d85a50bbd"
94+
8995
[[package]]
9096
name = "js-sys"
9197
version = "0.3.61"
@@ -183,6 +189,7 @@ dependencies = [
183189
"bincode",
184190
"bitcoin_hashes",
185191
"getrandom",
192+
"hex_lit",
186193
"rand",
187194
"rand_core",
188195
"secp256k1-sys",

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ rand_core = "0.6"
4545
serde_cbor = "0.10.0"
4646
serde_test = "1.0.19"
4747
bincode = "1.3.3"
48+
hex_lit = "0.1.1"
4849

4950
[target.wasm32-unknown-unknown.dev-dependencies]
5051
wasm-bindgen-test = "0.3"

src/key.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,8 @@ impl<'de> serde::Deserialize<'de> for XOnlyPublicKey {
15271527
mod test {
15281528
use core::str::FromStr;
15291529

1530+
#[cfg(not(secp256k1_fuzz))]
1531+
use hex_lit::hex;
15301532
#[cfg(feature = "rand")]
15311533
use rand::{self, rngs::mock::StepRng, RngCore};
15321534
use serde_test::{Configure, Token};
@@ -1537,15 +1539,6 @@ mod test {
15371539
use crate::Error::{InvalidPublicKey, InvalidSecretKey};
15381540
use crate::{constants, from_hex, to_hex, Scalar};
15391541

1540-
#[cfg(not(secp256k1_fuzz))]
1541-
macro_rules! hex {
1542-
($hex:expr) => {{
1543-
let mut result = vec![0; $hex.len() / 2];
1544-
from_hex($hex, &mut result).expect("valid hex string");
1545-
result
1546-
}};
1547-
}
1548-
15491542
#[test]
15501543
fn skey_from_slice() {
15511544
let sk = SecretKey::from_slice(&[1; 31]);

src/lib.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -508,19 +508,12 @@ pub(crate) fn random_32_bytes<R: rand::Rng + ?Sized>(rng: &mut R) -> [u8; 32] {
508508
mod tests {
509509
use std::str::FromStr;
510510

511+
use hex_lit::hex;
511512
#[cfg(target_arch = "wasm32")]
512513
use wasm_bindgen_test::wasm_bindgen_test as test;
513514

514515
use super::*;
515516

516-
macro_rules! hex {
517-
($hex:expr) => {{
518-
let mut result = vec![0; $hex.len() / 2];
519-
from_hex($hex, &mut result).expect("valid hex string");
520-
result
521-
}};
522-
}
523-
524517
#[test]
525518
#[cfg(all(feature = "rand", feature = "std"))]
526519
// In rustc 1.72 this Clippy lint was pulled out of clippy and into rustc, and
@@ -678,17 +671,17 @@ mod tests {
678671

679672
#[test]
680673
fn signature_display() {
681-
let hex_str = "3046022100839c1fbc5304de944f697c9f4b1d01d1faeba32d751c0f7acb21ac8a0f436a72022100e89bd46bb3a5a62adc679f659b7ce876d83ee297c7a5587b2011c4fcc72eab45";
682-
let byte_str = hex!(hex_str);
674+
const HEX_STR: &str = "3046022100839c1fbc5304de944f697c9f4b1d01d1faeba32d751c0f7acb21ac8a0f436a72022100e89bd46bb3a5a62adc679f659b7ce876d83ee297c7a5587b2011c4fcc72eab45";
675+
let byte_str = hex!(HEX_STR);
683676

684677
assert_eq!(
685678
ecdsa::Signature::from_der(&byte_str).expect("byte str decode"),
686-
ecdsa::Signature::from_str(hex_str).expect("byte str decode")
679+
ecdsa::Signature::from_str(HEX_STR).expect("byte str decode")
687680
);
688681

689-
let sig = ecdsa::Signature::from_str(hex_str).expect("byte str decode");
690-
assert_eq!(&sig.to_string(), hex_str);
691-
assert_eq!(&format!("{:?}", sig), hex_str);
682+
let sig = ecdsa::Signature::from_str(HEX_STR).expect("byte str decode");
683+
assert_eq!(&sig.to_string(), HEX_STR);
684+
assert_eq!(&format!("{:?}", sig), HEX_STR);
692685

693686
assert!(ecdsa::Signature::from_str(
694687
"3046022100839c1fbc5304de944f697c9f4b1d01d1faeba32d751c0f7acb21ac8a0f436a\

0 commit comments

Comments
 (0)