Browse Source

examples: Add misc test for remaining accounts with state (#343)

Armani Ferrante 4 years ago
parent
commit
3321ed3daa
2 changed files with 20 additions and 2 deletions
  1. 12 2
      examples/misc/programs/misc/src/lib.rs
  2. 8 0
      examples/misc/tests/misc.js

+ 12 - 2
examples/misc/programs/misc/src/lib.rs

@@ -2,7 +2,7 @@
 //! It's not too instructive/coherent by itself, so please see other examples.
 
 use anchor_lang::prelude::*;
-use misc2::misc2::MyState;
+use misc2::misc2::MyState as Misc2State;
 use misc2::Auth;
 
 #[program]
@@ -20,6 +20,13 @@ pub mod misc {
         pub fn new(_ctx: Context<Ctor>) -> Result<Self, ProgramError> {
             Ok(Self { v: vec![] })
         }
+
+        pub fn remaining_accounts(&mut self, ctx: Context<RemainingAccounts>) -> ProgramResult {
+            if ctx.remaining_accounts.len() != 1 {
+                return Err(ProgramError::Custom(1)); // Arbitrary error.
+            }
+            Ok(())
+        }
     }
 
     pub fn initialize(ctx: Context<Initialize>, udata: u128, idata: i128) -> ProgramResult {
@@ -91,6 +98,9 @@ pub mod misc {
 #[derive(Accounts)]
 pub struct Ctor {}
 
+#[derive(Accounts)]
+pub struct RemainingAccounts {}
+
 #[derive(Accounts)]
 pub struct Initialize<'info> {
     #[account(init)]
@@ -116,7 +126,7 @@ pub struct TestStateCpi<'info> {
     #[account(signer)]
     authority: AccountInfo<'info>,
     #[account(mut, state = misc2_program)]
-    cpi_state: CpiState<'info, MyState>,
+    cpi_state: CpiState<'info, Misc2State>,
     #[account(executable)]
     misc2_program: AccountInfo<'info>,
 }

+ 8 - 0
examples/misc/tests/misc.js

@@ -17,6 +17,14 @@ describe("misc", () => {
     assert.ok(accountInfo.data.length === 99);
   });
 
+  it("Can use remaining accounts for a state instruction", async () => {
+    await program.state.rpc.remainingAccounts({
+      remainingAccounts: [
+        { pubkey: misc2Program.programId, isWritable: false, isSigner: false },
+      ],
+    });
+  });
+
   const data = anchor.web3.Keypair.generate();
 
   it("Can use u128 and i128", async () => {