Add a little proptest around Magic's Debug impl

This commit is contained in:
Deirdre Connolly 2019-12-16 16:21:50 -05:00 committed by Deirdre Connolly
parent 9709b54c57
commit c2411f4315
3 changed files with 32 additions and 40 deletions

View File

@ -1,8 +1,12 @@
use hex;
use std::fmt;
#[cfg(test)]
use proptest_derive::Arbitrary;
/// A magic number identifying the network.
#[derive(Copy, Clone, Eq, PartialEq)]
#[cfg_attr(test, derive(Arbitrary))]
pub struct Magic(pub [u8; 4]);
impl fmt::Debug for Magic {
@ -67,3 +71,31 @@ mod tests {
assert_eq!(format!("{:?}", magics::TESTNET), "Magic(\"fa1af9bf\")");
}
}
#[cfg(test)]
mod proptest {
use hex;
use proptest::prelude::*;
use super::Magic;
// impl Arbitrary for Magic {
// type Parameters = ();
// fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
// Magic
// }
// type Strategy = BoxedStrategy<Self>;
// }
proptest! {
#[test]
fn proptest_magic_from_array(data in any::<[u8; 4]>()) {
assert_eq!(format!("{:?}", Magic(data)), format!("Magic({:x?})", hex::encode(data)));
}
}
}

View File

@ -1,40 +1,20 @@
#[cfg(test)]
use proptest_derive::Arbitrary;
use crate::meta_addr::MetaAddr;
use super::super::types::Nonce;
/// A network request, represented in internal format.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(test, derive(Arbitrary))]
pub enum Request {
/// Requests additional peers from the server.
GetPeers,
/// Advertises peers to the remote server.
#[cfg_attr(test, proptest(skip))]
PushPeers(Vec<MetaAddr>),
/// Heartbeats triggered on peer connection start.
// This is included as a bit of a hack, it should only be used
// internally for connection management. You should not expect to
// be firing or handling `Ping` requests or `Pong` responses.
#[cfg_attr(test, proptest(skip))]
Ping(Nonce),
/// Requests the transactions the remote server has verified but
/// not yet confirmed.
GetMempool,
}
#[cfg(test)]
mod test {
use proptest::prelude::*;
use super::Request;
proptest! {
#[test]
fn proptest_response(res in any::<Request>()) {
println!("{:?}", res);
}
}
}

View File

@ -1,8 +1,5 @@
use std::error::Error;
#[cfg(test)]
use proptest_derive::Arbitrary;
// XXX clean module layout of zebra_chain
use zebra_chain::transaction::Transaction;
@ -10,17 +7,14 @@ use crate::meta_addr::MetaAddr;
/// A response to a network request, represented in internal format.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(test, derive(Arbitrary))]
pub enum Response {
/// Generic success.
Ok,
/// Generic error.
Error,
/// A list of peers, used to respond to `GetPeers`.
#[cfg_attr(test, proptest(skip))]
Peers(Vec<MetaAddr>),
/// A list of transactions, such as in response to `GetMempool
#[cfg_attr(test, proptest(skip))]
Transactions(Vec<Transaction>),
}
@ -32,17 +26,3 @@ where
Self::Error
}
}
#[cfg(test)]
mod test {
use proptest::prelude::*;
use super::Response;
proptest! {
#[test]
fn proptest_response(res in any::<Response>()) {
println!("{:?}", res);
}
}
}