0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/cctest/node_test_fixture.h
Anna Henningsen 290315ace7
src: refactor #include handling
`node_internals.h` already includes the most common headers,
so double includes can be avoided in a lot of cases. Also don’t include
`node_internals.h` from `node.h` implicitly anymore, as that is mostly
unnecessary.

PR-URL: https://github.com/nodejs/node/pull/14697
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-09-14 17:38:38 +02:00

111 lines
2.5 KiB
C++

#ifndef TEST_CCTEST_NODE_TEST_FIXTURE_H_
#define TEST_CCTEST_NODE_TEST_FIXTURE_H_
#include <stdlib.h>
#include "gtest/gtest.h"
#include "node.h"
#include "node_platform.h"
#include "env.h"
#include "v8.h"
#include "libplatform/libplatform.h"
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
virtual void* Allocate(size_t length) {
return AllocateUninitialized(length);
}
virtual void* AllocateUninitialized(size_t length) {
return calloc(length, 1);
}
virtual void Free(void* data, size_t) {
free(data);
}
};
struct Argv {
public:
Argv() : Argv({"node", "-p", "process.version"}) {}
Argv(const std::initializer_list<const char*> &args) {
nr_args_ = args.size();
int total_len = 0;
for (auto it = args.begin(); it != args.end(); ++it) {
total_len += strlen(*it) + 1;
}
argv_ = static_cast<char**>(malloc(nr_args_ * sizeof(char*)));
argv_[0] = static_cast<char*>(malloc(total_len));
int i = 0;
int offset = 0;
for (auto it = args.begin(); it != args.end(); ++it, ++i) {
int len = strlen(*it) + 1;
snprintf(argv_[0] + offset, len, "%s", *it);
// Skip argv_[0] as it points the correct location already
if (i > 0) {
argv_[i] = argv_[0] + offset;
}
offset += len;
}
}
~Argv() {
free(argv_[0]);
free(argv_);
}
int nr_args() const {
return nr_args_;
}
char** operator*() const {
return argv_;
}
private:
char** argv_;
int nr_args_;
};
extern uv_loop_t current_loop;
class NodeTestFixture : public ::testing::Test {
public:
static uv_loop_t* CurrentLoop() { return &current_loop; }
protected:
v8::Isolate::CreateParams params_;
ArrayBufferAllocator allocator_;
v8::Isolate* isolate_;
~NodeTestFixture() {
TearDown();
}
virtual void SetUp() {
CHECK_EQ(0, uv_loop_init(&current_loop));
platform_ = new node::NodePlatform(8, &current_loop, nullptr);
v8::V8::InitializePlatform(platform_);
v8::V8::Initialize();
params_.array_buffer_allocator = &allocator_;
isolate_ = v8::Isolate::New(params_);
}
virtual void TearDown() {
if (platform_ == nullptr) return;
platform_->Shutdown();
while (uv_loop_alive(&current_loop)) {
uv_run(&current_loop, UV_RUN_ONCE);
}
v8::V8::ShutdownPlatform();
delete platform_;
platform_ = nullptr;
CHECK_EQ(0, uv_loop_close(&current_loop));
}
private:
node::NodePlatform* platform_ = nullptr;
};
#endif // TEST_CCTEST_NODE_TEST_FIXTURE_H_