1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/invalidation/impl/object_id_invalidation_map_test_util.h"
9 #include "base/basictypes.h"
13 using ::testing::MakeMatcher
;
14 using ::testing::MatchResultListener
;
15 using ::testing::Matcher
;
16 using ::testing::MatcherInterface
;
17 using ::testing::PrintToString
;
21 class ObjectIdInvalidationMapEqMatcher
22 : public MatcherInterface
<const ObjectIdInvalidationMap
&> {
24 explicit ObjectIdInvalidationMapEqMatcher(
25 const ObjectIdInvalidationMap
& expected
);
27 virtual bool MatchAndExplain(const ObjectIdInvalidationMap
& lhs
,
28 MatchResultListener
* listener
) const;
29 virtual void DescribeTo(::std::ostream
* os
) const;
30 virtual void DescribeNegationTo(::std::ostream
* os
) const;
33 const ObjectIdInvalidationMap expected_
;
35 DISALLOW_COPY_AND_ASSIGN(ObjectIdInvalidationMapEqMatcher
);
38 ObjectIdInvalidationMapEqMatcher::ObjectIdInvalidationMapEqMatcher(
39 const ObjectIdInvalidationMap
& expected
) : expected_(expected
) {
42 struct InvalidationEqPredicate
{
43 InvalidationEqPredicate(const Invalidation
& inv1
)
46 bool operator()(const Invalidation
& inv2
) {
47 return inv1_
.Equals(inv2
);
50 const Invalidation
& inv1_
;
53 bool ObjectIdInvalidationMapEqMatcher::MatchAndExplain(
54 const ObjectIdInvalidationMap
& actual
,
55 MatchResultListener
* listener
) const {
57 std::vector
<syncer::Invalidation
> expected_invalidations
;
58 std::vector
<syncer::Invalidation
> actual_invalidations
;
60 expected_
.GetAllInvalidations(&expected_invalidations
);
61 actual
.GetAllInvalidations(&actual_invalidations
);
63 std::vector
<syncer::Invalidation
> expected_only
;
64 std::vector
<syncer::Invalidation
> actual_only
;
66 for (std::vector
<syncer::Invalidation
>::iterator it
=
67 expected_invalidations
.begin();
68 it
!= expected_invalidations
.end(); ++it
) {
69 if (std::find_if(actual_invalidations
.begin(),
70 actual_invalidations
.end(),
71 InvalidationEqPredicate(*it
))
72 == actual_invalidations
.end()) {
73 expected_only
.push_back(*it
);
77 for (std::vector
<syncer::Invalidation
>::iterator it
=
78 actual_invalidations
.begin();
79 it
!= actual_invalidations
.end(); ++it
) {
80 if (std::find_if(expected_invalidations
.begin(),
81 expected_invalidations
.end(),
82 InvalidationEqPredicate(*it
))
83 == expected_invalidations
.end()) {
84 actual_only
.push_back(*it
);
88 if (expected_only
.empty() && actual_only
.empty())
91 bool printed_header
= false;
92 if (!actual_only
.empty()) {
93 *listener
<< " which has these unexpected elements: "
94 << PrintToString(actual_only
);
95 printed_header
= true;
98 if (!expected_only
.empty()) {
99 *listener
<< (printed_header
? ",\nand" : "which")
100 << " doesn't have these expected elements: "
101 << PrintToString(expected_only
);
102 printed_header
= true;
108 void ObjectIdInvalidationMapEqMatcher::DescribeTo(::std::ostream
* os
) const {
109 *os
<< " is equal to " << PrintToString(expected_
);
112 void ObjectIdInvalidationMapEqMatcher::DescribeNegationTo(
113 ::std::ostream
* os
) const {
114 *os
<< " isn't equal to " << PrintToString(expected_
);
119 Matcher
<const ObjectIdInvalidationMap
&> Eq(
120 const ObjectIdInvalidationMap
& expected
) {
121 return MakeMatcher(new ObjectIdInvalidationMapEqMatcher(expected
));
124 } // namespace syncer