galvanic_assert/matchers/
collection.rs

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
/* Copyright 2017 Christopher Bacher
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//! The collection module contains matchers for asserting properties of collections and iterators.

use std::fmt::Debug;
use super::super::*;

use std::iter::FromIterator;

/// Matches if the asserted collection contains *all and only* the expected elements in any order.
pub struct ContainsInAnyOrder<T> {
    expected_elements: Vec<T>
}

/// Matches if the asserted collection contains *all and only* of the expected elements in any order.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::collection::*;
/// # fn main() {
/// assert_that!(&vec![1,2,3,4,5,6], contains_in_any_order(vec![2,4,1,5,3,6]));
/// assert_that!(
///     // 6 is missing
///     assert_that!(&vec![1,2,3,4,5,6], contains_in_any_order(vec![2,4,1,5,3])),
///     panics
/// );
/// assert_that!(
///     // 7 is added
///     assert_that!(&vec![1,2,3,4,5,6], contains_in_any_order(vec![2,4,1,5,3,6,7])),
///     panics
/// );
/// # }
pub fn contains_in_any_order<'a,T:'a,I:'a,J:'a>(expected_elements: I) -> Box<Matcher<'a,J> + 'a>
where T: PartialEq + Debug,
      I: IntoIterator<Item=T>,
      J: IntoIterator<Item=T>,
      ContainsInAnyOrder<T>: Matcher<'a,J> {
    Box::new(ContainsInAnyOrder {
        expected_elements: expected_elements.into_iter().collect()
    })
}

impl<'a,T,I> Matcher<'a,I> for ContainsInAnyOrder<T>
where T: PartialEq + Debug + 'a,
      &'a I: IntoIterator<Item=&'a T> + Debug + 'a {
    fn check(&self, actual: &'a I) -> MatchResult {
        let repr = format!("{:?}", actual);
        let builder = MatchResultBuilder::for_("contains_in_any_order");
        let mut expected_elements = Vec::from_iter(self.expected_elements.iter());

        for ref element in actual.into_iter() {
            let maybe_pos = expected_elements.iter()
                                             .position(|candidate| element == candidate);
            if let Some(idx) = maybe_pos {
                expected_elements.remove(idx);
            } else {
                return builder.failed_because(
                    &format!("{} contains an unexpected element: {:?}", repr, element)
                );
            }
        }

        if !expected_elements.is_empty() {
            builder.failed_because(
                &format!("{} did not contain the following elements: {:?}", repr, expected_elements)
            )
        } else { builder.matched() }
    }
}

/// Matches if the asserted collection contains *all and only* of the expected elements in the given order.
pub struct ContainsInOrder<T> {
    expected_elements: Vec<T>
}

/// Matches if the asserted collection contains *all and only* of the expected elements in the given order.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::collection::*;
/// # fn main() {
/// assert_that!(&vec![1,2,3,4,5,6], contains_in_order(vec![1,2,3,4,5,6]));
/// assert_that!(
///     // 6 is missing
///     assert_that!(&vec![1,2,3,4,5,6], contains_in_order(vec![1,2,3,4,5])),
///     panics
/// );
/// assert_that!(
///     // 7 is added
///     assert_that!(&vec![1,2,3,4,5,6], contains_in_order(vec![1,2,3,4,5,6,7])),
///     panics
/// );
/// # }
pub fn contains_in_order<'a,T:'a,I:'a,J:'a>(expected_elements: I) -> Box<Matcher<'a,J> + 'a>
where T: PartialEq + Debug,
      I: IntoIterator<Item=T>,
      J: IntoIterator<Item=T>,
      ContainsInOrder<T>: Matcher<'a,J> {
    Box::new(ContainsInOrder {
        expected_elements: expected_elements.into_iter().collect()
    })
}

impl<'a, T, I:'a> Matcher<'a,I> for ContainsInOrder<T>
where T: PartialEq + Debug + 'a,
      &'a I: IntoIterator<Item=&'a T> + Debug + 'a {
    fn check(&self, actual: &'a I) -> MatchResult {
        let builder = MatchResultBuilder::for_("contains_in_order");
        let actual_list: Vec<_> = actual.into_iter().collect();

        if actual_list.len() > self.expected_elements.len() {
            return builder.failed_because(
                &format!("The expected list is shorter than the actual list by {} elements",
                         actual_list.len() - self.expected_elements.len())
            );
        }

        if actual_list.len() < self.expected_elements.len() {
            return builder.failed_because(
                &format!("The actual list is shorter than the expected list by {} elements",
                         self.expected_elements.len() - actual_list.len())
            );
        }

        let nonmatching: Vec<_> = actual_list.into_iter()
                                             .zip(self.expected_elements.iter())
                                             .filter(|&(act, exp)| act != exp)
                                             .collect();
        if !nonmatching.is_empty() {
            builder.failed_because(
                &format!("the following actual/expected pairs do not match: {:?}", nonmatching)
            )
        } else { builder.matched() }
    }
}

/// Matches if the asserted collection contains *all* (possibly more) of the expected elements.
pub struct ContainsSubset<T> {
    expected_elements: Vec<T>
}

/// Matches if the asserted collection contains *all* (possibly more) of the expected elements.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::collection::*;
/// # fn main() {
/// assert_that!(&vec![1,2,3,4,5,6], contains_subset(vec![3,1,2,4]));
/// # }
pub fn contains_subset<'a,T:'a,I:'a,J:'a>(expected_elements: I) -> Box<Matcher<'a,J> + 'a>
where T: PartialEq + Debug,
      I: IntoIterator<Item=T>,
      J: IntoIterator<Item=T>,
      ContainsSubset<T>: Matcher<'a,J> {
    Box::new(ContainsSubset {
        expected_elements: expected_elements.into_iter().collect()
    })
}

impl<'a, T, I:'a> Matcher<'a,I> for ContainsSubset<T>
where T: PartialEq + Debug + 'a,
      &'a I: IntoIterator<Item=&'a T> + Debug + 'a {
    fn check(&self, actual: &'a I) -> MatchResult {
        let repr = format!("{:?}", actual);
        let builder = MatchResultBuilder::for_("contains_subset");
        let mut expected_elements = Vec::from_iter(self.expected_elements.iter());

        for element in actual.into_iter() {
            let maybe_pos = expected_elements.iter()
                                             .position(|candidate| element == *candidate);
            if let Some(idx) = maybe_pos {
                expected_elements.remove(idx);
            }
        }

        if !expected_elements.is_empty() {
            builder.failed_because(
                &format!("{} did not contain the following elements: {:?}", repr, expected_elements)
            )
        } else { builder.matched() }
    }
}

/// Matches if the asserted (single) value is contained in the expected elements.
pub struct ContainedIn<T> {
    expected_to_contain: Vec<T>
}

/// Matches if the asserted (single) value is contained in the expected elements.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::collection::*;
/// # fn main() {
/// assert_that!(&5, contained_in(vec![1,2,3,4,5,6,7,8]));
/// # }
pub fn contained_in<'a,T:'a,I>(expected_to_contain: I) -> Box<Matcher<'a,T> + 'a>
where T: PartialEq + Debug,
      I: IntoIterator<Item=T> {
    Box::new(ContainedIn {
        expected_to_contain: expected_to_contain.into_iter().collect()
    })
}

impl<'a,T> Matcher<'a,T> for ContainedIn<T>
where T: PartialEq + Debug + 'a  {
    fn check(&self, element: &T) -> MatchResult {
        let builder = MatchResultBuilder::for_("containd_in");
        if let None = self.expected_to_contain.iter().position(|e| e == element) {
            builder.failed_because(
                &format!("{:?} does not contain: {:?}", self.expected_to_contain, element)
            )
        } else { builder.matched() }
    }
}

/// Matches if the elements in the asserted collection are sorted weakly monotone according to the given `predicate` in the expected order.
///
/// The `predicate` is applied to all consecutive pairs of elements and returns the `Ordering` of the pair.
/// The ordering is allowed to be weakly monotone, i.e., equal elements are allowed to follow each other.
/// An empty collection is assumed to be always sorted.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::collection::*;
/// use std::cmp::Ordering;
/// # fn main() {
/// assert_that!(&vec![1,2,2,3,3,4,5,6], sorted_by(|a: &i32, b: &i32| a.cmp(b), Ordering::Less));
/// # }
pub fn sorted_by<'a,T,I,P>(predicate: P, expected_ordering: std::cmp::Ordering) -> Box<Fn(&'a I) -> MatchResult>
where &'a I: IntoIterator<Item=&'a T> + 'a,
      T: Ord + Debug + 'a,
      P: Fn(&'a T,&'a T) -> std::cmp::Ordering + 'static {
    Box::new(move |elements: &'a I| {
        let builder = MatchResultBuilder::for_("sorted_by");
        let mut iter = elements.into_iter();
        let maybe_prev = iter.next();

        if maybe_prev.is_none() { return builder.matched() }
        let mut prev = maybe_prev.unwrap();

        for cur in iter {
            let ordering = predicate(&prev, &cur);
            if ordering != std::cmp::Ordering::Equal
                      && expected_ordering != ordering  {
                return builder.failed_because(
                    &format!("ordering is not monotone: predicate({:?}, {:?}) != {:?}",
                             prev, cur, expected_ordering)
                );
            }
            prev = cur;
        }
        builder.matched()
    })
}

/// Matches if the elements in the asserted collection are sorted strictly monotone according to the given `predicate` in the expected order`.
///
/// The `predicate` is applied to all consecutive pairs of elements and returns the `Ordering` of the pair.
/// The ordering is allowed to be weakly monotone, i.e., equal elements are allowed to follow each other.
/// An empty collection is assumed to be always sorted.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::collection::*;
/// use std::cmp::Ordering;
/// # fn main() {
/// assert_that!(&vec![1,2,3,4,5,6], sorted_strictly_by(|a: &i32, b: &i32| a.cmp(b), Ordering::Less));
/// # }
pub fn sorted_strictly_by<'a,T,I,P>(predicate: P, expected_ordering: std::cmp::Ordering) -> Box<Fn(&'a I) -> MatchResult>
where &'a I: IntoIterator<Item=&'a T> + 'a,
      T: Ord + Debug + 'a,
      P: Fn(&'a T,&'a T) -> std::cmp::Ordering + 'static {
    Box::new(move |elements: &'a I| {
        let builder = MatchResultBuilder::for_("sorted_strictly_by");
        let mut iter = elements.into_iter();
        let maybe_prev = iter.next();

        if maybe_prev.is_none() { return builder.matched() }
        let mut prev = maybe_prev.unwrap();

        for cur in iter {
            let ordering = predicate(&prev, &cur);
            if expected_ordering != ordering  {
                return builder.failed_because(
                    &format!("ordering is not strictly monotone: predicate({:?}, {:?}) != {:?}", prev, cur, expected_ordering)
                );
            }
            prev = cur;
        }
        builder.matched()
    })
}

/// Matches if the elements in the asserted collection are sorted weakly monotone according to the given `predicate` in any order.
///
/// The `predicate` is applied to all consecutive pairs of elements and returns the `Ordering` of the pair.
/// The first `Ordering` different to `Ordering::Equal` defines the expected order of the collection.
/// The ordering is allowed to be weakly monotone, i.e., equal elements are allowed to follow each other.
/// An empty collection is assumed to be always sorted.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::collection::*;
/// # fn main() {
/// assert_that!(&vec![5,4,3,3,2,1,1], sorted_by_in_any_order(|a: &i32, b: &i32| a.cmp(b)));
/// assert_that!(&vec![1,1,2,3,3,4,5], sorted_by_in_any_order(|a: &i32, b: &i32| a.cmp(b)));
/// # }
pub fn sorted_by_in_any_order<'a,T,I,P>(predicate: P) -> Box<Fn(&'a I) -> MatchResult>
where &'a I: IntoIterator<Item=&'a T> + 'a,
      T: Ord + Debug + 'a,
      P: Fn(&'a T,&'a T) -> std::cmp::Ordering + 'static {
    Box::new(move |elements: &'a I| {
        let builder = MatchResultBuilder::for_("sorted_by_in_any_order");
        let mut iter = elements.into_iter();
        let mut expected_ordering: Option<std::cmp::Ordering> = None;
        let maybe_prev = iter.next();
        if maybe_prev.is_none() {
            return MatchResult::Matched { name: "sorted_by_in_any_order".to_owned() };
        }
        let mut prev = maybe_prev.unwrap();

        for cur in iter {
            let ordering = predicate(&prev, &cur);
            if expected_ordering == None && ordering != std::cmp::Ordering::Equal {
                expected_ordering = Some(ordering);
            } else if ordering != std::cmp::Ordering::Equal
                      && expected_ordering.unwrap() != ordering  {
                return builder.failed_because(
                    &format!("ordering is not monotone: predicate({:?}, {:?}) != {:?}",
                             prev, cur, expected_ordering.unwrap())
                );
            }
            prev = cur;
        }
        builder.matched()
    })
}

/// Matches if the elements in the asserted collection are sorted strictly monotone according to the given `predicate` in any order.
///
/// The `predicate` is applied to all consecutive pairs of elements and returns the `Ordering` of the pair.
/// The first `Ordering` different to `Ordering::Equal` defines the expected order of the collection.
/// The ordering is allowed to be weakly monotone, i.e., equal elements are allowed to follow each other.
/// An empty collection is assumed to be always sorted.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::collection::*;
/// # fn main() {
/// assert_that!(&vec![5,4,3,2,1], sorted_strictly_by_in_any_order(|a: &i32, b: &i32| a.cmp(b)));
/// assert_that!(&vec![1,2,3,4,5], sorted_strictly_by_in_any_order(|a: &i32, b: &i32| a.cmp(b)));
/// # }
pub fn sorted_strictly_by_in_any_order<'a,T,I,P>(predicate: P) -> Box<Fn(&'a I) -> MatchResult>
where &'a I: IntoIterator<Item=&'a T> + 'a,
      T: Ord + Debug + 'a,
      P: Fn(&'a T,&'a T) -> std::cmp::Ordering + 'static {
    Box::new(move |elements: &'a I| {
        let builder = MatchResultBuilder::for_("sorted_strictly_by_in_any_order");
        let mut iter = elements.into_iter();
        let mut expected_ordering: Option<std::cmp::Ordering> = None;
        let maybe_prev = iter.next();
        if maybe_prev.is_none() {
            return builder.matched();
        }
        let mut prev = maybe_prev.unwrap();

        for cur in iter {
            let ordering = predicate(&prev, &cur);
            if ordering == std::cmp::Ordering::Equal {
                return builder.failed_because(
                    &format!("ordering is not strictly monotone: predicate({:?}, {:?}) = {:?}",
                             prev, cur, ordering)
                );
            }
            if expected_ordering == None {
                expected_ordering = Some(ordering);
            } else if expected_ordering.unwrap() != ordering  {
                return builder.failed_because(
                    &format!("ordering is not strictly monotone: predicate({:?}, {:?}) != {:?}",
                             prev, cur, expected_ordering.unwrap())
                );
            }
            prev = cur;
        }
        builder.matched()
    })
}

/// Matches if the asserted collection is sorted weakly ascending.
///
/// An empty collection is assumed to be always sorted.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::collection::*;
/// # fn main() {
/// assert_that!(&vec![1,2,2,3,4,4,5], sorted_ascending());
/// # }
pub fn sorted_ascending<'a,T,I>() -> Box<Fn(&'a I) -> MatchResult>
where &'a I: IntoIterator<Item=&'a T> + 'a,
      T: Ord + Debug + 'a {
    sorted_by(|a: &T, b: &T| a.cmp(b), std::cmp::Ordering::Less)
}

/// Matches if the asserted collection is sorted strictly ascending.
///
/// An empty collection is assumed to be always sorted.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::collection::*;
/// # fn main() {
/// assert_that!(&vec![1,2,3,4,5], sorted_strictly_ascending());
/// # }
pub fn sorted_strictly_ascending<'a,T,I>() -> Box<Fn(&'a I) -> MatchResult>
where &'a I: IntoIterator<Item=&'a T> + 'a,
      T: Ord + Debug + 'a {
    sorted_strictly_by(|a: &T, b: &T| a.cmp(b), std::cmp::Ordering::Less)
}

/// Matches if the asserted collection is sorted weakly descending.
///
/// An empty collection is assumed to be always sorted.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::collection::*;
/// # fn main() {
/// assert_that!(&vec![5,4,4,3,3,2,1], sorted_descending());
/// # }
pub fn sorted_descending<'a,T,I>() -> Box<Fn(&'a I) -> MatchResult>
where &'a I: IntoIterator<Item=&'a T> + 'a,
      T: Ord + Debug + 'a {
    sorted_by(|a: &T, b: &T| a.cmp(b), std::cmp::Ordering::Greater)
}

/// Matches if the asserted collection is sorted strictly descending.
///
/// An empty collection is assumed to be always sorted.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::collection::*;
/// # fn main() {
/// assert_that!(&vec![5,4,3,2,1], sorted_strictly_descending());
/// # }
pub fn sorted_strictly_descending<'a,T,I>() -> Box<Fn(&'a I) -> MatchResult>
where &'a I: IntoIterator<Item=&'a T> + 'a,
      T: Ord + Debug + 'a {
    sorted_strictly_by(|a: &T, b: &T| a.cmp(b), std::cmp::Ordering::Greater)
}

/// Matches if all elements in the asserted collection satisfy the given `predicate`.
///
/// An empty collection always satisfies this matcher as all (=no) element satisfies the predicate.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::collection::*;
/// # fn main() {
/// assert_that!(&vec![1,2,3,4,5], all_elements_satisfy(|&a| 0 <= a && a < 100));
/// # }
pub fn all_elements_satisfy<'a,T,I,P>(predicate: P) -> Box<Fn(&'a I) -> MatchResult>
where T: Debug + 'a,
      &'a I: IntoIterator<Item=&'a T> + 'a,
      P: Fn(&'a T) -> bool + 'static {
    Box::new(move |elements: &'a I| {
        let builder = MatchResultBuilder::for_("all_elements_satisfy");
        let nonsatisfying_elements: Vec<_> = elements.into_iter().filter(|e| !predicate(e)).collect();
        if !nonsatisfying_elements.is_empty() {
            builder.failed_because(
                &format!("the following elements do not satisfy the predicate: {:?}", nonsatisfying_elements)
            )
        } else {
            builder.matched()
        }
    })
}

/// Matches if at least one element in the asserted collection satisfy the given `predicate`.
///
/// An empty collection never satisfies this matcher as no element satisfies the predicate.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::collection::*;
/// # fn main() {
/// assert_that!(&vec![1,2,3,4,5], some_elements_satisfy(|&a| 2 <= a && a < 5));
/// # }
pub fn some_elements_satisfy<'a,T,I,P>(predicate: P) -> Box<Fn(&'a I) -> MatchResult>
where T: Debug + 'a,
      &'a I: IntoIterator<Item=&'a T> + 'a,
      P: Fn(&T) -> bool + 'static {
    Box::new(move |elements: &'a I| {
        let builder = MatchResultBuilder::for_("some_elements_satisfy");
        if !elements.into_iter().any(|ref e| predicate(e)) {
            builder.failed_because("no elements satisfy the predicate")
        } else {
            builder.matched()
        }
    })
}

/// Matches if the map-like collection contains the given key/value pair.
///
/// The `Matcher` tests for this by converting the map-like data structure
/// into a key/value pair iterator.
///
/// The alternative would be to use the Index trait though experiments showed
/// that this would not be composable with `all_of!` or `any_of!`.
pub struct HasEntry<K,V> {
    key: K,
    value: V
}

/// Matches if the map-like collection contains the given key/value pair.
///
/// The `Matcher` tests for this by converting the map-like data structure
/// into a key/value pair iterator.
///
/// The alternative would be to use the Index trait though experiments showed
/// that this would not be composable with `all_of!` or `any_of!`.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::collection::*;
/// # fn main() {
/// let mut map = std::collections::HashMap::<i32,i32>::new();
/// map.insert(0, 2);
/// map.insert(1, 2);
/// map.insert(2, 5);
/// map.insert(3, 3);
/// map.insert(4, 3);
///
/// assert_that!(&map, has_entry(1, 2));
/// # }
pub fn has_entry<'a,K:'a,V:'a,M:'a>(key: K, value: V) -> Box<Matcher<'a,M> + 'a>
where &'a M: IntoIterator<Item=(&'a K,&'a V)> + 'a,
      HasEntry<K,V>: Matcher<'a,M> {
    Box::new(HasEntry {
        key: key,
        value: value
    })
}

impl<'a,K,V,M> Matcher<'a,M> for HasEntry<K,V>
where V: PartialEq + Debug + 'a,
      K: PartialEq + Debug + 'a,
      &'a M: IntoIterator<Item=(&'a K,&'a V)> + 'a {

    fn check(&self, map: &'a M) -> MatchResult {
        let builder = MatchResultBuilder::for_("has_entry");
        let mut same_keys = Vec::new();
        let mut same_values = Vec::new();
        for (key, value) in map.into_iter() {
            if key == &self.key && value == &self.value {
                return builder.matched()
            }
            if key == &self.key {
                same_keys.push(value);
            }
            if value == &self.value {
                same_values.push(key);
            }
        }

        builder.failed_because(&format!(
            "Entry ({:?}, {:?}) not found.\n\tEntries with same key: {:?}\n\tEntries with same value: {:?}",
            &self.key, &self.value,
            same_keys, same_values
        ))
    }
}

/// Matches if the map-like collection contains the given key.
///
/// The `Matcher` tests for this by converting the map-like data structure
/// into a key/value pair iterator.
///
/// The alternative would be to use the Index trait though experiments showed
/// that this would not be composable with `all_of!` or `any_of!`.
pub struct HasKey<K> {
    key: K
}

/// Matches if the map-like collection contains the given key.
///
/// The `Matcher` tests for this by converting the map-like data structure
/// into a key/value pair iterator.
///
/// The alternative would be to use the Index trait though experiments showed
/// that this would not be composable with `all_of!` or `any_of!`.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::collection::*;
/// # fn main() {
/// let mut map = std::collections::HashMap::<i32,i32>::new();
/// map.insert(0, 2);
/// map.insert(1, 2);
/// map.insert(2, 5);
/// map.insert(3, 3);
/// map.insert(4, 3);
///
/// assert_that!(&map, has_key(2));
/// # }
pub fn has_key<'a,K:'a,V:'a,M:'a>(key: K) -> Box<Matcher<'a,M> + 'a>
where &'a M: IntoIterator<Item=(&'a K,&'a V)> + 'a,
      HasKey<K>: Matcher<'a,M> {
    Box::new(HasKey {
        key: key
    })
}

impl<'a,K,V,M> Matcher<'a,M> for HasKey<K>
where V: PartialEq + Debug + 'a,
      K: PartialEq + Debug + 'a,
      &'a M: IntoIterator<Item=(&'a K,&'a V)> + 'a {

    fn check(&self, map: &'a M) -> MatchResult {
        let builder = MatchResultBuilder::for_("has_key");
        for (key, _) in map.into_iter() {
            if key == &self.key {
                return builder.matched();
            }
        }

        builder.failed_because(&format!("No entrywith key {:?} found", &self.key))
    }
}


/// Matches if the map-like collection contains the given value.
///
/// The `Matcher` tests for this by converting the map-like data structure
/// into a key/value pair iterator.
pub struct HasValue<V> {
    value: V
}

/// Matches if the map-like collection contains the given value.
///
/// The `Matcher` tests for this by converting the map-like data structure
/// into a key/value pair iterator.
///
/// #Examples
/// ```rust
/// # #[macro_use] extern crate galvanic_assert;
/// use galvanic_assert::matchers::collection::*;
/// # fn main() {
/// let mut map = std::collections::HashMap::<i32,i32>::new();
/// map.insert(0, 2);
/// map.insert(1, 2);
/// map.insert(2, 5);
/// map.insert(3, 3);
/// map.insert(4, 3);
///
/// assert_that!(&map, has_value(3));
/// # }
pub fn has_value<'a,K:'a,V:'a,M:'a>(key: K) -> Box<Matcher<'a,M> + 'a>
where &'a M: IntoIterator<Item=(&'a K,&'a V)> + 'a,
      HasKey<K>: Matcher<'a,M> {
    Box::new(HasKey {
        key: key
    })
}

impl<'a,K,V,M> Matcher<'a,M> for HasValue<V>
where V: PartialEq + Debug + 'a,
      K: PartialEq + Debug + 'a,
      &'a M: IntoIterator<Item=(&'a K,&'a V)> + 'a {

    fn check(&self, map: &'a M) -> MatchResult {
        let builder = MatchResultBuilder::for_("has_value");
        for (_, value) in map.into_iter() {
            if value == &self.value {
                return builder.matched();
            }
        }

        builder.failed_because(&format!("No entry with value {:?} found", &self.value))
    }
}