Skip to content

Commit

Permalink
Merge pull request #4 from Sajjon/improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon authored Nov 19, 2023
2 parents d7fac1a + 113e428 commit e50b986
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 19 deletions.
35 changes: 35 additions & 0 deletions hiearchal_deterministic/src/bip32/hd_path_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,38 @@ impl ToString for HDPathComponent {
format!("{}{}", self.value(), h_or_empty)
}
}

#[cfg(test)]
mod tests {
use super::HDPathComponent;

#[test]
fn harden() {
assert!(HDPathComponent::harden(0).is_hardened())
}

#[test]
fn hardened_value() {
assert_eq!(HDPathComponent::harden(3).value(), 3)
}

#[test]
fn non_hardended_value() {
assert_eq!(HDPathComponent::from_value(3).value(), 3)
}

#[test]
fn non_hardended_value_isnt_hardened() {
assert!(!HDPathComponent::from_value(3).is_hardened())
}

#[test]
fn hardened_to_string() {
assert_eq!(HDPathComponent::harden(5).to_string(), "5H")
}

#[test]
fn non_hardened_to_string() {
assert_eq!(HDPathComponent::from_value(7).to_string(), "7")
}
}
17 changes: 17 additions & 0 deletions hiearchal_deterministic/src/cap26/cap26_entity_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,20 @@ impl CAP26EntityKind {
}
}
}

#[cfg(test)]
mod tests {
use crate::cap26::cap26_entity_kind::CAP26EntityKind;

#[test]
fn discriminant() {
assert_eq!(CAP26EntityKind::Account.discriminant(), 525);
assert_eq!(CAP26EntityKind::Identity.discriminant(), 618);
}

#[test]
fn format() {
assert_eq!(format!("{}", CAP26EntityKind::Account), "Account");
assert_eq!(format!("{}", CAP26EntityKind::Identity), "Identity");
}
}
35 changes: 28 additions & 7 deletions hiearchal_deterministic/src/cap26/cap26_key_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,41 @@ pub enum CAP26KeyKind {

impl Display for CAP26KeyKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.description())
write!(f, "{:?}", self)
}
}

impl CAP26KeyKind {
/// The raw representation of this key kind, an `HDPathValue`.
pub fn discriminant(&self) -> HDPathValue {
*self as HDPathValue
}
}

#[cfg(test)]
mod tests {
use crate::cap26::cap26_key_kind::CAP26KeyKind;

#[test]
fn discriminant() {
assert_eq!(CAP26KeyKind::TransactionSigning.discriminant(), 1460);
assert_eq!(CAP26KeyKind::AuthenticationSigning.discriminant(), 1678);
assert_eq!(CAP26KeyKind::AuthenticationSigning.discriminant(), 1678);
}

fn description(&self) -> String {
match self {
Self::TransactionSigning => "TransactionSigning".to_string(),
Self::AuthenticationSigning => "AuthenticationSigning".to_string(),
Self::MessageEncryption => "MessageEncryption".to_string(),
}
#[test]
fn format() {
assert_eq!(
format!("{}", CAP26KeyKind::TransactionSigning),
"TransactionSigning"
);
assert_eq!(
format!("{}", CAP26KeyKind::AuthenticationSigning),
"AuthenticationSigning"
);
assert_eq!(
format!("{}", CAP26KeyKind::MessageEncryption),
"MessageEncryption"
);
}
}
12 changes: 12 additions & 0 deletions profile/src/v100/address/account_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ mod tests {
.is_ok());
}

#[test]
fn format() {
let a = AccountAddress::try_from_bech32(
"account_rdx16xlfcpp0vf7e3gqnswv8j9k58n6rjccu58vvspmdva22kf3aplease",
)
.unwrap();
assert_eq!(
format!("{}", a),
"account_rdx16xlfcpp0vf7e3gqnswv8j9k58n6rjccu58vvspmdva22kf3aplease"
);
}

#[test]
fn from_public_key_bytes_and_network_id() {
let public_key = Ed25519PublicKey::from_str(
Expand Down
12 changes: 12 additions & 0 deletions profile/src/v100/address/identity_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ mod tests {
.is_ok());
}

#[test]
fn format() {
let a = IdentityAddress::try_from_bech32(
"identity_rdx12tgzjrz9u0xz4l28vf04hz87eguclmfaq4d2p8f8lv7zg9ssnzku8j",
)
.unwrap();
assert_eq!(
format!("{}", a),
"identity_rdx12tgzjrz9u0xz4l28vf04hz87eguclmfaq4d2p8f8lv7zg9ssnzku8j"
);
}

#[test]
fn from_public_key_bytes_and_network_id() {
let public_key = Ed25519PublicKey::from_str(
Expand Down
3 changes: 3 additions & 0 deletions profile/src/v100/entity/account/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ impl HierarchicalDeterministicFactorInstance {

// Getters
impl Account {
/// Returns this accounts `display_name` as **a clone**.
///
/// Use [`self::set_display_name()`] to update it.
pub fn get_display_name(&self) -> String {
self.display_name.borrow().clone().to_string()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::cell::RefCell;
use std::cell::{Ref, RefCell};

use serde::{Deserialize, Serialize};

Expand All @@ -21,6 +21,7 @@ pub struct OnLedgerSettings {
}

impl OnLedgerSettings {
/// Instantiates a new `OnLedgerSettings` with the specified `ThirdPartyDeposits``
pub fn new(third_party_deposits: ThirdPartyDeposits) -> Self {
Self {
third_party_deposits: RefCell::new(third_party_deposits),
Expand All @@ -29,8 +30,9 @@ impl OnLedgerSettings {
}

impl OnLedgerSettings {
pub fn get_third_party_deposits(&self) -> ThirdPartyDeposits {
self.third_party_deposits.borrow().clone()
/// Returns the `ThirdPartyDeposits` value
pub fn get_third_party_deposits(&self) -> Ref<ThirdPartyDeposits> {
self.third_party_deposits.borrow()
}

pub fn set_third_party_deposits(&self, new: ThirdPartyDeposits) {
Expand Down Expand Up @@ -61,6 +63,22 @@ mod tests {

use super::OnLedgerSettings;

#[test]
fn get_third_party_deposits_then_mutate() {
let settings = OnLedgerSettings::default();
assert_eq!(
settings.get_third_party_deposits().get_deposit_rule(),
DepositRule::AcceptAll
);
settings
.get_third_party_deposits()
.set_deposit_rule(DepositRule::DenyAll);
assert_eq!(
settings.get_third_party_deposits().get_deposit_rule(),
DepositRule::DenyAll
);
}

#[test]
fn json_roundtrip() {
let model = OnLedgerSettings::default();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,25 @@ impl ThirdPartyDeposits {

// Getters
impl ThirdPartyDeposits {
/// Returns the deposit rule.
/// Returns the general `deposit_rule` **as a clone**.
///
/// Use [`self::set_deposit_rule(new)`] to update it.
pub fn get_deposit_rule(&self) -> DepositRule {
self.deposit_rule.get().clone()
}

/// Returns the `assets_exception_list` set value.
/// Returns the `assets_exception_list` **as a clone**.
///
/// Use [`self::set_assets_exception_list()`], [`self::add_asset_exception()`]
/// or [`self::remove_asset_exception`] to update it.
pub fn get_assets_exception_list(&self) -> BTreeSet<AssetException> {
self.assets_exception_list.borrow().clone()
}

/// Returns the `depositors_allow_list` set value.
/// Returns the `depositors_allow_list` **as a clone**.
///
/// Use [`self::set_depositors_allow_list()`], [`self::allow_depositor()`],
/// or [`self::remove_allowed_depositor()`] to update it.
pub fn get_depositors_allow_list(&self) -> BTreeSet<DepositorAddress> {
self.depositors_allow_list.borrow().clone()
}
Expand Down Expand Up @@ -108,12 +116,12 @@ impl ThirdPartyDeposits {
///
/// If the set did not previously contain an equal value, true is returned.
/// If the set already contained an equal value, false is returned, and the entry is not updated.
pub fn allow_depositors(&self, depositor: DepositorAddress) -> bool {
pub fn allow_depositor(&self, depositor: DepositorAddress) -> bool {
self.depositors_allow_list.borrow_mut().insert(depositor)
}

// If the set contains an element equal to `DepositorAddress`, removes it from the set and drops it. Returns whether such an element was present.
pub fn remove_allowed_depositer(&self, depositor: &DepositorAddress) -> bool {
pub fn remove_allowed_depositor(&self, depositor: &DepositorAddress) -> bool {
self.depositors_allow_list.borrow_mut().remove(depositor)
}
}
Expand Down Expand Up @@ -247,13 +255,13 @@ mod tests {
)
.unwrap(),
);
assert!(settings.allow_depositors(depositor.clone()));
assert!(settings.allow_depositor(depositor.clone()));
assert_eq!(settings.get_depositors_allow_list().len(), 1);
assert!(settings.remove_allowed_depositer(&depositor));
assert!(settings.remove_allowed_depositor(&depositor));
assert_eq!(settings.get_depositors_allow_list().len(), 0);
settings.set_depositors_allow_list(BTreeSet::from_iter([depositor.clone()]));
assert!(
!settings.allow_depositors(depositor.clone()),
!settings.allow_depositor(depositor.clone()),
"Expected `false` since already present."
);
}
Expand Down
1 change: 1 addition & 0 deletions profile/src/v100/entity/entity_flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub enum EntityFlag {
}

impl EntityFlag {
/// Human readable form of the flag
pub fn discriminant(&self) -> String {
format!("{}", self)
}
Expand Down
14 changes: 14 additions & 0 deletions profile/src/v100/entity/entity_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ impl EntityFlags {
self.0.remove(flag)
}

///Returns true if the set contains the `flag` equal to the value.
pub fn contains(&self, flag: &EntityFlag) -> bool {
self.0.contains(flag)
}

/// Returns the number of flags in the set.
pub fn len(&self) -> usize {
self.0.len()
}
Expand Down Expand Up @@ -87,6 +89,18 @@ mod tests {
);
}

#[test]
fn remove_existing_flag() {
assert!(EntityFlags::with_flag(EntityFlag::DeletedByUser)
.remove_flag(&EntityFlag::DeletedByUser));
}

#[test]
fn remove_non_existing_flag() {
assert!(!EntityFlags::default().remove_flag(&EntityFlag::DeletedByUser));
// does not exist
}

#[test]
fn new_with_duplicates_of_f_contains_only_f() {
assert_eq!(
Expand Down
7 changes: 6 additions & 1 deletion profile/src/v100/header/profilesnapshot_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl ProfileSnapshotVersion {

impl Display for ProfileSnapshotVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{}", self.discriminant())
write!(f, "{}", self.discriminant())
}
}

Expand Down Expand Up @@ -76,4 +76,9 @@ mod tests {
fn discriminant() {
assert_eq!(ProfileSnapshotVersion::V100.discriminant(), 100)
}

#[test]
fn display() {
assert_eq!(format!("{}", ProfileSnapshotVersion::V100), "100")
}
}
15 changes: 15 additions & 0 deletions wallet_kit_common/src/network_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ mod tests {

use super::NetworkID;

#[test]
fn mainnet_is_default() {
assert_eq!(NetworkID::default(), NetworkID::Mainnet);
}

#[test]
fn mainnet_logical_name_is_lowercase_mainnet() {
assert_eq!(NetworkID::Mainnet.logical_name(), "mainnet");
}

#[test]
fn mainnet_fmt() {
assert_eq!(format!("{}", NetworkID::Mainnet), "mainnet");
}

#[test]
fn json() {
assert_json_value_eq_after_roundtrip(&NetworkID::Mainnet, json!(1));
Expand Down
35 changes: 35 additions & 0 deletions wallet_kit_common/src/utils/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,38 @@ pub fn iso8601(dt: &NaiveDateTime) -> String {
pub fn date(dt: &NaiveDateTime) -> String {
date_to_string(dt, "%Y-%m-%d")
}

#[cfg(test)]
mod tests {
use std::collections::BTreeSet;

use chrono::NaiveDateTime;
use uuid::Uuid;

use crate::utils::factory::iso8601;

use super::{date, id, now};

#[test]
fn date_now() {
let d0 = now();
let mut d1 = now();
for _ in 0..10 {
d1 = now();
}
assert!(d1 > d0);
}

#[test]
fn id_unique() {
let n = 100;
let set = (0..n).into_iter().map(|_| id()).collect::<BTreeSet<Uuid>>();
assert_eq!(set.len(), n);
}

#[test]
fn date_str() {
assert_eq!(date(&NaiveDateTime::UNIX_EPOCH), "1970-01-01");
assert_eq!(iso8601(&NaiveDateTime::UNIX_EPOCH), "1970-01-01 00:00:00");
}
}

0 comments on commit e50b986

Please sign in to comment.