Skip to content

Commit fd32720

Browse files
committed
f more representative test
1 parent 7b4c89b commit fd32720

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

lightning/src/util/ser_macros.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,22 +1862,32 @@ mod tests {
18621862
(0, old_field, (legacy, u8, {
18631863
// Sadly the type-checker needs some help
18641864
let _: &(u8, u8) = &new_field;
1865-
if let Some(old_field) = old_field {
1866-
new_field.0 = old_field;
1865+
// If new_field.0 is 0, we assume we hit the default_value case below and overwrite the
1866+
// value with the old data.
1867+
if new_field == (0, 0) {
1868+
if let Some(old_field) = old_field {
1869+
new_field.0 = old_field;
1870+
}
18671871
}
18681872
}, |us: &ExpandedField| Some(us.new_field.0))),
1869-
(1, new_field, required),
1873+
(1, new_field, (default_value, (0, 0))),
18701874
});
18711875

18721876
#[test]
18731877
fn test_legacy_conversion() {
1874-
let mut encoded = ExpandedField { new_field: (42, 43) }.encode();
1875-
assert_eq!(encoded, <Vec<u8>>::from_hex("0700012a01022a2b").unwrap());
1878+
let mut encoded = ExpandedField { new_field: (43, 42) }.encode();
1879+
assert_eq!(encoded, <Vec<u8>>::from_hex("0700012b01022b2a").unwrap());
18761880

1877-
// On read, the post-read action will run, using the old_field value to overwrite the
1878-
// new_field.
1881+
// On read, we'll read the `new_field` and have a value other than `(0, 0)`, causing us to
1882+
// ignore the old field value (in byte 3).
18791883
encoded[3] = 10;
18801884
let read = <ExpandedField as Readable>::read(&mut &encoded[..]).unwrap();
1881-
assert_eq!(read, ExpandedField { new_field: (10, 43) });
1885+
assert_eq!(read, ExpandedField { new_field: (43, 42) });
1886+
1887+
// On read, if we read an old `ExpandedField` that just has a type-0 `old_field` entry,
1888+
// we'll copy that into the first position of `new_field`.
1889+
let encoded = <Vec<u8>>::from_hex("0300012a").unwrap();
1890+
let read = <ExpandedField as Readable>::read(&mut &encoded[..]).unwrap();
1891+
assert_eq!(read, ExpandedField { new_field: (42, 0) });
18821892
}
18831893
}

0 commit comments

Comments
 (0)