| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project 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 #ifndef V8_TESTING_GMOCK_SUPPORT_H_ | 5 #ifndef V8_TESTING_GMOCK_SUPPORT_H_ |
| 6 #define V8_TESTING_GMOCK_SUPPORT_H_ | 6 #define V8_TESTING_GMOCK_SUPPORT_H_ |
| 7 | 7 |
| 8 #include <cstring> |
| 9 |
| 8 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
| 9 | 11 |
| 10 namespace testing { | 12 namespace testing { |
| 11 | 13 |
| 12 template <typename T> | 14 template <typename T> |
| 13 class Capture { | 15 class Capture { |
| 14 public: | 16 public: |
| 15 Capture() : value_(), has_value_(false) {} | 17 Capture() : value_(), has_value_(false) {} |
| 16 | 18 |
| 17 const T& value() const { return value_; } | 19 const T& value() const { return value_; } |
| 18 bool has_value() const { return has_value_; } | 20 bool has_value() const { return has_value_; } |
| 19 | 21 |
| 20 void SetValue(const T& value) { | 22 void SetValue(const T& value) { |
| 21 DCHECK(!has_value()); | 23 DCHECK(!has_value()); |
| 22 value_ = value; | 24 value_ = value; |
| 23 has_value_ = true; | 25 has_value_ = true; |
| 24 } | 26 } |
| 25 | 27 |
| 26 private: | 28 private: |
| 27 T value_; | 29 T value_; |
| 28 bool has_value_; | 30 bool has_value_; |
| 29 }; | 31 }; |
| 30 | 32 |
| 31 | 33 |
| 32 namespace internal { | 34 namespace internal { |
| 33 | 35 |
| 36 struct AnyBitEq { |
| 37 template <typename A, typename B> |
| 38 bool operator()(A const& a, B const& b) const { |
| 39 if (sizeof(A) != sizeof(B)) return false; |
| 40 return std::memcmp(&a, &b, sizeof(A)) == 0; |
| 41 } |
| 42 }; |
| 43 |
| 44 |
| 45 template <typename Rhs> |
| 46 class BitEqMatcher : public ComparisonBase<BitEqMatcher<Rhs>, Rhs, AnyBitEq> { |
| 47 public: |
| 48 explicit BitEqMatcher(Rhs const& rhs) |
| 49 : ComparisonBase<BitEqMatcher<Rhs>, Rhs, AnyBitEq>(rhs) {} |
| 50 static const char* Desc() { return "is bitwise equal to"; } |
| 51 static const char* NegatedDesc() { return "isn't bitwise equal to"; } |
| 52 }; |
| 53 |
| 54 |
| 34 template <typename T> | 55 template <typename T> |
| 35 class CaptureEqMatcher : public MatcherInterface<T> { | 56 class CaptureEqMatcher : public MatcherInterface<T> { |
| 36 public: | 57 public: |
| 37 explicit CaptureEqMatcher(Capture<T>* capture) : capture_(capture) {} | 58 explicit CaptureEqMatcher(Capture<T>* capture) : capture_(capture) {} |
| 38 | 59 |
| 39 virtual void DescribeTo(std::ostream* os) const { | 60 virtual void DescribeTo(std::ostream* os) const { |
| 40 *os << "captured by " << static_cast<const void*>(capture_); | 61 *os << "captured by " << static_cast<const void*>(capture_); |
| 41 if (capture_->has_value()) *os << " which has value " << capture_->value(); | 62 if (capture_->has_value()) *os << " which has value " << capture_->value(); |
| 42 } | 63 } |
| 43 | 64 |
| 44 virtual bool MatchAndExplain(T value, MatchResultListener* listener) const { | 65 virtual bool MatchAndExplain(T value, MatchResultListener* listener) const { |
| 45 if (!capture_->has_value()) { | 66 if (!capture_->has_value()) { |
| 46 capture_->SetValue(value); | 67 capture_->SetValue(value); |
| 47 return true; | 68 return true; |
| 48 } | 69 } |
| 49 if (value != capture_->value()) { | 70 if (value != capture_->value()) { |
| 50 *listener << "which is not equal to " << capture_->value(); | 71 *listener << "which is not equal to " << capture_->value(); |
| 51 return false; | 72 return false; |
| 52 } | 73 } |
| 53 return true; | 74 return true; |
| 54 } | 75 } |
| 55 | 76 |
| 56 private: | 77 private: |
| 57 Capture<T>* capture_; | 78 Capture<T>* capture_; |
| 58 }; | 79 }; |
| 59 | 80 |
| 60 } // namespace internal | 81 } // namespace internal |
| 61 | 82 |
| 62 | 83 |
| 84 // Creates a polymorphic matcher that matches anything whose bit representation |
| 85 // is equal to that of x. |
| 86 template <typename T> |
| 87 inline internal::BitEqMatcher<T> BitEq(T const& x) { |
| 88 return internal::BitEqMatcher<T>(x); |
| 89 } |
| 90 |
| 91 |
| 63 // CaptureEq(capture) captures the value passed in during matching as long as it | 92 // CaptureEq(capture) captures the value passed in during matching as long as it |
| 64 // is unset, and once set, compares the value for equality with the argument. | 93 // is unset, and once set, compares the value for equality with the argument. |
| 65 template <typename T> | 94 template <typename T> |
| 66 Matcher<T> CaptureEq(Capture<T>* capture) { | 95 inline Matcher<T> CaptureEq(Capture<T>* capture) { |
| 67 return MakeMatcher(new internal::CaptureEqMatcher<T>(capture)); | 96 return MakeMatcher(new internal::CaptureEqMatcher<T>(capture)); |
| 68 } | 97 } |
| 69 | 98 |
| 70 } // namespace testing | 99 } // namespace testing |
| 71 | 100 |
| 72 #endif // V8_TESTING_GMOCK_SUPPORT_H_ | 101 #endif // V8_TESTING_GMOCK_SUPPORT_H_ |
| OLD | NEW |