Skip to content

Commit

Permalink
Adding program tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Dec 9, 2024
1 parent f35402b commit 146c593
Show file tree
Hide file tree
Showing 23 changed files with 924 additions and 28 deletions.
52 changes: 49 additions & 3 deletions programs/mpl-core/src/plugins/external_plugin_adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, msg, program_error::ProgramError,
pubkey::Pubkey,
};
use strum::EnumCount;
use strum::{EnumCount, EnumIter};

use crate::{
error::MplCoreError,
Expand All @@ -22,7 +22,17 @@ use super::{
/// List of third party plugin types.
#[repr(C)]
#[derive(
Clone, Copy, Debug, BorshSerialize, BorshDeserialize, Eq, PartialEq, EnumCount, PartialOrd, Ord,
Clone,
Copy,
Debug,
BorshSerialize,
BorshDeserialize,
Eq,
PartialEq,
EnumCount,
PartialOrd,
Ord,
EnumIter,
)]
pub enum ExternalPluginAdapterType {
/// Lifecycle Hook.
Expand Down Expand Up @@ -391,7 +401,9 @@ impl From<&ExternalPluginAdapterInitInfo> for ExternalPluginAdapter {
}

#[repr(C)]
#[derive(Eq, PartialEq, Clone, BorshSerialize, BorshDeserialize, Debug, PartialOrd, Ord, Hash)]
#[derive(
Eq, PartialEq, Clone, BorshSerialize, BorshDeserialize, Debug, PartialOrd, Ord, Hash, EnumIter,
)]
/// An enum listing all the lifecyle events available for external plugin adapter hooks. Note that some
/// lifecycle events such as adding and removing plugins will be checked by default as they are
/// inherently part of the external plugin adapter system.
Expand Down Expand Up @@ -763,3 +775,37 @@ impl From<&ExternalPluginAdapterInitInfo> for ExternalPluginAdapterKey {
}
}
}

/// Test DataBlob sizing
#[cfg(test)]
mod test {
use strum::IntoEnumIterator;

use super::*;

#[test]
fn test_external_plugin_adapter_type_size() {
for fixture in ExternalPluginAdapterType::iter() {
let serialized = fixture.try_to_vec().unwrap();
assert_eq!(
serialized.len(),
fixture.len(),
"Serialized {:?} should match size returned by len()",
fixture
);
}
}

#[test]
fn test_hookable_lifecycle_event_size() {
for fixture in HookableLifecycleEvent::iter() {
let serialized = fixture.try_to_vec().unwrap();
assert_eq!(
serialized.len(),
fixture.len(),
"Serialized {:?} should match size returned by len()",
fixture
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,15 @@ impl PluginValidation for AddBlocker {
reject!()
}
}

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

#[test]
fn test_add_blocker_len() {
let add_blocker = AddBlocker {};
let serialized = add_blocker.try_to_vec().unwrap();
assert_eq!(serialized.len(), add_blocker.len());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,43 @@ impl DataBlob for Attributes {
}

impl PluginValidation for Attributes {}

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

#[test]
fn test_attribute_len() {
let attribute = Attribute {
key: "test".to_string(),
value: "test".to_string(),
};
let serialized = attribute.try_to_vec().unwrap();
assert_eq!(serialized.len(), attribute.len());
}

#[test]
fn test_attributes_default_len() {
let attributes = Attributes::new();
let serialized = attributes.try_to_vec().unwrap();
assert_eq!(serialized.len(), attributes.len());
}

#[test]
fn test_attributes_len() {
let attributes = Attributes {
attribute_list: vec![
Attribute {
key: "test".to_string(),
value: "test".to_string(),
},
Attribute {
key: "test2".to_string(),
value: "test2".to_string(),
},
],
};
let serialized = attributes.try_to_vec().unwrap();
assert_eq!(serialized.len(), attributes.len());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,15 @@ impl PluginValidation for ImmutableMetadata {
reject!()
}
}

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

#[test]
fn test_immutable_metadata_len() {
let immutable_metadata = ImmutableMetadata {};
let serialized = immutable_metadata.try_to_vec().unwrap();
assert_eq!(serialized.len(), immutable_metadata.len());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,26 @@ impl DataBlob for MasterEdition {
+ self.uri.as_ref().map_or(0, |uri| 4 + uri.len())
}
}

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

#[test]
fn test_master_edition_default_len() {
let master_edition = MasterEdition::default();
let serialized = master_edition.try_to_vec().unwrap();
assert_eq!(serialized.len(), master_edition.len());
}

#[test]
fn test_master_edition_len() {
let master_edition = MasterEdition {
max_supply: Some(100),
name: Some("test".to_string()),
uri: Some("test".to_string()),
};
let serialized = master_edition.try_to_vec().unwrap();
assert_eq!(serialized.len(), master_edition.len());
}
}
101 changes: 96 additions & 5 deletions programs/mpl-core/src/plugins/internal/authority_managed/royalties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ use crate::state::DataBlob;
/// The creator on an asset and whether or not they are verified.
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq)]
pub struct Creator {
address: Pubkey, // 32
percentage: u8, // 1
/// The address of the creator.
pub address: Pubkey, // 32
/// The percentage of royalties to be paid to the creator.
pub percentage: u8, // 1
}

impl DataBlob for Creator {
Expand Down Expand Up @@ -54,11 +56,11 @@ impl DataBlob for RuleSet {
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, Eq, PartialEq)]
pub struct Royalties {
/// The percentage of royalties to be paid to the creators.
basis_points: u16, // 2
pub basis_points: u16, // 2
/// A list of creators to receive royalties.
creators: Vec<Creator>, // 4
pub creators: Vec<Creator>, // 4
/// The rule set for the asset to enforce royalties.
rule_set: RuleSet, // 1
pub rule_set: RuleSet, // 1
}

impl DataBlob for Royalties {
Expand Down Expand Up @@ -169,3 +171,92 @@ impl PluginValidation for Royalties {
}
}
}

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

#[test]
fn test_creator_len() {
let creator = Creator {
address: Pubkey::default(),
percentage: 100,
};
let serialized = creator.try_to_vec().unwrap();
assert_eq!(serialized.len(), creator.len());
}

#[test]
fn test_rule_set_default_len() {
let rule_set = RuleSet::None;
let serialized = rule_set.try_to_vec().unwrap();
assert_eq!(serialized.len(), rule_set.len());
}

#[test]
fn test_rule_set_len() {
let rule_sets = vec![
RuleSet::ProgramAllowList(vec![Pubkey::default()]),
RuleSet::ProgramDenyList(vec![Pubkey::default(), Pubkey::default()]),
];
for rule_set in rule_sets {
let serialized = rule_set.try_to_vec().unwrap();
assert_eq!(serialized.len(), rule_set.len());
}
}

#[test]
fn test_royalties_len() {
let royalties = vec![
Royalties {
basis_points: 0,
creators: vec![],
rule_set: RuleSet::None,
},
Royalties {
basis_points: 1,
creators: vec![Creator {
address: Pubkey::default(),
percentage: 1,
}],
rule_set: RuleSet::ProgramAllowList(vec![]),
},
Royalties {
basis_points: 2,
creators: vec![
Creator {
address: Pubkey::default(),
percentage: 2,
},
Creator {
address: Pubkey::default(),
percentage: 3,
},
],
rule_set: RuleSet::ProgramDenyList(vec![Pubkey::default()]),
},
Royalties {
basis_points: 3,
creators: vec![
Creator {
address: Pubkey::default(),
percentage: 3,
},
Creator {
address: Pubkey::default(),
percentage: 4,
},
Creator {
address: Pubkey::default(),
percentage: 5,
},
],
rule_set: RuleSet::ProgramDenyList(vec![Pubkey::default(), Pubkey::default()]),
},
];
for royalty in royalties {
let serialized = royalty.try_to_vec().unwrap();
assert_eq!(serialized.len(), royalty.len());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,31 @@ impl PluginValidation for UpdateDelegate {
abstain!()
}
}

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

#[test]
fn test_update_delegate_default_len() {
let update_delegate = UpdateDelegate::default();
let serialized = update_delegate.try_to_vec().unwrap();
assert_eq!(serialized.len(), update_delegate.len());
}

#[test]
fn test_update_delegate_len() {
let update_delegates = vec![
UpdateDelegate {
additional_delegates: vec![Pubkey::default()],
},
UpdateDelegate {
additional_delegates: vec![Pubkey::default(), Pubkey::default()],
},
];
for update_delegate in update_delegates {
let serialized = update_delegate.try_to_vec().unwrap();
assert_eq!(serialized.len(), update_delegate.len());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ use crate::state::DataBlob;
/// The creator on an asset and whether or not they are verified.
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq, Hash)]
pub struct VerifiedCreatorsSignature {
address: Pubkey, // 32
verified: bool, // 1
/// The address of the creator.
pub address: Pubkey, // 32
/// Whether or not the creator is verified.
pub verified: bool, // 1
}

impl DataBlob for VerifiedCreatorsSignature {
Expand All @@ -30,7 +32,7 @@ impl DataBlob for VerifiedCreatorsSignature {
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, Eq, PartialEq)]
pub struct VerifiedCreators {
/// A list of signatures
signatures: Vec<VerifiedCreatorsSignature>, // 4 + len * VerifiedCreatorsSignature
pub signatures: Vec<VerifiedCreatorsSignature>, // 4 + len * VerifiedCreatorsSignature
}

impl DataBlob for VerifiedCreators {
Expand Down Expand Up @@ -217,3 +219,43 @@ impl PluginValidation for VerifiedCreators {
}
}
}

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

#[test]
fn test_verified_creators_signature_len() {
let verified_creators_signature = VerifiedCreatorsSignature {
address: Pubkey::default(),
verified: false,
};
let serialized = verified_creators_signature.try_to_vec().unwrap();
assert_eq!(serialized.len(), verified_creators_signature.len());
}

#[test]
fn test_verified_creators_default_len() {
let verified_creators = VerifiedCreators { signatures: vec![] };
let serialized = verified_creators.try_to_vec().unwrap();
assert_eq!(serialized.len(), verified_creators.len());
}

#[test]
fn test_verified_creators_len() {
let verified_creators = VerifiedCreators {
signatures: vec![
VerifiedCreatorsSignature {
address: Pubkey::default(),
verified: false,
},
VerifiedCreatorsSignature {
address: Pubkey::default(),
verified: true,
},
],
};
let serialized = verified_creators.try_to_vec().unwrap();
assert_eq!(serialized.len(), verified_creators.len());
}
}
Loading

0 comments on commit 146c593

Please sign in to comment.