hegeltest 0.4.2

Property-based testing for Rust, built on Hypothesis
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
use super::{BasicGenerator, Generator, TestCase};
use crate::cbor_utils::{cbor_array, cbor_map, map_insert};
use ciborium::Value;

/// Generator for Unicode text strings. Created by [`text()`].
pub struct TextGenerator {
    min_size: usize,
    max_size: Option<usize>,
}

impl TextGenerator {
    /// Set the minimum length in characters.
    pub fn min_size(mut self, min_size: usize) -> Self {
        self.min_size = min_size;
        self
    }

    /// Set the maximum length in characters.
    pub fn max_size(mut self, max_size: usize) -> Self {
        self.max_size = Some(max_size);
        self
    }

    fn build_schema(&self) -> Value {
        if let Some(max) = self.max_size {
            assert!(self.min_size <= max, "Cannot have max_size < min_size");
        }

        let mut schema = cbor_map! {
            "type" => "string",
            "min_size" => self.min_size as u64
        };

        if let Some(max) = self.max_size {
            map_insert(&mut schema, "max_size", max as u64);
        }

        schema
    }
}

impl Generator<String> for TextGenerator {
    fn do_draw(&self, tc: &TestCase) -> String {
        super::generate_from_schema(tc, &self.build_schema())
    }

    fn as_basic(&self) -> Option<BasicGenerator<'_, String>> {
        Some(BasicGenerator::new(self.build_schema(), |raw| {
            super::deserialize_value(raw)
        }))
    }
}

/// Generate arbitrary Unicode text strings.
pub fn text() -> TextGenerator {
    TextGenerator {
        min_size: 0,
        max_size: None,
    }
}

/// Generator for strings matching a regex pattern. Created by [`from_regex()`].
///
/// By default generates strings that contain a match. Use [`fullmatch()`](Self::fullmatch)
/// to require the entire string to match.
pub struct RegexGenerator {
    pattern: String,
    fullmatch: bool,
}

impl RegexGenerator {
    /// Set whether the entire string must match the pattern, not just contain a match.
    // nocov start
    pub fn fullmatch(mut self, fullmatch: bool) -> Self {
        self.fullmatch = fullmatch;
        self
        // nocov end
    }

    // nocov start
    fn build_schema(&self) -> Value {
        cbor_map! {
            "type" => "regex",
            "pattern" => self.pattern.as_str(),
            "fullmatch" => self.fullmatch
        // nocov end
        }
    }
}

impl Generator<String> for RegexGenerator {
    // nocov start
    fn do_draw(&self, tc: &TestCase) -> String {
        super::generate_from_schema(tc, &self.build_schema())
        // nocov end
    }

    // nocov start
    fn as_basic(&self) -> Option<BasicGenerator<'_, String>> {
        Some(BasicGenerator::new(self.build_schema(), |raw| {
            super::deserialize_value(raw)
            // nocov end
        }))
    }
}

/// Generate strings matching a regex pattern.
// nocov start
pub fn from_regex(pattern: &str) -> RegexGenerator {
    RegexGenerator {
        pattern: pattern.to_string(),
        fullmatch: false,
        // nocov end
    }
}

/// Generator for arbitrary byte sequences. Created by [`binary()`].
pub struct BinaryGenerator {
    min_size: usize,
    max_size: Option<usize>,
}

impl BinaryGenerator {
    /// Set the minimum length in bytes.
    pub fn min_size(mut self, min_size: usize) -> Self {
        self.min_size = min_size;
        self
    }

    /// Set the maximum length in bytes.
    pub fn max_size(mut self, max_size: usize) -> Self {
        self.max_size = Some(max_size);
        self
    }

    fn build_schema(&self) -> Value {
        if let Some(max) = self.max_size {
            assert!(self.min_size <= max, "Cannot have max_size < min_size");
        }

        let mut schema = cbor_map! {
            "type" => "binary",
            "min_size" => self.min_size as u64
        };

        if let Some(max) = self.max_size {
            map_insert(&mut schema, "max_size", max as u64);
        }

        schema
    }
}

fn parse_binary(raw: Value) -> Vec<u8> {
    match raw {
        Value::Bytes(bytes) => bytes,
        _ => panic!("expected Value::Bytes, got {:?}", raw), // nocov
    }
}

impl Generator<Vec<u8>> for BinaryGenerator {
    fn do_draw(&self, tc: &TestCase) -> Vec<u8> {
        parse_binary(super::generate_raw(tc, &self.build_schema()))
    }

    fn as_basic(&self) -> Option<BasicGenerator<'_, Vec<u8>>> {
        Some(BasicGenerator::new(self.build_schema(), parse_binary))
    }
}

/// Generate arbitrary byte sequences (`Vec<u8>`).
pub fn binary() -> BinaryGenerator {
    BinaryGenerator {
        min_size: 0,
        max_size: None,
    }
}

/// Generator for email address strings. Created by [`emails()`].
pub struct EmailGenerator;

impl Generator<String> for EmailGenerator {
    // nocov start
    fn do_draw(&self, tc: &TestCase) -> String {
        super::generate_from_schema(tc, &cbor_map! {"type" => "email"})
        // nocov end
    }

    // nocov start
    fn as_basic(&self) -> Option<BasicGenerator<'_, String>> {
        Some(BasicGenerator::new(cbor_map! {"type" => "email"}, |raw| {
            super::deserialize_value(raw)
            // nocov end
        }))
    }
}

/// Generate email address strings.
// nocov start
pub fn emails() -> EmailGenerator {
    EmailGenerator
    // nocov end
}

/// Generator for URL strings. Created by [`urls()`].
pub struct UrlGenerator;

impl Generator<String> for UrlGenerator {
    // nocov start
    fn do_draw(&self, tc: &TestCase) -> String {
        super::generate_from_schema(tc, &cbor_map! {"type" => "url"})
        // nocov end
    }

    // nocov start
    fn as_basic(&self) -> Option<BasicGenerator<'_, String>> {
        Some(BasicGenerator::new(cbor_map! {"type" => "url"}, |raw| {
            super::deserialize_value(raw)
            // nocov end
        }))
    }
}

/// Generate URL strings.
// nocov start
pub fn urls() -> UrlGenerator {
    UrlGenerator
    // nocov end
}

/// Generator for domain name strings. Created by [`domains()`].
pub struct DomainGenerator {
    max_length: usize,
}

impl DomainGenerator {
    /// Set the maximum length (must be between 4 and 255).
    pub fn max_length(mut self, max_length: usize) -> Self {
        self.max_length = max_length;
        self
    }

    fn build_schema(&self) -> Value {
        assert!(
            self.max_length >= 4 && self.max_length <= 255,
            "max_length must be between 4 and 255"
        );

        cbor_map! { // nocov
            "type" => "domain",
            "max_length" => self.max_length as u64 // nocov
        }
    }
}

impl Generator<String> for DomainGenerator {
    // nocov start
    fn do_draw(&self, tc: &TestCase) -> String {
        super::generate_from_schema(tc, &self.build_schema())
        // nocov end
    }

    fn as_basic(&self) -> Option<BasicGenerator<'_, String>> {
        Some(BasicGenerator::new(self.build_schema(), |raw| {
            super::deserialize_value(raw) // nocov
        }))
    }
}

/// Generate domain name strings.
pub fn domains() -> DomainGenerator {
    DomainGenerator { max_length: 255 }
}

#[derive(Clone, Copy)]
pub enum IpVersion {
    V4,
    V6,
}

/// Generator for IP address strings. Created by [`ip_addresses()`].
///
/// By default generates both IPv4 and IPv6 addresses.
pub struct IpAddressGenerator {
    version: Option<IpVersion>,
}

impl IpAddressGenerator {
    /// Only generate IPv4 addresses.
    // nocov start
    pub fn v4(mut self) -> Self {
        self.version = Some(IpVersion::V4);
        self
        // nocov end
    }

    /// Only generate IPv6 addresses.
    // nocov start
    pub fn v6(mut self) -> Self {
        self.version = Some(IpVersion::V6);
        self
        // nocov end
    }

    // nocov start
    fn build_schema(&self) -> Value {
        match self.version {
            Some(IpVersion::V4) => cbor_map! {"type" => "ipv4"},
            Some(IpVersion::V6) => cbor_map! {"type" => "ipv6"},
            None => cbor_map! {
                "one_of" => cbor_array![
                    cbor_map!{"type" => "ipv4"},
                    cbor_map!{"type" => "ipv6"}
            // nocov end
                ]
            },
        }
    }
}

impl Generator<String> for IpAddressGenerator {
    // nocov start
    fn do_draw(&self, tc: &TestCase) -> String {
        super::generate_from_schema(tc, &self.build_schema())
        // nocov end
    }

    // nocov start
    fn as_basic(&self) -> Option<BasicGenerator<'_, String>> {
        Some(BasicGenerator::new(self.build_schema(), |raw| {
            super::deserialize_value(raw)
            // nocov end
        }))
    }
}

/// Generate IP address strings (IPv4 or IPv6).
// nocov start
pub fn ip_addresses() -> IpAddressGenerator {
    IpAddressGenerator { version: None }
    // nocov end
}

/// Generator for date strings in YYYY-MM-DD format. Created by [`dates()`].
pub struct DateGenerator;

impl Generator<String> for DateGenerator {
    // nocov start
    fn do_draw(&self, tc: &TestCase) -> String {
        super::generate_from_schema(tc, &cbor_map! {"type" => "date"})
        // nocov end
    }

    // nocov start
    fn as_basic(&self) -> Option<BasicGenerator<'_, String>> {
        Some(BasicGenerator::new(cbor_map! {"type" => "date"}, |raw| {
            super::deserialize_value(raw)
            // nocov end
        }))
    }
}

/// Generate date strings in YYYY-MM-DD format.
// nocov start
pub fn dates() -> DateGenerator {
    DateGenerator
    // nocov end
}

/// Generator for time strings in HH:MM:SS format. Created by [`times()`].
pub struct TimeGenerator;

impl Generator<String> for TimeGenerator {
    // nocov start
    fn do_draw(&self, tc: &TestCase) -> String {
        super::generate_from_schema(tc, &cbor_map! {"type" => "time"})
        // nocov end
    }

    // nocov start
    fn as_basic(&self) -> Option<BasicGenerator<'_, String>> {
        Some(BasicGenerator::new(cbor_map! {"type" => "time"}, |raw| {
            super::deserialize_value(raw)
            // nocov end
        }))
    }
}

/// Generate time strings in HH:MM:SS format.
// nocov start
pub fn times() -> TimeGenerator {
    TimeGenerator
    // nocov end
}

/// Generator for ISO 8601 datetime strings. Created by [`datetimes()`].
pub struct DateTimeGenerator;

impl Generator<String> for DateTimeGenerator {
    // nocov start
    fn do_draw(&self, tc: &TestCase) -> String {
        super::generate_from_schema(tc, &cbor_map! {"type" => "datetime"})
        // nocov end
    }

    // nocov start
    fn as_basic(&self) -> Option<BasicGenerator<'_, String>> {
        Some(BasicGenerator::new(
            cbor_map! {"type" => "datetime"},
            super::deserialize_value,
            // nocov end
        ))
    }
}

/// Generate ISO 8601 datetime strings.
// nocov start
pub fn datetimes() -> DateTimeGenerator {
    DateTimeGenerator
    // nocov end
}