فهرست منبع

feat: add rust test for realloc example (#452)

Perelyn 5 روز پیش
والد
کامیت
4b840d930d
3فایلهای تغییر یافته به همراه115 افزوده شده و 1 حذف شده
  1. 6 0
      Cargo.lock
  2. 8 1
      basics/realloc/native/program/Cargo.toml
  3. 101 0
      basics/realloc/native/program/tests/test.rs

+ 6 - 0
Cargo.lock

@@ -2307,8 +2307,14 @@ version = "0.1.0"
 dependencies = [
  "borsh 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-pubkey 3.0.0",
  "solana-system-interface 2.0.0",
+ "solana-transaction",
 ]
 
 [[package]]

+ 8 - 1
basics/realloc/native/program/Cargo.toml

@@ -13,9 +13,16 @@ solana-system-interface.workspace = true
 crate-type = ["cdylib", "lib"]
 
 [features]
-anchor-debug = []
 custom-heap = []
 custom-panic = []
 
 [lints.rust]
 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-native-token = "3.0.0"
+solana-pubkey = "3.0.0"
+solana-transaction = "3.0.1"

+ 101 - 0
basics/realloc/native/program/tests/test.rs

@@ -0,0 +1,101 @@
+use litesvm::LiteSVM;
+use realloc_program::state::{AddressInfo, WorkInfo};
+use realloc_program::{processor::ReallocInstruction, state::EnhancedAddressInfoExtender};
+use solana_instruction::Instruction;
+use solana_keypair::{Keypair, Signer};
+use solana_native_token::LAMPORTS_PER_SOL;
+use solana_pubkey::Pubkey;
+use solana_transaction::{AccountMeta, Transaction};
+
+#[test]
+fn test_realloc() {
+    let mut svm = LiteSVM::new();
+
+    let program_id = Pubkey::new_unique();
+    let program_bytes = include_bytes!("../../../../../target/deploy/realloc_program.so");
+
+    svm.add_program(program_id, program_bytes).unwrap();
+
+    let payer = Keypair::new();
+    svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL).unwrap();
+
+    let test_account = Keypair::new();
+
+    let address_info = AddressInfo {
+        name: "Jacob".to_string(),
+        house_number: 123,
+        street: "Main St.".to_string(),
+        city: "Chicago".to_string(),
+    };
+
+    let data = borsh::to_vec(&ReallocInstruction::Create(address_info)).unwrap();
+
+    let ix = Instruction {
+        program_id,
+        accounts: vec![
+            AccountMeta::new(test_account.pubkey(), true),
+            AccountMeta::new(payer.pubkey(), true),
+            AccountMeta::new(solana_system_interface::program::ID, false),
+        ],
+        data,
+    };
+
+    let tx = Transaction::new_signed_with_payer(
+        &[ix],
+        Some(&payer.pubkey()),
+        &[&payer, &test_account],
+        svm.latest_blockhash(),
+    );
+
+    let _ = svm.send_transaction(tx).is_ok();
+
+    let data = borsh::to_vec(&ReallocInstruction::ReallocateWithoutZeroInit(
+        EnhancedAddressInfoExtender {
+            state: "Illinois".to_string(),
+            zip: 12345,
+        },
+    ))
+    .unwrap();
+
+    let ix = Instruction {
+        program_id,
+        accounts: vec![
+            AccountMeta::new(test_account.pubkey(), false),
+            AccountMeta::new(payer.pubkey(), true),
+            AccountMeta::new(solana_system_interface::program::ID, false),
+        ],
+        data,
+    };
+
+    let tx = Transaction::new_signed_with_payer(
+        &[ix],
+        Some(&payer.pubkey()),
+        &[&payer],
+        svm.latest_blockhash(),
+    );
+
+    let _ = svm.send_transaction(tx).is_ok();
+
+    let data = borsh::to_vec(&ReallocInstruction::ReallocateZeroInit(WorkInfo {
+        name: "Pete".to_string(),
+        position: "Engineer".to_string(),
+        company: "Solana Labs".to_string(),
+        years_employed: 2,
+    }))
+    .unwrap();
+
+    let ix = Instruction {
+        program_id,
+        accounts: vec![AccountMeta::new(test_account.pubkey(), false)],
+        data,
+    };
+
+    let tx = Transaction::new_signed_with_payer(
+        &[ix],
+        Some(&payer.pubkey()),
+        &[&payer],
+        svm.latest_blockhash(),
+    );
+
+    let _ = svm.send_transaction(tx).is_ok();
+}