Skip to content

Commit

Permalink
tests: Move IDL related tests in misc to idl (coral-xyz#2606)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto committed Aug 19, 2023
1 parent 6eacad4 commit 6f9f7d9
Show file tree
Hide file tree
Showing 26 changed files with 587 additions and 641 deletions.
2 changes: 2 additions & 0 deletions tests/idl/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
seeds = true

[programs.localnet]
client_interactions = "C1ient1nteractions1111111111111111111111111"
docs = "Docs111111111111111111111111111111111111111"
external = "Externa1111111111111111111111111111111111111"
generics = "Generics111111111111111111111111111111111111"
idl = "id11111111111111111111111111111111111111111"
Expand Down
5 changes: 5 additions & 0 deletions tests/idl/idls/build.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
"type": "u8",
"value": "6"
},
{
"name": "BYTE_STR",
"type": "u8",
"value": "116"
},
{
"name": "FOO_CONST",
"type": "u128",
Expand Down
10 changes: 10 additions & 0 deletions tests/idl/idls/parse.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
"name": "BAR_CONST",
"type": "u8",
"value": "6"
},
{
"name": "BYTES_STR",
"type": "bytes",
"value": "[116, 101, 115, 116]"
},
{
"name": "BYTE_STR",
"type": "u8",
"value": "116"
}
],
"instructions": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "idl_doc"
name = "client-interactions"
version = "0.1.0"
description = "Created with Anchor"
rust-version = "1.60"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib"]
name = "idl_doc"
name = "client_interactions"

[features]
no-entrypoint = []
Expand All @@ -16,4 +16,4 @@ cpi = ["no-entrypoint"]
default = []

[dependencies]
anchor-lang = { path = "../../../../lang", features = ["init-if-needed"] }
anchor-lang = { path = "../../../../lang" }
File renamed without changes.
95 changes: 95 additions & 0 deletions tests/idl/programs/client-interactions/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use anchor_lang::prelude::*;

declare_id!("C1ient1nteractions1111111111111111111111111");

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

pub fn int(ctx: Context<Int>, i8: i8, i16: i16, i32: i32, i64: i64, i128: i128) -> Result<()> {
ctx.accounts.account.i8 = i8;
ctx.accounts.account.i16 = i16;
ctx.accounts.account.i32 = i32;
ctx.accounts.account.i64 = i64;
ctx.accounts.account.i128 = i128;
Ok(())
}

pub fn uint(
ctx: Context<UnsignedInt>,
u8: u8,
u16: u16,
u32: u32,
u64: u64,
u128: u128,
) -> Result<()> {
ctx.accounts.account.u8 = u8;
ctx.accounts.account.u16 = u16;
ctx.accounts.account.u32 = u32;
ctx.accounts.account.u64 = u64;
ctx.accounts.account.u128 = u128;
Ok(())
}

pub fn enm(ctx: Context<Enum>, enum_arg: MyEnum) -> Result<()> {
ctx.accounts.account.enum_field = enum_arg;
Ok(())
}
}

#[derive(Accounts)]
pub struct Int<'info> {
#[account(zero)]
pub account: Account<'info, IntAccount>,
}

#[account]
pub struct IntAccount {
pub i8: i8,
pub i16: i16,
pub i32: i32,
pub i64: i64,
pub i128: i128,
}

#[derive(Accounts)]
pub struct UnsignedInt<'info> {
#[account(zero)]
pub account: Account<'info, UnsignedIntAccount>,
}

#[account]
pub struct UnsignedIntAccount {
pub u8: u8,
pub u16: u16,
pub u32: u32,
pub u64: u64,
pub u128: u128,
}

#[derive(Accounts)]
pub struct Enum<'info> {
#[account(zero)]
pub account: Account<'info, EnumAccount>,
}

#[account]
pub struct EnumAccount {
pub enum_field: MyEnum,
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy, Debug, Eq, PartialEq)]
pub enum MyEnum {
Unit,
Named { x: u64, y: u64 },
Unnamed(u8, u8, u16, u16),
UnnamedStruct(MyStruct),
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy, Debug, Eq, PartialEq)]
pub struct MyStruct {
pub u8: u8,
pub u16: u16,
pub u32: u32,
pub u64: u64,
}
19 changes: 19 additions & 0 deletions tests/idl/programs/docs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "docs"
version = "0.1.0"
description = "Created with Anchor"
rust-version = "1.60"
edition = "2021"

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

[features]
no-entrypoint = []
no-idl = []
cpi = ["no-entrypoint"]
default = []

[dependencies]
anchor-lang = { path = "../../../../lang" }
2 changes: 2 additions & 0 deletions tests/idl/programs/docs/Xargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

use anchor_lang::prelude::*;


declare_id!("BqmKjZGVa8fqyWuojJzG16zaKSV1GjAisZToNuvEaz6m");
declare_id!("Docs111111111111111111111111111111111111111");

/// This is a doc comment for the program
#[program]
pub mod idl_doc {
pub mod docs {
use super::*;

/// This instruction doc should appear in the IDL
pub fn test_idl_doc_parse(
_ctx: Context<TestIdlDocParse>,
) -> Result<()> {
pub fn test_idl_doc_parse(_ctx: Context<TestIdlDocParse>) -> Result<()> {
Ok(())
}
}
Expand All @@ -25,7 +22,6 @@ pub struct DataWithDoc {
pub data: u16,
}


#[derive(Accounts)]
pub struct TestIdlDocParse<'info> {
/// This account doc comment should appear in the IDL
Expand Down
9 changes: 9 additions & 0 deletions tests/idl/programs/idl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ declare_id!("id11111111111111111111111111111111111111111");

#[constant]
pub const FOO_CONST: u128 = 1_000_000;

#[constant]
pub const BAR_CONST: u8 = 6;

#[constant]
pub const BYTES_STR: &[u8] = b"test";

#[constant]
pub const BYTE_STR: u8 = b't';

pub const NO_IDL: u16 = 55;

/// IDL test program documentation.
#[program]
pub mod idl {
Expand Down
121 changes: 121 additions & 0 deletions tests/idl/tests/client-interactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import * as anchor from "@coral-xyz/anchor";
import { assert } from "chai";

import { ClientInteractions } from "../target/types/client_interactions";

describe("Client interactions", () => {
anchor.setProvider(anchor.AnchorProvider.env());
const program = anchor.workspace
.clientInteractions as anchor.Program<ClientInteractions>;

it("Can use integers", async () => {
const kp = anchor.web3.Keypair.generate();

const i8 = -3;
const i16 = 1;
const i32 = -5555551;
const i64 = new anchor.BN("384535471");
const i128 = new anchor.BN(-8342491);

await program.methods
.int(i8, i16, i32, i64, i128)
.accounts({ account: kp.publicKey })
.signers([kp])
.preInstructions([await program.account.intAccount.createInstruction(kp)])
.rpc();

const account = await program.account.intAccount.fetch(kp.publicKey);
assert.strictEqual(account.i8, i8);
assert.strictEqual(account.i16, i16);
assert.strictEqual(account.i32, i32);
assert(account.i64.eq(i64));
assert(account.i128.eq(i128));
});

it("Can use unsigned integers", async () => {
const kp = anchor.web3.Keypair.generate();

const u8 = 123;
const u16 = 7888;
const u32 = 5555551;
const u64 = new anchor.BN("384535471");
const u128 = new anchor.BN(8888888);

await program.methods
.uint(u8, u16, u32, u64, u128)
.accounts({ account: kp.publicKey })
.signers([kp])
.preInstructions([
await program.account.unsignedIntAccount.createInstruction(kp),
])
.rpc();

const account = await program.account.unsignedIntAccount.fetch(
kp.publicKey
);
assert.strictEqual(account.u8, u8);
assert.strictEqual(account.u16, u16);
assert.strictEqual(account.u32, u32);
assert(account.u64.eq(u64));
assert(account.u128.eq(u128));
});

it("Can use enum", async () => {
const testAccountEnum = async (
...args: Parameters<typeof program["methods"]["enm"]>
) => {
const kp = anchor.web3.Keypair.generate();
await program.methods
.enm(...(args as any))
.accounts({ account: kp.publicKey })
.signers([kp])
.preInstructions([
await program.account.enumAccount.createInstruction(kp),
])
.rpc();
return await program.account.enumAccount.fetch(kp.publicKey);
};

// Unit
const unit = await testAccountEnum({ unit: {} });
assert.deepEqual(unit.enumField.unit, {});

// Named
const x = new anchor.BN(1);
const y = new anchor.BN(2);
const named = await testAccountEnum({ named: { x, y } });
assert(named.enumField.named.x.eq(x));
assert(named.enumField.named.y.eq(y));

// Unnamed
const tupleArg = [1, 2, 3, 4] as const;
const unnamed = await testAccountEnum({ unnamed: tupleArg });
assert.strictEqual(unnamed.enumField.unnamed[0], tupleArg[0]);
assert.strictEqual(unnamed.enumField.unnamed[1], tupleArg[1]);
assert.strictEqual(unnamed.enumField.unnamed[2], tupleArg[2]);
assert.strictEqual(unnamed.enumField.unnamed[3], tupleArg[3]);

// Unnamed struct
const tupleStructArg = [
{ u8: 1, u16: 11, u32: 111, u64: new anchor.BN(1111) },
] as const;
const unnamedStruct = await testAccountEnum({
unnamedStruct: tupleStructArg,
});
assert.strictEqual(
unnamedStruct.enumField.unnamedStruct[0].u8,
tupleStructArg[0].u8
);
assert.strictEqual(
unnamedStruct.enumField.unnamedStruct[0].u16,
tupleStructArg[0].u16
);
assert.strictEqual(
unnamedStruct.enumField.unnamedStruct[0].u32,
tupleStructArg[0].u32
);
assert(
unnamedStruct.enumField.unnamedStruct[0].u64.eq(tupleStructArg[0].u64)
);
});
});
46 changes: 46 additions & 0 deletions tests/idl/tests/docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { assert } from "chai";

import { Docs } from "../target/types/docs";

describe("Docs", () => {
anchor.setProvider(anchor.AnchorProvider.env());
const program = anchor.workspace.docs as Program<Docs>;

const instruction = program.idl.instructions.find(
(i) => i.name === "testIdlDocParse"
);

it("includes instruction doc comment", () => {
assert.deepEqual(instruction.docs, [
"This instruction doc should appear in the IDL",
]);
});

it("includes account doc comment", () => {
const act = instruction.accounts.find((i) => i.name === "act");
assert.deepEqual(act.docs, [
"This account doc comment should appear in the IDL",
"This is a multi-line comment",
]);
});

const dataWithDoc = program.idl.accounts.find(
// @ts-expect-error
(acc) => acc.name === "DataWithDoc"
);

it("includes accounts doc comment", () => {
assert.deepEqual(dataWithDoc.docs, [
"Custom account doc comment should appear in the IDL",
]);
});

it("includes account attribute doc comment", () => {
const dataField = dataWithDoc.type.fields.find((i) => i.name === "data");
assert.deepEqual(dataField.docs, [
"Account attribute doc comment should appear in the IDL",
]);
});
});
Loading

0 comments on commit 6f9f7d9

Please sign in to comment.