protest 1.1.0

An ergonomic, powerful, and feature-rich property testing library with minimal boilerplate.
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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
//! Error types and result handling for property-based testing.

use std::fmt;
use std::marker::PhantomData;
use std::time::Duration;

use crate::config::{GenerationStats, TestConfig};

/// Comprehensive error type for property testing failures
#[derive(Debug, Clone)]
pub enum PropertyError {
    /// Property test failed with a specific message and optional context
    PropertyFailed {
        message: String,
        context: Option<String>,
        iteration: Option<usize>,
    },

    /// Generation of test data failed
    GenerationFailed {
        message: String,
        context: Option<String>,
    },

    /// Shrinkage process timed out
    ShrinkageTimeout {
        iterations: usize,
        last_successful_shrink: Option<String>,
    },

    /// Configuration error
    ConfigError {
        message: String,
        field: Option<String>,
    },

    /// Test execution was cancelled
    TestCancelled { reason: String },

    /// Internal error in the testing framework
    InternalError {
        message: String,
        source_message: Option<String>,
    },
}

impl fmt::Display for PropertyError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PropertyError::PropertyFailed {
                message,
                context,
                iteration,
            } => {
                write!(f, "Property failed: {}", message)?;
                if let Some(ctx) = context {
                    write!(f, " (context: {})", ctx)?;
                }
                if let Some(iter) = iteration {
                    write!(f, " (iteration: {})", iter)?;
                }
                Ok(())
            }
            PropertyError::GenerationFailed { message, context } => {
                write!(f, "Generation failed: {}", message)?;
                if let Some(ctx) = context {
                    write!(f, " (context: {})", ctx)?;
                }
                Ok(())
            }
            PropertyError::ShrinkageTimeout {
                iterations,
                last_successful_shrink,
            } => {
                write!(f, "Shrinkage timeout after {} iterations", iterations)?;
                if let Some(shrink) = last_successful_shrink {
                    write!(f, " (last successful shrink: {})", shrink)?;
                }
                Ok(())
            }
            PropertyError::ConfigError { message, field } => {
                write!(f, "Configuration error: {}", message)?;
                if let Some(field_name) = field {
                    write!(f, " (field: {})", field_name)?;
                }
                Ok(())
            }
            PropertyError::TestCancelled { reason } => {
                write!(f, "Test cancelled: {}", reason)
            }
            PropertyError::InternalError {
                message,
                source_message,
            } => {
                write!(f, "Internal error: {}", message)?;
                if let Some(src) = source_message {
                    write!(f, " (source: {})", src)?;
                }
                Ok(())
            }
        }
    }
}

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

/// Result of a property test execution
pub type PropertyResult<T> = Result<TestSuccess<T>, TestFailure<T>>;

/// Helper functions for creating PropertyError instances with context
impl PropertyError {
    /// Create a simple property failed error
    pub fn property_failed(message: impl Into<String>) -> Self {
        Self::PropertyFailed {
            message: message.into(),
            context: None,
            iteration: None,
        }
    }

    /// Create a property failed error with context
    pub fn property_failed_with_context(
        message: impl Into<String>,
        context: Option<impl Into<String>>,
        iteration: Option<usize>,
    ) -> Self {
        Self::PropertyFailed {
            message: message.into(),
            context: context.map(|c| c.into()),
            iteration,
        }
    }

    /// Create a generation failed error with context
    pub fn generation_failed_with_context(
        message: impl Into<String>,
        context: Option<impl Into<String>>,
    ) -> Self {
        Self::GenerationFailed {
            message: message.into(),
            context: context.map(|c| c.into()),
        }
    }

    /// Create a configuration error with field information
    pub fn config_error_with_field(
        message: impl Into<String>,
        field: Option<impl Into<String>>,
    ) -> Self {
        Self::ConfigError {
            message: message.into(),
            field: field.map(|f| f.into()),
        }
    }

    /// Create a shrinkage timeout error with last successful shrink
    pub fn shrinkage_timeout_with_context(
        iterations: usize,
        last_successful_shrink: Option<impl Into<String>>,
    ) -> Self {
        Self::ShrinkageTimeout {
            iterations,
            last_successful_shrink: last_successful_shrink.map(|s| s.into()),
        }
    }

    /// Create an internal error
    pub fn internal_error(
        message: impl Into<String>,
        source_message: Option<impl Into<String>>,
    ) -> Self {
        Self::InternalError {
            message: message.into(),
            source_message: source_message.map(|s| s.into()),
        }
    }

    /// Create a test cancelled error
    pub fn test_cancelled(reason: impl Into<String>) -> Self {
        Self::TestCancelled {
            reason: reason.into(),
        }
    }

    /// Create an execution failed error (convenience method)
    pub fn execution_failed(message: impl Into<String>) -> Self {
        Self::InternalError {
            message: message.into(),
            source_message: None,
        }
    }
}

/// Information about a successful test run
#[derive(Debug)]
pub struct TestSuccess<T> {
    /// Number of iterations completed
    pub iterations: usize,
    /// Test configuration used
    pub config: TestConfig,
    /// Optional statistics about generated values
    pub stats: Option<GenerationStats>,
    _phantom: PhantomData<T>,
}

impl<T> TestSuccess<T> {
    /// Create a new TestSuccess instance
    pub fn new(iterations: usize, config: TestConfig, stats: Option<GenerationStats>) -> Self {
        Self {
            iterations,
            config,
            stats,
            _phantom: PhantomData,
        }
    }
}

/// Information about a failed test run
#[derive(Debug)]
pub struct TestFailure<T> {
    /// The error that caused the failure
    pub error: PropertyError,
    /// Original input that caused the failure
    pub original_input: T,
    /// Shrunk input (if shrinking was successful)
    pub shrunk_input: Option<T>,
    /// Number of shrinking steps performed
    pub shrink_steps: usize,
    /// Test configuration used
    pub config: TestConfig,
    /// Iteration number where the failure occurred
    pub failed_iteration: usize,
    /// Total time spent on the test
    pub test_duration: std::time::Duration,
    /// Time spent on shrinking
    pub shrink_duration: std::time::Duration,
}

impl<T> TestFailure<T> {
    /// Create a new TestFailure instance
    pub fn new(
        error: PropertyError,
        original_input: T,
        shrunk_input: Option<T>,
        shrink_steps: usize,
        config: TestConfig,
        failed_iteration: usize,
        test_duration: std::time::Duration,
        shrink_duration: std::time::Duration,
    ) -> Self {
        Self {
            error,
            original_input,
            shrunk_input,
            shrink_steps,
            config,
            failed_iteration,
            test_duration,
            shrink_duration,
        }
    }

    /// Get a detailed report of the test failure
    pub fn detailed_report(&self) -> String
    where
        T: fmt::Debug,
    {
        let mut report = String::new();

        report.push_str(&format!(
            "Property test failed on iteration {}\n",
            self.failed_iteration
        ));
        report.push_str(&format!("Error: {}\n", self.error));
        report.push_str(&format!("Original input: {:?}\n", self.original_input));

        if let Some(ref shrunk) = self.shrunk_input {
            report.push_str(&format!("Shrunk input: {:?}\n", shrunk));
            report.push_str(&format!("Shrinking steps: {}\n", self.shrink_steps));
            report.push_str(&format!("Shrinking time: {:?}\n", self.shrink_duration));
        } else {
            report.push_str("No shrinking performed\n");
        }

        report.push_str(&format!("Total test time: {:?}\n", self.test_duration));
        report.push_str(&format!(
            "Test configuration: iterations={}, seed={:?}\n",
            self.config.iterations, self.config.seed
        ));

        report
    }

    /// Get a concise summary of the test failure
    pub fn summary(&self) -> String
    where
        T: fmt::Debug,
    {
        if let Some(ref shrunk) = self.shrunk_input {
            format!(
                "Property failed with input {:?} (shrunk from {:?}) on iteration {}",
                shrunk, self.original_input, self.failed_iteration
            )
        } else {
            format!(
                "Property failed with input {:?} on iteration {}",
                self.original_input, self.failed_iteration
            )
        }
    }
}

impl<T: fmt::Debug> fmt::Display for TestFailure<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.summary())
    }
}

/// Comprehensive error reporter with various output modes
pub struct ErrorReporter {
    pub verbose: bool,
    pub show_shrink_progress: bool,
    pub show_timing: bool,
    pub show_config: bool,
}

impl ErrorReporter {
    /// Create a new error reporter with default settings
    pub fn new() -> Self {
        Self {
            verbose: false,
            show_shrink_progress: false,
            show_timing: true,
            show_config: false,
        }
    }

    /// Enable verbose output mode
    pub fn verbose(mut self) -> Self {
        self.verbose = true;
        self
    }

    /// Enable shrinkage progress visualization
    pub fn show_shrink_progress(mut self) -> Self {
        self.show_shrink_progress = true;
        self
    }

    /// Enable timing information display
    pub fn show_timing(mut self) -> Self {
        self.show_timing = true;
        self
    }

    /// Enable configuration display
    pub fn show_config(mut self) -> Self {
        self.show_config = true;
        self
    }

    /// Generate a comprehensive error report
    pub fn format_failure<T>(&self, failure: &TestFailure<T>) -> String
    where
        T: fmt::Debug,
    {
        let mut report = String::new();

        // Header
        report.push_str("═══════════════════════════════════════════════════════════════\n");
        report.push_str("                    PROPERTY TEST FAILURE                     \n");
        report.push_str("═══════════════════════════════════════════════════════════════\n\n");

        // Basic failure information
        report.push_str(&format!(
            "❌ Test failed on iteration {}\n",
            failure.failed_iteration
        ));
        report.push_str(&format!("📝 Error: {}\n\n", failure.error));

        // Input information
        report.push_str("📊 INPUT INFORMATION:\n");
        report.push_str(&format!(
            "   Original input: {:?}\n",
            failure.original_input
        ));

        if let Some(ref shrunk) = failure.shrunk_input {
            report.push_str(&format!("   Shrunk input:   {:?}\n", shrunk));
            report.push_str(&format!("   Shrink steps:   {}\n", failure.shrink_steps));

            if self.show_shrink_progress {
                report.push_str(&self.format_shrink_progress(failure));
            }
        } else {
            report.push_str("   No shrinking performed\n");
        }
        report.push('\n');

        // Timing information
        if self.show_timing {
            report.push_str("⏱️  TIMING INFORMATION:\n");
            report.push_str(&format!(
                "   Total test time:  {:?}\n",
                failure.test_duration
            ));
            report.push_str(&format!(
                "   Shrinking time:   {:?}\n",
                failure.shrink_duration
            ));
            if failure.shrink_steps > 0 {
                let avg_shrink_time =
                    failure.shrink_duration.as_nanos() / failure.shrink_steps as u128;
                report.push_str(&format!(
                    "   Avg shrink time:  {:?}\n",
                    Duration::from_nanos(avg_shrink_time as u64)
                ));
            }
            report.push('\n');
        }

        // Configuration information
        if self.show_config {
            report.push_str("⚙️  CONFIGURATION:\n");
            report.push_str(&format!(
                "   Iterations:       {}\n",
                failure.config.iterations
            ));
            report.push_str(&format!("   Seed:             {:?}\n", failure.config.seed));
            report.push_str(&format!(
                "   Max shrink iter:  {}\n",
                failure.config.max_shrink_iterations
            ));
            report.push_str(&format!(
                "   Shrink timeout:   {:?}\n",
                failure.config.shrink_timeout
            ));
            report.push('\n');
        }

        // Verbose error context
        if self.verbose {
            report.push_str("🔍 DETAILED ERROR CONTEXT:\n");
            report.push_str(&self.format_error_context(&failure.error));
            report.push('\n');
        }

        // Suggestions
        report.push_str("💡 SUGGESTIONS:\n");
        report.push_str(&self.generate_suggestions(failure));

        report.push_str("═══════════════════════════════════════════════════════════════\n");

        report
    }

    /// Format shrinkage progress visualization
    fn format_shrink_progress<T>(&self, failure: &TestFailure<T>) -> String
    where
        T: fmt::Debug,
    {
        let mut progress = String::new();

        if failure.shrink_steps > 0 {
            progress.push_str("   Shrink progress:\n");

            // Create a simple progress visualization
            let max_width = 50;
            let progress_width = if failure.shrink_steps > max_width {
                max_width
            } else {
                failure.shrink_steps
            };

            progress.push_str("   ");
            for i in 0..progress_width {
                if i < progress_width - 1 {
                    progress.push('');
                } else {
                    progress.push('');
                }
            }

            if failure.shrink_steps > max_width {
                progress.push_str(&format!(" ({} steps)", failure.shrink_steps));
            }
            progress.push('\n');

            // Show shrinking efficiency
            let efficiency = if failure.shrink_duration.as_millis() > 0 {
                failure.shrink_steps as f64 / failure.shrink_duration.as_millis() as f64 * 1000.0
            } else {
                0.0
            };
            progress.push_str(&format!("   Shrink rate:    {:.1} steps/sec\n", efficiency));
        }

        progress
    }

    /// Format detailed error context
    pub fn format_error_context(&self, error: &PropertyError) -> String {
        let mut context = String::new();

        match error {
            PropertyError::PropertyFailed {
                message,
                context: ctx,
                iteration,
            } => {
                context.push_str("   Type: Property assertion failure\n");
                context.push_str(&format!("   Message: {}\n", message));
                if let Some(ctx) = ctx {
                    context.push_str(&format!("   Context: {}\n", ctx));
                }
                if let Some(iter) = iteration {
                    context.push_str(&format!("   Failed at iteration: {}\n", iter));
                }
            }
            PropertyError::GenerationFailed {
                message,
                context: ctx,
            } => {
                context.push_str("   Type: Test data generation failure\n");
                context.push_str(&format!("   Message: {}\n", message));
                if let Some(ctx) = ctx {
                    context.push_str(&format!("   Context: {}\n", ctx));
                }
            }
            PropertyError::ShrinkageTimeout {
                iterations,
                last_successful_shrink,
            } => {
                context.push_str("   Type: Shrinkage process timeout\n");
                context.push_str(&format!("   Iterations attempted: {}\n", iterations));
                if let Some(shrink) = last_successful_shrink {
                    context.push_str(&format!("   Last successful shrink: {}\n", shrink));
                }
            }
            PropertyError::ConfigError { message, field } => {
                context.push_str("   Type: Configuration error\n");
                context.push_str(&format!("   Message: {}\n", message));
                if let Some(field) = field {
                    context.push_str(&format!("   Field: {}\n", field));
                }
            }
            PropertyError::TestCancelled { reason } => {
                context.push_str("   Type: Test cancellation\n");
                context.push_str(&format!("   Reason: {}\n", reason));
            }
            PropertyError::InternalError {
                message,
                source_message,
            } => {
                context.push_str("   Type: Internal framework error\n");
                context.push_str(&format!("   Message: {}\n", message));
                if let Some(source) = source_message {
                    context.push_str(&format!("   Source: {}\n", source));
                }
            }
        }

        context
    }

    /// Generate helpful suggestions based on the failure
    pub fn generate_suggestions<T>(&self, failure: &TestFailure<T>) -> String
    where
        T: fmt::Debug,
    {
        let mut suggestions = String::new();

        match &failure.error {
            PropertyError::PropertyFailed { .. } => {
                suggestions.push_str("   • Check if your property logic is correct\n");
                suggestions.push_str("   • Verify that the failing input reveals a real bug\n");
                if failure.shrunk_input.is_some() {
                    suggestions.push_str("   • Focus on the shrunk input for easier debugging\n");
                } else {
                    suggestions
                        .push_str("   • Consider implementing shrinking for your data type\n");
                }
            }
            PropertyError::GenerationFailed { .. } => {
                suggestions.push_str("   • Check your generator implementation for panics\n");
                suggestions.push_str("   • Verify generator constraints are satisfiable\n");
                suggestions.push_str("   • Consider adding bounds checking to your generator\n");
            }
            PropertyError::ShrinkageTimeout { .. } => {
                suggestions.push_str("   • Increase shrink timeout if needed\n");
                suggestions.push_str("   • Optimize your shrinking strategy\n");
                suggestions.push_str("   • Consider reducing max shrink iterations\n");
            }
            PropertyError::ConfigError { .. } => {
                suggestions.push_str("   • Review your test configuration parameters\n");
                suggestions.push_str("   • Check for valid ranges and constraints\n");
            }
            PropertyError::TestCancelled { .. } => {
                suggestions.push_str("   • Check if cancellation was intentional\n");
                suggestions.push_str("   • Review timeout settings if applicable\n");
            }
            PropertyError::InternalError { .. } => {
                suggestions.push_str("   • This may be a bug in the testing framework\n");
                suggestions.push_str("   • Consider reporting this issue\n");
                suggestions.push_str("   • Try simplifying your test case\n");
            }
        }

        // General suggestions
        suggestions.push_str("   • Run with a fixed seed for reproducible results\n");
        suggestions.push_str("   • Try increasing iterations to find edge cases\n");

        suggestions
    }

    /// Generate a concise summary for quick debugging
    pub fn format_summary<T>(&self, failure: &TestFailure<T>) -> String
    where
        T: fmt::Debug,
    {
        let mut summary = String::new();

        summary.push_str("🚨 QUICK SUMMARY:\n");
        summary.push_str(&format!("   {}\n", failure.summary()));

        if let Some(ref shrunk) = failure.shrunk_input {
            summary.push_str(&format!("   Focus on input: {:?}\n", shrunk));
        } else {
            summary.push_str(&format!(
                "   Focus on input: {:?}\n",
                failure.original_input
            ));
        }

        summary.push_str(&format!("   Error: {}\n", failure.error));

        summary
    }

    /// Format error for integration with standard test output
    pub fn format_for_test_output<T>(&self, failure: &TestFailure<T>) -> String
    where
        T: fmt::Debug,
    {
        if self.verbose {
            self.format_failure(failure)
        } else {
            self.format_summary(failure)
        }
    }
}

impl Default for ErrorReporter {
    fn default() -> Self {
        Self::new()
    }
}

/// Shrinkage progress tracker for visualization
#[derive(Debug, Clone)]
pub struct ShrinkProgress {
    pub steps: Vec<ShrinkStep>,
    pub total_time: Duration,
    pub completed: bool,
}

/// Individual shrinking step information
#[derive(Debug, Clone)]
pub struct ShrinkStep {
    pub step_number: usize,
    pub input_description: String,
    pub step_time: Duration,
    pub successful: bool,
}

impl ShrinkProgress {
    /// Create a new shrink progress tracker
    pub fn new() -> Self {
        Self {
            steps: Vec::new(),
            total_time: Duration::from_secs(0),
            completed: false,
        }
    }

    /// Add a shrinking step
    pub fn add_step(&mut self, step: ShrinkStep) {
        self.steps.push(step);
    }

    /// Mark shrinking as completed
    pub fn complete(&mut self, total_time: Duration) {
        self.total_time = total_time;
        self.completed = true;
    }

    /// Get a visualization of the shrinking progress
    pub fn visualize(&self) -> String {
        let mut viz = String::new();

        viz.push_str("Shrinking Progress:\n");
        for step in &self.steps {
            let status = if step.successful { "" } else { "" };
            viz.push_str(&format!(
                "  {} Step {}: {} ({:?})\n",
                status, step.step_number, step.input_description, step.step_time
            ));
        }

        if self.completed {
            viz.push_str(&format!(
                "Completed in {:?} ({} steps)\n",
                self.total_time,
                self.steps.len()
            ));
        } else {
            viz.push_str("In progress...\n");
        }

        viz
    }

    /// Get shrinking statistics
    pub fn statistics(&self) -> ShrinkStatistics {
        let successful_steps = self.steps.iter().filter(|s| s.successful).count();
        let total_steps = self.steps.len();
        let avg_step_time = if total_steps > 0 {
            self.total_time / total_steps as u32
        } else {
            Duration::from_secs(0)
        };

        ShrinkStatistics {
            total_steps,
            successful_steps,
            total_time: self.total_time,
            average_step_time: avg_step_time,
            success_rate: if total_steps > 0 {
                successful_steps as f64 / total_steps as f64
            } else {
                0.0
            },
        }
    }
}

impl Default for ShrinkProgress {
    fn default() -> Self {
        Self::new()
    }
}

/// Statistics about shrinking performance
#[derive(Debug, Clone)]
pub struct ShrinkStatistics {
    pub total_steps: usize,
    pub successful_steps: usize,
    pub total_time: Duration,
    pub average_step_time: Duration,
    pub success_rate: f64,
}

impl fmt::Display for ShrinkStatistics {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Shrink Stats: {}/{} steps successful ({:.1}%), avg time: {:?}",
            self.successful_steps,
            self.total_steps,
            self.success_rate * 100.0,
            self.average_step_time
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::error::Error;
    use std::time::Duration;

    #[test]
    fn test_property_error_display_with_context() {
        let error = PropertyError::PropertyFailed {
            message: "test failed".to_string(),
            context: Some("during validation".to_string()),
            iteration: Some(42),
        };
        let display = format!("{}", error);
        assert!(display.contains("Property failed: test failed"));
        assert!(display.contains("context: during validation"));
        assert!(display.contains("iteration: 42"));
    }

    #[test]
    fn test_property_error_display_without_context() {
        let error = PropertyError::PropertyFailed {
            message: "test failed".to_string(),
            context: None,
            iteration: None,
        };
        let display = format!("{}", error);
        assert_eq!(display, "Property failed: test failed");
    }

    #[test]
    fn test_generation_failed_error_display() {
        let error = PropertyError::GenerationFailed {
            message: "generation error".to_string(),
            context: Some("while generating integers".to_string()),
        };
        let display = format!("{}", error);
        assert!(display.contains("Generation failed: generation error"));
        assert!(display.contains("context: while generating integers"));
    }

    #[test]
    fn test_shrinkage_timeout_error_display() {
        let error = PropertyError::ShrinkageTimeout {
            iterations: 1000,
            last_successful_shrink: Some("42".to_string()),
        };
        let display = format!("{}", error);
        assert!(display.contains("Shrinkage timeout after 1000 iterations"));
        assert!(display.contains("last successful shrink: 42"));
    }

    #[test]
    fn test_config_error_display() {
        let error = PropertyError::ConfigError {
            message: "invalid value".to_string(),
            field: Some("iterations".to_string()),
        };
        let display = format!("{}", error);
        assert!(display.contains("Configuration error: invalid value"));
        assert!(display.contains("field: iterations"));
    }

    #[test]
    fn test_test_cancelled_error_display() {
        let error = PropertyError::TestCancelled {
            reason: "user requested cancellation".to_string(),
        };
        let display = format!("{}", error);
        assert_eq!(display, "Test cancelled: user requested cancellation");
    }

    #[test]
    fn test_internal_error_display() {
        let error = PropertyError::InternalError {
            message: "unexpected state".to_string(),
            source_message: Some("internal failure".to_string()),
        };
        let display = format!("{}", error);
        assert!(display.contains("Internal error: unexpected state"));
        assert!(display.contains("source: internal failure"));
    }

    #[test]
    fn test_property_error_helper_functions() {
        let error = PropertyError::property_failed_with_context(
            "test failed",
            Some("during execution"),
            Some(10),
        );
        match error {
            PropertyError::PropertyFailed {
                message,
                context,
                iteration,
            } => {
                assert_eq!(message, "test failed");
                assert_eq!(context, Some("during execution".to_string()));
                assert_eq!(iteration, Some(10));
            }
            _ => panic!("Expected PropertyFailed variant"),
        }

        let error = PropertyError::generation_failed_with_context("gen error", Some("context"));
        match error {
            PropertyError::GenerationFailed { message, context } => {
                assert_eq!(message, "gen error");
                assert_eq!(context, Some("context".to_string()));
            }
            _ => panic!("Expected GenerationFailed variant"),
        }

        let error = PropertyError::config_error_with_field("bad config", Some("field_name"));
        match error {
            PropertyError::ConfigError { message, field } => {
                assert_eq!(message, "bad config");
                assert_eq!(field, Some("field_name".to_string()));
            }
            _ => panic!("Expected ConfigError variant"),
        }
    }

    #[test]
    fn test_test_failure_detailed_report() {
        let error = PropertyError::PropertyFailed {
            message: "assertion failed".to_string(),
            context: None,
            iteration: None,
        };
        let config = TestConfig::default();
        let failure = TestFailure::new(
            error,
            42,
            Some(0),
            5,
            config,
            10,
            Duration::from_millis(100),
            Duration::from_millis(50),
        );

        let report = failure.detailed_report();
        assert!(report.contains("Property test failed on iteration 10"));
        assert!(report.contains("Error: Property failed: assertion failed"));
        assert!(report.contains("Original input: 42"));
        assert!(report.contains("Shrunk input: 0"));
        assert!(report.contains("Shrinking steps: 5"));
        assert!(report.contains("Total test time:"));
    }

    #[test]
    fn test_test_failure_summary() {
        let error = PropertyError::PropertyFailed {
            message: "test failed".to_string(),
            context: None,
            iteration: None,
        };
        let config = TestConfig::default();

        // Test with shrinking
        let failure = TestFailure::new(
            error.clone(),
            100,
            Some(0),
            3,
            config.clone(),
            5,
            Duration::from_millis(100),
            Duration::from_millis(30),
        );
        let summary = failure.summary();
        assert!(summary.contains("Property failed with input 0"));
        assert!(summary.contains("shrunk from 100"));
        assert!(summary.contains("iteration 5"));

        // Test without shrinking
        let failure = TestFailure::new(
            error,
            100,
            None,
            0,
            config,
            5,
            Duration::from_millis(100),
            Duration::from_millis(0),
        );
        let summary = failure.summary();
        assert!(summary.contains("Property failed with input 100"));
        assert!(summary.contains("iteration 5"));
        assert!(!summary.contains("shrunk"));
    }

    #[test]
    fn test_test_success_constructor() {
        let config = TestConfig::default();
        let stats = GenerationStats {
            total_generated: 100,
            ..Default::default()
        };
        let success: TestSuccess<i32> = TestSuccess::new(100, config.clone(), Some(stats));

        assert_eq!(success.iterations, 100);
        assert_eq!(success.config.iterations, config.iterations);
        assert!(success.stats.is_some());
        assert_eq!(success.stats.unwrap().total_generated, 100);
    }

    #[test]
    fn test_property_error_source() {
        let error = PropertyError::GenerationFailed {
            message: "original error".to_string(),
            context: None,
        };

        // Since we simplified the error structure, source() will return None
        assert!(error.source().is_none());

        let error = PropertyError::PropertyFailed {
            message: "test".to_string(),
            context: None,
            iteration: None,
        };
        assert!(error.source().is_none());
    }

    #[test]
    fn test_error_reporter_basic() {
        let reporter = ErrorReporter::new();
        assert!(!reporter.verbose);
        assert!(!reporter.show_shrink_progress);
        assert!(reporter.show_timing);
        assert!(!reporter.show_config);
    }

    #[test]
    fn test_error_reporter_builder() {
        let reporter = ErrorReporter::new()
            .verbose()
            .show_shrink_progress()
            .show_config();

        assert!(reporter.verbose);
        assert!(reporter.show_shrink_progress);
        assert!(reporter.show_timing);
        assert!(reporter.show_config);
    }

    #[test]
    fn test_error_reporter_format_failure() {
        let error = PropertyError::PropertyFailed {
            message: "test assertion failed".to_string(),
            context: Some("during validation".to_string()),
            iteration: Some(42),
        };
        let config = TestConfig::default();
        let failure = TestFailure::new(
            error,
            100,
            Some(0),
            5,
            config,
            42,
            Duration::from_millis(150),
            Duration::from_millis(75),
        );

        let reporter = ErrorReporter::new().verbose().show_config();
        let report = reporter.format_failure(&failure);

        assert!(report.contains("PROPERTY TEST FAILURE"));
        assert!(report.contains("Test failed on iteration 42"));
        assert!(report.contains("Original input: 100"));
        assert!(report.contains("Shrunk input:   0"));
        assert!(report.contains("Shrink steps:   5"));
        assert!(report.contains("TIMING INFORMATION"));
        assert!(report.contains("CONFIGURATION"));
        assert!(report.contains("DETAILED ERROR CONTEXT"));
        assert!(report.contains("SUGGESTIONS"));
    }

    #[test]
    fn test_error_reporter_format_summary() {
        let error = PropertyError::PropertyFailed {
            message: "test failed".to_string(),
            context: None,
            iteration: None,
        };
        let config = TestConfig::default();
        let failure = TestFailure::new(
            error,
            42,
            Some(0),
            3,
            config,
            10,
            Duration::from_millis(100),
            Duration::from_millis(50),
        );

        let reporter = ErrorReporter::new();
        let summary = reporter.format_summary(&failure);

        assert!(summary.contains("QUICK SUMMARY"));
        assert!(summary.contains("Focus on input: 0"));
        assert!(summary.contains("Property failed"));
    }

    #[test]
    fn test_error_reporter_suggestions() {
        let reporter = ErrorReporter::new();

        // Test PropertyFailed suggestions
        let error = PropertyError::PropertyFailed {
            message: "test".to_string(),
            context: None,
            iteration: None,
        };
        let config = TestConfig::default();
        let failure = TestFailure::new(
            error,
            42,
            Some(0),
            1,
            config.clone(),
            0,
            Duration::from_millis(10),
            Duration::from_millis(5),
        );
        let suggestions = reporter.generate_suggestions(&failure);
        assert!(suggestions.contains("Check if your property logic is correct"));
        assert!(suggestions.contains("Focus on the shrunk input"));

        // Test GenerationFailed suggestions
        let error = PropertyError::GenerationFailed {
            message: "gen failed".to_string(),
            context: None,
        };
        let failure = TestFailure::new(
            error,
            42,
            None,
            0,
            config,
            0,
            Duration::from_millis(10),
            Duration::from_millis(0),
        );
        let suggestions = reporter.generate_suggestions(&failure);
        assert!(suggestions.contains("Check your generator implementation"));
    }

    #[test]
    fn test_shrink_progress_tracking() {
        let mut progress = ShrinkProgress::new();
        assert!(progress.steps.is_empty());
        assert!(!progress.completed);

        let step1 = ShrinkStep {
            step_number: 1,
            input_description: "100 -> 50".to_string(),
            step_time: Duration::from_millis(10),
            successful: true,
        };
        progress.add_step(step1);

        let step2 = ShrinkStep {
            step_number: 2,
            input_description: "50 -> 25".to_string(),
            step_time: Duration::from_millis(8),
            successful: true,
        };
        progress.add_step(step2);

        progress.complete(Duration::from_millis(20));

        assert_eq!(progress.steps.len(), 2);
        assert!(progress.completed);
        assert_eq!(progress.total_time, Duration::from_millis(20));

        let viz = progress.visualize();
        assert!(viz.contains("Shrinking Progress"));
        assert!(viz.contains("Step 1: 100 -> 50"));
        assert!(viz.contains("Step 2: 50 -> 25"));
        assert!(viz.contains("Completed in"));

        let stats = progress.statistics();
        assert_eq!(stats.total_steps, 2);
        assert_eq!(stats.successful_steps, 2);
        assert_eq!(stats.success_rate, 1.0);
    }

    #[test]
    fn test_shrink_statistics_display() {
        let stats = ShrinkStatistics {
            total_steps: 10,
            successful_steps: 7,
            total_time: Duration::from_millis(100),
            average_step_time: Duration::from_millis(10),
            success_rate: 0.7,
        };

        let display = format!("{}", stats);
        assert!(display.contains("7/10 steps successful"));
        assert!(display.contains("70.0%"));
        assert!(display.contains("10ms"));
    }

    #[test]
    fn test_error_reporter_different_error_types() {
        let reporter = ErrorReporter::new().verbose();

        // Test ShrinkageTimeout error
        let error = PropertyError::ShrinkageTimeout {
            iterations: 1000,
            last_successful_shrink: Some("42".to_string()),
        };
        let context = reporter.format_error_context(&error);
        assert!(context.contains("Shrinkage process timeout"));
        assert!(context.contains("Iterations attempted: 1000"));
        assert!(context.contains("Last successful shrink: 42"));

        // Test ConfigError
        let error = PropertyError::ConfigError {
            message: "invalid iterations".to_string(),
            field: Some("iterations".to_string()),
        };
        let context = reporter.format_error_context(&error);
        assert!(context.contains("Configuration error"));
        assert!(context.contains("Field: iterations"));

        // Test TestCancelled
        let error = PropertyError::TestCancelled {
            reason: "user interrupt".to_string(),
        };
        let context = reporter.format_error_context(&error);
        assert!(context.contains("Test cancellation"));
        assert!(context.contains("Reason: user interrupt"));

        // Test InternalError
        let error = PropertyError::InternalError {
            message: "unexpected state".to_string(),
            source_message: Some("null pointer".to_string()),
        };
        let context = reporter.format_error_context(&error);
        assert!(context.contains("Internal framework error"));
        assert!(context.contains("Source: null pointer"));
    }

    #[test]
    fn test_error_reporter_shrink_progress_visualization() {
        let error = PropertyError::PropertyFailed {
            message: "test failed".to_string(),
            context: None,
            iteration: None,
        };
        let config = TestConfig::default();
        let failure = TestFailure::new(
            error,
            100,
            Some(1),
            25,
            config,
            5,
            Duration::from_millis(200),
            Duration::from_millis(100),
        );

        let reporter = ErrorReporter::new().show_shrink_progress();
        let report = reporter.format_failure(&failure);

        assert!(report.contains("Shrink progress:"));
        assert!(report.contains("Shrink rate:"));
        // Should show progress bar for 25 steps
        assert!(report.contains(""));
    }

    #[test]
    fn test_error_reporter_no_shrinking() {
        let error = PropertyError::PropertyFailed {
            message: "test failed".to_string(),
            context: None,
            iteration: None,
        };
        let config = TestConfig::default();
        let failure = TestFailure::new(
            error,
            42,
            None,
            0,
            config,
            0,
            Duration::from_millis(50),
            Duration::from_millis(0),
        );

        let reporter = ErrorReporter::new();
        let report = reporter.format_failure(&failure);

        assert!(report.contains("No shrinking performed"));
        assert!(report.contains("Consider implementing shrinking"));
    }

    #[test]
    fn test_error_reporter_format_for_test_output() {
        let error = PropertyError::PropertyFailed {
            message: "test failed".to_string(),
            context: None,
            iteration: None,
        };
        let config = TestConfig::default();
        let failure = TestFailure::new(
            error,
            42,
            None,
            0,
            config,
            0,
            Duration::from_millis(50),
            Duration::from_millis(0),
        );

        let reporter_verbose = ErrorReporter::new().verbose();
        let verbose_output = reporter_verbose.format_for_test_output(&failure);
        assert!(verbose_output.contains("PROPERTY TEST FAILURE"));

        let reporter_concise = ErrorReporter::new();
        let concise_output = reporter_concise.format_for_test_output(&failure);
        assert!(concise_output.contains("QUICK SUMMARY"));
        assert!(concise_output.len() < verbose_output.len());
    }
}