1 // Copyright 2013 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 "sync/internal_api/public/base/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
) {
44 struct InvalidationEqPredicate
{
45 InvalidationEqPredicate(const Invalidation
& inv1
)
48 bool operator()(const Invalidation
& inv2
) {
49 return inv1_
.Equals(inv2
);
52 const Invalidation
& inv1_
;
57 bool ObjectIdInvalidationMapEqMatcher::MatchAndExplain(
58 const ObjectIdInvalidationMap
& actual
,
59 MatchResultListener
* listener
) const {
61 std::vector
<syncer::Invalidation
> expected_invalidations
;
62 std::vector
<syncer::Invalidation
> actual_invalidations
;
64 expected_
.GetAllInvalidations(&expected_invalidations
);
65 actual
.GetAllInvalidations(&actual_invalidations
);
67 std::vector
<syncer::Invalidation
> expected_only
;
68 std::vector
<syncer::Invalidation
> actual_only
;
70 for (std::vector
<syncer::Invalidation
>::iterator it
=
71 expected_invalidations
.begin();
72 it
!= expected_invalidations
.end(); ++it
) {
73 if (std::find_if(actual_invalidations
.begin(),
74 actual_invalidations
.end(),
75 InvalidationEqPredicate(*it
))
76 == actual_invalidations
.end()) {
77 expected_only
.push_back(*it
);
81 for (std::vector
<syncer::Invalidation
>::iterator it
=
82 actual_invalidations
.begin();
83 it
!= actual_invalidations
.end(); ++it
) {
84 if (std::find_if(expected_invalidations
.begin(),
85 expected_invalidations
.end(),
86 InvalidationEqPredicate(*it
))
87 == expected_invalidations
.end()) {
88 actual_only
.push_back(*it
);
92 if (expected_only
.empty() && actual_only
.empty())
95 bool printed_header
= false;
96 if (!actual_only
.empty()) {
97 *listener
<< " which has these unexpected elements: "
98 << PrintToString(actual_only
);
99 printed_header
= true;
102 if (!expected_only
.empty()) {
103 *listener
<< (printed_header
? ",\nand" : "which")
104 << " doesn't have these expected elements: "
105 << PrintToString(expected_only
);
106 printed_header
= true;
112 void ObjectIdInvalidationMapEqMatcher::DescribeTo(::std::ostream
* os
) const {
113 *os
<< " is equal to " << PrintToString(expected_
);
116 void ObjectIdInvalidationMapEqMatcher::DescribeNegationTo(
117 ::std::ostream
* os
) const {
118 *os
<< " isn't equal to " << PrintToString(expected_
);
123 Matcher
<const ObjectIdInvalidationMap
&> Eq(
124 const ObjectIdInvalidationMap
& expected
) {
125 return MakeMatcher(new ObjectIdInvalidationMapEqMatcher(expected
));
128 } // namespace syncer