test-utils.h 3.67 KB
Newer Older
1 2 3 4
// 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.

5 6
#ifndef V8_UNITTESTS_TEST_UTILS_H_
#define V8_UNITTESTS_TEST_UTILS_H_
7

8 9
#include <vector>

10 11
#include "include/v8.h"
#include "src/base/macros.h"
12
#include "src/base/utils/random-number-generator.h"
13 14 15
#include "src/handles.h"
#include "src/objects-inl.h"
#include "src/objects.h"
16 17
#include "src/zone/accounting-allocator.h"
#include "src/zone/zone.h"
18 19 20 21
#include "testing/gtest-support.h"

namespace v8 {

22 23
class ArrayBufferAllocator;

24 25
// Use v8::internal::TestWithIsolate  if you are testing internals,
// aka. directly work with Handles.
26
class TestWithIsolate : public virtual ::testing::Test {
27 28 29 30
 public:
  TestWithIsolate();
  virtual ~TestWithIsolate();

31 32 33
  v8::Isolate* isolate() const { return v8_isolate(); }

  v8::Isolate* v8_isolate() const { return isolate_; }
34

35 36 37 38
  v8::internal::Isolate* i_isolate() const {
    return reinterpret_cast<v8::internal::Isolate*>(isolate());
  }

39 40
  Local<Value> RunJS(const char* source);

41 42 43 44
  static void SetUpTestCase();
  static void TearDownTestCase();

 private:
45
  static v8::ArrayBuffer::Allocator* array_buffer_allocator_;
46 47 48
  static v8::Isolate* isolate_;
  v8::Isolate::Scope isolate_scope_;
  v8::HandleScope handle_scope_;
49 50 51 52

  DISALLOW_COPY_AND_ASSIGN(TestWithIsolate);
};

53 54
// Use v8::internal::TestWithNativeContext if you are testing internals,
// aka. directly work with Handles.
55
class TestWithContext : public virtual v8::TestWithIsolate {
56 57 58 59
 public:
  TestWithContext();
  virtual ~TestWithContext();

60 61 62
  const Local<Context>& context() const { return v8_context(); }
  const Local<Context>& v8_context() const { return context_; }

63
  v8::Local<v8::String> NewString(const char* string);
64
  void SetGlobalProperty(const char* name, v8::Local<v8::Value> value);
65 66 67

 private:
  Local<Context> context_;
68
  v8::Context::Scope context_scope_;
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

  DISALLOW_COPY_AND_ASSIGN(TestWithContext);
};

namespace internal {

// Forward declarations.
class Factory;


class TestWithIsolate : public virtual ::v8::TestWithIsolate {
 public:
  TestWithIsolate() {}
  virtual ~TestWithIsolate();

  Factory* factory() const;
85
  Isolate* isolate() const { return i_isolate(); }
86 87
  template <typename T = Object>
  Handle<T> RunJS(const char* source) {
Marja Hölttä's avatar
Marja Hölttä committed
88
    return Handle<T>::cast(RunJSInternal(source));
89
  }
Marja Hölttä's avatar
Marja Hölttä committed
90
  Handle<Object> RunJSInternal(const char* source);
91
  base::RandomNumberGenerator* random_number_generator() const;
92 93 94 95 96

 private:
  DISALLOW_COPY_AND_ASSIGN(TestWithIsolate);
};

97
class TestWithZone : public virtual ::testing::Test {
98
 public:
99
  TestWithZone() : zone_(&allocator_, ZONE_NAME) {}
100 101 102 103 104
  virtual ~TestWithZone();

  Zone* zone() { return &zone_; }

 private:
105
  v8::internal::AccountingAllocator allocator_;
106 107 108 109 110
  Zone zone_;

  DISALLOW_COPY_AND_ASSIGN(TestWithZone);
};

111 112
class TestWithIsolateAndZone : public virtual TestWithIsolate {
 public:
113
  TestWithIsolateAndZone() : zone_(&allocator_, ZONE_NAME) {}
114 115 116 117 118
  virtual ~TestWithIsolateAndZone();

  Zone* zone() { return &zone_; }

 private:
119
  v8::internal::AccountingAllocator allocator_;
120 121 122 123 124
  Zone zone_;

  DISALLOW_COPY_AND_ASSIGN(TestWithIsolateAndZone);
};

125 126 127 128 129 130 131 132 133 134 135 136
class TestWithNativeContext : public virtual ::v8::TestWithContext,
                              public virtual TestWithIsolate {
 public:
  TestWithNativeContext() {}
  virtual ~TestWithNativeContext();

  Handle<Context> native_context() const;

 private:
  DISALLOW_COPY_AND_ASSIGN(TestWithNativeContext);
};

137 138 139 140 141 142
class SaveFlags {
 public:
  SaveFlags();
  ~SaveFlags();

 private:
143
  std::vector<const char*>* non_default_flags_;
144 145 146 147

  DISALLOW_COPY_AND_ASSIGN(SaveFlags);
};

148 149 150
}  // namespace internal
}  // namespace v8

151
#endif  // V8_UNITTESTS_TEST_UTILS_H_