| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 // Testing utilities that extend gtest. | 5 // Testing utilities that extend gtest. |
| 6 | 6 |
| 7 #ifndef NET_TEST_GTEST_UTIL_H_ | 7 #ifndef NET_TEST_GTEST_UTIL_H_ |
| 8 #define NET_TEST_GTEST_UTIL_H_ | 8 #define NET_TEST_GTEST_UTIL_H_ |
| 9 | 9 |
| 10 #include <string> |
| 11 |
| 12 #include "base/macros.h" |
| 13 #include "base/strings/string_piece.h" |
| 10 #include "base/test/mock_log.h" | 14 #include "base/test/mock_log.h" |
| 11 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 12 #include "net/test/scoped_disable_exit_on_dfatal.h" | 16 #include "net/test/scoped_disable_exit_on_dfatal.h" |
| 17 #include "testing/gmock/include/gmock/gmock-matchers.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 20 |
| 16 namespace net { | 21 namespace net { |
| 17 namespace test { | 22 namespace test { |
| 18 | 23 |
| 19 // A GMock matcher that checks whether the argument is the expected net::Error. | 24 // A GMock matcher that checks whether the argument is the expected net::Error. |
| 20 // On failure, the expected and actual net::Error names will be printed. | 25 // On failure, the expected and actual net::Error names will be printed. |
| 21 // Usage: EXPECT_THAT(foo(), IsError(net::ERR_INVALID_ARGUMENT)); | 26 // Usage: EXPECT_THAT(foo(), IsError(net::ERR_INVALID_ARGUMENT)); |
| 22 MATCHER_P(IsError, | 27 MATCHER_P(IsError, |
| 23 expected, | 28 expected, |
| 24 std::string(negation ? "not " : "") + net::ErrorToString(expected)) { | 29 std::string(negation ? "not " : "") + net::ErrorToString(expected)) { |
| 25 if (arg <= 0) | 30 if (arg <= 0) |
| 26 *result_listener << net::ErrorToString(arg); | 31 *result_listener << net::ErrorToString(arg); |
| 27 return arg == expected; | 32 return arg == expected; |
| 28 } | 33 } |
| 29 | 34 |
| 30 // Shorthand for IsError(net::OK). | 35 // Shorthand for IsError(net::OK). |
| 31 // Usage: EXPECT_THAT(foo(), IsOk()); | 36 // Usage: EXPECT_THAT(foo(), IsOk()); |
| 32 MATCHER(IsOk, | 37 MATCHER(IsOk, |
| 33 std::string(negation ? "not " : "") + net::ErrorToString(net::OK)) { | 38 std::string(negation ? "not " : "") + net::ErrorToString(net::OK)) { |
| 34 if (arg <= 0) | 39 if (arg <= 0) |
| 35 *result_listener << net::ErrorToString(arg); | 40 *result_listener << net::ErrorToString(arg); |
| 36 return arg == net::OK; | 41 return arg == net::OK; |
| 37 } | 42 } |
| 38 | 43 |
| 44 // A gMock matcher for base::StringPiece arguments. |
| 45 // gMock's built-in HasSubstrMatcher does not work, |
| 46 // because base::StringPiece cannot be implicitly converted to std::string. |
| 47 class StringPieceHasSubstrMatcher { |
| 48 public: |
| 49 explicit StringPieceHasSubstrMatcher(const std::string& substring) |
| 50 : substring_(substring) {} |
| 51 |
| 52 bool MatchAndExplain(base::StringPiece s, |
| 53 ::testing::MatchResultListener* listener) const { |
| 54 return s.as_string().find(substring_) != std::string::npos; |
| 55 } |
| 56 |
| 57 // Describe what this matcher matches. |
| 58 void DescribeTo(std::ostream* os) const { |
| 59 *os << "has substring " << substring_; |
| 60 } |
| 61 |
| 62 void DescribeNegationTo(std::ostream* os) const { |
| 63 *os << "has no substring " << substring_; |
| 64 } |
| 65 |
| 66 private: |
| 67 const std::string substring_; |
| 68 |
| 69 DISALLOW_ASSIGN(StringPieceHasSubstrMatcher); |
| 70 }; |
| 71 |
| 39 // Internal implementation for the EXPECT_DFATAL and ASSERT_DFATAL | 72 // Internal implementation for the EXPECT_DFATAL and ASSERT_DFATAL |
| 40 // macros. Do not use this directly. | 73 // macros. Do not use this directly. |
| 41 #define GTEST_DFATAL_(statement, severity, matcher, fail) \ | 74 #define GTEST_DFATAL_(statement, severity, matcher, fail) \ |
| 42 do { \ | 75 do { \ |
| 43 ::base::test::MockLog gtest_log; \ | 76 ::base::test::MockLog gtest_log; \ |
| 44 ::net::test::ScopedDisableExitOnDFatal gtest_disable_exit; \ | 77 ::net::test::ScopedDisableExitOnDFatal gtest_disable_exit; \ |
| 45 using ::testing::_; \ | 78 using ::testing::_; \ |
| 46 EXPECT_CALL(gtest_log, Log(_, _, _, _, _)) \ | 79 EXPECT_CALL(gtest_log, Log(_, _, _, _, _)) \ |
| 47 .WillRepeatedly(::testing::Return(false)); \ | 80 .WillRepeatedly(::testing::Return(false)); \ |
| 48 EXPECT_CALL(gtest_log, Log(::logging::LOG_##severity, _, _, _, matcher)) \ | 81 EXPECT_CALL(gtest_log, Log(::logging::LOG_##severity, _, _, _, matcher)) \ |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 (void)(regex); \ | 164 (void)(regex); \ |
| 132 statement; \ | 165 statement; \ |
| 133 } while (false) | 166 } while (false) |
| 134 | 167 |
| 135 #endif // DCHECK_IS_ON() | 168 #endif // DCHECK_IS_ON() |
| 136 | 169 |
| 137 } // namespace test | 170 } // namespace test |
| 138 } // namespace net | 171 } // namespace net |
| 139 | 172 |
| 140 #endif // NET_TEST_GTEST_UTIL_H_ | 173 #endif // NET_TEST_GTEST_UTIL_H_ |
| OLD | NEW |