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
/// Macro to define different expected values for `debug` and `release`
///
/// This is useful when testing logic with differing expected results based on build.
///
/// Basically erogonomifies the following ...
///
/// ```rust
/// # use cfg_if::cfg_if;
/// cfg_if! {
/// if #[cfg(not(debug_assertions))] {
/// let expected = 42; // release build value
/// } else {
/// let expected = 0; // debug build value
/// }
/// }
/// ```
/// with ...
/// ```rust
/// # use test_toolbox::expect;
/// expect! { expected = 42, 0 }
/// ```
///
/// # Features
///
/// * lets you define both `release` and `debug` initialization values for expected variable
/// * lets you optionally provide an explicit `type` when defining an expected variable
///
/// \* `release` _initialization value is defined first, followed by the_ `debug` _value_
///
/// # Examples
///
/// * implicit type
///
/// ```rust
/// # use test_toolbox::expect;
/// // 42 is the expected value for release
/// // default is the expected value for debug
/// expect! { expected = 42, 0 }
/// ```
///
/// * explict type
///
/// ```rust
/// # use test_toolbox::expect;
/// // 42 is the expected value for release
/// // default is the expected value for debug
/// expect! { expected: usize = 42, Default::default() }
/// ```
#[macro_export]
macro_rules! expect {
// declare expected variable implicitly typed
($var:ident = $rls:expr, $dgb:expr) => {
cfg_if::cfg_if! {
if #[cfg(not(debug_assertions))] {
let $var = $rls;
} else {
let $var = $dgb;
}
}
};
// declare expected variable explicitly typed
($var:ident: $typ:ty = $rls:expr, $dgb:expr) => {
cfg_if::cfg_if! {
if #[cfg(not(debug_assertions))] {
let $var: $typ = $rls;
} else {
let $var: $typ = $dgb;
}
}
};
}