Repository navigation
Expand file tree
/
Copy pathlib.rs
More file actions
96 lines (89 loc) · 2.82 KB
/
Copy pathlib.rs
File metadata and controls
96 lines (89 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use borsh::BorshDeserialize;
use helloworld::{process_instruction, GreetingAccount};
use solana_program_test::*;
use solana_sdk::{
account::Account,
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
signature::Signer,
transaction::Transaction,
};
use std::mem;
#[tokio::test]
async fn test_helloworld() {
let program_id = Pubkey::new_unique();
let greeted_pubkey = Pubkey::new_unique();
let mut program_test = ProgramTest::new(
"helloworld", // Run the BPF version with `cargo test-bpf`
program_id,
processor!(process_instruction), // Run the native version with `cargo test`
);
program_test.add_account(
greeted_pubkey,
Account {
lamports: 5,
data: vec![0_u8; mem::size_of::<u32>()],
owner: program_id,
..Account::default()
},
);
let (mut banks_client, payer, recent_blockhash) = program_test.start().await;
// Verify account has zero greetings
let greeted_account = banks_client
.get_account(greeted_pubkey)
.await
.expect("get_account")
.expect("greeted_account not found");
assert_eq!(
GreetingAccount::try_from_slice(&greeted_account.data)
.unwrap()
.counter,
0
);
// Greet once
let mut transaction = Transaction::new_with_payer(
&[Instruction::new_with_bincode(
program_id,
&[0], // ignored but makes the instruction unique in the slot
vec![AccountMeta::new(greeted_pubkey, false)],
)],
Some(&payer.pubkey()),
);
transaction.sign(&[&payer], recent_blockhash);
banks_client.process_transaction(transaction).await.unwrap();
// Verify account has one greeting
let greeted_account = banks_client
.get_account(greeted_pubkey)
.await
.expect("get_account")
.expect("greeted_account not found");
assert_eq!(
GreetingAccount::try_from_slice(&greeted_account.data)
.unwrap()
.counter,
1
);
// Greet again
let mut transaction = Transaction::new_with_payer(
&[Instruction::new_with_bincode(
program_id,
&[1], // ignored but makes the instruction unique in the slot
vec![AccountMeta::new(greeted_pubkey, false)],
)],
Some(&payer.pubkey()),
);
transaction.sign(&[&payer], recent_blockhash);
banks_client.process_transaction(transaction).await.unwrap();
// Verify account has two greetings
let greeted_account = banks_client
.get_account(greeted_pubkey)
.await
.expect("get_account")
.expect("greeted_account not found");
assert_eq!(
GreetingAccount::try_from_slice(&greeted_account.data)
.unwrap()
.counter,
2
);
}