GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../test/cctest/node_test_fixture.h Lines: 72 72 100.0 %
Date: 2019-01-07 12:15:22 Branches: 16 24 66.7 %

Line Branch Exec Source
1
#ifndef TEST_CCTEST_NODE_TEST_FIXTURE_H_
2
#define TEST_CCTEST_NODE_TEST_FIXTURE_H_
3
4
#include <stdlib.h>
5
#include "gtest/gtest.h"
6
#include "node.h"
7
#include "node_platform.h"
8
#include "node_internals.h"
9
#include "env.h"
10
#include "v8.h"
11
#include "libplatform/libplatform.h"
12
13
struct Argv {
14
 public:
15
11
  Argv() : Argv({"node", "-p", "process.version"}) {}
16
17
11
  Argv(const std::initializer_list<const char*> &args) {
18
11
    nr_args_ = args.size();
19
11
    int total_len = 0;
20
44
    for (auto it = args.begin(); it != args.end(); ++it) {
21
33
      total_len += strlen(*it) + 1;
22
    }
23
11
    argv_ = static_cast<char**>(malloc(nr_args_ * sizeof(char*)));
24
11
    argv_[0] = static_cast<char*>(malloc(total_len));
25
11
    int i = 0;
26
11
    int offset = 0;
27
44
    for (auto it = args.begin(); it != args.end(); ++it, ++i) {
28
33
      int len = strlen(*it) + 1;
29
33
      snprintf(argv_[0] + offset, len, "%s", *it);
30
      // Skip argv_[0] as it points the correct location already
31
33
      if (i > 0) {
32
22
        argv_[i] = argv_[0] + offset;
33
      }
34
33
      offset += len;
35
    }
36
11
  }
37
38
11
  ~Argv() {
39
11
    free(argv_[0]);
40
11
    free(argv_);
41
11
  }
42
43
12
  int nr_args() const {
44
12
    return nr_args_;
45
  }
46
47
24
  char** operator*() const {
48
24
    return argv_;
49
  }
50
51
 private:
52
  char** argv_;
53
  int nr_args_;
54
};
55
56
using ArrayBufferUniquePtr = std::unique_ptr<node::ArrayBufferAllocator,
57
      decltype(&node::FreeArrayBufferAllocator)>;
58
using TracingAgentUniquePtr = std::unique_ptr<node::tracing::Agent>;
59
using NodePlatformUniquePtr = std::unique_ptr<node::NodePlatform>;
60
61
54
class NodeTestFixture : public ::testing::Test {
62
 protected:
63
  static ArrayBufferUniquePtr allocator;
64
  static TracingAgentUniquePtr tracing_agent;
65
  static NodePlatformUniquePtr platform;
66
  static uv_loop_t current_loop;
67
  v8::Isolate* isolate_;
68
69
4
  static void SetUpTestCase() {
70
4
    tracing_agent.reset(new node::tracing::Agent());
71
4
    node::tracing::TraceEventHelper::SetAgent(tracing_agent.get());
72
4
    CHECK_EQ(0, uv_loop_init(&current_loop));
73
    platform.reset(static_cast<node::NodePlatform*>(
74
4
          node::InitializeV8Platform(4)));
75
4
    v8::V8::Initialize();
76
4
  }
77
78
4
  static void TearDownTestCase() {
79
4
    platform->Shutdown();
80
12
    while (uv_loop_alive(&current_loop)) {
81
4
      uv_run(&current_loop, UV_RUN_ONCE);
82
    }
83
4
    v8::V8::ShutdownPlatform();
84
4
    CHECK_EQ(0, uv_loop_close(&current_loop));
85
4
  }
86
87
27
  virtual void SetUp() {
88
54
    allocator = ArrayBufferUniquePtr(node::CreateArrayBufferAllocator(),
89
27
                                     &node::FreeArrayBufferAllocator);
90
27
    isolate_ = NewIsolate(allocator.get(), &current_loop);
91
27
    CHECK_NE(isolate_, nullptr);
92
27
  }
93
94
27
  virtual void TearDown() {
95
27
    isolate_->Dispose();
96
27
    platform->UnregisterIsolate(isolate_);
97
27
    isolate_ = nullptr;
98
27
  }
99
};
100
101
102
26
class EnvironmentTestFixture : public NodeTestFixture {
103
 public:
104
  class Env {
105
   public:
106
12
    Env(const v8::HandleScope& handle_scope, const Argv& argv) {
107
12
      auto isolate = handle_scope.GetIsolate();
108
12
      context_ = node::NewContext(isolate);
109
24
      CHECK(!context_.IsEmpty());
110
24
      context_->Enter();
111
112
      isolate_data_ = node::CreateIsolateData(isolate,
113
                                              &NodeTestFixture::current_loop,
114
12
                                              platform.get());
115
12
      CHECK_NE(nullptr, isolate_data_);
116
      environment_ = node::CreateEnvironment(isolate_data_,
117
                                             context_,
118
12
                                             1, *argv,
119
24
                                             argv.nr_args(), *argv);
120
12
      CHECK_NE(nullptr, environment_);
121
12
    }
122
123
12
    ~Env() {
124
12
      node::FreeEnvironment(environment_);
125
12
      node::FreeIsolateData(isolate_data_);
126
24
      context_->Exit();
127
12
    }
128
129
20
    node::Environment* operator*() const {
130
20
      return environment_;
131
    }
132
133
5
    v8::Local<v8::Context> context()  const {
134
5
      return context_;
135
    }
136
137
   private:
138
    v8::Local<v8::Context> context_;
139
    node::IsolateData* isolate_data_;
140
    node::Environment* environment_;
141
    DISALLOW_COPY_AND_ASSIGN(Env);
142
  };
143
};
144
145
#endif  // TEST_CCTEST_NODE_TEST_FIXTURE_H_