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
//! Comprehensive async integration tests for the protest library

use protest::generator::ConstantGenerator;
use protest::primitives::{HashMapGenerator, IntGenerator, StringGenerator};
use protest::{
    AsyncProperty, Property, PropertyError, PropertyTestBuilder, TestConfig, check, check_async,
    check_async_with_config,
};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

/// Test basic async property execution
#[tokio::test]
async fn test_basic_async_property() {
    let generator = IntGenerator::new(1, 100);

    struct PositiveValueProperty;
    impl AsyncProperty<i32> for PositiveValueProperty {
        type Output = ();
        async fn test(&self, value: i32) -> Result<Self::Output, PropertyError> {
            // Simulate some async work
            tokio::time::sleep(Duration::from_millis(1)).await;

            if value <= 0 {
                Err(PropertyError::PropertyFailed {
                    message: "Value must be positive".to_string(),
                    context: None,
                    iteration: None,
                })
            } else {
                Ok(())
            }
        }
    }

    let result = check_async(generator, PositiveValueProperty).await;
    assert!(result.is_ok());

    if let Ok(success) = result {
        assert_eq!(success.iterations, 100); // Default iterations
        assert!(success.stats.is_some());
    }
}

/// Test async property with custom configuration
#[tokio::test]
async fn test_async_property_with_config() {
    let generator = IntGenerator::new(1, 50);
    let config = TestConfig {
        iterations: 20,
        seed: Some(42),
        max_shrink_iterations: 100,
        shrink_timeout: Duration::from_secs(5),
        ..TestConfig::default()
    };

    struct ValueRangeProperty;
    impl AsyncProperty<i32> for ValueRangeProperty {
        type Output = ();
        async fn test(&self, value: i32) -> Result<Self::Output, PropertyError> {
            tokio::time::sleep(Duration::from_millis(2)).await;

            if value > 100 {
                Err(PropertyError::PropertyFailed {
                    message: "Value too large".to_string(),
                    context: None,
                    iteration: None,
                })
            } else {
                Ok(())
            }
        }
    }

    let result = check_async_with_config(generator, ValueRangeProperty, config).await;
    assert!(result.is_ok());

    if let Ok(success) = result {
        assert_eq!(success.iterations, 20);
        assert_eq!(success.config.seed, Some(42));
    }
}

/// Test async property that fails and triggers shrinking
#[tokio::test]
async fn test_async_property_failure_with_shrinking() {
    let generator = IntGenerator::new(50, 200);
    let config = TestConfig {
        iterations: 10,
        max_shrink_iterations: 50,
        shrink_timeout: Duration::from_secs(2),
        ..TestConfig::default()
    };

    struct LargeValueProperty;
    impl AsyncProperty<i32> for LargeValueProperty {
        type Output = ();
        async fn test(&self, value: i32) -> Result<Self::Output, PropertyError> {
            tokio::time::sleep(Duration::from_millis(1)).await;

            if value > 100 {
                Err(PropertyError::PropertyFailed {
                    message: format!("Value {} is too large", value),
                    context: Some("async test".to_string()),
                    iteration: None,
                })
            } else {
                Ok(())
            }
        }
    }

    let result = check_async_with_config(generator, LargeValueProperty, config).await;
    assert!(result.is_err());

    if let Err(failure) = result {
        assert!(failure.original_input >= 50);
        assert!(failure.original_input <= 200);

        // Should have attempted shrinking
        if let Some(shrunk) = failure.shrunk_input {
            assert!(shrunk > 100); // Should still fail the property
            assert!(shrunk <= failure.original_input); // Should be smaller than or equal to original
        }

        // Verify error details
        match &failure.error {
            PropertyError::PropertyFailed {
                message,
                context,
                iteration,
            } => {
                assert!(message.contains("too large"));
                assert_eq!(context, &Some("async test".to_string()));
                assert!(iteration.is_some());
            }
            _ => panic!("Expected PropertyFailed error"),
        }
    }
}

/// Test async/sync interoperability - mixing async and sync properties in the same test suite
#[tokio::test]
async fn test_async_sync_interoperability() {
    // Sync property
    struct SyncProperty;
    impl Property<i32> for SyncProperty {
        type Output = ();
        fn test(&self, input: i32) -> Result<Self::Output, PropertyError> {
            if input < 0 {
                Err(PropertyError::PropertyFailed {
                    message: "Negative value".to_string(),
                    context: None,
                    iteration: None,
                })
            } else {
                Ok(())
            }
        }
    }

    // Async property
    struct AsyncPropertyTest;
    impl AsyncProperty<i32> for AsyncPropertyTest {
        type Output = ();
        async fn test(&self, input: i32) -> Result<Self::Output, PropertyError> {
            tokio::time::sleep(Duration::from_millis(1)).await;
            if input > 1000 {
                Err(PropertyError::PropertyFailed {
                    message: "Value too large".to_string(),
                    context: None,
                    iteration: None,
                })
            } else {
                Ok(())
            }
        }
    }

    let generator1 = IntGenerator::new(0, 500);
    let generator2 = IntGenerator::new(0, 500);

    // Test sync property
    let sync_result = check(generator1, SyncProperty);
    assert!(sync_result.is_ok());

    // Test async property
    let async_result = check_async(generator2, AsyncPropertyTest).await;
    assert!(async_result.is_ok());

    // Both should succeed with the same generator range
    if let (Ok(sync_success), Ok(async_success)) = (sync_result, async_result) {
        assert_eq!(sync_success.iterations, async_success.iterations);
    }
}

/// Test async property with complex data structures
#[tokio::test]
async fn test_async_property_with_complex_data() {
    use std::collections::HashMap;

    let generator = HashMapGenerator::new(
        StringGenerator::ascii_printable(5, 10),
        IntGenerator::new(1, 100),
        0,
        10,
    );

    struct SumProperty;
    impl AsyncProperty<HashMap<String, i32>> for SumProperty {
        type Output = ();
        async fn test(&self, map: HashMap<String, i32>) -> Result<Self::Output, PropertyError> {
            tokio::time::sleep(Duration::from_millis(1)).await;

            // Property: sum of all values should be less than 1000
            let sum: i32 = map.values().sum();
            if sum >= 1000 {
                Err(PropertyError::PropertyFailed {
                    message: format!("Sum {} is too large", sum),
                    context: Some("complex data test".to_string()),
                    iteration: None,
                })
            } else {
                Ok(())
            }
        }
    }

    let result = check_async(generator, SumProperty).await;
    // This might pass or fail depending on the generated data
    // The important thing is that it doesn't crash and handles complex types
    match result {
        Ok(_) => {
            // Property passed for all generated maps
        }
        Err(failure) => {
            // Property failed, verify error handling
            assert!(!failure.original_input.is_empty() || failure.original_input.is_empty());
            match &failure.error {
                PropertyError::PropertyFailed { message, .. } => {
                    assert!(message.contains("too large"));
                }
                _ => panic!("Expected PropertyFailed error"),
            }
        }
    }
}

/// Test async property with timeout scenarios
#[tokio::test]
async fn test_async_property_with_timeout() {
    let generator = ConstantGenerator::new(42);
    let config = TestConfig {
        iterations: 5,
        shrink_timeout: Duration::from_millis(100), // Short timeout for shrinking
        ..TestConfig::default()
    };

    struct SlowFailingProperty;
    impl AsyncProperty<i32> for SlowFailingProperty {
        type Output = ();
        async fn test(&self, _value: i32) -> Result<Self::Output, PropertyError> {
            // Simulate slow async operation
            tokio::time::sleep(Duration::from_millis(50)).await;

            // Always fail to trigger shrinking
            Err(PropertyError::PropertyFailed {
                message: "Always fails".to_string(),
                context: None,
                iteration: None,
            })
        }
    }

    let result = check_async_with_config(generator, SlowFailingProperty, config).await;
    assert!(result.is_err());

    if let Err(failure) = result {
        // Should have attempted shrinking but may have timed out
        assert_eq!(failure.original_input, 42);
        // Shrinking timeout should be respected
        assert!(failure.shrink_duration <= Duration::from_millis(150)); // Allow some tolerance
    }
}

/// Test async error handling and propagation
#[tokio::test]
async fn test_async_error_handling() {
    let generator = IntGenerator::new(1, 10);

    struct ErrorHandlingProperty;
    impl AsyncProperty<i32> for ErrorHandlingProperty {
        type Output = ();
        async fn test(&self, value: i32) -> Result<Self::Output, PropertyError> {
            tokio::time::sleep(Duration::from_millis(1)).await;

            match value {
                1 => Err(PropertyError::PropertyFailed {
                    message: "Value is 1".to_string(),
                    context: Some("specific case".to_string()),
                    iteration: None,
                }),
                2 => Err(PropertyError::GenerationFailed {
                    message: "Simulated generation error".to_string(),
                    context: Some("error test".to_string()),
                }),
                3 => Err(PropertyError::TestCancelled {
                    reason: "User requested cancellation".to_string(),
                }),
                _ => Ok(()),
            }
        }
    }

    let result = check_async(generator, ErrorHandlingProperty).await;
    assert!(result.is_err());

    if let Err(failure) = result {
        // Should capture the first error encountered
        match &failure.error {
            PropertyError::PropertyFailed {
                message,
                context,
                iteration,
            } => {
                assert_eq!(message, "Value is 1");
                assert_eq!(context, &Some("specific case".to_string()));
                assert!(iteration.is_some()); // Should be set by execution engine
            }
            PropertyError::GenerationFailed { .. } => {
                // Could also be this if value 2 was generated first
            }
            PropertyError::TestCancelled { .. } => {
                // Could also be this if value 3 was generated first
            }
            _ => panic!("Unexpected error type: {:?}", failure.error),
        }
    }
}

/// Test async property with PropertyTestBuilder
#[tokio::test]
async fn test_async_property_with_builder() {
    let generator = IntGenerator::new(10, 50);

    struct DivisibleBy7Property;
    impl AsyncProperty<i32> for DivisibleBy7Property {
        type Output = ();
        async fn test(&self, value: i32) -> Result<Self::Output, PropertyError> {
            tokio::time::sleep(Duration::from_millis(1)).await;

            if value % 7 == 0 {
                Err(PropertyError::PropertyFailed {
                    message: "Divisible by 7".to_string(),
                    context: None,
                    iteration: None,
                })
            } else {
                Ok(())
            }
        }
    }

    let result = PropertyTestBuilder::new()
        .iterations(30)
        .seed(123)
        .max_shrink_iterations(20)
        .shrink_timeout(Duration::from_secs(1))
        .run_async(generator, DivisibleBy7Property)
        .await;

    // This might pass or fail depending on whether multiples of 7 are generated
    match result {
        Ok(success) => {
            assert_eq!(success.iterations, 30);
            assert_eq!(success.config.seed, Some(123));
        }
        Err(failure) => {
            assert!(failure.original_input % 7 == 0);
            assert_eq!(failure.config.seed, Some(123));
        }
    }
}

/// Performance test for async execution
#[tokio::test]
async fn test_async_performance() {
    let generator = IntGenerator::new(1, 1000);
    let config = TestConfig {
        iterations: 100,
        ..TestConfig::default()
    };

    struct FastAsyncProperty;
    impl AsyncProperty<i32> for FastAsyncProperty {
        type Output = ();
        async fn test(&self, value: i32) -> Result<Self::Output, PropertyError> {
            // Simulate minimal async work
            tokio::task::yield_now().await;

            if value < 0 {
                Err(PropertyError::PropertyFailed {
                    message: "Negative value".to_string(),
                    context: None,
                    iteration: None,
                })
            } else {
                Ok(())
            }
        }
    }

    let start = Instant::now();
    let result = check_async_with_config(generator, FastAsyncProperty, config).await;
    let duration = start.elapsed();

    assert!(result.is_ok());

    // Should complete reasonably quickly (less than 1 second for 100 iterations)
    assert!(
        duration < Duration::from_secs(1),
        "Async execution took too long: {:?}",
        duration
    );

    if let Ok(success) = result {
        assert_eq!(success.iterations, 100);
        println!("Async performance test completed in {:?}", duration);
    }
}

/// Test concurrent async property execution
#[tokio::test]
async fn test_concurrent_async_properties() {
    let generator1 = IntGenerator::new(1, 50);
    let generator2 = IntGenerator::new(51, 100);

    struct Property1;
    impl AsyncProperty<i32> for Property1 {
        type Output = ();
        async fn test(&self, value: i32) -> Result<Self::Output, PropertyError> {
            tokio::time::sleep(Duration::from_millis(2)).await;
            if value > 25 {
                Err(PropertyError::PropertyFailed {
                    message: "Value too large for property1".to_string(),
                    context: None,
                    iteration: None,
                })
            } else {
                Ok(())
            }
        }
    }

    struct Property2;
    impl AsyncProperty<i32> for Property2 {
        type Output = ();
        async fn test(&self, value: i32) -> Result<Self::Output, PropertyError> {
            tokio::time::sleep(Duration::from_millis(3)).await;
            if value < 75 {
                Err(PropertyError::PropertyFailed {
                    message: "Value too small for property2".to_string(),
                    context: None,
                    iteration: None,
                })
            } else {
                Ok(())
            }
        }
    }

    let config = TestConfig {
        iterations: 10,
        ..TestConfig::default()
    };

    // Run both properties concurrently
    let (result1, result2) = tokio::join!(
        check_async_with_config(generator1, Property1, config.clone()),
        check_async_with_config(generator2, Property2, config)
    );

    // Both should fail due to their respective conditions
    assert!(result1.is_err());
    assert!(result2.is_err());

    if let (Err(failure1), Err(failure2)) = (result1, result2) {
        // Verify each failure has the expected error message
        match &failure1.error {
            PropertyError::PropertyFailed { message, .. } => {
                assert!(message.contains("property1"));
            }
            _ => panic!("Expected PropertyFailed for property1"),
        }

        match &failure2.error {
            PropertyError::PropertyFailed { message, .. } => {
                assert!(message.contains("property2"));
            }
            _ => panic!("Expected PropertyFailed for property2"),
        }
    }
}

/// Test async property with shared state
#[tokio::test]
async fn test_async_property_with_shared_state() {
    let counter = Arc::new(Mutex::new(0));
    let generator = IntGenerator::new(1, 20);

    struct SharedStateProperty {
        counter: Arc<Mutex<i32>>,
    }

    impl AsyncProperty<i32> for SharedStateProperty {
        type Output = ();
        async fn test(&self, value: i32) -> Result<Self::Output, PropertyError> {
            tokio::time::sleep(Duration::from_millis(1)).await;

            // Increment shared counter
            {
                let mut count = self.counter.lock().unwrap();
                *count += 1;
            }

            if value > 15 {
                Err(PropertyError::PropertyFailed {
                    message: "Value too large".to_string(),
                    context: None,
                    iteration: None,
                })
            } else {
                Ok(())
            }
        }
    }

    let config = TestConfig {
        iterations: 10,
        ..TestConfig::default()
    };

    let property = SharedStateProperty {
        counter: counter.clone(),
    };

    let result = check_async_with_config(generator, property, config).await;

    // Check that the shared state was modified
    let final_count = *counter.lock().unwrap();

    match result {
        Ok(_) => {
            // All properties passed, counter should equal iterations
            assert_eq!(final_count, 10);
        }
        Err(_) => {
            // Some property failed, counter includes both test iterations and shrinking iterations
            // Counter should be at least the number of iterations that ran before failure
            assert!(final_count > 0); // At least one iteration should have run
            // Shrinking may cause additional increments, so we don't assert an upper bound
        }
    }
}