Refactor the serde Witness unit tests
In preparation for moving unit tests to `primitives` give the serde tests some love by doing: - Split them up to do one thing only - Round trip arbitrary witness - Use better names
This commit is contained in:
parent
9860453b5b
commit
f6a74ef4af
|
@ -925,11 +925,9 @@ mod test {
|
||||||
assert!(deserialize::<Witness>(&bytes).is_err()); // OversizedVectorAllocation
|
assert!(deserialize::<Witness>(&bytes).is_err()); // OversizedVectorAllocation
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serde_bincode() {
|
#[cfg(feature = "serde")]
|
||||||
use bincode;
|
fn serde_bincode_backward_compatibility() {
|
||||||
|
|
||||||
let old_witness_format = vec![vec![0u8], vec![2]];
|
let old_witness_format = vec![vec![0u8], vec![2]];
|
||||||
let new_witness_format = Witness::from_slice(&old_witness_format);
|
let new_witness_format = Witness::from_slice(&old_witness_format);
|
||||||
|
|
||||||
|
@ -937,24 +935,44 @@ mod test {
|
||||||
let new = bincode::serialize(&new_witness_format).unwrap();
|
let new = bincode::serialize(&new_witness_format).unwrap();
|
||||||
|
|
||||||
assert_eq!(old, new);
|
assert_eq!(old, new);
|
||||||
|
|
||||||
let back: Witness = bincode::deserialize(&new).unwrap();
|
|
||||||
assert_eq!(new_witness_format, back);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
|
fn arbitrary_witness() -> Witness {
|
||||||
|
let mut witness = Witness::default();
|
||||||
|
|
||||||
|
witness.push(&[0_u8]);
|
||||||
|
witness.push(&[1_u8; 32]);
|
||||||
|
witness.push(&[2_u8; 72]);
|
||||||
|
|
||||||
|
witness
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serde_human() {
|
#[cfg(feature = "serde")]
|
||||||
use serde_json;
|
fn serde_bincode_roundtrips() {
|
||||||
|
let original = arbitrary_witness();
|
||||||
|
let ser = bincode::serialize(&original).unwrap();
|
||||||
|
let rinsed: Witness = bincode::deserialize(&ser).unwrap();
|
||||||
|
assert_eq!(rinsed, original);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
fn serde_human_roundtrips() {
|
||||||
|
let original = arbitrary_witness();
|
||||||
|
let ser = serde_json::to_string(&original).unwrap();
|
||||||
|
let rinsed: Witness = serde_json::from_str(&ser).unwrap();
|
||||||
|
assert_eq!(rinsed, original);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
fn serde_human() {
|
||||||
let witness = Witness::from_slice(&[vec![0u8, 123, 75], vec![2u8, 6, 3, 7, 8]]);
|
let witness = Witness::from_slice(&[vec![0u8, 123, 75], vec![2u8, 6, 3, 7, 8]]);
|
||||||
|
|
||||||
let json = serde_json::to_string(&witness).unwrap();
|
let json = serde_json::to_string(&witness).unwrap();
|
||||||
|
|
||||||
assert_eq!(json, r#"["007b4b","0206030708"]"#);
|
assert_eq!(json, r#"["007b4b","0206030708"]"#);
|
||||||
|
|
||||||
let back: Witness = serde_json::from_str(&json).unwrap();
|
|
||||||
assert_eq!(witness, back);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue