Skip to content

Commit

Permalink
common: Re-split serde proptest into bcs / json
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxFangX committed Oct 7, 2022
1 parent e5682b7 commit f9ed446
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
6 changes: 3 additions & 3 deletions common/src/api/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ impl OpaqueUserAuthToken {
mod test {
use super::*;
use crate::test_utils::roundtrip::{
serde_roundtrip_proptest, signed_roundtrip_proptest,
bcs_roundtrip_proptest, signed_roundtrip_proptest,
};

#[test]
fn test_user_signup_request_canonical() {
serde_roundtrip_proptest::<UserSignupRequest>();
bcs_roundtrip_proptest::<UserSignupRequest>();
}

#[test]
Expand All @@ -192,7 +192,7 @@ mod test {

#[test]
fn test_user_auth_request_canonical() {
serde_roundtrip_proptest::<UserAuthRequest>();
bcs_roundtrip_proptest::<UserAuthRequest>();
}

#[test]
Expand Down
8 changes: 4 additions & 4 deletions common/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ mod test {
}

#[test]
fn user_pk_bcs() {
roundtrip::serde_roundtrip_proptest::<UserPk>();
fn user_pk_json() {
roundtrip::json_roundtrip_proptest::<UserPk>();
}

#[test]
Expand All @@ -200,7 +200,7 @@ mod test {
}

#[test]
fn node_pk_bcs() {
roundtrip::serde_roundtrip_proptest::<NodePk>();
fn node_pk_json() {
roundtrip::json_roundtrip_proptest::<NodePk>();
}
}
2 changes: 1 addition & 1 deletion common/src/ln/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ mod test {

#[test]
fn invoice_serde_roundtrip() {
roundtrip::serde_roundtrip_proptest::<LxInvoice>();
roundtrip::json_roundtrip_proptest::<LxInvoice>();
}

#[test]
Expand Down
21 changes: 16 additions & 5 deletions common/src/test_utils/roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use serde::Serialize;

use crate::ed25519;

/// Quickly create a serde roundtrip proptest.
/// Quickly create a BCS roundtrip proptest.
///
/// ```ignore
/// serde_roundtrip_proptest::<UserAuthRequest>();
/// ```
#[cfg_attr(target_env = "sgx", allow(dead_code))]
pub fn serde_roundtrip_proptest<T>()
pub fn bcs_roundtrip_proptest<T>()
where
T: Arbitrary + PartialEq + Serialize + DeserializeOwned,
{
Expand All @@ -24,14 +24,25 @@ where
let value2 = bcs::from_bytes::<T>(&bcs_value1).unwrap();
let bcs_value2 = bcs::to_bytes(&value2).unwrap();
prop_assert_eq!(&value1, &value2);
// Serialized form should be canonical too
prop_assert_eq!(&bcs_value1, &bcs_value2);
});
}

// JSON: human readable
/// Quickly create a BCS roundtrip proptest.
///
/// ```ignore
/// serde_roundtrip_proptest::<UserAuthRequest>();
/// ```
#[cfg_attr(target_env = "sgx", allow(dead_code))]
pub fn json_roundtrip_proptest<T>()
where
T: Arbitrary + PartialEq + Serialize + DeserializeOwned,
{
proptest!(|(value1: T)| {
let json_value1 = serde_json::to_string(&value1).unwrap();
let value2 = serde_json::from_str::<T>(&json_value1).unwrap();
let json_value2 = serde_json::to_string(&value2).unwrap();
prop_assert_eq!(&value1, &value2);
prop_assert_eq!(&json_value1, &json_value2);
});
}

Expand Down

0 comments on commit f9ed446

Please sign in to comment.