Skip to content

Commit fdd9edf

Browse files
seanmakesgamesFayti1703
authored andcommitted
actually use v8 in the test
1 parent f17c160 commit fdd9edf

File tree

4 files changed

+75
-18
lines changed

4 files changed

+75
-18
lines changed

test/gtest/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ enable_testing()
1818
include_directories(${CMAKE_SOURCE_DIR}/../../vendor/v8/include/)
1919

2020
add_executable(
21-
hello_test
22-
hello_test.cc
21+
c_v8_tests
22+
c_v8_tests.cc
2323
)
2424
target_link_libraries(
25-
hello_test
25+
c_v8_tests
2626
GTest::gtest_main
2727
${CMAKE_SOURCE_DIR}/../../vendor/v8/x86_64-darwin/libv8/obj/libv8_monolith.a
2828
)
2929

3030
include(GoogleTest)
31-
gtest_discover_tests(hello_test)
31+
gtest_discover_tests(c_v8_tests)

test/gtest/Framework.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#pragma once
2+
3+
#include <v8.h>
4+
#include <libplatform/libplatform.h>
5+
#include <functional>
6+
7+
struct Framework {
8+
typedef std::function<void()> basic_main;
9+
typedef std::function<void(v8::Isolate*)> iso_main;
10+
typedef std::function<void(v8::Local<v8::Context>&)> ctx_main;
11+
12+
inline static void run(basic_main main) {
13+
std::shared_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
14+
v8::V8::InitializePlatform(platform.get());
15+
v8::V8::Initialize();
16+
main();
17+
18+
v8::V8::Dispose();
19+
v8::V8::ShutdownPlatform();
20+
}
21+
22+
inline static void runWithIsolateRaw(iso_main main) {
23+
Framework::run([main]() -> void {
24+
v8::Isolate::CreateParams p;
25+
p.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
26+
v8::Isolate* iso = v8::Isolate::New(p);
27+
28+
main(iso);
29+
30+
iso->Dispose();
31+
delete p.array_buffer_allocator;
32+
});
33+
}
34+
35+
inline static void runWithIsolate(iso_main main) {
36+
Framework::runWithIsolateRaw([main](v8::Isolate* iso) -> void {
37+
v8::Locker lock { iso };
38+
v8::Isolate::Scope iScope { iso };
39+
v8::HandleScope hScope { iso };
40+
41+
main(iso);
42+
});
43+
}
44+
45+
inline static void runWithContext(ctx_main main) {
46+
Framework::runWithIsolate([main](v8::Isolate* iso) -> void {
47+
v8::Local<v8::Context> ctx = v8::Context::New(iso);
48+
v8::Context::Scope cScope { ctx };
49+
50+
main(ctx);
51+
});
52+
}
53+
};

test/gtest/c_v8_tests.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <v8.h>
4+
#include <libplatform/libplatform.h>
5+
6+
#include "Framework.h"
7+
8+
// Demonstrate some basic assertions.
9+
TEST(FRLocaleTest, LocaleTests) {
10+
Framework::runWithContext([](v8::Local<v8::Context>& ctx) -> void {
11+
v8::Local<v8::Script> script = v8::Script::Compile(ctx, v8::String::NewFromUtf8Literal(ctx->GetIsolate(), "new Date('April 28 2021').toLocaleDateString('fr-FR');")).ToLocalChecked();
12+
v8::Local<v8::Value> result = script->Run(ctx).ToLocalChecked();
13+
v8::Local<v8::String> resultStr = result->ToString(ctx).ToLocalChecked();
14+
v8::String::Utf8Value resultUTF8(ctx->GetIsolate(), resultStr);
15+
16+
EXPECT_STREQ(*resultUTF8, "28/04/2021");
17+
});
18+
}

test/gtest/hello_test.cc

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)