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
//! Custom generator examples
//!
//! This example demonstrates how to create custom generators for complex data types,
//! including domain-specific generators, composite generators, and generators with
//! custom shrinking strategies.

use protest::{
    Generator, GeneratorConfig, Property, PropertyError, PropertyTestBuilder, TestConfig, check,
    check_with_config,
};
use rand::RngCore;
use std::collections::{HashMap, HashSet};

// Example 1: Simple custom generator for a domain-specific type
#[derive(Debug, Clone, PartialEq)]
struct EmailAddress {
    local: String,
    domain: String,
}

impl EmailAddress {
    fn new(local: String, domain: String) -> Self {
        Self { local, domain }
    }

    fn as_string(&self) -> String {
        format!("{}@{}", self.local, self.domain)
    }

    fn is_valid(&self) -> bool {
        !self.local.is_empty()
            && !self.domain.is_empty()
            && !self.local.contains('@')
            && !self.domain.contains('@')
            && self.domain.contains('.')
    }
}

struct EmailGenerator {
    local_chars: Vec<char>,
    domain_parts: Vec<String>,
    tlds: Vec<String>,
}

impl EmailGenerator {
    fn new() -> Self {
        Self {
            local_chars: "abcdefghijklmnopqrstuvwxyz0123456789._-".chars().collect(),
            domain_parts: vec![
                "gmail".to_string(),
                "yahoo".to_string(),
                "hotmail".to_string(),
                "example".to_string(),
                "test".to_string(),
                "company".to_string(),
            ],
            tlds: vec![
                "com".to_string(),
                "org".to_string(),
                "net".to_string(),
                "edu".to_string(),
                "gov".to_string(),
            ],
        }
    }
}

impl Generator<EmailAddress> for EmailGenerator {
    fn generate(&self, rng: &mut dyn RngCore, _config: &GeneratorConfig) -> EmailAddress {
        // Generate local part (1-20 characters)
        let local_len = (rng.next_u32() % 20) + 1;
        let local: String = (0..local_len)
            .map(|_| {
                let idx = (rng.next_u32() as usize) % self.local_chars.len();
                self.local_chars[idx]
            })
            .collect();

        // Generate domain part
        let domain_idx = (rng.next_u32() as usize) % self.domain_parts.len();
        let tld_idx = (rng.next_u32() as usize) % self.tlds.len();
        let domain = format!("{}.{}", self.domain_parts[domain_idx], self.tlds[tld_idx]);

        EmailAddress::new(local, domain)
    }

    fn shrink(&self, value: &EmailAddress) -> Box<dyn Iterator<Item = EmailAddress>> {
        let mut shrinks = Vec::new();

        // Shrink local part
        if value.local.len() > 1 {
            let shorter_local = value.local[..value.local.len() - 1].to_string();
            if !shorter_local.is_empty() {
                shrinks.push(EmailAddress::new(shorter_local, value.domain.clone()));
            }
        }

        // Try simpler local parts
        if value.local != "a" {
            shrinks.push(EmailAddress::new("a".to_string(), value.domain.clone()));
        }

        // Try simpler domain
        if value.domain != "test.com" {
            shrinks.push(EmailAddress::new(
                value.local.clone(),
                "test.com".to_string(),
            ));
        }

        Box::new(shrinks.into_iter())
    }
}

fn example_1_custom_email_generator() {
    println!("=== Example 1: Custom Email Generator ===");

    struct EmailValidityProperty;
    impl Property<EmailAddress> for EmailValidityProperty {
        type Output = ();

        fn test(&self, email: EmailAddress) -> Result<Self::Output, PropertyError> {
            if email.is_valid() {
                Ok(())
            } else {
                Err(PropertyError::property_failed(format!(
                    "Invalid email: {}",
                    email.as_string()
                )))
            }
        }
    }

    match check(EmailGenerator::new(), EmailValidityProperty) {
        Ok(success) => {
            println!(
                "✓ Email validity property passed! ({} iterations)",
                success.iterations
            );
        }
        Err(failure) => {
            println!("✗ Property failed: {}", failure.error);
            println!("  Email: {}", failure.original_input.as_string());
            if let Some(shrunk) = failure.shrunk_input {
                println!("  Shrunk to: {}", shrunk.as_string());
            }
        }
    }
}

// Example 2: Composite generator for complex structures
#[derive(Debug, Clone, PartialEq)]
struct User {
    id: u32,
    email: EmailAddress,
    age: u8,
    preferences: HashMap<String, String>,
    tags: HashSet<String>,
}

struct UserGenerator {
    email_gen: EmailGenerator,
    preference_keys: Vec<String>,
    preference_values: Vec<String>,
    available_tags: Vec<String>,
}

impl UserGenerator {
    fn new() -> Self {
        Self {
            email_gen: EmailGenerator::new(),
            preference_keys: vec![
                "theme".to_string(),
                "language".to_string(),
                "timezone".to_string(),
                "notifications".to_string(),
            ],
            preference_values: vec![
                "dark".to_string(),
                "light".to_string(),
                "en".to_string(),
                "es".to_string(),
                "fr".to_string(),
                "UTC".to_string(),
                "EST".to_string(),
                "enabled".to_string(),
                "disabled".to_string(),
            ],
            available_tags: vec![
                "premium".to_string(),
                "beta".to_string(),
                "admin".to_string(),
                "verified".to_string(),
                "new".to_string(),
            ],
        }
    }
}

impl Generator<User> for UserGenerator {
    fn generate(&self, rng: &mut dyn RngCore, config: &GeneratorConfig) -> User {
        let id = rng.next_u32();
        let email = self.email_gen.generate(rng, config);
        let age = (rng.next_u32() % 100) as u8 + 18; // 18-117 years old

        // Generate preferences (0-4 preferences)
        let pref_count = rng.next_u32() % 5;
        let mut preferences = HashMap::new();
        for _ in 0..pref_count {
            let key_idx = (rng.next_u32() as usize) % self.preference_keys.len();
            let val_idx = (rng.next_u32() as usize) % self.preference_values.len();
            preferences.insert(
                self.preference_keys[key_idx].clone(),
                self.preference_values[val_idx].clone(),
            );
        }

        // Generate tags (0-3 tags)
        let tag_count = rng.next_u32() % 4;
        let mut tags = HashSet::new();
        for _ in 0..tag_count {
            let tag_idx = (rng.next_u32() as usize) % self.available_tags.len();
            tags.insert(self.available_tags[tag_idx].clone());
        }

        User {
            id,
            email,
            age,
            preferences,
            tags,
        }
    }

    fn shrink(&self, value: &User) -> Box<dyn Iterator<Item = User>> {
        let mut shrinks = Vec::new();

        // Shrink ID towards 0
        if value.id > 0 {
            shrinks.push(User {
                id: value.id / 2,
                ..value.clone()
            });
            shrinks.push(User {
                id: 0,
                ..value.clone()
            });
        }

        // Shrink age towards minimum
        if value.age > 18 {
            shrinks.push(User {
                age: 18,
                ..value.clone()
            });
        }

        // Shrink preferences by removing entries
        if !value.preferences.is_empty() {
            let mut smaller_prefs = value.preferences.clone();
            if let Some(key) = smaller_prefs.keys().next().cloned() {
                smaller_prefs.remove(&key);
                shrinks.push(User {
                    preferences: smaller_prefs,
                    ..value.clone()
                });
            }
        }

        // Shrink tags by removing entries
        if !value.tags.is_empty() {
            let mut smaller_tags = value.tags.clone();
            if let Some(tag) = smaller_tags.iter().next().cloned() {
                smaller_tags.remove(&tag);
                shrinks.push(User {
                    tags: smaller_tags,
                    ..value.clone()
                });
            }
        }

        Box::new(shrinks.into_iter())
    }
}

fn example_2_composite_user_generator() {
    println!("\n=== Example 2: Composite User Generator ===");

    struct UserConsistencyProperty;
    impl Property<User> for UserConsistencyProperty {
        type Output = ();

        fn test(&self, user: User) -> Result<Self::Output, PropertyError> {
            // Property 1: Email should be valid
            if !user.email.is_valid() {
                return Err(PropertyError::property_failed(format!(
                    "User has invalid email: {}",
                    user.email.as_string()
                )));
            }

            // Property 2: Age should be reasonable
            if user.age < 18 || user.age > 120 {
                return Err(PropertyError::property_failed(format!(
                    "User age {} is unreasonable",
                    user.age
                )));
            }

            // Property 3: Premium users should be verified
            if user.tags.contains("premium") && !user.tags.contains("verified") {
                return Err(PropertyError::property_failed(
                    "Premium users must be verified".to_string(),
                ));
            }

            Ok(())
        }
    }

    let config = TestConfig {
        iterations: 50,
        seed: Some(999),
        ..TestConfig::default()
    };

    match check_with_config(UserGenerator::new(), UserConsistencyProperty, config) {
        Ok(success) => {
            println!(
                "✓ User consistency property passed! ({} iterations)",
                success.iterations
            );
        }
        Err(failure) => {
            println!("✗ Property failed: {}", failure.error);
            println!("  User ID: {}", failure.original_input.id);
            println!("  Email: {}", failure.original_input.email.as_string());
            println!("  Age: {}", failure.original_input.age);
            println!("  Tags: {:?}", failure.original_input.tags);
            if let Some(shrunk) = failure.shrunk_input {
                println!("  Shrunk user ID: {}", shrunk.id);
                println!("  Shrunk tags: {:?}", shrunk.tags);
            }
        }
    }
}

// Example 3: Generator with weighted choices
#[derive(Debug, Clone, PartialEq, Eq)]
enum LogLevel {
    Debug,
    Info,
    Warning,
    Error,
    Critical,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct LogEntry {
    level: LogLevel,
    message: String,
    timestamp: u64,
    module: String,
}

struct LogEntryGenerator {
    modules: Vec<String>,
    message_templates: Vec<String>,
}

impl LogEntryGenerator {
    fn new() -> Self {
        Self {
            modules: vec![
                "auth".to_string(),
                "database".to_string(),
                "api".to_string(),
                "cache".to_string(),
                "scheduler".to_string(),
            ],
            message_templates: vec![
                "Operation completed successfully".to_string(),
                "Connection established".to_string(),
                "Request processed".to_string(),
                "Cache miss occurred".to_string(),
                "Validation failed".to_string(),
                "Timeout exceeded".to_string(),
                "Resource not found".to_string(),
                "Permission denied".to_string(),
            ],
        }
    }

    fn generate_log_level(&self, rng: &mut dyn RngCore) -> LogLevel {
        // Weighted distribution: Info and Debug are more common
        let weight = rng.next_u32() % 100;
        match weight {
            0..=40 => LogLevel::Info,
            41..=70 => LogLevel::Debug,
            71..=85 => LogLevel::Warning,
            86..=95 => LogLevel::Error,
            _ => LogLevel::Critical,
        }
    }
}

impl Generator<LogEntry> for LogEntryGenerator {
    fn generate(&self, rng: &mut dyn RngCore, _config: &GeneratorConfig) -> LogEntry {
        let level = self.generate_log_level(rng);

        let msg_idx = (rng.next_u32() as usize) % self.message_templates.len();
        let message = self.message_templates[msg_idx].clone();

        let timestamp = rng.next_u64() % 1_000_000_000; // Reasonable timestamp range

        let mod_idx = (rng.next_u32() as usize) % self.modules.len();
        let module = self.modules[mod_idx].clone();

        LogEntry {
            level,
            message,
            timestamp,
            module,
        }
    }

    fn shrink(&self, value: &LogEntry) -> Box<dyn Iterator<Item = LogEntry>> {
        let mut shrinks = Vec::new();

        // Shrink to simpler log level
        match value.level {
            LogLevel::Critical => {
                shrinks.push(LogEntry {
                    level: LogLevel::Error,
                    ..value.clone()
                });
            }
            LogLevel::Error => {
                shrinks.push(LogEntry {
                    level: LogLevel::Warning,
                    ..value.clone()
                });
            }
            LogLevel::Warning => {
                shrinks.push(LogEntry {
                    level: LogLevel::Info,
                    ..value.clone()
                });
            }
            _ => {}
        }

        // Shrink timestamp towards 0
        if value.timestamp > 0 {
            shrinks.push(LogEntry {
                timestamp: value.timestamp / 2,
                ..value.clone()
            });
        }

        // Shrink to simpler module
        if value.module != "api" {
            shrinks.push(LogEntry {
                module: "api".to_string(),
                ..value.clone()
            });
        }

        Box::new(shrinks.into_iter())
    }
}

fn example_3_weighted_log_generator() {
    println!("\n=== Example 3: Weighted Log Entry Generator ===");

    struct LogLevelDistributionProperty;
    impl Property<Vec<LogEntry>> for LogLevelDistributionProperty {
        type Output = ();

        fn test(&self, logs: Vec<LogEntry>) -> Result<Self::Output, PropertyError> {
            if logs.is_empty() {
                return Ok(());
            }

            let critical_count = logs
                .iter()
                .filter(|log| matches!(log.level, LogLevel::Critical))
                .count();
            let total_count = logs.len();

            // Property: Critical logs should be rare (less than 10% of total)
            let critical_ratio = critical_count as f64 / total_count as f64;
            if critical_ratio > 0.1 {
                return Err(PropertyError::property_failed(format!(
                    "Too many critical logs: {:.1}% (expected < 10%)",
                    critical_ratio * 100.0
                )));
            }

            // Property: All timestamps should be reasonable
            for log in &logs {
                if log.timestamp > 2_000_000_000 {
                    return Err(PropertyError::property_failed(format!(
                        "Unreasonable timestamp: {}",
                        log.timestamp
                    )));
                }
            }

            Ok(())
        }
    }

    let log_vec_generator =
        protest::primitives::VecGenerator::new(LogEntryGenerator::new(), 10, 100);

    match check(log_vec_generator, LogLevelDistributionProperty) {
        Ok(success) => {
            println!(
                "✓ Log distribution property passed! ({} iterations)",
                success.iterations
            );
        }
        Err(failure) => {
            println!("✗ Property failed: {}", failure.error);
            println!("  Log count: {}", failure.original_input.len());
            let critical_count = failure
                .original_input
                .iter()
                .filter(|log| matches!(log.level, LogLevel::Critical))
                .count();
            println!("  Critical logs: {}", critical_count);
        }
    }
}

// Example 4: Generator with configuration-dependent behavior
struct ConfigurableStringGenerator {
    charset: Vec<char>,
    min_length: usize,
    max_length: usize,
}

impl ConfigurableStringGenerator {
    fn ascii_only() -> Self {
        Self {
            charset: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
                .chars()
                .collect(),
            min_length: 1,
            max_length: 50,
        }
    }

    fn with_special_chars() -> Self {
        Self {
            charset: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-=[]{}|;:,.<>?".chars().collect(),
            min_length: 1,
            max_length: 50,
        }
    }

    #[allow(dead_code)]
    fn unicode_friendly() -> Self {
        Self {
            charset: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789αβγδεζηθικλμνξοπρστυφχψω🚀🎉🔥💡".chars().collect(),
            min_length: 1,
            max_length: 30,
        }
    }
}

impl Generator<String> for ConfigurableStringGenerator {
    fn generate(&self, rng: &mut dyn RngCore, config: &GeneratorConfig) -> String {
        // Use config.size_hint to influence string length
        let adjusted_max = (self.max_length * config.size_hint / 10).max(self.min_length);
        let length =
            self.min_length + (rng.next_u32() as usize % (adjusted_max - self.min_length + 1));

        (0..length)
            .map(|_| {
                let idx = (rng.next_u32() as usize) % self.charset.len();
                self.charset[idx]
            })
            .collect()
    }

    fn shrink(&self, value: &String) -> Box<dyn Iterator<Item = String>> {
        let mut shrinks = Vec::new();

        // Shrink length
        if value.len() > self.min_length {
            shrinks.push(value[..value.len() - 1].to_string());
            if value.len() > self.min_length + 1 {
                shrinks.push(value[..self.min_length].to_string());
            }
        }

        // Try simpler characters
        if value.chars().any(|c| !c.is_ascii_alphanumeric()) {
            let simplified: String = value
                .chars()
                .map(|c| if c.is_ascii_alphanumeric() { c } else { 'a' })
                .collect();
            if simplified != *value {
                shrinks.push(simplified);
            }
        }

        Box::new(shrinks.into_iter())
    }
}

fn example_4_configurable_generator() {
    println!("\n=== Example 4: Configurable String Generator ===");

    struct StringCharsetProperty {
        allow_special: bool,
        allow_unicode: bool,
    }

    impl Property<String> for StringCharsetProperty {
        type Output = ();

        fn test(&self, input: String) -> Result<Self::Output, PropertyError> {
            for ch in input.chars() {
                if !self.allow_unicode && !ch.is_ascii() {
                    return Err(PropertyError::property_failed(format!(
                        "Non-ASCII character '{}' not allowed",
                        ch
                    )));
                }

                if !self.allow_special && !ch.is_alphanumeric() && ch != ' ' {
                    return Err(PropertyError::property_failed(format!(
                        "Special character '{}' not allowed",
                        ch
                    )));
                }
            }

            Ok(())
        }
    }

    // Test with ASCII-only generator
    println!("  Testing ASCII-only strings:");
    let result1 = PropertyTestBuilder::new()
        .iterations(30)
        // Note: generator_config method doesn't exist, using default
        .run(
            ConfigurableStringGenerator::ascii_only(),
            StringCharsetProperty {
                allow_special: false,
                allow_unicode: false,
            },
        );

    match result1 {
        Ok(success) => {
            println!(
                "    ✓ ASCII-only property passed! ({} iterations)",
                success.iterations
            );
        }
        Err(failure) => {
            println!("    ✗ Property failed: {}", failure.error);
            println!("    String: {:?}", failure.original_input);
        }
    }

    // Test with special characters
    println!("  Testing strings with special characters:");
    let result2 = PropertyTestBuilder::new()
        .iterations(30)
        // Note: generator_config method doesn't exist, using default
        .run(
            ConfigurableStringGenerator::with_special_chars(),
            StringCharsetProperty {
                allow_special: true,
                allow_unicode: false,
            },
        );

    match result2 {
        Ok(success) => {
            println!(
                "    ✓ Special characters property passed! ({} iterations)",
                success.iterations
            );
        }
        Err(failure) => {
            println!("    ✗ Property failed: {}", failure.error);
            println!("    String: {:?}", failure.original_input);
        }
    }
}

fn main() {
    println!("Protest Library - Custom Generator Examples");
    println!("==========================================");

    example_1_custom_email_generator();
    example_2_composite_user_generator();
    example_3_weighted_log_generator();
    example_4_configurable_generator();

    println!("\n=== Summary ===");
    println!("These custom generator examples demonstrate:");
    println!("• Creating domain-specific generators (EmailAddress)");
    println!("• Building composite generators for complex types (User)");
    println!("• Implementing weighted random choices (LogLevel)");
    println!("• Making generators configurable and context-aware");
    println!("• Custom shrinking strategies for better debugging");
    println!("• Integration with Protest's property testing framework");
    println!("\nCustom generators allow you to:");
    println!("• Model your domain accurately");
    println!("• Control the distribution of generated values");
    println!("• Implement domain-specific shrinking logic");
    println!("• Create reusable generators for common patterns");
}