bdk icon indicating copy to clipboard operation
bdk copied to clipboard

assert_matches!

Open RCasatta opened this issue 4 years ago • 4 comments

As briefly discussed in https://github.com/bitcoindevkit/bdk/pull/341#discussion_r627858886 and following

we should add https://docs.rs/crate/assert_matches/1.5.0 as a dev-dep which will also be standardized one day https://doc.rust-lang.org/nightly/std/macro.assert_matches.html

And changes our pattern matching test to be like:

assert_matches!(wallet.sign(&mut psbt, options), Err(Error::Signer(SignerError::InputIndexOutOfRange)))

RCasatta avatar May 07 '21 11:05 RCasatta

The matches! macro has been stabilized since 1.42.0.

We can (and already do) use it like this: assert!(matches!(...)) to have behavior similar to assert_matches! (though the errors may be less informative).

This way we'll avoid an additional dev-dependency.

eupn avatar Jul 03 '21 07:07 eupn

though the errors may be less informative

Yeah that's what I like, when assert!(matches!()) test fails I end up printing the values before and launch tests with --nocapture which is unpleasant

RCasatta avatar Jul 03 '21 17:07 RCasatta

what's wrong with

assert_eq!(wallet.sign(&mut psbt, options), Err(Error::Signer(SignerError::InputIndexOutOfRange)))

?

LLFourn avatar Jul 04 '21 03:07 LLFourn

what's wrong with

assert_eq!(wallet.sign(&mut psbt, options), Err(Error::Signer(SignerError::InputIndexOutOfRange)))

?

You may want to test something that doesn't impl Eq or do stuff like:

assert_matches!(&policy.item, Signature(pk_or_f) if pk_or_f.fingerprint.unwrap() == fingerprint));

By the way, I think it would be good to have assert_matches! but if people have doubts it's absolutely fine without it

RCasatta avatar Jul 04 '21 07:07 RCasatta

closed by #821

notmandatory avatar Dec 26 '22 21:12 notmandatory