behave 0.9.1

BDD testing framework with expressive expect! matchers and a zero-keyword DSL.
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//! Matcher combinators for composing multiple matchers.
//!
//! Combinators let you build complex assertions from simple matchers.
//! All three types implement [`BehaveMatch<T>`], so they compose recursively.
//!
//! | Function | Semantics |
//! |----------|-----------|
//! | [`all_of`] | All matchers must pass (empty = pass) |
//! | [`any_of`] | At least one must pass (empty = fail) |
//! | [`not_matching`] | Inverts a single matcher |
//!
//! # Examples
//!
//! ```
//! use behave::prelude::*;
//! use behave::combinators::{all_of, any_of, not_matching};
//!
//! struct GreaterThan(i32);
//! # #[allow(clippy::unnecessary_literal_bound)]
//! impl BehaveMatch<i32> for GreaterThan {
//!     fn matches(&self, actual: &i32) -> bool { *actual > self.0 }
//!     fn description(&self) -> &str { "to be greater than threshold" }
//! }
//!
//! struct LessThan(i32);
//! # #[allow(clippy::unnecessary_literal_bound)]
//! impl BehaveMatch<i32> for LessThan {
//!     fn matches(&self, actual: &i32) -> bool { *actual < self.0 }
//!     fn description(&self) -> &str { "to be less than threshold" }
//! }
//!
//! let matcher = all_of(vec![
//!     Box::new(GreaterThan(0)) as Box<dyn BehaveMatch<i32>>,
//!     Box::new(LessThan(100)),
//! ]);
//!
//! let result = Expectation::new(42, "42").to_match(matcher);
//! assert!(result.is_ok());
//! ```

use crate::custom::BehaveMatch;

/// Builds a bullet-list description from a prefix and matcher descriptions.
///
/// Produces output like:
/// ```text
/// to match all of:
///   - to be positive
///   - to be less than 100
/// ```
fn build_list_description(prefix: &str, matchers: &[Box<dyn BehaveMatch<impl Sized>>]) -> String {
    let mut desc = format!("{prefix}:");
    for m in matchers {
        let sub = m.description();
        desc.push_str("\n  - ");
        desc.push_str(&indent_subsequent_lines(sub, "    "));
    }
    desc
}

/// Indents all lines after the first by the given prefix.
///
/// This handles nested combinator descriptions where sub-descriptions
/// are multi-line (e.g. an `any_of` inside an `all_of`).
fn indent_subsequent_lines(text: &str, indent: &str) -> String {
    let mut lines = text.lines();
    let Some(first) = lines.next() else {
        return String::new();
    };
    let mut result = first.to_string();
    for line in lines {
        result.push('\n');
        result.push_str(indent);
        result.push_str(line);
    }
    result
}

// ---------------------------------------------------------------------------
// AllOf
// ---------------------------------------------------------------------------

/// Matcher that passes when **all** inner matchers pass.
///
/// Created by [`all_of`]. An empty list passes (vacuous truth).
///
/// # Examples
///
/// ```
/// use behave::prelude::*;
/// use behave::combinators::all_of;
///
/// struct IsPositive;
/// # #[allow(clippy::unnecessary_literal_bound)]
/// impl BehaveMatch<i32> for IsPositive {
///     fn matches(&self, actual: &i32) -> bool { *actual > 0 }
///     fn description(&self) -> &str { "to be positive" }
/// }
///
/// let matcher = all_of(vec![Box::new(IsPositive) as Box<dyn BehaveMatch<i32>>]);
/// let result = Expectation::new(5, "5").to_match(matcher);
/// assert!(result.is_ok());
/// ```
#[non_exhaustive]
pub struct AllOf<T> {
    matchers: Vec<Box<dyn BehaveMatch<T>>>,
    description: String,
}

impl<T> core::fmt::Debug for AllOf<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("AllOf")
            .field("description", &self.description)
            .field("matcher_count", &self.matchers.len())
            .finish()
    }
}

impl<T> BehaveMatch<T> for AllOf<T> {
    fn matches(&self, actual: &T) -> bool {
        self.matchers.iter().all(|m| m.matches(actual))
    }

    fn description(&self) -> &str {
        &self.description
    }
}

/// Creates a matcher that passes when **all** inner matchers pass.
///
/// An empty list passes (vacuous truth), matching the semantics of
/// `Iterator::all` on an empty iterator.
///
/// # Examples
///
/// ```
/// use behave::prelude::*;
/// use behave::combinators::all_of;
///
/// struct IsPositive;
/// # #[allow(clippy::unnecessary_literal_bound)]
/// impl BehaveMatch<i32> for IsPositive {
///     fn matches(&self, actual: &i32) -> bool { *actual > 0 }
///     fn description(&self) -> &str { "to be positive" }
/// }
///
/// struct IsEven;
/// # #[allow(clippy::unnecessary_literal_bound)]
/// impl BehaveMatch<i32> for IsEven {
///     fn matches(&self, actual: &i32) -> bool { actual % 2 == 0 }
///     fn description(&self) -> &str { "to be even" }
/// }
///
/// let matcher = all_of(vec![
///     Box::new(IsPositive) as Box<dyn BehaveMatch<i32>>,
///     Box::new(IsEven),
/// ]);
/// let result = Expectation::new(4, "4").to_match(matcher);
/// assert!(result.is_ok());
/// ```
pub fn all_of<T>(matchers: Vec<Box<dyn BehaveMatch<T>>>) -> AllOf<T> {
    let description = build_list_description("to match all of", &matchers);
    AllOf {
        matchers,
        description,
    }
}

// ---------------------------------------------------------------------------
// AnyOf
// ---------------------------------------------------------------------------

/// Matcher that passes when **at least one** inner matcher passes.
///
/// Created by [`any_of`]. An empty list fails.
///
/// # Examples
///
/// ```
/// use behave::prelude::*;
/// use behave::combinators::any_of;
///
/// struct IsZero;
/// # #[allow(clippy::unnecessary_literal_bound)]
/// impl BehaveMatch<i32> for IsZero {
///     fn matches(&self, actual: &i32) -> bool { *actual == 0 }
///     fn description(&self) -> &str { "to be zero" }
/// }
///
/// struct IsPositive;
/// # #[allow(clippy::unnecessary_literal_bound)]
/// impl BehaveMatch<i32> for IsPositive {
///     fn matches(&self, actual: &i32) -> bool { *actual > 0 }
///     fn description(&self) -> &str { "to be positive" }
/// }
///
/// let matcher = any_of(vec![
///     Box::new(IsZero) as Box<dyn BehaveMatch<i32>>,
///     Box::new(IsPositive),
/// ]);
/// let result = Expectation::new(0, "0").to_match(matcher);
/// assert!(result.is_ok());
/// ```
#[non_exhaustive]
pub struct AnyOf<T> {
    matchers: Vec<Box<dyn BehaveMatch<T>>>,
    description: String,
}

impl<T> core::fmt::Debug for AnyOf<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("AnyOf")
            .field("description", &self.description)
            .field("matcher_count", &self.matchers.len())
            .finish()
    }
}

impl<T> BehaveMatch<T> for AnyOf<T> {
    fn matches(&self, actual: &T) -> bool {
        self.matchers.iter().any(|m| m.matches(actual))
    }

    fn description(&self) -> &str {
        &self.description
    }
}

/// Creates a matcher that passes when **at least one** inner matcher passes.
///
/// An empty list fails, matching the semantics of `Iterator::any` on an
/// empty iterator.
///
/// # Examples
///
/// ```
/// use behave::prelude::*;
/// use behave::combinators::any_of;
///
/// struct IsZero;
/// # #[allow(clippy::unnecessary_literal_bound)]
/// impl BehaveMatch<i32> for IsZero {
///     fn matches(&self, actual: &i32) -> bool { *actual == 0 }
///     fn description(&self) -> &str { "to be zero" }
/// }
///
/// struct IsNegative;
/// # #[allow(clippy::unnecessary_literal_bound)]
/// impl BehaveMatch<i32> for IsNegative {
///     fn matches(&self, actual: &i32) -> bool { *actual < 0 }
///     fn description(&self) -> &str { "to be negative" }
/// }
///
/// let matcher = any_of(vec![
///     Box::new(IsZero) as Box<dyn BehaveMatch<i32>>,
///     Box::new(IsNegative),
/// ]);
/// let result = Expectation::new(-3, "-3").to_match(matcher);
/// assert!(result.is_ok());
/// ```
pub fn any_of<T>(matchers: Vec<Box<dyn BehaveMatch<T>>>) -> AnyOf<T> {
    let description = build_list_description("to match any of", &matchers);
    AnyOf {
        matchers,
        description,
    }
}

// ---------------------------------------------------------------------------
// NotMatching
// ---------------------------------------------------------------------------

/// Matcher that inverts a single inner matcher.
///
/// Created by [`not_matching`]. Use this inside combinators to negate
/// one matcher without affecting the outer expectation chain.
///
/// # Examples
///
/// ```
/// use behave::prelude::*;
/// use behave::combinators::not_matching;
///
/// struct IsEven;
/// # #[allow(clippy::unnecessary_literal_bound)]
/// impl BehaveMatch<i32> for IsEven {
///     fn matches(&self, actual: &i32) -> bool { actual % 2 == 0 }
///     fn description(&self) -> &str { "to be even" }
/// }
///
/// let matcher = not_matching(Box::new(IsEven));
/// let result = Expectation::new(3, "3").to_match(matcher);
/// assert!(result.is_ok());
/// ```
#[non_exhaustive]
pub struct NotMatching<T> {
    inner: Box<dyn BehaveMatch<T>>,
    description: String,
}

impl<T> core::fmt::Debug for NotMatching<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("NotMatching")
            .field("description", &self.description)
            .finish_non_exhaustive()
    }
}

impl<T> BehaveMatch<T> for NotMatching<T> {
    fn matches(&self, actual: &T) -> bool {
        !self.inner.matches(actual)
    }

    fn description(&self) -> &str {
        &self.description
    }
}

/// Creates a matcher that inverts a single inner matcher.
///
/// Use `not_matching` inside combinators to negate one matcher without
/// affecting the outer expectation. For negating the whole chain, use
/// [`.not()`](crate::Expectation::not) instead.
///
/// # Examples
///
/// ```
/// use behave::prelude::*;
/// use behave::combinators::not_matching;
///
/// struct IsEven;
/// # #[allow(clippy::unnecessary_literal_bound)]
/// impl BehaveMatch<i32> for IsEven {
///     fn matches(&self, actual: &i32) -> bool { actual % 2 == 0 }
///     fn description(&self) -> &str { "to be even" }
/// }
///
/// let matcher = not_matching(Box::new(IsEven));
/// let result = Expectation::new(7, "7").to_match(matcher);
/// assert!(result.is_ok());
/// ```
pub fn not_matching<T>(matcher: Box<dyn BehaveMatch<T>>) -> NotMatching<T> {
    let description = format!("not {}", matcher.description());
    NotMatching {
        inner: matcher,
        description,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Expectation;

    struct IsPositive;

    #[allow(clippy::unnecessary_literal_bound)]
    impl BehaveMatch<i32> for IsPositive {
        fn matches(&self, actual: &i32) -> bool {
            *actual > 0
        }
        fn description(&self) -> &str {
            "to be positive"
        }
    }

    struct IsEven;

    #[allow(clippy::unnecessary_literal_bound)]
    impl BehaveMatch<i32> for IsEven {
        fn matches(&self, actual: &i32) -> bool {
            actual % 2 == 0
        }
        fn description(&self) -> &str {
            "to be even"
        }
    }

    struct IsZero;

    #[allow(clippy::unnecessary_literal_bound)]
    impl BehaveMatch<i32> for IsZero {
        fn matches(&self, actual: &i32) -> bool {
            *actual == 0
        }
        fn description(&self) -> &str {
            "to be zero"
        }
    }

    // -- all_of --

    #[test]
    fn all_of_all_pass() {
        let m = all_of(vec![
            Box::new(IsPositive) as Box<dyn BehaveMatch<i32>>,
            Box::new(IsEven),
        ]);
        assert!(Expectation::new(4, "4").to_match(m).is_ok());
    }

    #[test]
    fn all_of_one_fails() {
        let m = all_of(vec![
            Box::new(IsPositive) as Box<dyn BehaveMatch<i32>>,
            Box::new(IsEven),
        ]);
        assert!(Expectation::new(3, "3").to_match(m).is_err());
    }

    #[test]
    fn all_of_empty_passes() {
        let m: AllOf<i32> = all_of(vec![]);
        assert!(Expectation::new(99, "99").to_match(m).is_ok());
    }

    #[test]
    fn all_of_description_format() {
        let m = all_of(vec![
            Box::new(IsPositive) as Box<dyn BehaveMatch<i32>>,
            Box::new(IsEven),
        ]);
        let desc = m.description();
        assert!(desc.contains("to match all of:"));
        assert!(desc.contains("- to be positive"));
        assert!(desc.contains("- to be even"));
    }

    // -- any_of --

    #[test]
    fn any_of_one_passes() {
        let m = any_of(vec![
            Box::new(IsZero) as Box<dyn BehaveMatch<i32>>,
            Box::new(IsPositive),
        ]);
        assert!(Expectation::new(5, "5").to_match(m).is_ok());
    }

    #[test]
    fn any_of_none_pass() {
        let m = any_of(vec![
            Box::new(IsZero) as Box<dyn BehaveMatch<i32>>,
            Box::new(IsEven),
        ]);
        assert!(Expectation::new(3, "3").to_match(m).is_err());
    }

    #[test]
    fn any_of_empty_fails() {
        let m: AnyOf<i32> = any_of(vec![]);
        assert!(Expectation::new(1, "1").to_match(m).is_err());
    }

    #[test]
    fn any_of_description_format() {
        let m = any_of(vec![
            Box::new(IsZero) as Box<dyn BehaveMatch<i32>>,
            Box::new(IsPositive),
        ]);
        let desc = m.description();
        assert!(desc.contains("to match any of:"));
        assert!(desc.contains("- to be zero"));
        assert!(desc.contains("- to be positive"));
    }

    // -- not_matching --

    #[test]
    fn not_matching_inverts_pass() {
        let m = not_matching(Box::new(IsEven));
        assert!(Expectation::new(3, "3").to_match(m).is_ok());
    }

    #[test]
    fn not_matching_inverts_fail() {
        let m = not_matching(Box::new(IsEven));
        assert!(Expectation::new(4, "4").to_match(m).is_err());
    }

    #[test]
    fn not_matching_description() {
        let m = not_matching(Box::new(IsEven));
        assert_eq!(m.description(), "not to be even");
    }

    // -- nested composition --

    #[test]
    fn nested_all_of_inside_any_of() {
        let inner = all_of(vec![
            Box::new(IsPositive) as Box<dyn BehaveMatch<i32>>,
            Box::new(IsEven),
        ]);
        let m = any_of(vec![
            Box::new(IsZero) as Box<dyn BehaveMatch<i32>>,
            Box::new(inner),
        ]);
        // 4 is positive and even → inner passes → any_of passes
        assert!(Expectation::new(4, "4").to_match(m).is_ok());
    }

    #[test]
    fn nested_description_indentation() {
        let inner = all_of(vec![
            Box::new(IsPositive) as Box<dyn BehaveMatch<i32>>,
            Box::new(IsEven),
        ]);
        let m = any_of(vec![
            Box::new(IsZero) as Box<dyn BehaveMatch<i32>>,
            Box::new(inner),
        ]);
        let desc = m.description();
        // The nested all_of description should be indented
        assert!(desc.contains("to match any of:"));
        assert!(desc.contains("- to match all of:"));
    }

    #[test]
    fn not_matching_inside_all_of_pass() {
        let m = all_of(vec![
            Box::new(IsPositive) as Box<dyn BehaveMatch<i32>>,
            Box::new(not_matching(Box::new(IsEven))),
        ]);
        // 3 is positive and odd → passes
        assert!(Expectation::new(3, "3").to_match(m).is_ok());
    }

    #[test]
    fn not_matching_inside_all_of_fail() {
        let m = all_of(vec![
            Box::new(IsPositive) as Box<dyn BehaveMatch<i32>>,
            Box::new(not_matching(Box::new(IsEven))),
        ]);
        // 4 is positive but even → fails
        assert!(Expectation::new(4, "4").to_match(m).is_err());
    }

    #[test]
    fn all_of_negated_via_expectation() {
        let m = all_of(vec![
            Box::new(IsPositive) as Box<dyn BehaveMatch<i32>>,
            Box::new(IsEven),
        ]);
        // 3 fails all_of, so negated passes
        assert!(Expectation::new(3, "3").negate().to_match(m).is_ok());
    }

    #[test]
    fn boxed_matcher_delegates() {
        let boxed: Box<dyn BehaveMatch<i32>> = Box::new(IsEven);
        assert!(Expectation::new(4, "4").to_match(boxed).is_ok());
    }

    #[test]
    fn debug_impls() {
        let a = all_of::<i32>(vec![]);
        let formatted = format!("{a:?}");
        assert!(formatted.contains("AllOf"));

        let b = any_of::<i32>(vec![]);
        let formatted = format!("{b:?}");
        assert!(formatted.contains("AnyOf"));

        let c = not_matching(Box::new(IsEven));
        let formatted = format!("{c:?}");
        assert!(formatted.contains("NotMatching"));
    }
}