Skip to content

Commit c107a62

Browse files
committed
Add cargo tests
1 parent 1b7c29e commit c107a62

File tree

2 files changed

+80
-0
lines changed
  • solana/programs/example-native-token-transfers/tests

2 files changed

+80
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#![cfg(feature = "test-sbf")]
2+
#![feature(type_changing_struct_update)]
3+
4+
use example_native_token_transfers::error::NTTError;
5+
use ntt_messages::mode::Mode;
6+
use solana_program_test::*;
7+
use solana_sdk::{instruction::InstructionError, signer::Signer, transaction::TransactionError};
8+
9+
use crate::{
10+
common::{setup::setup, submit::Submittable},
11+
sdk::instructions::admin::{set_threshold, SetThreshold},
12+
};
13+
14+
pub mod common;
15+
pub mod sdk;
16+
17+
#[tokio::test]
18+
async fn test_zero_threshold() {
19+
let (mut ctx, test_data) = setup(Mode::Locking).await;
20+
21+
let err = set_threshold(
22+
&test_data.ntt,
23+
SetThreshold {
24+
owner: test_data.program_owner.pubkey(),
25+
},
26+
0,
27+
)
28+
.submit_with_signers(&[&test_data.program_owner], &mut ctx)
29+
.await
30+
.unwrap_err();
31+
assert_eq!(
32+
err.unwrap(),
33+
TransactionError::InstructionError(
34+
0,
35+
InstructionError::Custom(NTTError::ZeroThreshold.into())
36+
)
37+
);
38+
}
39+
40+
#[tokio::test]
41+
async fn test_threshold_too_high() {
42+
let (mut ctx, test_data) = setup(Mode::Burning).await;
43+
44+
let err = set_threshold(
45+
&test_data.ntt,
46+
SetThreshold {
47+
owner: test_data.program_owner.pubkey(),
48+
},
49+
2,
50+
)
51+
.submit_with_signers(&[&test_data.program_owner], &mut ctx)
52+
.await
53+
.unwrap_err();
54+
assert_eq!(
55+
err.unwrap(),
56+
TransactionError::InstructionError(
57+
0,
58+
InstructionError::Custom(NTTError::ThresholdTooHigh.into())
59+
)
60+
);
61+
}

solana/programs/example-native-token-transfers/tests/sdk/instructions/admin.rs

+19
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,22 @@ pub fn register_transceiver(ntt: &NTT, accounts: RegisterTransceiver) -> Instruc
7373
data: data.data(),
7474
}
7575
}
76+
77+
pub struct SetThreshold {
78+
pub owner: Pubkey,
79+
}
80+
81+
pub fn set_threshold(ntt: &NTT, accounts: SetThreshold, threshold: u8) -> Instruction {
82+
let data = example_native_token_transfers::instruction::SetThreshold { threshold };
83+
84+
let accounts = example_native_token_transfers::accounts::SetThreshold {
85+
config: ntt.config(),
86+
owner: accounts.owner,
87+
};
88+
89+
Instruction {
90+
program_id: example_native_token_transfers::ID,
91+
accounts: accounts.to_account_metas(None),
92+
data: data.data(),
93+
}
94+
}

0 commit comments

Comments
 (0)