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. 4 5 #include "sync/internal_api/public/base/object_id_invalidation_map_test_util.h" 6 7 #include <algorithm> 8 9 #include "base/basictypes.h" 10 11 namespace syncer { 12 13 using ::testing::MakeMatcher; 14 using ::testing::MatchResultListener; 15 using ::testing::Matcher; 16 using ::testing::MatcherInterface; 17 using ::testing::PrintToString; 18 19 namespace { 20 21 class ObjectIdInvalidationMapEqMatcher 22 : public MatcherInterface<const ObjectIdInvalidationMap&> { 23 public: 24 explicit ObjectIdInvalidationMapEqMatcher( 25 const ObjectIdInvalidationMap& expected); 26 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; 31 32 private: 33 const ObjectIdInvalidationMap expected_; 34 35 DISALLOW_COPY_AND_ASSIGN(ObjectIdInvalidationMapEqMatcher); 36 }; 37 38 ObjectIdInvalidationMapEqMatcher::ObjectIdInvalidationMapEqMatcher( 39 const ObjectIdInvalidationMap& expected) : expected_(expected) { 40 } 41 42 namespace { 43 44 struct InvalidationEqPredicate { 45 InvalidationEqPredicate(const Invalidation& inv1) 46 : inv1_(inv1) { } 47 48 bool operator()(const Invalidation& inv2) { 49 return inv1_.Equals(inv2); 50 } 51 52 const Invalidation& inv1_; 53 }; 54 55 } 56 57 bool ObjectIdInvalidationMapEqMatcher::MatchAndExplain( 58 const ObjectIdInvalidationMap& actual, 59 MatchResultListener* listener) const { 60 61 std::vector<syncer::Invalidation> expected_invalidations; 62 std::vector<syncer::Invalidation> actual_invalidations; 63 64 expected_.GetAllInvalidations(&expected_invalidations); 65 actual.GetAllInvalidations(&actual_invalidations); 66 67 std::vector<syncer::Invalidation> expected_only; 68 std::vector<syncer::Invalidation> actual_only; 69 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); 78 } 79 } 80 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); 89 } 90 } 91 92 if (expected_only.empty() && actual_only.empty()) 93 return true; 94 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; 100 } 101 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; 107 } 108 109 return false; 110 } 111 112 void ObjectIdInvalidationMapEqMatcher::DescribeTo(::std::ostream* os) const { 113 *os << " is equal to " << PrintToString(expected_); 114 } 115 116 void ObjectIdInvalidationMapEqMatcher::DescribeNegationTo( 117 ::std::ostream* os) const { 118 *os << " isn't equal to " << PrintToString(expected_); 119 } 120 121 } // namespace 122 123 Matcher<const ObjectIdInvalidationMap&> Eq( 124 const ObjectIdInvalidationMap& expected) { 125 return MakeMatcher(new ObjectIdInvalidationMapEqMatcher(expected)); 126 } 127 128 } // namespace syncer 129