Skip to content

Commit

Permalink
tests: Refactor IDL tests (coral-xyz#2573)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto committed Jul 17, 2023
1 parent cf057ac commit c306463
Show file tree
Hide file tree
Showing 26 changed files with 103 additions and 117 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/reusable-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ jobs:
path: tests/anchor-cli-account
- cmd: cd tests/bench && anchor test --skip-lint
path: tests/bench
- cmd: cd tests/idl-build && ./test.sh
path: tests/idl-build
- cmd: cd tests/idl && ./test.sh
path: tests/idl
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/setup/
Expand Down
13 changes: 0 additions & 13 deletions tests/idl-build/Anchor.toml

This file was deleted.

11 changes: 0 additions & 11 deletions tests/idl-build/gen_testdata.sh

This file was deleted.

15 changes: 15 additions & 0 deletions tests/idl/Anchor.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[features]
seeds = true

[programs.localnet]
external = "Externa1111111111111111111111111111111111111"
generics = "Generics111111111111111111111111111111111111"
idl = "id11111111111111111111111111111111111111111"
relations_derivation = "Re1ationsDerivation111111111111111111111111"

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

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
File renamed without changes.
13 changes: 13 additions & 0 deletions tests/idl/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

idls_dir=$PWD/idls

cd programs/idl
anchor idl parse --file src/lib.rs -o $idls_dir/idl_parse_exp.json
anchor idl build -o $idls_dir/idl_build_exp.json

cd ../generics
anchor idl build -o $idls_dir/generics_build_exp.json

cd ../relations-derivation
anchor idl build -o $idls_dir/relations_build_exp.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{
"version": "0.1.0",
"name": "example_program",
"docs": [
"This is an example program used for testing"
],
"name": "generics",
"instructions": [
{
"name": "generic",
Expand Down Expand Up @@ -77,6 +74,18 @@
}
],
"types": [
{
"name": "Baz",
"type": {
"kind": "struct",
"fields": [
{
"name": "someField",
"type": "u8"
}
]
}
},
{
"name": "GenericEnum",
"generics": [
Expand Down Expand Up @@ -404,18 +413,6 @@
}
]
}
},
{
"name": "Baz",
"type": {
"kind": "struct",
"fields": [
{
"name": "someField",
"type": "u8"
}
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"version": "0.1.0",
"name": "example_program",
"name": "idl",
"docs": [
"This is an example program used for testing"
"IDL test program documentation."
],
"constants": [
{
Expand Down Expand Up @@ -475,6 +475,18 @@
}
],
"types": [
{
"name": "external::Baz",
"type": {
"kind": "struct",
"fields": [
{
"name": "someField",
"type": "u8"
}
]
}
},
{
"name": "BarStruct",
"docs": [
Expand Down Expand Up @@ -658,18 +670,6 @@
}
]
}
},
{
"name": "some_external_program::Baz",
"type": {
"kind": "struct",
"fields": [
{
"name": "someField",
"type": "u8"
}
]
}
}
],
"events": [
Expand All @@ -684,7 +684,7 @@
{
"name": "externalBaz",
"type": {
"defined": "some_external_program::Baz"
"defined": "external::Baz"
},
"index": false
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"version": "0.1.0",
"name": "example_program",
"name": "idl",
"docs": [
"This is an example program used for testing"
"IDL test program documentation."
],
"constants": [
{
Expand Down Expand Up @@ -672,7 +672,7 @@
{
"name": "externalBaz",
"type": {
"defined": "some_external_program::Baz"
"defined": "external::Baz"
},
"index": false
},
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/idl-build/package.json → tests/idl/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "idl-build",
"name": "idl",
"version": "0.28.0",
"license": "(MIT OR Apache-2.0)",
"homepage": "https://github.com/coral-xyz/anchor#readme",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "some-external-program"
name = "external"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib"]
name = "some_external_program"
name = "external"

[features]
no-entrypoint = []
Expand All @@ -19,4 +19,3 @@ default = []
[dependencies]
anchor-lang = { path = "../../../../lang" }
anchor-spl = { path = "../../../../spl" }

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anchor_lang::prelude::*;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
declare_id!("Externa1111111111111111111111111111111111111");

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

pub fn initialize(_ctx: Context<Initialize>, _baz: Baz) -> Result<()> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ no-entrypoint = []
no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]
idl-build = [
"anchor-lang/idl-build",
"some-external-program/idl-build",
]
idl-build = ["anchor-lang/idl-build", "external/idl-build"]
default = []

[dependencies]
anchor-lang = { path = "../../../../lang" }
anchor-spl = { path = "../../../../spl" }
some-external-program = { path = "../some_external_program", features = ["no-entrypoint"] }
external = { path = "../external", features = ["no-entrypoint"] }
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use anchor_lang::prelude::*;
use some_external_program;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
declare_id!("Generics111111111111111111111111111111111111");

/// This is an example program used for testing
#[program]
pub mod example_program {
pub mod generics {
use super::*;

pub fn generic(
ctx: Context<GenericCtx>,
generic_field: GenericType::<u32, u64, 10>
) -> Result<()>{
generic_field: GenericType<u32, u64, 10>,
) -> Result<()> {
ctx.accounts.generic_acc.data = generic_field;
Ok(())
}
Expand All @@ -28,7 +26,7 @@ pub struct GenericCtx<'info> {

#[account]
pub struct GenericAccount {
pub data: GenericType<u32, u64, 10>
pub data: GenericType<u32, u64, 10>,
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
Expand All @@ -40,7 +38,7 @@ where
pub gen1: T,
pub gen2: U,
pub gen3: GenericNested<u32, U>,
pub gen4: GenericNested<T, some_external_program::Baz>,
pub gen4: GenericNested<T, external::Baz>,
pub gen5: GenericNested<T, U>,
pub gen6: GenericNested<u32, u64>,
pub gen7: GenericNested<T, GenericNested<T, U>>,
Expand Down Expand Up @@ -71,10 +69,7 @@ where
U: AnchorSerialize + AnchorDeserialize,
{
Unnamed(T, U),
Named {
gen1: T,
gen2: U,
},
Named { gen1: T, gen2: U },
Struct(GenericNested<T, U>),
Arr([T; N]),
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ no-entrypoint = []
no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]
idl-build = [
"anchor-lang/idl-build",
"some-external-program/idl-build",
]
idl-build = ["anchor-lang/idl-build", "external/idl-build"]
default = []

[dependencies]
anchor-lang = { path = "../../../../lang" }
anchor-spl = { path = "../../../../spl" }
bytemuck = {version = "1.4.0", features = ["derive", "min_const_generics"]}
some-external-program = { path = "../some_external_program", features = ["no-entrypoint"] }
external = { path = "../external", features = ["no-entrypoint"] }
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use anchor_lang::prelude::*;
use some_external_program;
use std::str::FromStr;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
declare_id!("id11111111111111111111111111111111111111111");

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

/// This is an example program used for testing
/// IDL test program documentation.
#[program]
pub mod example_program {
pub mod idl {
use super::*;

pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
Expand Down Expand Up @@ -88,8 +87,11 @@ pub mod example_program {
vec_of_option: Vec<Option<u64>>,
box_field: Box<bool>,
) -> Result<SomeRetStruct> {
ctx.accounts.state.set_inner(State2 { vec_of_option, box_field });
Ok(SomeRetStruct { some_field: 3})
ctx.accounts.state.set_inner(State2 {
vec_of_option,
box_field,
});
Ok(SomeRetStruct { some_field: 3 })
}

pub fn cause_error(_ctx: Context<CauseError>) -> Result<()> {
Expand Down Expand Up @@ -306,7 +308,7 @@ mod some_other_module {
#[event]
pub struct SomeEvent {
bool_field: bool,
external_baz: some_external_program::Baz,
external_baz: external::Baz,
other_module_baz: some_other_module::Baz,
}

Expand All @@ -323,4 +325,4 @@ pub struct SomeZcAccount {
#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct SomeRetStruct {
pub some_field: u8,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ name = "relations_derivation"
no-entrypoint = []
no-idl = []
cpi = ["no-entrypoint"]
idl-build = [
"anchor-lang/idl-build",
]
idl-build = ["anchor-lang/idl-build"]
default = []

[dependencies]
Expand Down
Loading

0 comments on commit c306463

Please sign in to comment.