1- /// Comprehensive tests for AccountInfoTrait implementations:
2- /// - Solana implementation (solana_account_info::AccountInfo)
3- /// - Pinocchio implementation (pinocchio::account_info::AccountInfo)
4- ///
5- /// Tests cover all trait methods with both functional and failing test cases:
6- /// 1. key() - Returns account public key
7- /// 2. is_writable() - Check if account is writable
8- /// 3. is_signer() - Check if account is a signer
9- /// 4. executable() - Check if account is executable
10- /// 5. lamports() - Get account lamport balance
11- /// 6. data_len() - Get account data length
12- /// 7. try_borrow_data() - Borrow account data immutably
13- /// 8. try_borrow_mut_data() - Borrow account data mutably
14- /// 9. is_owned_by() - Check account ownership
15- /// 10. find_program_address() - Find PDA (static method)
16- /// 11. create_program_address() - Create PDA (static method)
17- /// 12. data_is_empty() - Check if account data is empty
18- /// 13. get_min_rent_balance() - Get minimum rent balance for size
19-
20- use light_account_checks:: { error:: AccountError , AccountInfoTrait } ;
1+ #![ cfg( all( feature = "solana" , feature = "pinocchio" ) ) ]
2+ // Comprehensive tests for AccountInfoTrait implementations:
3+ // - Solana implementation (solana_account_info::AccountInfo)
4+ // - Pinocchio implementation (pinocchio::account_info::AccountInfo)
5+ //
6+ // Tests cover all trait methods with both functional and failing test cases:
7+ // 1. key() - Returns account public key
8+ // 2. is_writable() - Check if account is writable
9+ // 3. is_signer() - Check if account is a signer
10+ // 4. executable() - Check if account is executable
11+ // 5. lamports() - Get account lamport balance
12+ // 6. data_len() - Get account data length
13+ // 7. try_borrow_data() - Borrow account data immutably
14+ // 8. try_borrow_mut_data() - Borrow account data mutably
15+ // 9. is_owned_by() - Check account ownership
16+ // 10. find_program_address() - Find PDA (static method)
17+ // 11. create_program_address() - Create PDA (static method)
18+ // 12. data_is_empty() - Check if account data is empty
19+ // 13. get_min_rent_balance() - Get minimum rent balance for size
20+
21+ use light_account_checks:: AccountInfoTrait ;
2122
2223// Test helper functions
2324#[ cfg( feature = "solana" ) ]
@@ -32,7 +33,9 @@ fn create_test_account_solana(
3233) -> light_account_checks:: account_info:: test_account_info:: solana_program:: TestAccount {
3334 let mut account =
3435 light_account_checks:: account_info:: test_account_info:: solana_program:: TestAccount :: new (
35- key, owner, data. len ( ) ,
36+ key,
37+ owner,
38+ data. len ( ) ,
3639 ) ;
3740 account. data = data;
3841 account. lamports = lamports;
@@ -109,22 +112,29 @@ fn test_solana_account_info_trait() {
109112
110113 // Test try_borrow_mut_data() - functional (writable account)
111114 {
112- let mut borrowed_mut_data = AccountInfoTrait :: try_borrow_mut_data ( & account_info) . unwrap ( ) ;
115+ let mut borrowed_mut_data =
116+ AccountInfoTrait :: try_borrow_mut_data ( & account_info) . unwrap ( ) ;
113117 borrowed_mut_data[ 0 ] = 99 ;
114118 } // Drop mutable borrow
115-
119+
116120 // Verify mutation worked
117121 {
118122 let borrowed_data = AccountInfoTrait :: try_borrow_data ( & account_info) . unwrap ( ) ;
119123 assert_eq ! ( borrowed_data[ 0 ] , 99 ) ;
120124 }
121125
122126 // Test is_owned_by() - functional (correct owner)
123- assert ! ( AccountInfoTrait :: is_owned_by( & account_info, & owner. to_bytes( ) ) ) ;
127+ assert ! ( AccountInfoTrait :: is_owned_by(
128+ & account_info,
129+ & owner. to_bytes( )
130+ ) ) ;
124131
125132 // Test is_owned_by() - failing (wrong owner)
126133 let wrong_owner = create_pubkey ( ) ;
127- assert ! ( !AccountInfoTrait :: is_owned_by( & account_info, & wrong_owner. to_bytes( ) ) ) ;
134+ assert ! ( !AccountInfoTrait :: is_owned_by(
135+ & account_info,
136+ & wrong_owner. to_bytes( )
137+ ) ) ;
128138
129139 // Test data_is_empty() - failing (has data)
130140 assert ! ( !AccountInfoTrait :: data_is_empty( & account_info) ) ;
@@ -191,23 +201,25 @@ fn test_solana_account_info_trait() {
191201
192202 // Test static methods (find_program_address and create_program_address)
193203 {
204+ use light_account_checks:: error:: AccountError ;
194205 use solana_account_info:: AccountInfo ;
195206
196207 let program_id = create_pubkey ( ) ;
197208 let seeds = & [ b"test_seed" . as_ref ( ) , & [ 1 , 2 , 3 ] ] ;
198209
199210 // Test find_program_address() - functional
200211 let ( pda, bump) = AccountInfo :: find_program_address ( seeds, & program_id. to_bytes ( ) ) ;
201-
212+
202213 // Verify the PDA is valid by using Solana's function
203- let ( expected_pda, expected_bump) =
214+ let ( expected_pda, expected_bump) =
204215 solana_pubkey:: Pubkey :: find_program_address ( seeds, & program_id) ;
205216 assert_eq ! ( pda, expected_pda. to_bytes( ) ) ;
206217 assert_eq ! ( bump, expected_bump) ;
207218
208219 // Test create_program_address() - functional
209220 let seeds_with_bump = & [ b"test_seed" . as_ref ( ) , & [ 1 , 2 , 3 ] , & [ bump] ] ;
210- let created_pda = AccountInfo :: create_program_address ( seeds_with_bump, & program_id. to_bytes ( ) ) . unwrap ( ) ;
221+ let created_pda =
222+ AccountInfo :: create_program_address ( seeds_with_bump, & program_id. to_bytes ( ) ) . unwrap ( ) ;
211223 assert_eq ! ( created_pda, pda) ;
212224
213225 // Test create_program_address() - failing (invalid bump)
@@ -293,7 +305,7 @@ fn test_pinocchio_account_info_trait() {
293305 let mut borrowed_mut_data = AccountInfoTrait :: try_borrow_mut_data ( & account) . unwrap ( ) ;
294306 borrowed_mut_data[ 0 ] = 99 ;
295307 } // Drop mutable borrow
296-
308+
297309 // Verify mutation worked
298310 {
299311 let borrowed_data = AccountInfoTrait :: try_borrow_data ( & account) . unwrap ( ) ;
@@ -335,8 +347,8 @@ fn test_pinocchio_account_info_trait() {
335347 key,
336348 owner,
337349 data. clone ( ) ,
338- true , // writable
339- true , // signer
350+ true , // writable
351+ true , // signer
340352 false , // executable
341353 ) ;
342354
@@ -381,26 +393,38 @@ fn test_pinocchio_account_info_trait() {
381393 // Note: Pinocchio implementation falls back to Solana when solana feature is enabled
382394 #[ cfg( feature = "solana" ) ]
383395 {
396+ use light_account_checks:: { error:: AccountError , AccountInfoTrait } ;
397+
384398 let program_id = [ 4u8 ; 32 ] ;
385399 let seeds = & [ b"test_seed" . as_ref ( ) , & [ 1 , 2 , 3 ] ] ;
386400
387401 // Test find_program_address() - functional
388- let ( pda, bump) = pinocchio:: account_info:: AccountInfo :: find_program_address ( seeds, & program_id) ;
389-
402+ let ( pda, bump) =
403+ pinocchio:: account_info:: AccountInfo :: find_program_address ( seeds, & program_id) ;
404+
390405 // Verify the PDA is valid by using Solana's function
391- let ( expected_pda, expected_bump) =
392- solana_pubkey:: Pubkey :: find_program_address ( seeds, & solana_pubkey:: Pubkey :: from ( program_id) ) ;
406+ let ( expected_pda, expected_bump) = solana_pubkey:: Pubkey :: find_program_address (
407+ seeds,
408+ & solana_pubkey:: Pubkey :: from ( program_id) ,
409+ ) ;
393410 assert_eq ! ( pda, expected_pda. to_bytes( ) ) ;
394411 assert_eq ! ( bump, expected_bump) ;
395412
396413 // Test create_program_address() - functional
397414 let seeds_with_bump = & [ b"test_seed" . as_ref ( ) , & [ 1 , 2 , 3 ] , & [ bump] ] ;
398- let created_pda = pinocchio:: account_info:: AccountInfo :: create_program_address ( seeds_with_bump, & program_id) . unwrap ( ) ;
415+ let created_pda = pinocchio:: account_info:: AccountInfo :: create_program_address (
416+ seeds_with_bump,
417+ & program_id,
418+ )
419+ . unwrap ( ) ;
399420 assert_eq ! ( created_pda, pda) ;
400421
401422 // Test create_program_address() - failing (invalid bump)
402423 let invalid_seeds = & [ b"test_seed" . as_ref ( ) , & [ 1 , 2 , 3 ] , & [ 255u8 ] ] ; // Invalid bump
403- let result = pinocchio:: account_info:: AccountInfo :: create_program_address ( invalid_seeds, & program_id) ;
424+ let result = pinocchio:: account_info:: AccountInfo :: create_program_address (
425+ invalid_seeds,
426+ & program_id,
427+ ) ;
404428 assert ! ( result. is_err( ) ) ;
405429 assert_eq ! ( result. unwrap_err( ) , AccountError :: InvalidSeeds ) ;
406430 }
@@ -454,16 +478,16 @@ fn test_pinocchio_account_info_trait() {
454478 // Zero size should still have base rent (when solana feature enabled) or 0 (when not)
455479 let zero_rent = AccountInfo::get_min_rent_balance(0);
456480 assert!(zero_rent.is_ok());
457-
481+
458482 #[cfg(feature = "solana")]
459483 {
460484 assert_eq!(zero_rent.unwrap(), 890880);
461485 }
462-
486+
463487 #[cfg(not(feature = "solana"))]
464488 {
465489 assert_eq!(zero_rent.unwrap(), 0);
466490 }
467491 }
468492 */
469- }
493+ }
0 commit comments