gmock-support.h 3.2 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_TESTING_GMOCK_SUPPORT_H_
#define V8_TESTING_GMOCK_SUPPORT_H_

8
#include <cmath>
9
#include <cstring>
10
#include <string>
11

12
#include "include/v8-isolate.h"
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include "testing/gmock/include/gmock/gmock.h"

namespace testing {

template <typename T>
class Capture {
 public:
  Capture() : value_(), has_value_(false) {}

  const T& value() const { return value_; }
  bool has_value() const { return has_value_; }

  void SetValue(const T& value) {
    DCHECK(!has_value());
    value_ = value;
    has_value_ = true;
  }

 private:
  T value_;
  bool has_value_;
};


namespace internal {

template <typename T>
class CaptureEqMatcher : public MatcherInterface<T> {
 public:
  explicit CaptureEqMatcher(Capture<T>* capture) : capture_(capture) {}

  virtual void DescribeTo(std::ostream* os) const {
    *os << "captured by " << static_cast<const void*>(capture_);
    if (capture_->has_value()) *os << " which has value " << capture_->value();
  }

  virtual bool MatchAndExplain(T value, MatchResultListener* listener) const {
    if (!capture_->has_value()) {
      capture_->SetValue(value);
      return true;
    }
    if (value != capture_->value()) {
      *listener << "which is not equal to " << capture_->value();
      return false;
    }
    return true;
  }

 private:
  Capture<T>* capture_;
};

}  // namespace internal


68
// Creates a polymorphic matcher that matches anything whose bit representation
69 70 71 72 73
// is equal to that of {x}.
MATCHER_P(BitEq, x, std::string(negation ? "isn't" : "is") +
                        " bitwise equal to " + PrintToString(x)) {
  static_assert(sizeof(x) == sizeof(arg), "Size mismatch");
  return std::memcmp(&arg, &x, sizeof(x)) == 0;
74 75
}

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
// Creates a polymorphic matcher that matches JSValue to Int32.
MATCHER_P(IsInt32, expected,
          std::string(negation ? "isn't" : "is") + " Int32 " +
              PrintToString(expected)) {
  return arg->IsInt32() &&
         arg->Int32Value(v8::Isolate::GetCurrent()->GetCurrentContext())
                 .FromJust() == expected;
}

// Creates a polymorphic matcher that matches JSValue to String.
MATCHER_P(IsString, expected,
          std::string(negation ? "isn't" : "is") + " String " +
              PrintToString(expected)) {
  if (!arg->IsString()) {
    return false;
  }
  v8::String::Utf8Value utf8(v8::Isolate::GetCurrent(), arg);
  return strcmp(expected, *utf8) == 0;
}

// Creates a polymorphic matcher that matches JSValue to Undefined.
MATCHER(IsUndefined, std::string(negation ? "isn't" : "is") + " Undefined") {
  return arg->IsUndefined();
}
100

101 102 103
// CaptureEq(capture) captures the value passed in during matching as long as it
// is unset, and once set, compares the value for equality with the argument.
template <typename T>
104
inline Matcher<T> CaptureEq(Capture<T>* capture) {
105 106 107
  return MakeMatcher(new internal::CaptureEqMatcher<T>(capture));
}

108 109 110 111 112 113

// Creates a polymorphic matcher that matches any floating point NaN value.
MATCHER(IsNaN, std::string(negation ? "isn't" : "is") + " not a number") {
  return std::isnan(arg);
}

114 115 116
}  // namespace testing

#endif  // V8_TESTING_GMOCK_SUPPORT_H_