Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/wallet/locked_outpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,81 @@ impl Merge for ChangeSet {
self.outpoints.is_empty()
}
}

#[cfg(test)]
mod test {
use super::*;
use bdk_chain::Merge;
use bitcoin::{hashes::Hash, OutPoint, Txid};

/// Helper to create an `OutPoint` from an index byte.
fn outpoint(vout: u32) -> OutPoint {
OutPoint {
txid: Txid::from_byte_array([vout as u8; 32]),
vout,
}
}

#[test]
fn test_is_empty_default() {
let cs = ChangeSet::default();
assert!(cs.is_empty());
}

#[test]
fn test_is_empty_with_entries() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn test_is_empty_with_entries() {
fn test_is_non_empty_with_entries() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I saw the test name and read through what the test tests, I was confused. @ValuedMammal made a suggestion in src/wallet/changeset.rs to update the test names so they reflect the expected outcome rather than just the method being called. I think that applies to this too.

let cs = ChangeSet {
outpoints: [(outpoint(0), true)].into(),
};
assert!(!cs.is_empty());
}

#[test]
fn test_merge_into_empty() {
let mut cs = ChangeSet::default();
let other = ChangeSet {
outpoints: [(outpoint(1), true)].into(),
};
cs.merge(other);
assert_eq!(cs.outpoints.len(), 1);
assert!(cs.outpoints[&outpoint(1)]);
}

#[test]
fn test_merge_empty_into_non_empty() {
let mut cs = ChangeSet {
outpoints: [(outpoint(0), true)].into(),
};
let snapshot = cs.clone();
cs.merge(ChangeSet::default());
assert_eq!(cs, snapshot);
}

#[test]
fn test_merge_disjoint() {
let mut cs = ChangeSet {
outpoints: [(outpoint(0), true)].into(),
};
let other = ChangeSet {
outpoints: [(outpoint(1), false)].into(),
};
cs.merge(other);
assert_eq!(cs.outpoints.len(), 2);
assert!(cs.outpoints[&outpoint(0)]);
assert!(!cs.outpoints[&outpoint(1)]);
}

#[test]
fn test_merge_overwrites_duplicate() {
let op = outpoint(0);
let mut cs = ChangeSet {
outpoints: [(op, true)].into(),
};
let other = ChangeSet {
outpoints: [(op, false)].into(),
};
cs.merge(other);
assert_eq!(cs.outpoints.len(), 1);
assert!(!cs.outpoints[&op], "other's value should overwrite self");
}
}