Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
Add integration and compile tests, in sdk due to circularity
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyera Eulberg committed Jun 27, 2020
1 parent 5ad7e63 commit f8e5062
Show file tree
Hide file tree
Showing 18 changed files with 324 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ solana-sdk-program-macros = { path = "program-macros", version = "1.3.0" }

[dev-dependencies]
tiny-bip39 = "0.7.0"
trybuild = "1.0.30"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
Expand Down
15 changes: 15 additions & 0 deletions sdk/tests/compile-fail/account_desc_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use serde_derive::{Deserialize, Serialize};
use solana_sdk_program_macros::instructions;

mod test_program;

#[instructions(test_program::id())]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum TestInstruction {
#[accounts(
account(doc = "Account")
)]
Transfer { lamports: u64 },
}

fn main() {}
5 changes: 5 additions & 0 deletions sdk/tests/compile-fail/account_desc_name.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unrecognized account detail
--> $DIR/account_desc_name.rs:10:17
|
10 | account(doc = "Account")
| ^^^^^^^^^^^^^^^
15 changes: 15 additions & 0 deletions sdk/tests/compile-fail/account_optional_multiple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use serde_derive::{Deserialize, Serialize};
use solana_sdk_program_macros::instructions;

mod test_program;

#[instructions(test_program::id())]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum TestInstruction {
#[accounts(
from_account(optional, multiple, desc = "Account"),
)]
Transfer { lamports: u64 },
}

fn main() {}
5 changes: 5 additions & 0 deletions sdk/tests/compile-fail/account_optional_multiple.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: account cannot be optional and allow multiples
--> $DIR/account_optional_multiple.rs:10:32
|
10 | from_account(optional, multiple, desc = "Account"),
| ^^^^^^^^
15 changes: 15 additions & 0 deletions sdk/tests/compile-fail/account_tag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use serde_derive::{Deserialize, Serialize};
use solana_sdk_program_macros::instructions;

mod test_program;

#[instructions(test_program::id())]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum TestInstruction {
#[accounts(
from_account(writable, desc = "Account"),
)]
Transfer { lamports: u64 },
}

fn main() {}
5 changes: 5 additions & 0 deletions sdk/tests/compile-fail/account_tag.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unrecognized account detail
--> $DIR/account_tag.rs:10:22
|
10 | from_account(writable, desc = "Account"),
| ^^^^^^^^
15 changes: 15 additions & 0 deletions sdk/tests/compile-fail/accounts_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use serde_derive::{Deserialize, Serialize};
use solana_sdk_program_macros::instructions;

mod test_program;

#[instructions(test_program::id())]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum TestInstruction {
#[accounts(
(from_account, desc = "Account"),
)]
Transfer { lamports: u64 },
}

fn main() {}
7 changes: 7 additions & 0 deletions sdk/tests/compile-fail/accounts_list.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: unrecognized accounts format
--> $DIR/accounts_list.rs:9:5
|
9 | / #[accounts(
10 | | (from_account, desc = "Account"),
11 | | )]
| |______^
13 changes: 13 additions & 0 deletions sdk/tests/compile-fail/accounts_list_missing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use serde_derive::{Deserialize, Serialize};
use solana_sdk_program_macros::instructions;

mod test_program;

#[instructions(test_program::id())]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum TestInstruction {
#[accounts]
Transfer { lamports: u64 },
}

fn main() {}
5 changes: 5 additions & 0 deletions sdk/tests/compile-fail/accounts_list_missing.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: missing accounts list
--> $DIR/accounts_list_missing.rs:9:7
|
9 | #[accounts]
| ^^^^^^^^
1 change: 1 addition & 0 deletions sdk/tests/compile-fail/test_program.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
solana_sdk::declare_id!("8dGutFWpfHymgGDV6is389USqGRqSfpGZyhBrF1VPWDg");
16 changes: 16 additions & 0 deletions sdk/tests/compile-fail/unnamed_fields.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use serde_derive::{Deserialize, Serialize};
use solana_sdk_program_macros::instructions;

mod test_program;

#[instructions(test_program::id())]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum TestInstruction {
#[accounts(
from_account(desc = "Funding account"),
to_account(desc = "Recipient account")
)]
Transfer(u64),
}

fn main() {}
5 changes: 5 additions & 0 deletions sdk/tests/compile-fail/unnamed_fields.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: macro does not support unnamed variant fields
--> $DIR/unnamed_fields.rs:13:13
|
13 | Transfer(u64),
| ^^^^^
12 changes: 12 additions & 0 deletions sdk/tests/compile_fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
mod test_program;

#[test]
fn compile_test() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/compile-fail/account_desc_name.rs");
t.compile_fail("tests/compile-fail/accounts_list.rs");
t.compile_fail("tests/compile-fail/accounts_list_missing.rs");
t.compile_fail("tests/compile-fail/account_optional_multiple.rs");
t.compile_fail("tests/compile-fail/account_tag.rs");
t.compile_fail("tests/compile-fail/unnamed_fields.rs");
}
187 changes: 187 additions & 0 deletions sdk/tests/program_macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
use bincode::serialize;
use serde_derive::{Deserialize, Serialize};
use solana_sdk::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
};
use solana_sdk_program_macros::instructions;

#[instructions(test_program::id())]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum TestInstruction {
/// Transfer lamports
#[accounts(
from_account(SIGNER, WRITABLE, desc = "Funding account"),
to_account(WRITABLE, desc = "Recipient account")
)]
Transfer {
/// Test a field comment
lamports: u64,
},

/// Provide one required signature and a variable list of other signatures
#[accounts(
required_account(WRITABLE, desc = "Required account"),
signers(SIGNER, multiple, desc = "Signer")
)]
MultipleAccounts,

/// Consumes a stored nonce, replacing it with a successor
#[accounts(
required_account(SIGNER, WRITABLE, desc = "Required account"),
sysvar(desc = "Sysvar"),
authority(SIGNER, optional, desc = "Authority")
)]
OptionalAccount,
}

mod test_program {
solana_sdk::declare_id!("8dGutFWpfHymgGDV6is389USqGRqSfpGZyhBrF1VPWDg");
}

#[test]
fn test_helper_fns() {
let pubkey0 = Pubkey::new_rand();
let pubkey1 = Pubkey::new_rand();
let pubkey2 = Pubkey::new_rand();

assert_eq!(
transfer(pubkey0, pubkey1, 42),
Instruction {
program_id: test_program::id(),
accounts: vec![
AccountMeta {
pubkey: pubkey0,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: pubkey1,
is_signer: false,
is_writable: true,
}
],
data: serialize(&TestInstruction::Transfer { lamports: 42 }).unwrap(),
}
);

assert_eq!(
multiple_accounts(pubkey0, vec![pubkey1, pubkey2]),
Instruction {
program_id: test_program::id(),
accounts: vec![
AccountMeta {
pubkey: pubkey0,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: pubkey1,
is_signer: true,
is_writable: false,
},
AccountMeta {
pubkey: pubkey2,
is_signer: true,
is_writable: false,
}
],
data: serialize(&TestInstruction::MultipleAccounts).unwrap(),
}
);

assert_eq!(
multiple_accounts(pubkey0, vec![]),
Instruction {
program_id: test_program::id(),
accounts: vec![
AccountMeta {
pubkey: pubkey0,
is_signer: false,
is_writable: true,
}
],
data: serialize(&TestInstruction::MultipleAccounts).unwrap(),
}
);

assert_eq!(
optional_account(pubkey0, pubkey1, Some(pubkey2)),
Instruction {
program_id: test_program::id(),
accounts: vec![
AccountMeta {
pubkey: pubkey0,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: pubkey1,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: pubkey2,
is_signer: true,
is_writable: false,
}
],
data: serialize(&TestInstruction::OptionalAccount).unwrap(),
}
);

assert_eq!(
optional_account(pubkey0, pubkey1, None),
Instruction {
program_id: test_program::id(),
accounts: vec![
AccountMeta {
pubkey: pubkey0,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: pubkey1,
is_signer: false,
is_writable: false,
}
],
data: serialize(&TestInstruction::OptionalAccount).unwrap(),
}
);
}

#[test]
fn test_from_instruction() {
let transfer = TestInstruction::Transfer { lamports: 42 };
let verbose_transfer = TestInstructionVerbose::from_instruction(transfer, vec![2, 3]);
assert_eq!(
verbose_transfer,
TestInstructionVerbose::Transfer {
from_account: 2,
to_account: 3,
lamports: 42,
}
);

let multiple = TestInstruction::MultipleAccounts;
let verbose_multiple = TestInstructionVerbose::from_instruction(multiple, vec![2, 3, 4]);
assert_eq!(
verbose_multiple,
TestInstructionVerbose::MultipleAccounts {
required_account: 2,
signers: vec![3, 4],
}
);

let optional = TestInstruction::OptionalAccount;
let verbose_optional = TestInstructionVerbose::from_instruction(optional, vec![2, 3, 4]);
assert_eq!(
verbose_optional,
TestInstructionVerbose::OptionalAccount {
required_account: 2,
sysvar: 3,
authority: Some(4),
}
);
}
1 change: 1 addition & 0 deletions sdk/tests/test_program/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
solana_sdk::declare_id!("8dGutFWpfHymgGDV6is389USqGRqSfpGZyhBrF1VPWDg");

0 comments on commit f8e5062

Please sign in to comment.