浏览代码

add rust test for processing ix (#447)

* add rust test for processing ix

* Update test.rs
Perelyn 3 天之前
父节点
当前提交
f13aec1810

+ 6 - 0
Cargo.lock

@@ -2165,7 +2165,13 @@ version = "0.1.0"
 dependencies = [
 dependencies = [
  "borsh 1.5.7",
  "borsh 1.5.7",
  "borsh-derive 1.5.7",
  "borsh-derive 1.5.7",
+ "litesvm",
+ "solana-instruction 3.0.0",
+ "solana-keypair",
+ "solana-native-token 3.0.0",
  "solana-program 3.0.0",
  "solana-program 3.0.0",
+ "solana-pubkey 3.0.0",
+ "solana-transaction",
 ]
 ]
 
 
 [[package]]
 [[package]]

+ 8 - 1
basics/processing-instructions/native/program/Cargo.toml

@@ -12,9 +12,16 @@ solana-program.workspace = true
 crate-type = ["cdylib", "lib"]
 crate-type = ["cdylib", "lib"]
 
 
 [features]
 [features]
-anchor-debug = []
 custom-heap = []
 custom-heap = []
 custom-panic = []
 custom-panic = []
 
 
 [lints.rust]
 [lints.rust]
 unexpected_cfgs = { level = "warn", check-cfg = ['cfg(target_os, values("solana"))'] }
 unexpected_cfgs = { level = "warn", check-cfg = ['cfg(target_os, values("solana"))'] }
+
+[dev-dependencies]
+litesvm = "0.8.1"
+solana-instruction = "3.0.0"
+solana-keypair = "3.0.1"
+solana-pubkey = "3.0.0"
+solana-transaction = "3.0.1"
+solana-native-token = "3.0.0"

+ 2 - 2
basics/processing-instructions/native/program/src/lib.rs

@@ -27,6 +27,6 @@ fn process_instruction(
 
 
 #[derive(BorshSerialize, BorshDeserialize, Debug)]
 #[derive(BorshSerialize, BorshDeserialize, Debug)]
 pub struct InstructionData {
 pub struct InstructionData {
-    name: String,
-    height: u32,
+    pub name: String,
+    pub height: u32,
 }
 }

+ 51 - 0
basics/processing-instructions/native/program/tests/test.rs

@@ -0,0 +1,51 @@
+use litesvm::LiteSVM;
+use processing_instructions_program::InstructionData;
+use solana_instruction::{AccountMeta, Instruction};
+use solana_keypair::{Keypair, Signer};
+use solana_native_token::LAMPORTS_PER_SOL;
+use solana_pubkey::Pubkey;
+use solana_transaction::Transaction;
+
+#[test]
+fn test_processing_ixs() {
+    let mut svm = LiteSVM::new();
+
+    let program_id = Pubkey::new_unique();
+    let program_bytes = include_bytes!("../../tests/fixtures/processing_instructions_program.so");
+
+    svm.add_program(program_id, program_bytes).unwrap();
+
+    let payer = Keypair::new();
+    svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap();
+
+    let jimmy = InstructionData {
+        name: "Jimmy".to_string(),
+        height: 3,
+    };
+
+    let mary = InstructionData {
+        name: "Mary".to_string(),
+        height: 3,
+    };
+
+    let ix1 = Instruction {
+        program_id,
+        accounts: vec![AccountMeta::new(payer.pubkey(), true)],
+        data: borsh::to_vec(&jimmy).unwrap(),
+    };
+
+    let ix2 = Instruction {
+        program_id,
+        accounts: vec![AccountMeta::new(payer.pubkey(), true)],
+        data: borsh::to_vec(&mary).unwrap(),
+    };
+
+    let tx = Transaction::new_signed_with_payer(
+        &[ix1, ix2],
+        Some(&payer.pubkey()),
+        &[&payer],
+        svm.latest_blockhash(),
+    );
+
+    let _ = svm.send_transaction(tx).is_ok();
+}