2 releases
0.0.1-alpha.2 | Apr 28, 2024 |
---|---|
0.0.1-alpha.1 | Apr 27, 2024 |
#9 in #proptest
327 downloads per month
40KB
719 lines
Rust Bitcoin Proptest Strategies
Proptest strategies for Bitcoin-related code.
This collection of generators (called strategies in proptest's terminology) helps developers of Bitcoin-related software written in Rust supply random data – both valid and invalid – to their property-based tests.
Visit documentation for details.
Example
#[cfg(test)]
mod tests {
proptest! {
#[test]
// generates valid hex-encoded public keys, both compressed and uncompressed
fn pubkey_parsing(s in prop::secp256k1::public_key::valid::hex()) {
prop_assert!(s.len() == 66 || s.len() == 130);
prop_assert!(my_parser(s).is_ok());
}
}
}
lib.rs
:
Rust Bitcoin Proptest Strategies
Proptest strategies for Bitcoin-related code.
This collection of generators (called strategies in proptest's terminology) helps developers of Bitcoin-related software written in Rust supply random data – both valid and invalid – to their property-based tests.
Basic rules
- unless documentation specifies, the strategy generates anything, i. e. all possible variants, both valid and invalid, as long as type permits, for example:
- “Generates descriptors” means any type, both valid and invalid
- “Generates valid descriptors” means any type, all valid
- every strategy also has an adjacent strategy called
build
where inputs (in form of strategies) can be specified, for example:
- to generate PKH descriptor, some public key needs to be supplied
descriptor::pkh()
will use a strategy generating any possible valid public keydescriptor::pkh::valid::build(impl Strategy<DescriptorPublicKey>)
allows to supply custom strategy for generating public keys
Organization
All strategies are placed in module prop
sorted by category. Inside each category, there is typically
a nested set of modules that further specify properties of the strategies. The modules are usually
organized as prop::category::what::what::what::how()
where how()
is the strategy returning a specific
format of what
, for example: prop::secp256k1::public_key::invalid::compressed::vec()
.
If a strategy (in form of function) is placed at the same level as other modules, they typically generate
mix of all modules at that level. For example prop::secp256k1::public_key::valid::vec()
will generate
byte vectors of both compressed an uncompressed public keys and prop::secp256k1::public_key::vec()
will
generate valid and invalid, compressed as well as uncompressed public keys.
Example
#[cfg(test)]
mod tests {
proptest! {
#[test]
// generates valid hex-encoded public keys, both compressed and uncompressed
fn pubkey_parsing(s in prop::secp256k1::public_key::valid::hex()) {
prop_assert!(s.len() == 66 || s.len() == 130);
prop_assert!(my_parser(s).is_ok());
}
}
}
Dependencies
~13MB
~166K SLoC