Skip to content

Commit c2e9a09

Browse files
committed
Add doctests
1 parent 28f9949 commit c2e9a09

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

src/assert.rs

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,14 @@ macro_rules! unwrap_or_err {
115115
///
116116
/// # Example
117117
///
118-
/// ```
119-
/// let one = 1;
120-
/// let two = 2;
118+
/// ```should_panic
119+
/// # use anchor_lang::prelude::*;
120+
/// # #[macro_use] extern crate vipers; fn main() -> ProgramResult {
121+
/// let one = 1_u64;
122+
/// let two = 2_u64;
121123
/// let my_value = unwrap_int!(one.checked_sub(2)); // returns an error
124+
/// Ok(())
125+
/// # }
122126
/// ```
123127
#[macro_export]
124128
macro_rules! unwrap_int {
@@ -131,8 +135,17 @@ macro_rules! unwrap_int {
131135
///
132136
/// # Example
133137
///
134-
/// ```
138+
/// ```should_panic
139+
/// # use anchor_lang::prelude::*;
140+
/// # impl From<ErrorCode> for ProgramError { fn from(code: ErrorCode) -> Self { ProgramError::Custom(10) } }
141+
/// # pub enum ErrorCode { MyError }
142+
/// # #[macro_use] extern crate vipers; fn main() -> ProgramResult {
143+
/// fn function_returning_result() -> Result<u64, u64> {
144+
/// Err(123)
145+
/// }
146+
///
135147
/// let my_value = try_or_err!(function_returning_result(), MyError);
148+
/// # Ok(()) }
136149
/// ```
137150
#[macro_export]
138151
macro_rules! try_or_err {
@@ -146,10 +159,16 @@ macro_rules! try_or_err {
146159
/// # Example
147160
///
148161
/// ```
162+
/// # use anchor_lang::prelude::*;
163+
/// # impl From<ErrorCode> for ProgramError { fn from(code: ErrorCode) -> Self { ProgramError::Custom(10) } }
164+
/// # pub enum ErrorCode { MyError }
165+
/// # #[macro_use] extern crate vipers; fn main() -> ProgramResult {
166+
/// let fail = false;
149167
/// if fail {
150168
/// return program_err!(MyError);
151169
/// }
152170
/// Ok(())
171+
/// # }
153172
/// ```
154173
#[macro_export]
155174
macro_rules! program_err {
@@ -158,32 +177,15 @@ macro_rules! program_err {
158177
};
159178
}
160179

161-
/// Require or return a [solana_program::program_error::ProgramError], logging the string representation to the program log.
162-
///
163-
/// # Example
164-
///
165-
/// ```
166-
/// if fail {
167-
/// return prog_require!(ProgramError::CustomError(10));
168-
/// }
169-
/// Ok(())
170-
/// ```
171-
#[macro_export]
172-
macro_rules! prog_require {
173-
($invariant:expr, $err:expr $(,)?) => {
174-
if !($invariant) {
175-
msg!("Invariant failed: {:?}", $err);
176-
return Err($err.into());
177-
}
178-
};
179-
}
180-
181180
/// Asserts that an invariant holds, otherwise logs the given message.
182181
///
183182
/// # Example
184183
///
185-
/// ```
184+
/// ```should_panic
185+
/// # use anchor_lang::prelude::*;
186+
/// # #[macro_use] extern crate vipers; fn main() -> ProgramResult {
186187
/// invariant!(1 == 2, "incorrect");
188+
/// # Ok(()) }
187189
/// ```
188190
#[macro_export]
189191
macro_rules! invariant {
@@ -199,17 +201,20 @@ macro_rules! invariant {
199201
///
200202
/// # Example
201203
///
202-
/// ```
203-
/// let one = 1;
204-
/// let two = 2;
204+
/// ```should_panic
205+
/// # use anchor_lang::prelude::*;
206+
/// # #[macro_use] extern crate vipers; fn main() -> ProgramResult {
207+
/// let one = 1_u64;
208+
/// let two = 2_u64;
205209
/// let my_value = unwrap_opt!(one.checked_sub(2), "cannot do this"); // returns an error
210+
/// # Ok(()) }
206211
/// ```
207212
#[macro_export]
208-
macro_rules! unwrap_opt{
213+
macro_rules! unwrap_opt {
209214
($option:expr, $err:expr $(,)?) => {
210-
$option.ok_or_else(|| -> {
215+
$option.ok_or_else(|| -> ProgramError {
211216
msg!("Option unwrap failed: {:?}", $err);
212-
ProgramError { $crate::VipersError::OptionUnwrapFailed.into() }
217+
$crate::VipersError::OptionUnwrapFailed.into()
213218
})?
214219
};
215220
}
@@ -239,6 +244,7 @@ mod tests {
239244

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

243249
Ok(())
244250
}

0 commit comments

Comments
 (0)