Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto committed Mar 25, 2024
1 parent e653a58 commit 5b4b1d3
Show file tree
Hide file tree
Showing 13 changed files with 319 additions and 1 deletion.
10 changes: 10 additions & 0 deletions tests/declare-program/Anchor.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[programs.localnet]
declare_program = "Dec1areProgram11111111111111111111111111111"
external = "Externa111111111111111111111111111111111111"

[provider]
cluster = "localnet"
wallet = "~/.config/solana/id.json"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
14 changes: 14 additions & 0 deletions tests/declare-program/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[workspace]
members = [
"programs/*"
]
resolver = "2"

[profile.release]
overflow-checks = true
lto = "fat"
codegen-units = 1
[profile.release.build-override]
opt-level = 3
incremental = false
codegen-units = 1
114 changes: 114 additions & 0 deletions tests/declare-program/idls/external.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{
"address": "Externa111111111111111111111111111111111111",
"metadata": {
"name": "external",
"version": "0.1.0",
"spec": "0.1.0",
"description": "Created with Anchor"
},
"instructions": [
{
"name": "init",
"discriminator": [
220,
59,
207,
236,
108,
250,
47,
100
],
"accounts": [
{
"name": "authority",
"writable": true,
"signer": true
},
{
"name": "my_account",
"writable": true,
"pda": {
"seeds": [
{
"kind": "account",
"path": "authority"
}
]
}
},
{
"name": "system_program",
"address": "11111111111111111111111111111111"
}
],
"args": []
},
{
"name": "update",
"discriminator": [
219,
200,
88,
176,
158,
63,
253,
127
],
"accounts": [
{
"name": "authority",
"signer": true
},
{
"name": "my_account",
"writable": true,
"pda": {
"seeds": [
{
"kind": "account",
"path": "authority"
}
]
}
}
],
"args": [
{
"name": "value",
"type": "u32"
}
]
}
],
"accounts": [
{
"name": "MyAccount",
"discriminator": [
246,
28,
6,
87,
251,
45,
50,
42
]
}
],
"types": [
{
"name": "MyAccount",
"type": {
"kind": "struct",
"fields": [
{
"name": "field",
"type": "u32"
}
]
}
}
]
}
16 changes: 16 additions & 0 deletions tests/declare-program/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "declare-program",
"version": "0.29.0",
"license": "(MIT OR Apache-2.0)",
"homepage": "https://github.com/coral-xyz/anchor#readme",
"bugs": {
"url": "https://github.com/coral-xyz/anchor/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/coral-xyz/anchor.git"
},
"engines": {
"node": ">=17"
}
}
20 changes: 20 additions & 0 deletions tests/declare-program/programs/declare-program/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "declare-program"
version = "0.1.0"
description = "Created with Anchor"
rust-version = "1.60"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib"]
name = "declare_program"

[features]
no-entrypoint = []
no-idl = []
cpi = ["no-entrypoint"]
default = []
idl-build = ["anchor-lang/idl-build"]

[dependencies]
anchor-lang = { path = "../../../../lang" }
2 changes: 2 additions & 0 deletions tests/declare-program/programs/declare-program/Xargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
39 changes: 39 additions & 0 deletions tests/declare-program/programs/declare-program/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use anchor_lang::prelude::*;

declare_id!("Dec1areProgram11111111111111111111111111111");

declare_program!(external);
use external::program::External;

#[program]
pub mod declare_program {
use super::*;

pub fn cpi(ctx: Context<Cpi>, value: u32) -> Result<()> {
let cpi_my_account = &mut ctx.accounts.cpi_my_account;
require_keys_eq!(external::accounts::MyAccount::owner(), external::ID);
require_eq!(cpi_my_account.field, 0);

let cpi_ctx = CpiContext::new(
ctx.accounts.external_program.to_account_info(),
external::cpi::accounts::Update {
authority: ctx.accounts.authority.to_account_info(),
my_account: cpi_my_account.to_account_info(),
},
);
external::cpi::update(cpi_ctx, value)?;

cpi_my_account.reload()?;
require_eq!(cpi_my_account.field, value);

Ok(())
}
}

#[derive(Accounts)]
pub struct Cpi<'info> {
pub authority: Signer<'info>,
#[account(mut)]
pub cpi_my_account: Account<'info, external::accounts::MyAccount>,
pub external_program: Program<'info, External>,
}
19 changes: 19 additions & 0 deletions tests/declare-program/programs/external/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "external"
version = "0.1.0"
description = "Created with Anchor"
rust-version = "1.60"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib"]

[features]
no-entrypoint = []
no-idl = []
cpi = ["no-entrypoint"]
default = []
idl-build = ["anchor-lang/idl-build"]

[dependencies]
anchor-lang = { path = "../../../../lang" }
2 changes: 2 additions & 0 deletions tests/declare-program/programs/external/Xargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
44 changes: 44 additions & 0 deletions tests/declare-program/programs/external/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use anchor_lang::prelude::*;

declare_id!("Externa111111111111111111111111111111111111");

#[program]
pub mod external {
use super::*;

pub fn init(_ctx: Context<Init>) -> Result<()> {
Ok(())
}

pub fn update(ctx: Context<Update>, value: u32) -> Result<()> {
ctx.accounts.my_account.field = value;
Ok(())
}
}

#[derive(Accounts)]
pub struct Init<'info> {
#[account(mut)]
pub authority: Signer<'info>,
#[account(
init,
payer = authority,
space = 8 + 4,
seeds = [authority.key.as_ref()],
bump
)]
pub my_account: Account<'info, MyAccount>,
pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct Update<'info> {
pub authority: Signer<'info>,
#[account(mut, seeds = [authority.key.as_ref()], bump)]
pub my_account: Account<'info, MyAccount>,
}

#[account]
pub struct MyAccount {
pub field: u32,
}
27 changes: 27 additions & 0 deletions tests/declare-program/tests/declare-program.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as anchor from "@coral-xyz/anchor";
import assert from "assert";

import type { DeclareProgram } from "../target/types/declare_program";
import type { External } from "../target/types/external";

describe("declare-program", () => {
anchor.setProvider(anchor.AnchorProvider.env());
const program: anchor.Program<DeclareProgram> =
anchor.workspace.declareProgram;
const externalProgram: anchor.Program<External> = anchor.workspace.external;

it("Can CPI", async () => {
const { pubkeys } = await externalProgram.methods.init().rpcAndKeys();

const value = 5;
await program.methods
.cpi(value)
.accounts({ cpiMyAccount: pubkeys.myAccount })
.rpc();

const myAccount = await externalProgram.account.myAccount.fetch(
pubkeys.myAccount
);
assert.strictEqual(myAccount.field, value);
});
});
10 changes: 10 additions & 0 deletions tests/declare-program/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"types": ["mocha", "chai"],
"lib": ["es2015"],
"module": "commonjs",
"target": "es6",
"esModuleInterop": true,
"strict": true
}
}
3 changes: 2 additions & 1 deletion tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"chat",
"composite",
"custom-coder",
"declare-id",
"declare-program",
"errors",
"escrow",
"events",
Expand Down Expand Up @@ -42,7 +44,6 @@
"typescript",
"validator-clone",
"zero-copy",
"declare-id",
"cpi-returns",
"multiple-suites",
"multiple-suites-run-single",
Expand Down

0 comments on commit 5b4b1d3

Please sign in to comment.