Skip to content

Commit 252c0ce

Browse files
committed
common: add test_utils::snapshot::parse_sample_data helper
Make it log both inputs and comments in the sample data for easier debugging when something breaks
1 parent 817448b commit 252c0ce

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

common/src/ln/payments.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ mod test {
718718
use proptest::{arbitrary::any, prop_assert_eq, proptest};
719719

720720
use super::*;
721-
use crate::test_utils::roundtrip;
721+
use crate::test_utils::{roundtrip, snapshot};
722722

723723
#[test]
724724
fn enums_roundtrips() {
@@ -861,18 +861,11 @@ mod test {
861861
{"index":"5155186382553476589-ln_2877475d06893a3c833caf5a0872253ca30372d5f79241fcf917d893fd0184f6","kind":"spontaneous","direction":"outbound","amount":"1775777429636972.896","fees":"686803029182910.189","status":"pending","status_str":"pending"}
862862
"#;
863863

864-
for input in sample_lines(inputs) {
864+
for input in snapshot::parse_sample_data(inputs) {
865865
let value1: BasicPayment = serde_json::from_str(input).unwrap();
866866
let output = serde_json::to_string(&value1).unwrap();
867867
let value2: BasicPayment = serde_json::from_str(&output).unwrap();
868868
assert_eq!(value1, value2);
869869
}
870870
}
871-
872-
/// Parse out the sample data json lines, ignoring comments
873-
fn sample_lines(data: &str) -> impl Iterator<Item = &str> {
874-
let data = data.trim();
875-
data.lines()
876-
.filter(|line| !line.is_empty() && !line.starts_with("---"))
877-
}
878871
}

common/src/test_utils/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
pub mod arbitrary;
33
/// Quickly create roundtrip proptest for various serialization schemes.
44
pub mod roundtrip;
5+
/// Extremely basic snapshot testing
6+
pub mod snapshot;
57

68
// Dummy values for some commonly appearing fields
79
pub const DUMMY_BACKEND_URL: &str = "http://[::1]:3030";

common/src/test_utils/snapshot.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// Parse out the sample data json lines `Ok(line)` and comments `Err(line)`
2+
pub fn parse_sample_data(data: &str) -> impl Iterator<Item = &str> {
3+
let data = data.trim();
4+
data.lines().filter(|line| {
5+
if line.is_empty() {
6+
return false;
7+
}
8+
9+
// Print data and comments for easier debugging when something breaks
10+
println!("{line}");
11+
12+
// Ignore comments
13+
!line.starts_with("---")
14+
})
15+
}

0 commit comments

Comments
 (0)