1 // Copyright (c) 2012 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/notifier/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& actual, 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 bool ObjectIdInvalidationMapEqMatcher::MatchAndExplain( 43 const ObjectIdInvalidationMap& actual, 44 MatchResultListener* listener) const { 45 ObjectIdInvalidationMap expected_only; 46 ObjectIdInvalidationMap actual_only; 47 typedef std::pair<invalidation::ObjectId, 48 std::pair<Invalidation, Invalidation> > 49 ValueDifference; 50 std::vector<ValueDifference> value_differences; 51 52 std::set_difference(expected_.begin(), expected_.end(), 53 actual.begin(), actual.end(), 54 std::inserter(expected_only, expected_only.begin()), 55 expected_.value_comp()); 56 std::set_difference(actual.begin(), actual.end(), 57 expected_.begin(), expected_.end(), 58 std::inserter(actual_only, actual_only.begin()), 59 actual.value_comp()); 60 61 for (ObjectIdInvalidationMap::const_iterator it = expected_.begin(); 62 it != expected_.end(); ++it) { 63 ObjectIdInvalidationMap::const_iterator find_it = 64 actual.find(it->first); 65 if (find_it != actual.end() && 66 !Matches(Eq(it->second))(find_it->second)) { 67 value_differences.push_back(std::make_pair( 68 it->first, std::make_pair(it->second, find_it->second))); 69 } 70 } 71 72 if (expected_only.empty() && actual_only.empty() && value_differences.empty()) 73 return true; 74 75 bool printed_header = false; 76 if (!actual_only.empty()) { 77 *listener << " which has these unexpected elements: " 78 << PrintToString(actual_only); 79 printed_header = true; 80 } 81 82 if (!expected_only.empty()) { 83 *listener << (printed_header ? ",\nand" : "which") 84 << " doesn't have these expected elements: " 85 << PrintToString(expected_only); 86 printed_header = true; 87 } 88 89 if (!value_differences.empty()) { 90 *listener << (printed_header ? ",\nand" : "which") 91 << " differ in the following values: " 92 << PrintToString(value_differences); 93 } 94 95 return false; 96 } 97 98 void ObjectIdInvalidationMapEqMatcher::DescribeTo(::std::ostream* os) const { 99 *os << " is equal to " << PrintToString(expected_); 100 } 101 102 void ObjectIdInvalidationMapEqMatcher::DescribeNegationTo 103 (::std::ostream* os) const { 104 *os << " isn't equal to " << PrintToString(expected_); 105 } 106 107 } // namespace 108 109 Matcher<const ObjectIdInvalidationMap&> Eq( 110 const ObjectIdInvalidationMap& expected) { 111 return MakeMatcher(new ObjectIdInvalidationMapEqMatcher(expected)); 112 } 113 114 } // namespace syncer 115