double 0.2.4

Full-featured mocking library in Rust, including rich failure messages and argument matchers
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
extern crate float_cmp;

use std::f32;
use std::f64;
use self::float_cmp::ApproxEqUlps;


include!(concat!(env!("OUT_DIR"), "/matcher_generated.rs"));


// ============================================================================
// * Comparison Matchers
// ============================================================================

/// Matcher that matches any arg value.
pub fn any<T>(_: &T) -> bool {
    true
}

/// Matcher that matches if `arg` is equal to `target_val`.
pub fn eq<T: PartialEq>(arg: &T, target_val: T) -> bool {
    *arg == target_val
}

/// Matcher that matches if `arg` is not equal to `target_val`.
pub fn ne<T: PartialEq>(arg: &T, target_val: T) -> bool {
    *arg != target_val
}

/// Matcher that matches if `arg` is less than `target_val`.
pub fn lt<T: PartialOrd>(arg: &T, target_val: T) -> bool {
    *arg < target_val
}

/// Matcher that matches if `arg` is less than or equal to `target_val`.
pub fn le<T: PartialEq + PartialOrd>(arg: &T, target_val: T) -> bool {
    *arg <= target_val
}

/// Matcher that matches if `arg` is greater than `target_val`.
pub fn gt<T: PartialOrd>(arg: &T, target_val: T) -> bool {
    *arg > target_val
}

/// Matcher that matches if `arg` is greater than or equal to `target_val`.
pub fn ge<T: PartialEq + PartialOrd>(arg: &T, target_val: T) -> bool {
    *arg >= target_val
}

/// Matcher that matches if `arg` is between the exclusive range `(low,high)`.
pub fn between_exc<T: PartialOrd>(arg: &T, low: T, high: T) -> bool {
    low < *arg && *arg < high
}

/// Matcher that matches if `arg` is between the inclusive range `[low,high]`.
pub fn between_inc<T: PartialEq + PartialOrd>(arg: &T, low: T, high: T) -> bool {
    low <= *arg && *arg <= high
}

/// Matcher that matches if `arg` is a populated `Option` whose stored value
/// matches the specified `matcher`.
pub fn is_some<T>(arg: &Option<T>, matcher: &dyn Fn(&T) -> bool) -> bool {
    match *arg {
        Some(ref x) => matcher(x),
        None => false
    }
}

/// Matcher that matches if `arg` is a `Result::Ok` whose stored value matches
/// the specified `matcher`.
pub fn is_ok<T, U>(arg: &Result<T, U>, matcher: &dyn Fn(&T) -> bool) -> bool {
    match *arg {
        Ok(ref x) => matcher(x),
        Err(_) => false
    }
}

/// Matcher that matches if `arg` is a `Result::Err` whose stored value matches
/// the specified `matcher`.
pub fn is_err<T, U>(arg: &Result<T, U>, matcher: &dyn Fn(&U) -> bool) -> bool {
    match *arg {
        Ok(_) => false,
        Err(ref x) => matcher(x)
    }
}


// ============================================================================
// * Float Matchers
// ============================================================================

/// Matcher that matches if `arg` is equal to `target_val`. This uses
/// approximate floating point equality, as defined by the `float-cmp` crate.
pub fn f32_eq(arg: &f32, target_val: f32) -> bool {
    if target_val.is_nan() && arg.is_nan() {
        false
    } else {
        arg.approx_eq_ulps(&target_val, 2)
    }
}

/// Matcher that matches if `arg` is equal to `target_val`. This uses
/// approximate floating point equality, as defined by the `float-cmp` crate.
pub fn f64_eq(arg: &f64, target_val: f64) -> bool {
    if target_val.is_nan() && arg.is_nan() {
        false
    } else {
        arg.approx_eq_ulps(&target_val, 2)
    }
}

/// Matcher that matches if `arg` is equal to `target_val`. This uses
/// approximate floating point equality, as defined by the `float-cmp` crate.
///
/// Unlike `f32_eq`, this matcher returns `true` if both the actual `arg` and
/// the `target_val` are NaN.
pub fn nan_sensitive_f32_eq(arg: &f32, target_val: f32) -> bool {
    if target_val.is_nan() && arg.is_nan() {
        true
    } else {
        arg.approx_eq_ulps(&target_val, 2)
    }
}

/// Matcher that matches if `arg` is equal to `target_val`. This uses
/// approximate floating point equality, as defined by the `float-cmp` crate.
///
/// Unlike `f64_eq`, this matcher returns `true` if both the actual `arg` and
/// the `target_val` are NaN.
pub fn nan_sensitive_f64_eq(arg: &f64, target_val: f64) -> bool {
    if target_val.is_nan() && arg.is_nan() {
        true
    } else {
        arg.approx_eq_ulps(&target_val, 2)
    }
}


// ============================================================================
// * String Matchers
// ============================================================================

/// Matcher that matches if `arg` contains the substring specified by `string`.
pub fn contains(arg: &str, string: &str) -> bool {
    arg.contains(string)
}

/// Matcher that matches if `arg` starts with the specified `prefix`.
pub fn starts_with(arg: &str, prefix: &str) -> bool {
    arg.starts_with(prefix)
}

/// Matcher that matches if `arg` ends with the specified `suffix`.
pub fn ends_with(arg: &str, suffix: &str) -> bool {
    arg.ends_with(suffix)
}

/// Matcher that matches if `arg` is equal to `string` after ignoring case.
pub fn eq_nocase(arg: &str, string: &str) -> bool {
    arg.to_lowercase() == string
}

/// Matcher that matches if `arg` is not equal to `string`, even after ignoring
/// case.
pub fn ne_nocase(arg: &str, string: &str) -> bool {
    arg.to_lowercase() != string
}


// ============================================================================
// * Container Matchers
// ============================================================================

// TODO


// ============================================================================
// * Composite Matchers
// ============================================================================

/// Matcher that matches if `arg` does _not_ match the specified `matcher`.
pub fn not<T>(arg: &T, matcher: &dyn Fn(&T) -> bool) -> bool {
    !matcher(arg)
}

/// Matcher that matches if `arg` matches *all* of the specified `matchers`. If
/// at least one of `matchers` doesn't match with `arg`, this matcher doesn't
/// match.
pub fn all_of<T>(arg: &T, matchers: Vec<&dyn Fn(&T) -> bool>) -> bool {
    for matcher in matchers {
        if !matcher(arg) {
            return false
        }
    }
    true
}

/// Matcher that matches if `arg` matches *any* of the specified `matchers`. If
/// none of the `matchers` match with `arg`, this matcher doesn't match.
pub fn any_of<T>(arg: &T, matchers: Vec<&dyn Fn(&T) -> bool>) -> bool {
    for matcher in matchers {
        if matcher(arg) {
            return true
        }
    }
    false
}


// ============================================================================
// * Unit Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn any_matcher() {
        assert!(any(&1));
        assert!(any(&Some(42)));
        assert!(any(&42.2));
        assert!(any(&vec!(1, 2, 3, 4, 5)));
    }

    #[test]
    fn eq_matcher() {
        let matcher1 = p!(eq, 1);
        assert!(matcher1(&1));
        assert!(!matcher1(&2));

        let matcher2 = p!(eq, "hello");
        assert!(matcher2(&"hello"));
        assert!(!matcher2(&"bye_bye"));

        let vec_arg1 = vec!(1, 2, 3, 4);
        let vec_arg2 = vec!(1, 2, 3, 5);
        let matcher3 = p!(eq, vec!(1, 2, 3, 4));
        assert!(matcher3(&vec_arg1));
        assert!(!matcher3(&vec_arg2));
    }

    #[test]
    fn ne_matcher() {
        let matcher1 = p!(ne, 1);
        assert!(!matcher1(&1));
        assert!(matcher1(&2));

        let matcher2 = p!(ne, "hello");
        assert!(!matcher2(&"hello"));
        assert!(matcher2(&"bye_bye"));

        let vec_arg1 = vec!(1, 2, 3, 4);
        let vec_arg2 = vec!(1, 2, 3, 5);
        let matcher3 = p!(ne, vec!(1, 2, 3, 4));
        assert!(!matcher3(&vec_arg1));
        assert!(matcher3(&vec_arg2));
    }

    #[test]
    fn lt_matcher() {
        let matcher1 = p!(lt, 10);
        assert!(matcher1(&9));
        assert!(!matcher1(&10));
        assert!(!matcher1(&11));

        let matcher2 = p!(lt, "hello1");
        assert!(matcher2(&"hello0"));
        assert!(!matcher2(&"hello1"));
        assert!(!matcher2(&"hello2"));
    }

    #[test]
    fn le_matcher() {
        let matcher1 = p!(le, 10);
        assert!(matcher1(&9));
        assert!(matcher1(&10));
        assert!(!matcher1(&11));

        let matcher2 = p!(le, "hello1");
        assert!(matcher2(&"hello0"));
        assert!(matcher2(&"hello1"));
        assert!(!matcher2(&"hello2"));
    }

    #[test]
    fn gt_matcher() {
        let matcher1 = p!(gt, 10);
        assert!(!matcher1(&9));
        assert!(!matcher1(&10));
        assert!(matcher1(&11));

        let matcher2 = p!(gt, "hello1");
        assert!(!matcher2(&"hello0"));
        assert!(!matcher2(&"hello1"));
        assert!(matcher2(&"hello2"));
    }

    #[test]
    fn ge_matcher() {
        let matcher1 = p!(ge, 10);
        assert!(!matcher1(&9));
        assert!(matcher1(&10));
        assert!(matcher1(&11));

        let matcher2 = p!(ge, "hello1");
        assert!(!matcher2(&"hello0"));
        assert!(matcher2(&"hello1"));
        assert!(matcher2(&"hello2"));
    }

    #[test]
    fn between_exc_matcher() {
        let matcher = p!(between_exc, 9, 11);
        assert!(!matcher(&8));
        assert!(!matcher(&9));
        assert!(matcher(&10));
        assert!(!matcher(&11));
        assert!(!matcher(&12));
    }

    #[test]
    fn between_inc_matcher() {
        let matcher = p!(between_inc, 9, 11);
        assert!(!matcher(&8));
        assert!(matcher(&9));
        assert!(matcher(&10));
        assert!(matcher(&11));
        assert!(!matcher(&12));
    }

    #[test]
    fn is_some_matcher() {
        let matcher = p!(is_some, p!(gt, 5));
        assert!(matcher(&Some(10)));
        assert!(!matcher(&Some(3)));
        assert!(!matcher(&None));
    }

    #[test]
    fn is_ok_matcher() {
        let matcher = p!(is_ok, p!(gt, 5));
        assert!(matcher(&Ok(10)));
        assert!(!matcher(&Ok(3)));
        assert!(!matcher(&Err("boo")));
    }

    #[test]
    fn is_err_matcher() {
        let matcher = p!(is_err, p!(gt, 0));
        assert!(matcher(&Err(8)));
        assert!(!matcher(&Err(0)));
        assert!(!matcher(&Ok(150.75)));
    }

    #[test]
    fn f32_eq_matcher() {
        let matcher = p!(f32_eq, 42.5572f32);
        assert!(!matcher(&0.0f32));
        assert!(!matcher(&42.0f32));
        assert!(!matcher(&42.55f32));
        assert!(matcher(&42.5572f32));

        let nan_matcher = p!(f32_eq, f32::NAN);
        assert!(!nan_matcher(&0.0f32));
        assert!(!nan_matcher(&42.0f32));
        assert!(!nan_matcher(&f32::NAN));
    }

    #[test]
    fn f64_eq_matcher() {
        let matcher = p!(f64_eq, 42.5572f64);
        assert!(!matcher(&0.0f64));
        assert!(!matcher(&42.0f64));
        assert!(!matcher(&42.55f64));
        assert!(matcher(&42.5572f64));

        let nan_matcher = p!(f64_eq, f64::NAN);
        assert!(!nan_matcher(&0.0f64));
        assert!(!nan_matcher(&42.0f64));
        assert!(!nan_matcher(&f64::NAN));
    }

    #[test]
    fn nan_sensitive_f32_eq_matcher() {
        let matcher = p!(nan_sensitive_f32_eq, 42.5572f32);
        assert!(!matcher(&0.0f32));
        assert!(!matcher(&42.0f32));
        assert!(!matcher(&42.55f32));
        assert!(matcher(&42.5572f32));

        let nan_matcher = p!(nan_sensitive_f32_eq, f32::NAN);
        assert!(!nan_matcher(&0.0f32));
        assert!(!nan_matcher(&42.0f32));
        assert!(nan_matcher(&f32::NAN));
    }

    #[test]
    fn nan_sensitive_f64_eq_matcher() {
        let matcher = p!(nan_sensitive_f64_eq, 42.5572f64);
        assert!(!matcher(&0.0f64));
        assert!(!matcher(&42.0f64));
        assert!(!matcher(&42.55f64));
        assert!(matcher(&42.5572f64));

        let nan_matcher = p!(nan_sensitive_f64_eq, f64::NAN);
        assert!(!nan_matcher(&0.0f64));
        assert!(!nan_matcher(&42.0f64));
        assert!(nan_matcher(&f64::NAN));
    }

    #[test]
    fn contains_matcher() {
        let empty_matcher = p!(contains, "");
        assert!(empty_matcher(""));
        assert!(empty_matcher("foo"));
        assert!(empty_matcher("barfooban"));
        assert!(empty_matcher("ban"));

        let matcher = p!(contains, "foo");
        assert!(!matcher(""));
        assert!(matcher("foo"));
        assert!(matcher("barfooban"));
        assert!(!matcher("ban"));
    }

    #[test]
    fn starts_with_matcher() {
        let empty_matcher = p!(starts_with, "");
        assert!(empty_matcher(""));
        assert!(empty_matcher("foo"));
        assert!(empty_matcher("barfooban"));
        assert!(empty_matcher("ban"));

        let matcher = p!(starts_with, "foo");
        assert!(!matcher(""));
        assert!(matcher("foo"));
        assert!(!matcher("barfooban"));
        assert!(!matcher("ban"));
    }
    #[test]
    fn ends_with_matcher() {
        let empty_matcher = p!(ends_with, "");
        assert!(empty_matcher(""));
        assert!(empty_matcher("foo"));
        assert!(empty_matcher("barfooban"));
        assert!(empty_matcher("ban"));

        let matcher = p!(ends_with, "ban");
        assert!(!matcher(""));
        assert!(!matcher("banfoo"));
        assert!(matcher("barfooban"));
        assert!(matcher("ban"));
    }

    #[test]
    fn eq_nocase_matcher() {
        let matcher = p!(eq_nocase, "foo");
        assert!(!matcher(""));
        assert!(matcher("FOo"));
        assert!(matcher("FOO"));
        assert!(matcher("foo"));
        assert!(!matcher("barfoo"));
        assert!(!matcher("barFOO"));
    }

    #[test]
    fn ne_nocase_matcher() {
        let matcher = p!(ne_nocase, "foo");
        assert!(matcher(""));
        assert!(!matcher("FOo"));
        assert!(!matcher("FOO"));
        assert!(!matcher("foo"));
        assert!(matcher("barfoo"));
        assert!(matcher("barFOO"));
    }

    #[test]
    fn not_matcher() {
        let matcher = p!(not, p!(eq, 10));
        assert!(matcher(&0));
        assert!(matcher(&5));
        assert!(!matcher(&10));
        assert!(matcher(&15));
    }

    #[test]
    fn all_of_matcher() {
        let matcher = p!(all_of, vec!(
            p!(ge, 0),
            p!(le, 10)
        ));
        assert!(!matcher(&-5));
        assert!(matcher(&0));
        assert!(matcher(&5));
        assert!(matcher(&10));
        assert!(!matcher(&15));
    }

    #[test]
    fn any_of_matcher() {
        let matcher = p!(any_of, vec!(
            p!(eq, 26),
            p!(le, 40)
        ));
        assert!(matcher(&0));    // matches one
        assert!(matcher(&26));   // matches both
        assert!(matcher(&30));   // matches one
        assert!(!matcher(&42));  // matches none
    }

}