Skip to content

Commit

Permalink
test: de-macro added rountrip test
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yf0μl committed Oct 8, 2024
1 parent 8c9124d commit e70919c
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
source: borsh/tests/roundtrip/test_ip_addr.rs
expression: encoded
---
[
3,
0,
0,
0,
0,
192,
168,
0,
1,
1,
32,
1,
13,
184,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
254,
128,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: borsh/tests/roundtrip/test_ip_addr.rs
expression: encoded
---
[
192,
168,
0,
1,
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: borsh/tests/roundtrip/test_ip_addr.rs
expression: encoded
---
[
0,
192,
168,
0,
1,
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: borsh/tests/roundtrip/test_ip_addr.rs
expression: encoded
---
[
32,
1,
13,
184,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: borsh/tests/roundtrip/test_ip_addr.rs
expression: encoded
---
[
1,
32,
1,
13,
184,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
]
76 changes: 48 additions & 28 deletions borsh/tests/roundtrip/test_ip_addr.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,55 @@

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

macro_rules! test_ip_roundtrip {
($test_name:ident, $original:expr) => {
#[test]
fn $test_name() {
let original = $original;
let encoded = borsh::to_vec(&original).expect("Serialization failed");
let decoded = borsh::from_slice::<IpAddr>(&encoded).expect("Deserialization failed");
assert_eq!(original, decoded);
}
};
#[test]
fn test_ipv4_addr_roundtrip_enum() {
let original = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1));
let encoded = borsh::to_vec(&original).expect("Serialization failed");
#[cfg(feature = "std")]
insta::assert_debug_snapshot!(encoded);
let decoded = borsh::from_slice::<IpAddr>(&encoded).expect("Deserialization failed");
assert_eq!(original, decoded);
}

test_ip_roundtrip!(test_ipv4_addr_roundtrip, IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)));
test_ip_roundtrip!(test_ipv6_addr_roundtrip, IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1)));
#[test]
fn test_ipv4_addr_roundtrip() {
let original = Ipv4Addr::new(192, 168, 0, 1);
let encoded = borsh::to_vec(&original).expect("Serialization failed");
#[cfg(feature = "std")]
insta::assert_debug_snapshot!(encoded);
let decoded = borsh::from_slice::<Ipv4Addr>(&encoded).expect("Deserialization failed");
assert_eq!(original, decoded);
}

macro_rules! test_ipaddr_vec_roundtrip {
($test_name:ident, $original:expr) => {
#[test]
fn $test_name() {
let original = $original;
let encoded = borsh::to_vec(&original).expect("Serialization failed");
let decoded = borsh::from_slice::<Vec<IpAddr>>(&encoded).expect("Deserialization failed");
assert_eq!(original, decoded);
}
};
#[test]
fn test_ipv6_addr_roundtrip_enum() {
let original = IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1));
let encoded = borsh::to_vec(&original).expect("Serialization failed");
#[cfg(feature = "std")]
insta::assert_debug_snapshot!(encoded);
let decoded = borsh::from_slice::<IpAddr>(&encoded).expect("Deserialization failed");
assert_eq!(original, decoded);
}

test_ipaddr_vec_roundtrip!(test_ip_addr_vec_roundtrip, vec![
IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)),
IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1)),
IpAddr::V6(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)),
]);
#[test]
fn test_ipv6_addr_roundtrip() {
let original = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1);
let encoded = borsh::to_vec(&original).expect("Serialization failed");
#[cfg(feature = "std")]
insta::assert_debug_snapshot!(encoded);
let decoded = borsh::from_slice::<Ipv6Addr>(&encoded).expect("Deserialization failed");
assert_eq!(original, decoded);
}

#[test]
fn test_ipaddr_vec_roundtrip() {
let original = vec![
IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)),
IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1)),
IpAddr::V6(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)),
];
let encoded = borsh::to_vec(&original).expect("Serialization failed");
#[cfg(feature = "std")]
insta::assert_debug_snapshot!(encoded);
let decoded = borsh::from_slice::<Vec<IpAddr>>(&encoded).expect("Deserialization failed");
assert_eq!(original, decoded);
}

0 comments on commit e70919c

Please sign in to comment.