chain-assertions 0.1.2

Insertable assertions into method chains
Documentation
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/// An extension trait to add the assertion_some methods.
pub trait AssertSomeExt {
    /// Asserts the [`Option`] is [`Some`].
    ///
    /// # Panics
    ///
    /// If it is [`None`], the method panics.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chain_assertions::prelude::*;
    ///
    /// let x: Option<i32> = Some(21);
    /// let x = x.assert_some().map(|x| x * 2);
    /// assert_eq!(x, Some(42));
    /// ```
    ///
    /// ```rust,should_panic
    /// use chain_assertions::prelude::*;
    ///
    /// let x: Option<i32> = None;
    /// let _ = x.assert_some().map(|x| x * 2);
    /// //        ^-- panics here
    fn assert_some(self) -> Self;

    /// Asserts the [`Option`] is [`Some`] only in debug builds.
    ///
    /// # Panics
    ///
    /// The method panics if all following conditions are satisfied:
    ///
    /// - It is [`None`]
    /// - `debug_assertions` is enabled
    /// - `passthrough` feature is disabled
    ///
    /// Otherwise, the method returns self as is.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chain_assertions::prelude::*;
    ///
    /// let x: Option<i32> = Some(21);
    /// let x = x.debug_assert_some().map(|x| x * 2);
    /// assert_eq!(x, Some(42));
    /// ```
    fn debug_assert_some(self) -> Self;
}

/// An extension trait to add the assertion_some_and methods.
pub trait AssertSomeAndExt<T> {
    /// Asserts the [`Option`] is [`Some`] and satisfies the condition.
    ///
    /// # Panics
    ///
    /// If it is [`None`] or the condition is not satisfied, the method panics.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chain_assertions::prelude::*;
    ///
    /// let x: Option<i32> = Some(21);
    /// let x = x.assert_some_and(|x| x >= &20).map(|x| x * 2);
    /// assert_eq!(x, Some(42));
    /// ```
    ///
    /// ```rust,should_panic
    /// use chain_assertions::prelude::*;
    ///
    /// let x: Option<i32> = Some(19);
    /// let _ = x.assert_some_and(|x| x >= &20).map(|x| x * 2);
    /// //        ^-- panics here
    /// ```
    fn assert_some_and(self, cond: impl FnOnce(&T) -> bool) -> Self;

    /// Asserts the [`Option`] is [`Some`] and satisfies the condition only in debug builds.
    ///
    /// # Panics
    ///
    /// The method panics if all following conditions are satisfied:
    ///
    /// - It is [`None`] or the condition is not satisfied
    /// - `debug_assertions` is enabled
    /// - `passthrough` feature is disabled
    ///
    /// Otherwise, the method returns self as is.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chain_assertions::prelude::*;
    ///
    /// let x: Option<i32> = Some(21);
    /// let x = x.debug_assert_some_and(|x| x >= &20).map(|x| x * 2);
    /// assert_eq!(x, Some(42));
    /// ```
    ///
    /// ```rust,should_panic,ignore
    /// use chain_assertions::prelude::*;
    ///
    /// let x: Option<i32> = Some(19);
    /// let _ = x.debug_assert_some_and(|x| x >= &20).map
    /// //        ^-- panics here if debug_assertion is enabled
    /// ```
    fn debug_assert_some_and(self, cond: impl FnOnce(&T) -> bool) -> Self;
}

/// An extension trait to add the assertion_none methods.
pub trait AssertNoneExt {
    /// Asserts the [`Option`] is [`None`].
    ///
    /// # Panics
    ///
    /// If it is [`Some`], the method panics.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chain_assertions::prelude::*;
    ///
    /// let x: Option<i32> = None;
    /// let x = x.assert_none().map(|x| x * 2);
    /// assert_eq!(x, None);
    /// ```
    ///
    /// ```rust,should_panic
    /// use chain_assertions::prelude::*;
    ///
    /// let x: Option<i32> = Some(21);
    /// let _ = x.assert_none().map(|x| x * 2);
    /// //        ^-- panics here
    /// ```
    fn assert_none(self) -> Self;

    /// Asserts the [`Option`] is [`None`] only in debug builds.
    ///
    /// # Panics
    ///
    /// The method panics if all following conditions are satisfied:
    ///
    /// - It is [`Some`]
    /// - `debug_assertions` is enabled
    /// - `passthrough` feature is disabled
    ///
    /// Otherwise, the method returns self as is.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chain_assertions::prelude::*;
    ///
    /// let x: Option<i32> = None;
    /// let x = x.debug_assert_none().map(|v| v * 2);
    /// assert_eq!(x, None);
    /// ```
    fn debug_assert_none(self) -> Self;
}

impl<T> AssertSomeExt for Option<T> {
    #[track_caller]
    #[inline]
    fn assert_some(self) -> Self {
        if self.is_none() {
            panic!("Expected Some(_), got None");
        }
        self
    }

    #[track_caller]
    #[inline]
    fn debug_assert_some(self) -> Self {
        #[cfg(all(debug_assertions, not(feature = "passthrough")))]
        {
            if self.is_none() {
                panic!("Expected Some(_), got None");
            }
        }
        self
    }
}

impl<T> AssertSomeAndExt<T> for Option<T>
where
    T: crate::fmt::Debug,
{
    #[track_caller]
    #[inline]
    fn assert_some_and(self, cond: impl FnOnce(&T) -> bool) -> Self {
        match self {
            Some(ref v) if cond(v) => { /* do nothing */ }
            Some(ref v) => panic!("Condition not satisfied for Some({:?})", v),
            None => panic!("Expected Some(_), got None"),
        }
        self
    }

    #[track_caller]
    #[inline]
    fn debug_assert_some_and(self, _cond: impl FnOnce(&T) -> bool) -> Self {
        #[cfg(all(debug_assertions, not(feature = "passthrough")))]
        {
            match self {
                Some(ref v) if _cond(v) => { /* do nothing */ }
                Some(ref v) => panic!("Condition not satisfied for Some({:?})", v),
                None => panic!("Expected Some(_), got None"),
            }
        }
        self
    }
}

impl<T> AssertNoneExt for Option<T>
where
    T: crate::fmt::Debug,
{
    #[track_caller]
    #[inline]
    fn assert_none(self) -> Self {
        if let Some(ref v) = self {
            panic!("Expected None, got Some({:?})", v);
        }
        self
    }

    #[track_caller]
    #[inline]
    fn debug_assert_none(self) -> Self {
        #[cfg(all(debug_assertions, not(feature = "passthrough")))]
        {
            if let Some(ref v) = self {
                panic!("Expected None, got Some({:?})", v);
            }
        }
        self
    }
}

#[cfg(test)]
mod tests {
    #[derive(PartialEq)]
    struct NonDebuggable;

    #[derive(Debug, PartialEq)]
    struct Debuggable;

    mod assert_some {
        use super::{super::*, *};

        #[test]
        fn it_succeeds_on_some() {
            let x: Option<NonDebuggable> = Some(NonDebuggable);
            let x = x.assert_some();

            assert!(
                matches!(x, Some(NonDebuggable)),
                "Expected Some(NonDebuggable)"
            );
        }

        #[test]
        #[should_panic(expected = "Expected Some(_), got None")]
        fn it_fails_on_none() {
            let opt: Option<NonDebuggable> = None;
            let _ = opt.assert_some();
            //          ^-- should panic here
        }
    }

    mod assert_some_and {
        use super::super::*;

        #[test]
        fn it_succeeds_on_ok_and_condition_satisfied() {
            let x: Option<i32> = Some(21);
            let x = x.assert_some_and(|x| x >= &20).map(|x| x * 2);
            assert_eq!(x, Some(42));
        }

        #[test]
        #[should_panic(expected = "Condition not satisfied for Some(19)")]
        fn it_fails_on_ok_and_condition_not_satisfied() {
            let x: Option<i32> = Some(19);
            let _ = x.assert_some_and(|x| x >= &20).map(|x| x * 2);
            //        ^-- should panic here
        }

        #[test]
        #[should_panic(expected = "Expected Some(_), got None")]
        fn it_fails_on_none() {
            let x: Option<i32> = None;
            let _ = x.assert_some_and(|x| x >= &20).map(|x| x * 2);
            //        ^-- should panic here
        }
    }

    mod debug_assert_some {
        use super::{super::*, *};

        #[test]
        fn it_succeeds_on_some() {
            let x: Option<NonDebuggable> = Some(NonDebuggable);
            let x = x.debug_assert_some();

            assert!(
                matches!(x, Some(NonDebuggable)),
                "Expected Some(NonDebuggable)"
            );
        }

        #[test]
        #[cfg_attr(
            all(debug_assertions, not(feature = "passthrough")),
            should_panic(expected = "Expected Some(_), got None")
        )]
        fn it_fails_on_none() {
            let x: Option<NonDebuggable> = None;
            let x = x.debug_assert_some();
            //               ^-- should panic here only in debug mode
            assert!(matches!(x, None), "Expected None");
        }
    }

    mod debug_assert_some_and {
        use super::super::*;

        #[test]
        fn it_succeeds_on_some_and_condition_satisfied() {
            let x: Option<i32> = Some(21);
            let x = x.debug_assert_some_and(|x| x >= &20).map(|x| x * 2);

            assert_eq!(x, Some(42));
        }

        #[test]
        #[cfg_attr(
            all(debug_assertions, not(feature = "passthrough")),
            should_panic(expected = "Condition not satisfied for Some(19)")
        )]
        fn it_fails_on_some_and_condition_not_satisfied() {
            let x: Option<i32> = Some(19);
            let x = x.debug_assert_some_and(|x| x >= &20).map(|x| x * 2);
            //        ^-- should panic here only in debug mode

            // for debug builds
            assert_eq!(x, Some(38));
        }

        #[test]
        #[cfg_attr(
            all(debug_assertions, not(feature = "passthrough")),
            should_panic(expected = "Expected Some(_), got None")
        )]
        fn it_fails_on_none() {
            let x: Option<i32> = None;
            let _ = x.debug_assert_some_and(|x| x >= &20).map(|x| x * 2);
            //        ^-- should panic here

            // for debug builds
            assert_eq!(x, None);
        }
    }

    mod assert_none {
        use super::{super::*, *};

        #[test]
        fn it_succeeds_on_none() {
            let x: Option<Debuggable> = None;
            let x = x.assert_none();

            assert!(matches!(x, None), "Expected None");
        }

        #[test]
        #[should_panic(expected = "Expected None, got Some(Debuggable)")]
        fn it_fails_on_some() {
            let x: Option<Debuggable> = Some(Debuggable);
            let _ = x.assert_none();
            //        ^-- should panic here
        }
    }

    mod debug_assert_none {
        use super::{super::*, *};

        #[test]
        fn it_succeeds_on_none() {
            let x: Option<Debuggable> = None;
            let x = x.debug_assert_none();

            assert!(matches!(x, None), "Expected None");
        }

        #[test]
        #[cfg_attr(
            all(debug_assertions, not(feature = "passthrough")),
            should_panic(expected = "Expected None, got Some(Debuggable)")
        )]
        fn it_fails_on_some() {
            let x: Option<Debuggable> = Some(Debuggable);
            let x = x.debug_assert_none();
            //        ^-- should panic here only in debug mode

            // for debug builds
            assert!(matches!(x, Some(Debuggable)), "Expected Some(Debuggable)");
        }
    }
}