| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sync/notifier/object_id_invalidation_map_test_util.h" | 5 #include "sync/internal_api/public/base/object_id_invalidation_map_test_util.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 | 10 |
| 11 namespace syncer { | 11 namespace syncer { |
| 12 | 12 |
| 13 using ::testing::MakeMatcher; | 13 using ::testing::MakeMatcher; |
| 14 using ::testing::MatchResultListener; | 14 using ::testing::MatchResultListener; |
| 15 using ::testing::Matcher; | 15 using ::testing::Matcher; |
| 16 using ::testing::MatcherInterface; | 16 using ::testing::MatcherInterface; |
| 17 using ::testing::PrintToString; | 17 using ::testing::PrintToString; |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 class ObjectIdInvalidationMapEqMatcher | 21 class ObjectIdInvalidationMapEqMatcher |
| 22 : public MatcherInterface<const ObjectIdInvalidationMap&> { | 22 : public MatcherInterface<const ObjectIdInvalidationMap&> { |
| 23 public: | 23 public: |
| 24 explicit ObjectIdInvalidationMapEqMatcher( | 24 explicit ObjectIdInvalidationMapEqMatcher( |
| 25 const ObjectIdInvalidationMap& expected); | 25 const ObjectIdInvalidationMap& expected); |
| 26 | 26 |
| 27 virtual bool MatchAndExplain(const ObjectIdInvalidationMap& actual, | 27 virtual bool MatchAndExplain(const ObjectIdInvalidationMap& lhs, |
| 28 MatchResultListener* listener) const; | 28 MatchResultListener* listener) const; |
| 29 virtual void DescribeTo(::std::ostream* os) const; | 29 virtual void DescribeTo(::std::ostream* os) const; |
| 30 virtual void DescribeNegationTo(::std::ostream* os) const; | 30 virtual void DescribeNegationTo(::std::ostream* os) const; |
| 31 | 31 |
| 32 private: | 32 private: |
| 33 const ObjectIdInvalidationMap expected_; | 33 const ObjectIdInvalidationMap expected_; |
| 34 | 34 |
| 35 DISALLOW_COPY_AND_ASSIGN(ObjectIdInvalidationMapEqMatcher); | 35 DISALLOW_COPY_AND_ASSIGN(ObjectIdInvalidationMapEqMatcher); |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 ObjectIdInvalidationMapEqMatcher::ObjectIdInvalidationMapEqMatcher( | 38 ObjectIdInvalidationMapEqMatcher::ObjectIdInvalidationMapEqMatcher( |
| 39 const ObjectIdInvalidationMap& expected) : expected_(expected) { | 39 const ObjectIdInvalidationMap& expected) : expected_(expected) { |
| 40 } | 40 } |
| 41 | 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 |
| 42 bool ObjectIdInvalidationMapEqMatcher::MatchAndExplain( | 57 bool ObjectIdInvalidationMapEqMatcher::MatchAndExplain( |
| 43 const ObjectIdInvalidationMap& actual, | 58 const ObjectIdInvalidationMap& actual, |
| 44 MatchResultListener* listener) const { | 59 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 | 60 |
| 52 std::set_difference(expected_.begin(), expected_.end(), | 61 std::vector<syncer::Invalidation> expected_invalidations; |
| 53 actual.begin(), actual.end(), | 62 std::vector<syncer::Invalidation> actual_invalidations; |
| 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 | 63 |
| 61 for (ObjectIdInvalidationMap::const_iterator it = expected_.begin(); | 64 expected_.GetAllInvalidations(&expected_invalidations); |
| 62 it != expected_.end(); ++it) { | 65 actual.GetAllInvalidations(&actual_invalidations); |
| 63 ObjectIdInvalidationMap::const_iterator find_it = | 66 |
| 64 actual.find(it->first); | 67 std::vector<syncer::Invalidation> expected_only; |
| 65 if (find_it != actual.end() && | 68 std::vector<syncer::Invalidation> actual_only; |
| 66 !Matches(Eq(it->second))(find_it->second)) { | 69 |
| 67 value_differences.push_back(std::make_pair( | 70 for (std::vector<syncer::Invalidation>::iterator it = |
| 68 it->first, std::make_pair(it->second, find_it->second))); | 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); |
| 69 } | 78 } |
| 70 } | 79 } |
| 71 | 80 |
| 72 if (expected_only.empty() && actual_only.empty() && value_differences.empty()) | 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()) |
| 73 return true; | 93 return true; |
| 74 | 94 |
| 75 bool printed_header = false; | 95 bool printed_header = false; |
| 76 if (!actual_only.empty()) { | 96 if (!actual_only.empty()) { |
| 77 *listener << " which has these unexpected elements: " | 97 *listener << " which has these unexpected elements: " |
| 78 << PrintToString(actual_only); | 98 << PrintToString(actual_only); |
| 79 printed_header = true; | 99 printed_header = true; |
| 80 } | 100 } |
| 81 | 101 |
| 82 if (!expected_only.empty()) { | 102 if (!expected_only.empty()) { |
| 83 *listener << (printed_header ? ",\nand" : "which") | 103 *listener << (printed_header ? ",\nand" : "which") |
| 84 << " doesn't have these expected elements: " | 104 << " doesn't have these expected elements: " |
| 85 << PrintToString(expected_only); | 105 << PrintToString(expected_only); |
| 86 printed_header = true; | 106 printed_header = true; |
| 87 } | 107 } |
| 88 | 108 |
| 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; | 109 return false; |
| 96 } | 110 } |
| 97 | 111 |
| 98 void ObjectIdInvalidationMapEqMatcher::DescribeTo(::std::ostream* os) const { | 112 void ObjectIdInvalidationMapEqMatcher::DescribeTo(::std::ostream* os) const { |
| 99 *os << " is equal to " << PrintToString(expected_); | 113 *os << " is equal to " << PrintToString(expected_); |
| 100 } | 114 } |
| 101 | 115 |
| 102 void ObjectIdInvalidationMapEqMatcher::DescribeNegationTo | 116 void ObjectIdInvalidationMapEqMatcher::DescribeNegationTo( |
| 103 (::std::ostream* os) const { | 117 ::std::ostream* os) const { |
| 104 *os << " isn't equal to " << PrintToString(expected_); | 118 *os << " isn't equal to " << PrintToString(expected_); |
| 105 } | 119 } |
| 106 | 120 |
| 107 } // namespace | 121 } // namespace |
| 108 | 122 |
| 109 Matcher<const ObjectIdInvalidationMap&> Eq( | 123 Matcher<const ObjectIdInvalidationMap&> Eq( |
| 110 const ObjectIdInvalidationMap& expected) { | 124 const ObjectIdInvalidationMap& expected) { |
| 111 return MakeMatcher(new ObjectIdInvalidationMapEqMatcher(expected)); | 125 return MakeMatcher(new ObjectIdInvalidationMapEqMatcher(expected)); |
| 112 } | 126 } |
| 113 | 127 |
| 114 } // namespace syncer | 128 } // namespace syncer |
| OLD | NEW |