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