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
3 // found in the LICENSE file.
5 #ifndef V8_TESTING_GMOCK_SUPPORT_H_
6 #define V8_TESTING_GMOCK_SUPPORT_H_
8 #include "testing/gmock/include/gmock/gmock.h"
15 Capture() : value_(), has_value_(false) {}
17 const T& value() const { return value_; }
18 bool has_value() const { return has_value_; }
20 void SetValue(const T& value) {
35 class CaptureEqMatcher : public MatcherInterface<T> {
37 explicit CaptureEqMatcher(Capture<T>* capture) : capture_(capture) {}
39 virtual void DescribeTo(std::ostream* os) const {
40 *os << "captured by " << static_cast<const void*>(capture_);
41 if (capture_->has_value()) *os << " which has value " << capture_->value();
44 virtual bool MatchAndExplain(T value, MatchResultListener* listener) const {
45 if (!capture_->has_value()) {
46 capture_->SetValue(value);
49 if (value != capture_->value()) {
50 *listener << "which is not equal to " << capture_->value();
60 } // namespace internal
63 // 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.
66 Matcher<T> CaptureEq(Capture<T>* capture) {
67 return MakeMatcher(new internal::CaptureEqMatcher<T>(capture));
70 } // namespace testing
72 #endif // V8_TESTING_GMOCK_SUPPORT_H_