claims/
assert_ready_ok.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
/// Asserts that the expression matches a [`Poll::Ready(Ok(_))`] variant, returning the contained
/// value.
///
/// ## Uses
///
/// Assertions are always checked in both debug and release builds, and cannot be disabled.
/// See [`debug_assert_ready_ok!`] for assertions that are not enabled in release builds by default.
///
/// ## Custom messages
///
/// This macro has a second form, where a custom panic message can be provided with or without
/// arguments for formatting. See [`std::fmt`] for syntax for this form.
///
/// ## Examples
///
/// ```rust
/// # #[macro_use] extern crate claims;
/// # use std::task::Poll;
/// # fn main() {
/// let res: Poll<Result<i32, ()>> = Poll::Ready(Ok(42));
///
/// assert_ready_ok!(res);
/// # }
/// ```
///
/// The contained value will also be returned from this macro call:
///
/// ```rust
/// # #[macro_use] extern crate claims;
/// # use std::task::Poll;
/// # fn main() {
/// let res: Poll<Result<i32, ()>> = Poll::Ready(Ok(42));
///
/// let value = assert_ready_ok!(res);
/// assert_eq!(value, 42);
/// # }
/// ```
///
/// Both `Poll::Ready(Err(..))` and [`Poll::Pending`] variants will panic:
///
/// ```rust,should_panic
/// # #[macro_use] extern crate claims;
/// # use std::task::Poll;
/// # fn main() {
/// let res: Poll<Result<i32, ()>> = Poll::Ready(Err(()));
///
/// assert_ready_ok!(res);  // Will panic
/// # }
/// ```
///
/// ```rust,should_panic
/// # #[macro_use] extern crate claims;
/// # use std::task::Poll;
/// # fn main() {
/// let res: Poll<Result<i32, ()>> = Poll::Pending;
///
/// assert_ready_ok!(res);  // Will panic
/// # }
/// ```
///
/// [`Poll::Ready(Ok(T))`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Ready
/// [`Poll::Pending`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Pending
/// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
/// [`debug_assert_ready_ok!`]: crate::debug_assert_ready_ok
#[macro_export]
macro_rules! assert_ready_ok {
    ($cond:expr $(,)?) => {
        match $cond {
            ::core::task::Poll::Ready(::core::result::Result::Ok(t)) => t,
            ::core::task::Poll::Ready(::core::result::Result::Err(e)) => ::core::panic!("assertion failed, expected Ready(Ok(_)), got Ready(Err({:?}))", e),
            ::core::task::Poll::Pending => ::core::panic!("assertion failed, expected Ready(Ok(_)), got Pending"),
        }
    };
    ($cond:expr, $($arg:tt)+) => {
        match $cond {
            ::core::task::Poll::Ready(::core::result::Result::Ok(t)) => t,
            ::core::task::Poll::Ready(::core::result::Result::Err(e)) => ::core::panic!("assertion failed, expected Ready(Ok(_)), got Ready(Err({:?})): {}", e, ::core::format_args!($($arg)+)),
            ::core::task::Poll::Pending => ::core::panic!("assertion failed, expected Ready(Ok(_)), got Pending: {}", ::core::format_args!($($arg)+)),
        }
    };
}

/// Asserts that the expression matches a [`Poll::Ready(Ok(_))`] variant on debug builds.
///
/// This macro behaves nearly the same as [`assert_ready_ok!`] on debug builds, although it does not
/// return the value contained in the `Poll::Ready(Ok(_))` variant. On release builds it is a no-op.
///
/// [`Poll::Ready(Ok(_))`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Ready
#[macro_export]
macro_rules! debug_assert_ready_ok {
    ($($arg:tt)*) => {
        #[cfg(debug_assertions)]
        $crate::assert_ready_ok!($($arg)*);
    }
}

#[cfg(test)]
mod tests {
    use core::task::Poll::{Pending, Ready};

    #[test]
    fn ready_ok() {
        assert_ready_ok!(Ready(Ok::<_, ()>(())));
    }

    #[test]
    #[should_panic(expected = "assertion failed, expected Ready(Ok(_)), got Ready(Err(()))")]
    fn ready_err() {
        assert_ready_ok!(Ready(Err::<(), _>(())));
    }

    #[test]
    #[should_panic(expected = "assertion failed, expected Ready(Ok(_)), got Pending")]
    fn not_ready() {
        assert_ready_ok!(Pending::<Result<(), ()>>);
    }

    #[test]
    #[should_panic(expected = "assertion failed, expected Ready(Ok(_)), got Ready(Err(())): foo")]
    fn ready_err_custom_message() {
        assert_ready_ok!(Ready(Err::<(), _>(())), "foo");
    }

    #[test]
    #[should_panic(expected = "assertion failed, expected Ready(Ok(_)), got Pending: foo")]
    fn not_ready_custom_message() {
        assert_ready_ok!(Pending::<Result<(), ()>>, "foo");
    }

    #[test]
    fn ready_ok_value_returned() {
        let value = assert_ready_ok!(Ready(Ok::<_, ()>(42)));
        assert_eq!(value, 42);
    }

    #[test]
    #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
    fn debug_ready_ok() {
        debug_assert_ready_ok!(Ready(Ok::<_, ()>(())));
    }

    #[test]
    #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
    #[should_panic(expected = "assertion failed, expected Ready(Ok(_)), got Ready(Err(()))")]
    fn debug_ready_err() {
        debug_assert_ready_ok!(Ready(Err::<(), _>(())));
    }

    #[test]
    #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
    #[should_panic(expected = "assertion failed, expected Ready(Ok(_)), got Pending")]
    fn debug_not_ready() {
        debug_assert_ready_ok!(Pending::<Result<(), ()>>);
    }

    #[test]
    #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
    #[should_panic(expected = "assertion failed, expected Ready(Ok(_)), got Ready(Err(())): foo")]
    fn debug_ready_err_custom_message() {
        debug_assert_ready_ok!(Ready(Err::<(), _>(())), "foo");
    }

    #[test]
    #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")]
    #[should_panic(expected = "assertion failed, expected Ready(Ok(_)), got Pending: foo")]
    fn debug_not_ready_custom_message() {
        debug_assert_ready_ok!(Pending::<Result<(), ()>>, "foo");
    }

    #[test]
    #[cfg_attr(debug_assertions, ignore = "only run in release mode")]
    fn debug_release_ready_err() {
        debug_assert_ready_ok!(Ready(Err::<(), _>(())));
    }

    #[test]
    #[cfg_attr(debug_assertions, ignore = "only run in release mode")]
    fn debug_release_not_ready() {
        debug_assert_ready_ok!(Pending::<Result<(), ()>>);
    }

    #[test]
    fn does_not_require_ok_to_impl_debug() {
        enum Foo {
            Bar,
        }

        assert_ready_ok!(Ready(Ok::<_, ()>(Foo::Bar)));
    }

    #[test]
    fn debug_does_not_require_ok_to_impl_debug() {
        #[allow(dead_code)]
        enum Foo {
            Bar,
        }

        debug_assert_ready_ok!(Ready(Ok::<_, ()>(Foo::Bar)));
    }

    #[test]
    fn does_not_require_ok_to_impl_debug_custom_message() {
        enum Foo {
            Bar,
        }

        assert_ready_ok!(Ready(Ok::<_, ()>(Foo::Bar)), "foo");
    }

    #[test]
    fn debug_does_not_require_ok_to_impl_debug_custom_message() {
        #[allow(dead_code)]
        enum Foo {
            Bar,
        }

        debug_assert_ready_ok!(Ready(Ok::<_, ()>(Foo::Bar)), "foo");
    }
}