Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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_state_map_test_util.h" | |
| 6 | |
| 7 #include <algorithm> | |
|
akalin
2012/08/22 22:24:31
newline after <algorithm>
dcheng
2012/08/22 22:53:19
Done.
| |
| 8 #include "base/basictypes.h" | |
| 9 | |
| 10 using ::testing::MakeMatcher; | |
|
akalin
2012/08/22 22:24:31
move into namespace
dcheng
2012/08/22 22:53:19
Done.
| |
| 11 using ::testing::MatchResultListener; | |
| 12 using ::testing::Matcher; | |
| 13 using ::testing::MatcherInterface; | |
| 14 using ::testing::PrintToString; | |
| 15 | |
| 16 namespace syncer { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 class ObjectIdStateMapEqMatcher | |
| 21 : public MatcherInterface<const ObjectIdStateMap&> { | |
| 22 public: | |
| 23 explicit ObjectIdStateMapEqMatcher(const ObjectIdStateMap& expected); | |
| 24 | |
| 25 virtual bool MatchAndExplain(const ObjectIdStateMap& actual, | |
| 26 MatchResultListener* listener) const; | |
| 27 virtual void DescribeTo(::std::ostream* os) const; | |
| 28 virtual void DescribeNegationTo(::std::ostream* os) const; | |
| 29 | |
| 30 private: | |
| 31 ObjectIdStateMap expected_; | |
|
akalin
2012/08/22 22:24:31
const
dcheng
2012/08/22 22:53:19
Done.
| |
| 32 | |
| 33 DISALLOW_COPY_AND_ASSIGN(ObjectIdStateMapEqMatcher); | |
| 34 }; | |
| 35 | |
| 36 ObjectIdStateMapEqMatcher::ObjectIdStateMapEqMatcher( | |
| 37 const ObjectIdStateMap& expected) : expected_(expected) { | |
| 38 } | |
| 39 | |
| 40 bool ObjectIdStateMapEqMatcher::MatchAndExplain( | |
| 41 const ObjectIdStateMap& actual, MatchResultListener* listener) const { | |
| 42 ObjectIdStateMap expected_only; | |
| 43 ObjectIdStateMap actual_only; | |
| 44 typedef std::pair<invalidation::ObjectId, | |
| 45 std::pair<InvalidationState, InvalidationState> > | |
| 46 ValueDifference; | |
| 47 std::vector<ValueDifference> value_differences; | |
| 48 | |
| 49 std::set_difference(expected_.begin(), expected_.end(), | |
| 50 actual.begin(), actual.end(), | |
| 51 std::inserter(expected_only, expected_only.begin()), | |
| 52 expected_.value_comp()); | |
| 53 std::set_difference(actual.begin(), actual.end(), | |
| 54 expected_.begin(), expected_.end(), | |
| 55 std::inserter(actual_only, actual_only.begin()), | |
| 56 actual.value_comp()); | |
| 57 | |
| 58 for (ObjectIdStateMap::const_iterator it = expected_.begin(); | |
| 59 it != expected_.end(); ++it) { | |
| 60 ObjectIdStateMap::const_iterator find_it = | |
| 61 actual.find(it->first); | |
| 62 if (find_it != actual.end() && | |
| 63 !Matches(Eq(it->second))(find_it->second)) { | |
| 64 value_differences.push_back(std::make_pair( | |
| 65 it->first, std::make_pair(it->second, find_it->second))); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 if (expected_only.empty() && actual_only.empty() && value_differences.empty()) | |
| 70 return true; | |
| 71 | |
| 72 bool printed_header = false; | |
| 73 if (!actual_only.empty()) { | |
| 74 *listener << " which has these unexpected elements: " | |
| 75 << PrintToString(actual_only); | |
| 76 printed_header = true; | |
| 77 } | |
| 78 | |
| 79 if (!expected_only.empty()) { | |
| 80 *listener << (printed_header ? ",\nand" : "which") | |
| 81 << " doesn't have these expected elements: " | |
| 82 << PrintToString(expected_only); | |
| 83 printed_header = true; | |
| 84 } | |
| 85 | |
| 86 if (!value_differences.empty()) { | |
| 87 *listener << (printed_header ? ",\nand" : "which") | |
| 88 << " differ in the following values: " | |
| 89 << PrintToString(value_differences); | |
| 90 } | |
| 91 | |
| 92 return false; | |
| 93 } | |
| 94 | |
| 95 void ObjectIdStateMapEqMatcher::DescribeTo(::std::ostream* os) const { | |
| 96 *os << " is equal to " << PrintToString(expected_); | |
| 97 } | |
| 98 | |
| 99 void ObjectIdStateMapEqMatcher::DescribeNegationTo(::std::ostream* os) const { | |
| 100 *os << " isn't equal to " << PrintToString(expected_); | |
| 101 } | |
| 102 | |
| 103 } // namespace | |
| 104 | |
| 105 Matcher<const ObjectIdStateMap&> Eq(const ObjectIdStateMap& expected) { | |
| 106 return MakeMatcher(new ObjectIdStateMapEqMatcher(expected)); | |
| 107 } | |
| 108 | |
| 109 } // namespace syncer | |
| OLD | NEW |