topic_invalidation_map_test_util.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2020 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. #include "components/invalidation/impl/topic_invalidation_map_test_util.h"
  5. #include <base/containers/contains.h>
  6. #include <algorithm>
  7. namespace invalidation {
  8. using ::testing::MakeMatcher;
  9. using ::testing::Matcher;
  10. using ::testing::MatcherInterface;
  11. using ::testing::MatchResultListener;
  12. using ::testing::PrintToString;
  13. namespace {
  14. class TopicInvalidationMapEqMatcher
  15. : public MatcherInterface<const TopicInvalidationMap&> {
  16. public:
  17. explicit TopicInvalidationMapEqMatcher(const TopicInvalidationMap& expected);
  18. ~TopicInvalidationMapEqMatcher() override = default;
  19. bool MatchAndExplain(const TopicInvalidationMap& lhs,
  20. MatchResultListener* listener) const override;
  21. void DescribeTo(::std::ostream* os) const override;
  22. void DescribeNegationTo(::std::ostream* os) const override;
  23. private:
  24. const TopicInvalidationMap expected_;
  25. };
  26. TopicInvalidationMapEqMatcher::TopicInvalidationMapEqMatcher(
  27. const TopicInvalidationMap& expected)
  28. : expected_(expected) {}
  29. bool TopicInvalidationMapEqMatcher::MatchAndExplain(
  30. const TopicInvalidationMap& actual,
  31. MatchResultListener* listener) const {
  32. std::vector<Invalidation> expected_invalidations;
  33. std::vector<Invalidation> actual_invalidations;
  34. expected_.GetAllInvalidations(&expected_invalidations);
  35. actual.GetAllInvalidations(&actual_invalidations);
  36. std::vector<Invalidation> expected_only;
  37. std::vector<Invalidation> actual_only;
  38. for (const auto& expected_invalidation : expected_invalidations) {
  39. if (!base::Contains(actual_invalidations, expected_invalidation)) {
  40. expected_only.push_back(expected_invalidation);
  41. }
  42. }
  43. for (const auto& actual_invalidation : actual_invalidations) {
  44. if (!base::Contains(expected_invalidations, actual_invalidation)) {
  45. actual_only.push_back(actual_invalidation);
  46. }
  47. }
  48. if (expected_only.empty() && actual_only.empty()) {
  49. return true;
  50. }
  51. bool printed_header = false;
  52. if (!actual_only.empty()) {
  53. *listener << " which has these unexpected elements: "
  54. << PrintToString(actual_only);
  55. printed_header = true;
  56. }
  57. if (!expected_only.empty()) {
  58. *listener << (printed_header ? ",\nand" : "which")
  59. << " doesn't have these expected elements: "
  60. << PrintToString(expected_only);
  61. printed_header = true;
  62. }
  63. return false;
  64. }
  65. void TopicInvalidationMapEqMatcher::DescribeTo(::std::ostream* os) const {
  66. // TODO(crbug.com/1055286): seems there is no custom printer for
  67. // TopicInvalidationMap.
  68. *os << " is equal to " << PrintToString(expected_);
  69. }
  70. void TopicInvalidationMapEqMatcher::DescribeNegationTo(
  71. ::std::ostream* os) const {
  72. *os << " isn't equal to " << PrintToString(expected_);
  73. }
  74. } // namespace
  75. Matcher<const TopicInvalidationMap&> Eq(const TopicInvalidationMap& expected) {
  76. return MakeMatcher(new TopicInvalidationMapEqMatcher(expected));
  77. }
  78. } // namespace invalidation