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