aprender-test-lib 0.40.0

Probar: Rust-native testing framework with pixel coverage, TUI snapshots, and visual regression
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
//! Soft Assertions (Feature 17)
//!
//! Collect multiple assertion failures without stopping test execution.
//!
//! ## EXTREME TDD: Tests written FIRST per spec
//!
//! ## Toyota Way Application:
//! - **Jidoka**: Collect all failures for comprehensive error reporting
//! - **Poka-Yoke**: Type-safe API prevents misuse

use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use std::time::Instant;

/// A single assertion failure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssertionFailure {
    /// Message describing the failure
    pub message: String,
    /// Location where the assertion failed (<file:line>)
    pub location: Option<String>,
    /// Timestamp when the failure occurred
    #[serde(skip)]
    pub timestamp: Option<Instant>,
    /// Index of this assertion in the sequence
    pub index: usize,
}

impl AssertionFailure {
    /// Create a new assertion failure
    #[must_use]
    pub fn new(message: impl Into<String>, index: usize) -> Self {
        Self {
            message: message.into(),
            location: None,
            timestamp: Some(Instant::now()),
            index,
        }
    }

    /// Set the location of the failure
    #[must_use]
    pub fn with_location(mut self, location: impl Into<String>) -> Self {
        self.location = Some(location.into());
        self
    }
}

/// Mode for soft assertions behavior
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum AssertionMode {
    /// Collect all failures (default)
    #[default]
    Collect,
    /// Stop on first failure (like hard assertions)
    FailFast,
}

/// Soft assertions collector
///
/// Collects multiple assertion failures without stopping test execution.
///
/// ## Example
///
/// ```ignore
/// let mut soft = SoftAssertions::new();
/// soft.assert_eq(&1, &2, "values should match");
/// soft.assert_true(false, "condition should be true");
/// // Both failures are collected
/// let result = soft.verify();
/// assert!(result.is_err());
/// ```
#[derive(Debug, Default)]
pub struct SoftAssertions {
    failures: Vec<AssertionFailure>,
    mode: AssertionMode,
    assertion_count: usize,
}

impl SoftAssertions {
    /// Create a new soft assertions collector
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create with a specific mode
    #[must_use]
    pub fn with_mode(mode: AssertionMode) -> Self {
        Self {
            mode,
            ..Self::default()
        }
    }

    /// Set the assertion mode
    #[must_use]
    pub const fn mode(mut self, mode: AssertionMode) -> Self {
        self.mode = mode;
        self
    }

    /// Assert two values are equal
    pub fn assert_eq<T: PartialEq + Debug>(&mut self, actual: &T, expected: &T, message: &str) {
        contract_pre_soft_assertion_collection!();
        self.assertion_count += 1;
        if actual != expected {
            let failure_msg = format!("{message}: expected {expected:?}, got {actual:?}");
            self.record_failure(failure_msg);
        }
    }

    /// Assert two values are not equal
    pub fn assert_ne<T: PartialEq + Debug>(&mut self, actual: &T, expected: &T, message: &str) {
        self.assertion_count += 1;
        if actual == expected {
            let failure_msg = format!("{message}: expected values to differ, both were {actual:?}");
            self.record_failure(failure_msg);
        }
    }

    /// Assert a condition is true
    pub fn assert_true(&mut self, condition: bool, message: &str) {
        self.assertion_count += 1;
        if !condition {
            self.record_failure(format!("{message}: expected true, got false"));
        }
    }

    /// Assert a condition is false
    pub fn assert_false(&mut self, condition: bool, message: &str) {
        self.assertion_count += 1;
        if condition {
            self.record_failure(format!("{message}: expected false, got true"));
        }
    }

    /// Assert a value is Some
    pub fn assert_some<T>(&mut self, opt: &Option<T>, message: &str) {
        self.assertion_count += 1;
        if opt.is_none() {
            self.record_failure(format!("{message}: expected Some, got None"));
        }
    }

    /// Assert a value is None
    pub fn assert_none<T>(&mut self, opt: &Option<T>, message: &str) {
        self.assertion_count += 1;
        if opt.is_some() {
            self.record_failure(format!("{message}: expected None, got Some"));
        }
    }

    /// Assert a Result is Ok
    pub fn assert_ok<T, E>(&mut self, result: &Result<T, E>, message: &str) {
        self.assertion_count += 1;
        if result.is_err() {
            self.record_failure(format!("{message}: expected Ok, got Err"));
        }
    }

    /// Assert a Result is Err
    pub fn assert_err<T, E>(&mut self, result: &Result<T, E>, message: &str) {
        self.assertion_count += 1;
        if result.is_ok() {
            self.record_failure(format!("{message}: expected Err, got Ok"));
        }
    }

    /// Assert a string contains a substring
    pub fn assert_contains(&mut self, haystack: &str, needle: &str, message: &str) {
        self.assertion_count += 1;
        if !haystack.contains(needle) {
            self.record_failure(format!(
                "{message}: expected '{haystack}' to contain '{needle}'"
            ));
        }
    }

    /// Assert a collection has expected length
    pub fn assert_len<T>(&mut self, collection: &[T], expected: usize, message: &str) {
        self.assertion_count += 1;
        if collection.len() != expected {
            self.record_failure(format!(
                "{message}: expected length {expected}, got {}",
                collection.len()
            ));
        }
    }

    /// Assert a collection is empty
    pub fn assert_empty<T>(&mut self, collection: &[T], message: &str) {
        self.assertion_count += 1;
        if !collection.is_empty() {
            self.record_failure(format!(
                "{message}: expected empty collection, got {} elements",
                collection.len()
            ));
        }
    }

    /// Assert a collection is not empty
    pub fn assert_not_empty<T>(&mut self, collection: &[T], message: &str) {
        self.assertion_count += 1;
        if collection.is_empty() {
            self.record_failure(format!("{message}: expected non-empty collection"));
        }
    }

    /// Assert two floats are approximately equal
    pub fn assert_approx_eq(&mut self, actual: f64, expected: f64, epsilon: f64, message: &str) {
        self.assertion_count += 1;
        if (actual - expected).abs() >= epsilon {
            self.record_failure(format!(
                "{message}: expected {actual} ≈ {expected} (epsilon: {epsilon})"
            ));
        }
    }

    /// Assert a value is in a range
    pub fn assert_in_range(&mut self, value: f64, min: f64, max: f64, message: &str) {
        self.assertion_count += 1;
        if value < min || value > max {
            self.record_failure(format!(
                "{message}: expected {value} to be in range [{min}, {max}]"
            ));
        }
    }

    /// Record a custom failure
    pub fn fail(&mut self, message: impl Into<String>) {
        self.assertion_count += 1;
        self.record_failure(message.into());
    }

    /// Record a failure with location info
    fn record_failure(&mut self, message: String) {
        let failure = AssertionFailure::new(message, self.failures.len());
        self.failures.push(failure);
    }

    /// Get all failures
    #[must_use]
    pub fn failures(&self) -> &[AssertionFailure] {
        &self.failures
    }

    /// Get the number of failures
    #[must_use]
    pub fn failure_count(&self) -> usize {
        self.failures.len()
    }

    /// Get the total number of assertions checked
    #[must_use]
    pub const fn assertion_count(&self) -> usize {
        self.assertion_count
    }

    /// Check if all assertions passed
    #[must_use]
    pub fn all_passed(&self) -> bool {
        self.failures.is_empty()
    }

    /// Verify all assertions passed, returning error if any failed
    ///
    /// # Errors
    ///
    /// Returns error containing all failure messages if any assertions failed
    pub fn verify(&self) -> Result<(), SoftAssertionError> {
        contract_pre_soft_assertion_collection!();
        if self.failures.is_empty() {
            Ok(())
        } else {
            Err(SoftAssertionError::new(&self.failures))
        }
    }

    /// Clear all recorded failures
    pub fn clear(&mut self) {
        self.failures.clear();
        self.assertion_count = 0;
    }

    /// Get a summary of the assertions
    #[must_use]
    pub fn summary(&self) -> AssertionSummary {
        AssertionSummary {
            total: self.assertion_count,
            passed: self.assertion_count - self.failures.len(),
            failed: self.failures.len(),
        }
    }
}

/// Summary of assertion results
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct AssertionSummary {
    /// Total assertions checked
    pub total: usize,
    /// Assertions that passed
    pub passed: usize,
    /// Assertions that failed
    pub failed: usize,
}

/// Error type for soft assertion failures
#[derive(Debug, Clone)]
pub struct SoftAssertionError {
    /// All failure messages
    pub failures: Vec<String>,
    /// Number of failed assertions
    pub count: usize,
}

impl SoftAssertionError {
    /// Create a new error from failures
    #[must_use]
    pub fn new(failures: &[AssertionFailure]) -> Self {
        Self {
            failures: failures.iter().map(|f| f.message.clone()).collect(),
            count: failures.len(),
        }
    }
}

impl std::fmt::Display for SoftAssertionError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "{} assertion(s) failed:", self.count)?;
        for (i, failure) in self.failures.iter().enumerate() {
            writeln!(f, "  {}. {failure}", i + 1)?;
        }
        Ok(())
    }
}

impl std::error::Error for SoftAssertionError {}

// ============================================================================
// EXTREME TDD: Tests written FIRST per spec
// ============================================================================

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    mod soft_assertions_basic {
        use super::*;

        #[test]
        fn test_new_creates_empty() {
            let soft = SoftAssertions::new();
            assert!(soft.all_passed());
            assert_eq!(soft.failure_count(), 0);
            assert_eq!(soft.assertion_count(), 0);
        }

        #[test]
        fn test_with_mode() {
            let soft = SoftAssertions::with_mode(AssertionMode::FailFast);
            assert_eq!(soft.mode, AssertionMode::FailFast);
        }

        #[test]
        fn test_mode_builder() {
            let soft = SoftAssertions::new().mode(AssertionMode::Collect);
            assert_eq!(soft.mode, AssertionMode::Collect);
        }
    }

    mod equality_assertions {
        use super::*;

        #[test]
        fn test_assert_eq_pass() {
            let mut soft = SoftAssertions::new();
            soft.assert_eq(&42, &42, "values should match");
            assert!(soft.all_passed());
            assert_eq!(soft.assertion_count(), 1);
        }

        #[test]
        fn test_assert_eq_fail() {
            let mut soft = SoftAssertions::new();
            soft.assert_eq(&1, &2, "values should match");
            assert!(!soft.all_passed());
            assert_eq!(soft.failure_count(), 1);
            assert!(soft.failures()[0].message.contains("expected"));
        }

        #[test]
        fn test_assert_ne_pass() {
            let mut soft = SoftAssertions::new();
            soft.assert_ne(&1, &2, "values should differ");
            assert!(soft.all_passed());
        }

        #[test]
        fn test_assert_ne_fail() {
            let mut soft = SoftAssertions::new();
            soft.assert_ne(&42, &42, "values should differ");
            assert!(!soft.all_passed());
        }
    }

    mod boolean_assertions {
        use super::*;

        #[test]
        fn test_assert_true_pass() {
            let mut soft = SoftAssertions::new();
            soft.assert_true(true, "should be true");
            assert!(soft.all_passed());
        }

        #[test]
        fn test_assert_true_fail() {
            let mut soft = SoftAssertions::new();
            soft.assert_true(false, "should be true");
            assert!(!soft.all_passed());
            assert!(soft.failures()[0].message.contains("expected true"));
        }

        #[test]
        fn test_assert_false_pass() {
            let mut soft = SoftAssertions::new();
            soft.assert_false(false, "should be false");
            assert!(soft.all_passed());
        }

        #[test]
        fn test_assert_false_fail() {
            let mut soft = SoftAssertions::new();
            soft.assert_false(true, "should be false");
            assert!(!soft.all_passed());
        }
    }

    mod option_assertions {
        use super::*;

        #[test]
        fn test_assert_some_pass() {
            let mut soft = SoftAssertions::new();
            soft.assert_some(&Some(42), "should be Some");
            assert!(soft.all_passed());
        }

        #[test]
        fn test_assert_some_fail() {
            let mut soft = SoftAssertions::new();
            soft.assert_some::<i32>(&None, "should be Some");
            assert!(!soft.all_passed());
        }

        #[test]
        fn test_assert_none_pass() {
            let mut soft = SoftAssertions::new();
            soft.assert_none::<i32>(&None, "should be None");
            assert!(soft.all_passed());
        }

        #[test]
        fn test_assert_none_fail() {
            let mut soft = SoftAssertions::new();
            soft.assert_none(&Some(42), "should be None");
            assert!(!soft.all_passed());
        }
    }

    mod result_assertions {
        use super::*;

        #[test]
        fn test_assert_ok_pass() {
            let mut soft = SoftAssertions::new();
            let result: Result<i32, &str> = Ok(42);
            soft.assert_ok(&result, "should be Ok");
            assert!(soft.all_passed());
        }

        #[test]
        fn test_assert_ok_fail() {
            let mut soft = SoftAssertions::new();
            let result: Result<i32, &str> = Err("error");
            soft.assert_ok(&result, "should be Ok");
            assert!(!soft.all_passed());
        }

        #[test]
        fn test_assert_err_pass() {
            let mut soft = SoftAssertions::new();
            let result: Result<i32, &str> = Err("error");
            soft.assert_err(&result, "should be Err");
            assert!(soft.all_passed());
        }

        #[test]
        fn test_assert_err_fail() {
            let mut soft = SoftAssertions::new();
            let result: Result<i32, &str> = Ok(42);
            soft.assert_err(&result, "should be Err");
            assert!(!soft.all_passed());
        }
    }

    mod string_assertions {
        use super::*;

        #[test]
        fn test_assert_contains_pass() {
            let mut soft = SoftAssertions::new();
            soft.assert_contains("hello world", "world", "should contain");
            assert!(soft.all_passed());
        }

        #[test]
        fn test_assert_contains_fail() {
            let mut soft = SoftAssertions::new();
            soft.assert_contains("hello", "world", "should contain");
            assert!(!soft.all_passed());
        }
    }

    mod collection_assertions {
        use super::*;

        #[test]
        fn test_assert_len_pass() {
            let mut soft = SoftAssertions::new();
            soft.assert_len(&[1, 2, 3], 3, "should have length 3");
            assert!(soft.all_passed());
        }

        #[test]
        fn test_assert_len_fail() {
            let mut soft = SoftAssertions::new();
            soft.assert_len(&[1, 2], 3, "should have length 3");
            assert!(!soft.all_passed());
        }

        #[test]
        fn test_assert_empty_pass() {
            let mut soft = SoftAssertions::new();
            let empty: Vec<i32> = vec![];
            soft.assert_empty(&empty, "should be empty");
            assert!(soft.all_passed());
        }

        #[test]
        fn test_assert_empty_fail() {
            let mut soft = SoftAssertions::new();
            soft.assert_empty(&[1], "should be empty");
            assert!(!soft.all_passed());
        }

        #[test]
        fn test_assert_not_empty_pass() {
            let mut soft = SoftAssertions::new();
            soft.assert_not_empty(&[1], "should not be empty");
            assert!(soft.all_passed());
        }

        #[test]
        fn test_assert_not_empty_fail() {
            let mut soft = SoftAssertions::new();
            let empty: Vec<i32> = vec![];
            soft.assert_not_empty(&empty, "should not be empty");
            assert!(!soft.all_passed());
        }
    }

    mod numeric_assertions {
        use super::*;

        #[test]
        fn test_assert_approx_eq_pass() {
            let mut soft = SoftAssertions::new();
            soft.assert_approx_eq(1.001, 1.0, 0.01, "should be approximately equal");
            assert!(soft.all_passed());
        }

        #[test]
        fn test_assert_approx_eq_fail() {
            let mut soft = SoftAssertions::new();
            soft.assert_approx_eq(1.5, 1.0, 0.01, "should be approximately equal");
            assert!(!soft.all_passed());
        }

        #[test]
        fn test_assert_in_range_pass() {
            let mut soft = SoftAssertions::new();
            soft.assert_in_range(5.0, 0.0, 10.0, "should be in range");
            assert!(soft.all_passed());
        }

        #[test]
        fn test_assert_in_range_fail() {
            let mut soft = SoftAssertions::new();
            soft.assert_in_range(15.0, 0.0, 10.0, "should be in range");
            assert!(!soft.all_passed());
        }

        #[test]
        fn test_assert_in_range_boundaries() {
            let mut soft = SoftAssertions::new();
            soft.assert_in_range(0.0, 0.0, 10.0, "min boundary");
            soft.assert_in_range(10.0, 0.0, 10.0, "max boundary");
            assert!(soft.all_passed());
        }
    }

    mod multiple_failures {
        use super::*;

        #[test]
        fn test_collects_multiple_failures() {
            let mut soft = SoftAssertions::new();
            soft.assert_eq(&1, &2, "first check");
            soft.assert_true(false, "second check");
            soft.assert_contains("hello", "world", "third check");

            assert_eq!(soft.failure_count(), 3);
            assert_eq!(soft.assertion_count(), 3);
        }

        #[test]
        fn test_mixed_pass_and_fail() {
            let mut soft = SoftAssertions::new();
            soft.assert_eq(&1, &1, "pass");
            soft.assert_eq(&1, &2, "fail");
            soft.assert_true(true, "pass");
            soft.assert_true(false, "fail");

            assert_eq!(soft.failure_count(), 2);
            assert_eq!(soft.assertion_count(), 4);
            assert_eq!(soft.summary().passed, 2);
        }
    }

    mod verify {
        use super::*;

        #[test]
        fn test_verify_pass() {
            let mut soft = SoftAssertions::new();
            soft.assert_eq(&1, &1, "match");
            assert!(soft.verify().is_ok());
        }

        #[test]
        fn test_verify_fail() {
            let mut soft = SoftAssertions::new();
            soft.assert_eq(&1, &2, "mismatch");
            let err = soft.verify().unwrap_err();
            assert_eq!(err.count, 1);
            assert!(!err.failures.is_empty());
        }

        #[test]
        fn test_error_display() {
            let mut soft = SoftAssertions::new();
            soft.assert_eq(&1, &2, "first");
            soft.assert_true(false, "second");
            let err = soft.verify().unwrap_err();
            let display = format!("{err}");
            assert!(display.contains("2 assertion(s) failed"));
            assert!(display.contains("first"));
            assert!(display.contains("second"));
        }
    }

    mod summary {
        use super::*;

        #[test]
        fn test_summary() {
            let mut soft = SoftAssertions::new();
            soft.assert_eq(&1, &1, "pass");
            soft.assert_eq(&1, &2, "fail");
            soft.assert_true(true, "pass");

            let summary = soft.summary();
            assert_eq!(summary.total, 3);
            assert_eq!(summary.passed, 2);
            assert_eq!(summary.failed, 1);
        }
    }

    mod clear {
        use super::*;

        #[test]
        fn test_clear() {
            let mut soft = SoftAssertions::new();
            soft.assert_eq(&1, &2, "fail");
            assert_eq!(soft.failure_count(), 1);

            soft.clear();
            assert_eq!(soft.failure_count(), 0);
            assert_eq!(soft.assertion_count(), 0);
            assert!(soft.all_passed());
        }
    }

    mod custom_failure {
        use super::*;

        #[test]
        fn test_fail_method() {
            let mut soft = SoftAssertions::new();
            soft.fail("custom failure message");
            assert!(!soft.all_passed());
            assert_eq!(soft.failures()[0].message, "custom failure message");
        }
    }

    mod assertion_failure {
        use super::*;

        #[test]
        fn test_assertion_failure_new() {
            let failure = AssertionFailure::new("test message", 0);
            assert_eq!(failure.message, "test message");
            assert_eq!(failure.index, 0);
            assert!(failure.timestamp.is_some());
            assert!(failure.location.is_none());
        }

        #[test]
        fn test_assertion_failure_with_location() {
            let failure = AssertionFailure::new("test", 0).with_location("test.rs:42");
            assert_eq!(failure.location, Some("test.rs:42".to_string()));
        }
    }
}