Chromium Code Reviews| Index: sync/notifier/object_id_state_map_test_util.cc |
| diff --git a/sync/notifier/object_id_state_map_test_util.cc b/sync/notifier/object_id_state_map_test_util.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9ea8b87dc295abd3d418cf44fcac8ec994d68669 |
| --- /dev/null |
| +++ b/sync/notifier/object_id_state_map_test_util.cc |
| @@ -0,0 +1,90 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "sync/notifier/object_id_state_map_test_util.h" |
| + |
| +#include <algorithm> |
| +#include "base/basictypes.h" |
| +#include "sync/internal_api/public/base/invalidation_state_test_util.h" |
| + |
| +using ::testing::MakeMatcher; |
| +using ::testing::MatchResultListener; |
| +using ::testing::Matcher; |
| +using ::testing::MatcherInterface; |
| +using ::testing::PrintToString; |
| + |
| +namespace syncer { |
| + |
| +namespace { |
| + |
| +class ObjectIdStateMapEqMatcher |
| + : public MatcherInterface<const ObjectIdStateMap&> { |
| + public: |
| + explicit ObjectIdStateMapEqMatcher(const ObjectIdStateMap& expected); |
| + |
| + virtual bool MatchAndExplain(const ObjectIdStateMap& actual, |
| + MatchResultListener* listener) const; |
| + virtual void DescribeTo(::std::ostream* os) const; |
| + virtual void DescribeNegationTo(::std::ostream* os) const; |
| + |
| + private: |
| + ObjectIdStateMap expected_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ObjectIdStateMapEqMatcher); |
| +}; |
| + |
| +ObjectIdStateMapEqMatcher::ObjectIdStateMapEqMatcher( |
| + const ObjectIdStateMap& expected) : expected_(expected) { |
| +} |
| + |
| +bool ObjectIdStateMapEqMatcher::MatchAndExplain( |
| + const ObjectIdStateMap& actual, MatchResultListener* listener) const { |
|
dcheng
2012/08/18 00:41:29
I think it would be nice if I pulled this out into
|
| + ObjectIdStateMap expected_only; |
| + ObjectIdStateMap actual_only; |
| + |
| + std::set_difference(expected_.begin(), expected_.end(), |
| + actual.begin(), actual.end(), |
| + std::inserter(expected_only, expected_only.begin()), |
| + expected_.value_comp()); |
| + std::set_difference(actual.begin(), actual.end(), |
| + expected_.begin(), expected_.end(), |
| + std::inserter(actual_only, actual_only.begin()), |
| + actual.value_comp()); |
| + |
| + if (expected_only.empty() && actual_only.empty()) |
| + return true; |
| + |
| + bool printed_header = false; |
| + if (!actual_only.empty()) { |
| + *listener << " which has these unexpected elements: " |
| + << PrintToString(actual_only); |
| + printed_header = true; |
| + } |
| + |
| + if (!expected_only.empty()) { |
| + *listener << (printed_header ? ",\nand" : "which") |
| + << "doesn't have these expected elements: " |
| + << PrintToString(expected_only); |
| + printed_header = true; |
| + } |
| + |
| + |
| + return false; |
| +} |
| + |
| +void ObjectIdStateMapEqMatcher::DescribeTo(::std::ostream* os) const { |
| + *os << " is equal to " << PrintToString(expected_); |
| +} |
| + |
| +void ObjectIdStateMapEqMatcher::DescribeNegationTo(::std::ostream* os) const { |
| + *os << " isn't equal to " << PrintToString(expected_); |
| +} |
| + |
| +} // namespace |
| + |
| +Matcher<const ObjectIdStateMap&> Eq(const ObjectIdStateMap& expected) { |
| + return MakeMatcher(new ObjectIdStateMapEqMatcher(expected)); |
| +} |
| + |
| +} // namespace syncer |