assert_cmp/lib.rs
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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
//! Convenient assertion macros that print the failed expressions and their evaluated values.
#![no_std]
#![no_implicit_prelude]
/// Create assertion macros for boolean binary operators.
#[macro_export]
macro_rules! operator_assertion_macros {
(
$(#![$shared_attr:meta])*
in $module:path;
use $assert:path;
$(#[$attr_simple:meta])* simple = $name_simple:ident;
$(#[$attr_expr:meta])* expr = $name_expr:ident;
) => {
$(#[$shared_attr])*
$(#[$attr_expr])*
macro_rules! $name_expr {
($left:expr, $op:tt, $right:expr) => {
match ($left, $right) {
(left, right) => {
$assert!(
left $op right,
"{left_expr} {op} {right_expr} ⇒ {left_value:?} {op} {right_value:?} ⇒ false",
op = stringify!($op),
left_expr = stringify!($left),
right_expr = stringify!($right),
left_value = left,
right_value = right,
)
}
}
};
}
$(#[$shared_attr])*
$(#[$attr_simple])*
macro_rules! $name_simple {
($left:ident $op:tt $right:ident) => {
$module::$name_expr!($left, $op, $right)
};
($left:ident $op:tt $right:literal) => {
$module::$name_expr!($left, $op, $right)
};
($left:literal $op:tt $right:ident) => {
$module::$name_expr!($left, $op, $right)
};
($left:literal $op:tt $right:literal) => {
$module::$name_expr!($left, $op, $right)
};
}
};
}
/// Create an assertion macro for boolean function calls.
#[macro_export]
macro_rules! function_assertion_macro {
(
$(#![$shared_attr:meta])*
use $assert:path;
$(#[$attr:meta])* $name:ident;
) => {
$(#[$shared_attr])*
$(#[$attr])*
macro_rules! $name {
($function:ident($left:expr, $right:expr)) => {
match ($left, $right) {
(left, right) => {
$assert!(
$function($left, $right),
"{func}({left_expr}, {right_expr}) ⇒ {func}({left_value:?}, {right_value:?}) ⇒ false",
func = stringify!($function),
left_expr = stringify!($left),
right_expr = stringify!($right),
left_value = left,
right_value = right,
)
}
}
};
(not $function:ident($left:expr, $right:expr)) => {
match ($left, $right) {
(left, right) => {
$assert!(
!$function($left, $right),
"{func}({left_expr}, {right_expr}) ⇒ {func}({left_value}, {right_value}) ⇒ true",
func = stringify!($function),
left_expr = stringify!($left),
right_expr = stringify!($right),
left_value = left,
right_value = right,
)
}
}
};
}
};
}
operator_assertion_macros! {
#![macro_export]
in ::assert_cmp;
use ::core::assert;
/// Assert that a binary expression of 2 identifiers/literals returns `true`.
///
/// **Syntax:**
///
/// ```ignore
/// assert_op!($left $op $right)
/// ```
///
/// * `$left` and `$right` are either identifiers or literals or both.
/// * `$op` is a binary operator (e.g. `>`, `<`, `>=`, `<=`, `==`, `!=`).
///
/// **Example:** An assertion that passes
///
/// ```
/// # use assert_cmp::assert_op;
/// assert_op!(123 < 456);
/// ```
///
/// **Example:** An assertion that fails
///
/// ```should_panic
/// # use assert_cmp::assert_op;
/// assert_op!(123 > 456); // panic: 123 > 456 ⇒ 123 > 456 ⇒ false
/// ```
simple = assert_op;
/// Assert that a binary expression of 2 expressions returns `true`.
///
/// **Syntax:**
///
/// ```ignore
/// assert_op_expr!($left, $op, $right)
/// ```
///
/// * `$left` and `$right` are expressions.
/// * `$op` is a binary operator (e.g. `>`, `<`, `>=`, `<=`, `==`, `!=`).
///
/// **Example:** An assertion that passes
///
/// ```
/// # use assert_cmp::assert_op_expr;
/// assert_op_expr!(12 + 34, ==, 34 + 12);
/// ```
///
/// **Example:** An assertion that fails
///
/// ```should_panic
/// # use assert_cmp::assert_op_expr;
/// assert_op_expr!(12 + 34, ==, 43 + 21); // panic: 12 + 34 == 43 + 21 ⇒ 46 == 64 ⇒ false
/// ```
expr = assert_op_expr;
}
operator_assertion_macros! {
#![macro_export]
in ::assert_cmp;
use ::core::debug_assert;
/// Assert that a binary expression of 2 identifiers/literals returns `true`.
///
/// This macro is the debug-only version of [`assert_op`].
/// It acts like `assert_op` in debug mode, but does nothing in release mode.
simple = debug_assert_op;
/// Assert that a binary expression of 2 expressions returns `true`.
///
/// This macro is the debug-only version of [`assert_op`].
/// It acts like `assert_op_expr` in debug mode, but does nothing in release mode.
expr = debug_assert_op_expr;
}
function_assertion_macro! {
#![macro_export]
use ::core::assert;
/// Assert that a binary function call of 2 expressions returns `true`.
///
/// **Syntax:**
///
/// ```ignore
/// assert_fn!($function($left, $right))
/// ```
///
/// ```ignore
/// assert_fn!(not $function($left, $right))
/// ```
///
/// * `$function` is an identifier of a binary function.
/// * `$left` and `$right` are expressions.
/// * `not`'s appearance means expecting the function call to returns `false` instead of `true`.
///
/// **Example:** An assertion that passes
///
/// ```
/// # use assert_cmp::assert_fn;
/// fn func<A, B>(_: A, _: B) -> bool {
/// true
/// }
/// assert_fn!(func(123, 456));
/// ```
///
/// **Example:** An assertion that fails
///
/// ```should_panic
/// # use assert_cmp::assert_fn;
/// fn func<A, B>(_: A, _: B) -> bool {
/// false
/// }
/// assert_fn!(func(123, 456)); // panic: func(123, 456) ⇒ func(123, 456) ⇒ false
/// ```
///
/// **Example:** A negative assertion that passes
///
/// ```
/// # use assert_cmp::assert_fn;
/// fn func<A, B>(_: A, _: B) -> bool {
/// false
/// }
/// assert_fn!(not func(123, 456));
/// ```
///
/// **Example:** A negative assertion that fails
///
/// ```should_panic
/// # use assert_cmp::assert_fn;
/// fn func<A, B>(_: A, _: B) -> bool {
/// true
/// }
/// assert_fn!(not func(123, 456)); // panic: func(123, 456) ⇒ func(123, 456) ⇒ true
/// ```
assert_fn;
}
function_assertion_macro! {
#![macro_export]
use ::core::debug_assert;
/// Assert that a binary function call of 2 expressions returns `true`.
///
/// This macro is the debug-only version of [`assert_fn`].
/// It acts like `assert_op_expr` in debug mode, but does nothing in release mode.
debug_assert_fn;
}