blob: 012775b5cbffbf6b33de4fb3bb67c6ae04583ef3 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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.
4
5#ifndef V8_TESTING_GMOCK_SUPPORT_H_
6#define V8_TESTING_GMOCK_SUPPORT_H_
7
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008#include <cmath>
9#include <cstring>
10
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "testing/gmock/include/gmock/gmock.h"
12
13namespace testing {
14
15template <typename T>
16class Capture {
17 public:
18 Capture() : value_(), has_value_(false) {}
19
20 const T& value() const { return value_; }
21 bool has_value() const { return has_value_; }
22
23 void SetValue(const T& value) {
24 DCHECK(!has_value());
25 value_ = value;
26 has_value_ = true;
27 }
28
29 private:
30 T value_;
31 bool has_value_;
32};
33
34
35namespace internal {
36
Emily Bernierd0a1eb72015-03-24 16:35:39 -040037struct AnyBitEq {
38 template <typename A, typename B>
39 bool operator()(A const& a, B const& b) const {
40 if (sizeof(A) != sizeof(B)) return false;
41 return std::memcmp(&a, &b, sizeof(A)) == 0;
42 }
43};
44
45
46template <typename Rhs>
47class BitEqMatcher : public ComparisonBase<BitEqMatcher<Rhs>, Rhs, AnyBitEq> {
48 public:
49 explicit BitEqMatcher(Rhs const& rhs)
50 : ComparisonBase<BitEqMatcher<Rhs>, Rhs, AnyBitEq>(rhs) {}
51 static const char* Desc() { return "is bitwise equal to"; }
52 static const char* NegatedDesc() { return "isn't bitwise equal to"; }
53};
54
55
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056template <typename T>
57class CaptureEqMatcher : public MatcherInterface<T> {
58 public:
59 explicit CaptureEqMatcher(Capture<T>* capture) : capture_(capture) {}
60
61 virtual void DescribeTo(std::ostream* os) const {
62 *os << "captured by " << static_cast<const void*>(capture_);
63 if (capture_->has_value()) *os << " which has value " << capture_->value();
64 }
65
66 virtual bool MatchAndExplain(T value, MatchResultListener* listener) const {
67 if (!capture_->has_value()) {
68 capture_->SetValue(value);
69 return true;
70 }
71 if (value != capture_->value()) {
72 *listener << "which is not equal to " << capture_->value();
73 return false;
74 }
75 return true;
76 }
77
78 private:
79 Capture<T>* capture_;
80};
81
82} // namespace internal
83
84
Emily Bernierd0a1eb72015-03-24 16:35:39 -040085// Creates a polymorphic matcher that matches anything whose bit representation
86// is equal to that of x.
87template <typename T>
88inline internal::BitEqMatcher<T> BitEq(T const& x) {
89 return internal::BitEqMatcher<T>(x);
90}
91
92
Ben Murdochb8a8cc12014-11-26 15:28:44 +000093// CaptureEq(capture) captures the value passed in during matching as long as it
94// is unset, and once set, compares the value for equality with the argument.
95template <typename T>
Emily Bernierd0a1eb72015-03-24 16:35:39 -040096inline Matcher<T> CaptureEq(Capture<T>* capture) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000097 return MakeMatcher(new internal::CaptureEqMatcher<T>(capture));
98}
99
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400100
101// Creates a polymorphic matcher that matches any floating point NaN value.
102MATCHER(IsNaN, std::string(negation ? "isn't" : "is") + " not a number") {
103 return std::isnan(arg);
104}
105
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000106} // namespace testing
107
108#endif // V8_TESTING_GMOCK_SUPPORT_H_