1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//! Vipers tests for Anchor 0.20.1.
#![cfg(test)]

#[cfg(feature = "anchor-0_21_0")]
extern crate anchor_lang_0_21_0 as anchor_lang;
#[cfg(feature = "anchor-0_21_0")]
extern crate anchor_spl_0_21_0 as anchor_spl;

#[cfg(feature = "anchor-0_20_1")]
extern crate anchor_lang_0_20_1 as anchor_lang;
#[cfg(feature = "anchor-0_20_1")]
extern crate anchor_spl_0_20_1 as anchor_spl;

#[cfg(feature = "anchor-0_19_0")]
extern crate anchor_lang_0_19_0 as anchor_lang;
#[cfg(feature = "anchor-0_19_0")]
extern crate anchor_spl_0_19_0 as anchor_spl;

#[cfg(feature = "anchor-0_18_2")]
extern crate anchor_lang_0_18_2 as anchor_lang;
#[cfg(feature = "anchor-0_18_2")]
extern crate anchor_spl_0_18_2 as anchor_spl;

use anchor_lang::prelude::*;

declare_id!("VipersTest111111111111111111111111111111111");

use anchor_spl::{associated_token::get_associated_token_address, token};
use vipers::*;

#[error]
pub enum ErrorCode {
    MyError,
}

#[account]
#[derive(Default)]
struct TestData {
    pub byte: u8,
}

#[test]
#[allow(deprecated)]
pub fn test_compiles_deprecated() -> ProgramResult {
    assert_keys!(token::ID, token::ID, "token program");

    Ok(())
}

#[test]
#[allow(deprecated)]
pub fn test_compiles() -> ProgramResult {
    let ata = get_associated_token_address(&token::ID, &token::ID);
    assert_ata!(ata, token::ID, token::ID, "ATA");

    let weird_math: Option<i32> = (1_i32).checked_add(2);
    let _result = unwrap_int!(weird_math);
    unwrap_opt!(weird_math, "aaa");

    Ok(())
}

#[test]
#[allow(deprecated)]
fn test_assert_owner() -> ProgramResult {
    let mut lamports: u64 = 8 + (TestData::default().try_to_vec().unwrap().len() as u64);

    let mut buffer: [u8; 16] = [0; 16];
    let mut buf: &mut [u8] = &mut buffer;
    TestData::default().try_serialize(&mut buf)?;

    let info: Account<TestData> = Account::try_from(&AccountInfo::new(
        &crate::ID,
        false,
        false,
        &mut lamports,
        &mut buffer,
        &crate::ID,
        false,
        0,
    ))?;
    assert_owner!(info, crate::ID);

    Ok(())
}

#[test]
fn test_unwrap_checked() -> ProgramResult {
    assert_throws!(
        {
            unwrap_checked!({
                let one: u64 = 1;
                let four = one.checked_add(u64::MAX)?;
                four.checked_add(3)
            });
        },
        VipersError::IntegerOverflow
    );
    Ok(())
}

#[test]
fn test_unwrap_opt_block() {
    assert_throws!(
        {
            unwrap_opt_block!(
                {
                    let one: u64 = 1;
                    one.checked_add(u64::MAX)
                },
                ErrorCode::MyError
            );
        },
        ErrorCode::MyError
    );
}

#[test]
#[allow(clippy::eq_op)]
fn test_invariant() {
    assert_does_not_throw!({
        invariant!(1 == 1, ErrorCode::MyError);
    });
    assert_throws!(
        {
            invariant!(1 == 2);
        },
        VipersError::InvariantFailed
    );
    assert_throws!(
        {
            invariant!(1 == 2, "this is stupid");
        },
        VipersError::InvariantFailed
    );
    assert_throws!(
        {
            invariant!(1 == 2, ErrorCode::MyError);
        },
        ErrorCode::MyError
    );
    assert_throws!(
        {
            invariant!(1 == 2, ErrorCode::MyError);
        },
        ErrorCode::MyError
    );
    assert_throws!(
        {
            invariant!(1 == 2, ErrorCode::MyError, "this is wack");
        },
        ErrorCode::MyError
    );
}

#[test]
fn test_assert_keys_eq_pass() {
    assert_does_not_throw!({
        let default = Pubkey::default();
        assert_keys_eq!(
            default,
            anchor_lang::solana_program::system_program::ID,
            ErrorCode::MyError,
            "this is wack"
        );
    });
}

#[test]
fn test_assert_keys_eq_no_match() {
    assert_throws!(
        {
            let default = Pubkey::default();
            assert_keys_eq!(
                default,
                anchor_lang::solana_program::sysvar::rent::ID,
                ErrorCode::MyError,
                "this is wack"
            )
        },
        ErrorCode::MyError
    );
}

#[test]
fn test_assert_keys_neq_pass() {
    assert_does_not_throw!({
        let default = Pubkey::default();
        assert_keys_neq!(
            default,
            anchor_lang::solana_program::sysvar::rent::ID,
            ErrorCode::MyError,
            "this is wack"
        );
    });
}

#[test]
fn test_assert_keys_neq_no_match() {
    assert_throws!(
        {
            let default = Pubkey::default();
            assert_keys_neq!(
                default,
                anchor_lang::solana_program::system_program::ID,
                ErrorCode::MyError,
                "this is wack"
            )
        },
        ErrorCode::MyError
    );
}