OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_SUGGESTION_TEST_HELPERS_H_ |
| 6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_SUGGESTION_TEST_HELPERS_H_ |
| 7 |
| 8 #include "components/autofill/core/browser/suggestion.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace autofill { |
| 13 |
| 14 // Gmock matcher that allows checking a member of the Suggestion class in a |
| 15 // vector. This wraps a GMock container matcher, converts the suggestion |
| 16 // members to a vector, and then runs the container matcher against the result |
| 17 // to test an argument. See SuggestionVectorIdsAre() below. |
| 18 template<typename EltType> |
| 19 class SuggestionVectorMembersAreMatcher |
| 20 : public testing::MatcherInterface<const std::vector<Suggestion>&> { |
| 21 public: |
| 22 typedef std::vector<EltType> Container; |
| 23 typedef testing::Matcher<Container> ContainerMatcher; |
| 24 |
| 25 SuggestionVectorMembersAreMatcher( |
| 26 const ContainerMatcher& seq_matcher, |
| 27 EltType Suggestion::*elt) |
| 28 : container_matcher_(seq_matcher), |
| 29 element_(elt) { |
| 30 } |
| 31 |
| 32 virtual bool MatchAndExplain(const std::vector<Suggestion>& suggestions, |
| 33 testing::MatchResultListener* listener) const { |
| 34 Container container; |
| 35 for (const auto& suggestion : suggestions) |
| 36 container.push_back(suggestion.*element_); |
| 37 return container_matcher_.MatchAndExplain(container, listener); |
| 38 } |
| 39 |
| 40 virtual void DescribeTo(::std::ostream* os) const { |
| 41 container_matcher_.DescribeTo(os); |
| 42 } |
| 43 |
| 44 virtual void DescribeNegationTo(::std::ostream* os) const { |
| 45 container_matcher_.DescribeNegationTo(os); |
| 46 } |
| 47 |
| 48 private: |
| 49 ContainerMatcher container_matcher_; |
| 50 EltType Suggestion::*element_; |
| 51 }; |
| 52 |
| 53 // Use this matcher to compare a sequence vector's IDs to a list. In an |
| 54 // EXPECT_CALL statement, use the following for an vector<Suggestion> argument |
| 55 // to compare the IDs against a constant list: |
| 56 // SuggestionVectorIdsAre(testing::ElementsAre(1, 2, 3, 4)) |
| 57 template<class EltsAreMatcher> |
| 58 inline testing::Matcher<const std::vector<Suggestion>&> |
| 59 SuggestionVectorIdsAre(const EltsAreMatcher& elts_are_matcher) { |
| 60 return testing::MakeMatcher( |
| 61 new SuggestionVectorMembersAreMatcher<int>( |
| 62 elts_are_matcher, &Suggestion::frontend_id)); |
| 63 } |
| 64 |
| 65 // Like SuggestionVectorIdsAre but tests the values instead of ID. |
| 66 template<class EltsAreMatcher> |
| 67 inline testing::Matcher<const std::vector<Suggestion>&> |
| 68 SuggestionVectorValuesAre(const EltsAreMatcher& elts_are_matcher) { |
| 69 return testing::MakeMatcher( |
| 70 new SuggestionVectorMembersAreMatcher<base::string16>( |
| 71 elts_are_matcher, &Suggestion::value)); |
| 72 } |
| 73 |
| 74 } // namespace autofill |
| 75 |
| 76 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_SUGGESTION_TEST_HELPERS_H_ |
OLD | NEW |