forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode_test_fixture.h
More file actions
83 lines (69 loc) · 1.92 KB
/
Copy pathnode_test_fixture.h
File metadata and controls
83 lines (69 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#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 "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, sizeof(int));
}
virtual void Free(void* data, size_t) {
free(data);
}
};
struct Argv {
public:
Argv(const char* prog, const char* arg1, const char* arg2) {
int prog_len = strlen(prog) + 1;
int arg1_len = strlen(arg1) + 1;
int arg2_len = strlen(arg2) + 1;
argv_ = static_cast<char**>(malloc(3 * sizeof(char*)));
argv_[0] = static_cast<char*>(malloc(prog_len + arg1_len + arg2_len));
snprintf(argv_[0], prog_len, "%s", prog);
snprintf(argv_[0] + prog_len, arg1_len, "%s", arg1);
snprintf(argv_[0] + prog_len + arg1_len, arg2_len, "%s", arg2);
argv_[1] = argv_[0] + prog_len;
argv_[2] = argv_[0] + prog_len + arg1_len;
}
~Argv() {
free(argv_[0]);
free(argv_);
}
char** operator *() const {
return argv_;
}
private:
char** argv_;
};
class NodeTestFixture : public ::testing::Test {
protected:
v8::Isolate::CreateParams params_;
ArrayBufferAllocator allocator_;
v8::Isolate* isolate_;
~NodeTestFixture() {
TearDown();
}
virtual void SetUp() {
platform_ = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(platform_);
v8::V8::Initialize();
params_.array_buffer_allocator = &allocator_;
isolate_ = v8::Isolate::New(params_);
}
virtual void TearDown() {
if (platform_ == nullptr) return;
v8::V8::ShutdownPlatform();
delete platform_;
platform_ = nullptr;
}
private:
v8::Platform* platform_ = nullptr;
};
#endif // TEST_CCTEST_NODE_TEST_FIXTURE_H_