Skip to content

Commit

Permalink
test(network-primitives): add unit tests for BlockTransactions (#1619)
Browse files Browse the repository at this point in the history
test(network-primitives): add unit tests for BlockTransactions
  • Loading branch information
tcoratger authored Nov 5, 2024
1 parent 0a2b334 commit 692f422
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions crates/network-primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,96 @@ mod tests {
let full = false;
assert_eq!(BlockTransactionsKind::Hashes, full.into());
}

#[test]
fn test_block_transactions_default() {
let default: BlockTransactions<()> = BlockTransactions::default();
assert!(default.is_hashes());
assert_eq!(default.len(), 0);
}

#[test]
fn test_block_transactions_is_methods() {
let hashes: BlockTransactions<()> = BlockTransactions::Hashes(vec![B256::ZERO]);
let full: BlockTransactions<u32> = BlockTransactions::Full(vec![42]);
let uncle: BlockTransactions<()> = BlockTransactions::Uncle;

assert!(hashes.is_hashes());
assert!(!hashes.is_full());
assert!(!hashes.is_uncle());

assert!(full.is_full());
assert!(!full.is_hashes());
assert!(!full.is_uncle());

assert!(uncle.is_uncle());
assert!(!uncle.is_full());
assert!(!uncle.is_hashes());
}

#[test]
fn test_as_hashes() {
let hashes = vec![B256::ZERO, B256::repeat_byte(1)];
let tx_hashes: BlockTransactions<()> = BlockTransactions::Hashes(hashes.clone());

assert_eq!(tx_hashes.as_hashes(), Some(hashes.as_slice()));
}

#[test]
fn test_as_transactions() {
let transactions = vec![42, 43];
let txs = BlockTransactions::Full(transactions.clone());

assert_eq!(txs.as_transactions(), Some(transactions.as_slice()));
}

#[test]
fn test_block_transactions_len_and_is_empty() {
let hashes: BlockTransactions<()> = BlockTransactions::Hashes(vec![B256::ZERO]);
let full = BlockTransactions::Full(vec![42]);
let uncle: BlockTransactions<()> = BlockTransactions::Uncle;

assert_eq!(hashes.len(), 1);
assert_eq!(full.len(), 1);
assert_eq!(uncle.len(), 0);

assert!(!hashes.is_empty());
assert!(!full.is_empty());
assert!(uncle.is_empty());
}

#[test]
fn test_block_transactions_txns_iterator() {
let transactions = vec![42, 43];
let txs = BlockTransactions::Full(transactions);
let mut iter = txs.txns();

assert_eq!(iter.next(), Some(&42));
assert_eq!(iter.next(), Some(&43));
assert_eq!(iter.next(), None);
}

#[test]
fn test_block_transactions_into_transactions() {
let transactions = vec![42, 43];
let txs = BlockTransactions::Full(transactions.clone());
let collected: Vec<_> = txs.into_transactions().collect();

assert_eq!(collected, transactions);
}

#[test]
fn test_block_transactions_kind_conversion() {
let full: BlockTransactionsKind = true.into();
assert_eq!(full, BlockTransactionsKind::Full);

let hashes: BlockTransactionsKind = false.into();
assert_eq!(hashes, BlockTransactionsKind::Hashes);

let bool_full: bool = BlockTransactionsKind::Full.into();
assert!(bool_full);

let bool_hashes: bool = BlockTransactionsKind::Hashes.into();
assert!(!bool_hashes);
}
}

0 comments on commit 692f422

Please sign in to comment.