Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulmth committed Aug 1, 2023
1 parent dfc7adc commit 5f628d1
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion sdk/src/types/block/context_input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,42 @@ impl ContextInput {
matches!(self, Self::Reward(_))
}

/// Checks whether the context input is a [`CommitmentContextInput`].
pub fn is_commitment(&self) -> bool {
matches!(self, Self::Commitment(_))
}

/// Checks whether the context input is a [`BlockIssuanceCreditContextInput`].
pub fn is_block_issuance_credit(&self) -> bool {
matches!(self, Self::BlockIssuanceCredit(_))
}

/// Gets the input as an actual [`RewardContextInput`].
/// PANIC: do not call on a non-reward context input.
pub fn as_reward(&self) -> &RewardContextInput {
if let Self::Reward(input) = self {
input
} else {
panic!("context input is not of type reward {}", self);
panic!("context input is not of type reward: {:?}", self);
}
}

/// Gets the input as an actual [`CommitmentContextInput`].
/// PANIC: do not call on a non-commitment context input.
pub fn as_commitment(&self) -> &CommitmentContextInput {
if let Self::Commitment(input) = self {
input
} else {
panic!("context input is not of type commitment: {:?}", self);
}
}
/// Gets the input as an actual [`BlockIssuanceCreditContextInput`].
/// PANIC: do not call on a non-block-issuance-credit context input.
pub fn as_block_issuance_credit(&self) -> &BlockIssuanceCreditContextInput {
if let Self::BlockIssuanceCredit(input) = self {
input
} else {
panic!("context input is not of type block issuance credit: {:?}", self);
}
}
}
Expand Down Expand Up @@ -108,3 +137,32 @@ pub mod dto {
}
}
}

#[cfg(test)]
mod tests {
use super::{CommitmentContextInput, ContextInput, RewardContextInput};
use crate::types::block::{
context_input::BlockIssuanceCreditContextInput, output::AccountId, slot::SlotCommitmentId,
};
use core::str::FromStr;

#[test]
fn test_context_input() {
let reward = ContextInput::Reward(RewardContextInput::new(10));
let reward: &RewardContextInput = reward.as_reward();
assert_eq!(reward.to_string(), "10");

let slot_commitment_id_str =
"0xedf5f572c58ddf4b4f9567d82bf96689cc68b730df796d822b4b9fb643f5efda4f9567d82bf96689";
let slot_commitment_id = SlotCommitmentId::from_str(slot_commitment_id_str).unwrap();
let commitment = ContextInput::Commitment(CommitmentContextInput::new(slot_commitment_id));
let commitment: &CommitmentContextInput = commitment.as_commitment();
assert_eq!(commitment.to_string(), slot_commitment_id_str);

let account_id_str = "0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649";
let account_id = AccountId::from_str(account_id_str).unwrap();
let block_issuance_credit = ContextInput::BlockIssuanceCredit(BlockIssuanceCreditContextInput::new(account_id));
let block_issuance_credit: &BlockIssuanceCreditContextInput = block_issuance_credit.as_block_issuance_credit();
assert_eq!(block_issuance_credit.to_string(), account_id_str);
}
}

0 comments on commit 5f628d1

Please sign in to comment.