Merge lp:~marcustomlinson/v8-cpp/add_tests into lp:v8-cpp

Proposed by Marcus Tomlinson
Status: Merged
Merged at revision: 12
Proposed branch: lp:~marcustomlinson/v8-cpp/add_tests
Merge into: lp:v8-cpp
Prerequisite: lp:~marcustomlinson/v8-cpp/v8runner
Diff against target: 947 lines (+803/-9)
20 files modified
CMakeLists.txt (+5/-2)
example/CMakeLists.txt (+6/-6)
example/example.js (+1/-1)
tests/CMakeLists.txt (+12/-0)
tests/functions/CMakeLists.txt (+35/-0)
tests/functions/module.cpp (+28/-0)
tests/functions/test.cpp (+57/-0)
tests/functions/test.h (+44/-0)
tests/members/CMakeLists.txt (+35/-0)
tests/members/module.cpp (+29/-0)
tests/members/test.cpp (+46/-0)
tests/members/test.h (+11/-0)
tests/methods/CMakeLists.txt (+35/-0)
tests/methods/module.cpp (+32/-0)
tests/methods/test.cpp (+100/-0)
tests/methods/test.h (+71/-0)
tests/objects/CMakeLists.txt (+35/-0)
tests/objects/module.cpp (+40/-0)
tests/objects/test.cpp (+108/-0)
tests/objects/test.h (+73/-0)
To merge this branch: bzr merge lp:~marcustomlinson/v8-cpp/add_tests
Reviewer Review Type Date Requested Status
Marcus Tomlinson Pending
Review via email: mp+263642@code.launchpad.net

Commit message

Added tests

To post a comment you must log in.
lp:~marcustomlinson/v8-cpp/add_tests updated
37. By Marcus Tomlinson

Merged v8runner

38. By Marcus Tomlinson

Merged v8runner

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'CMakeLists.txt'
--- CMakeLists.txt 2015-07-02 11:59:44 +0000
+++ CMakeLists.txt 2015-07-02 11:59:44 +0000
@@ -9,7 +9,7 @@
9 -fno-permissive9 -fno-permissive
10 -pedantic10 -pedantic
11 -Wall11 -Wall
12 -Wextra12# -Wextra
13 -fPIC13 -fPIC
14)14)
1515
@@ -21,6 +21,9 @@
21 ${CMAKE_CURRENT_SOURCE_DIR}/deps/v8/out/native/obj.target/tools/gyp21 ${CMAKE_CURRENT_SOURCE_DIR}/deps/v8/out/native/obj.target/tools/gyp
22)22)
2323
24add_subdirectory(example)
24add_subdirectory(src)25add_subdirectory(src)
25add_subdirectory(test)
26add_subdirectory(v8runner)26add_subdirectory(v8runner)
27
28enable_testing()
29add_subdirectory(tests)
2730
=== renamed directory 'test' => 'example'
=== modified file 'example/CMakeLists.txt'
--- test/CMakeLists.txt 2015-07-02 11:59:44 +0000
+++ example/CMakeLists.txt 2015-07-02 11:59:44 +0000
@@ -6,20 +6,20 @@
66
7file(7file(
8 GLOB8 GLOB
9 TEST_FILES9 EXAMPLE_FILES
10 *.h *.cpp *.js10 *.h *.cpp *.js
11)11)
1212
13configure_file(test.js test.js)13configure_file(example.js example.js)
1414
15add_library(15add_library(
16 v8-cpp-test SHARED16 v8-cpp-example SHARED
17 ${TEST_FILES}17 ${EXAMPLE_FILES}
18)18)
1919
20# This line sets the output binary name to "v8-cpp-exampleX.node"20# This line sets the output binary name to "v8-cpp-example.node"
21set_target_properties(21set_target_properties(
22 v8-cpp-test22 v8-cpp-example
23 PROPERTIES23 PROPERTIES
24 PREFIX ""24 PREFIX ""
25 SUFFIX ".node"25 SUFFIX ".node"
2626
=== renamed file 'test/test.js' => 'example/example.js'
--- test/test.js 2015-06-10 05:57:04 +0000
+++ example/example.js 2015-07-02 11:59:44 +0000
@@ -1,4 +1,4 @@
1var addon = require('./v8-cpp-test');1var addon = require('./v8-cpp-example');
22
3var obj = addon.new_MyObject(10);3var obj = addon.new_MyObject(10);
4console.log( obj.plus_one() ); // 114console.log( obj.plus_one() ); // 11
55
=== renamed file 'test/addon.cpp' => 'example/module.cpp'
=== added directory 'tests'
=== added file 'tests/CMakeLists.txt'
--- tests/CMakeLists.txt 1970-01-01 00:00:00 +0000
+++ tests/CMakeLists.txt 2015-07-02 11:59:44 +0000
@@ -0,0 +1,12 @@
1include(FindGMock)
2
3include_directories(
4 ${GTEST_INCLUDE_DIRS}
5 ${GMOCK_INCLUDE_DIRS}
6 ${TEST_INCLUDE_DIRS}
7)
8
9add_subdirectory(functions)
10add_subdirectory(members)
11add_subdirectory(methods)
12add_subdirectory(objects)
013
=== added directory 'tests/functions'
=== added file 'tests/functions/CMakeLists.txt'
--- tests/functions/CMakeLists.txt 1970-01-01 00:00:00 +0000
+++ tests/functions/CMakeLists.txt 2015-07-02 11:59:44 +0000
@@ -0,0 +1,35 @@
1# MODULE
2add_library(
3 test-functions-module SHARED
4 module.cpp
5)
6
7# This line sets the output binary name to "<target_name>.node"
8set_target_properties(
9 test-functions-module
10 PROPERTIES
11 PREFIX ""
12 SUFFIX ".node"
13)
14
15# TEST
16add_executable(
17 test-functions
18 test.h
19 test.cpp
20)
21
22target_link_libraries(
23 test-functions
24
25 v8-cpp
26
27 ${GTEST_BOTH_LIBRARIES}
28 ${GMOCK_LIBRARIES}
29 ${TEST_LDFLAGS}
30)
31
32add_test(
33 test-functions
34 test-functions
35)
036
=== added file 'tests/functions/module.cpp'
--- tests/functions/module.cpp 1970-01-01 00:00:00 +0000
+++ tests/functions/module.cpp 2015-07-02 11:59:44 +0000
@@ -0,0 +1,28 @@
1#include "test.h"
2
3#include <v8-cpp.h>
4
5using namespace v8;
6
7void InitAll(Handle<Object> exports)
8{
9 // Get current isolate
10 Isolate* isolate = Isolate::GetCurrent();
11
12 // Prepare TestClass binding
13 v8cpp::Class<TestCaller> testcaller(isolate);
14 testcaller
15 .set_constructor<Local<Function>>()
16 .add_method("call_me", &TestCaller::call_me);
17
18 // Prepare module
19 v8cpp::Module module(isolate);
20
21 module.add_class("TestCaller", testcaller);
22 module.add_function("simple_function", &simple_function);
23 module.add_function("complex_function", &complex_function);
24
25 exports->SetPrototype(module.create_prototype());
26}
27
28V8CPP_MODULE(addon, InitAll)
029
=== added file 'tests/functions/test.cpp'
--- tests/functions/test.cpp 1970-01-01 00:00:00 +0000
+++ tests/functions/test.cpp 2015-07-02 11:59:44 +0000
@@ -0,0 +1,57 @@
1#include "test.h"
2
3#include <v8-cpp.h>
4
5#include <gtest/gtest.h>
6
7// FUNCTIONS
8// call JS function from C++ (callback)
9// call C++ functions from JS
10
11TEST(Test, call_to_js)
12{
13 v8::Isolate* isolate = v8::Isolate::New();
14
15 auto callback_result = v8cpp::run_script<std::string>(isolate,
16 R"(
17 var module = require("./test-functions-module");
18
19 var caller = new module.TestCaller(function(message)
20 {
21 return message + " world" // "hello world"
22 });
23
24 caller.call_me();
25 )");
26
27 EXPECT_EQ(callback_result, "hello world");
28
29 isolate->Dispose();
30}
31
32TEST(Test, call_from_js)
33{
34 v8::Isolate* isolate = v8::Isolate::New();
35
36 auto result = v8cpp::run_script<std::string>(isolate,
37 R"(
38 var module = require("./test-functions-module");
39 module.simple_function();
40 )");
41
42 EXPECT_EQ(result, "hello there");
43
44 auto result2 = v8cpp::run_script<std::vector<float>>(isolate,
45 R"(
46 var module = require("./test-functions-module");
47 module.complex_function(4, "3", 2.1, false);
48 )");
49
50 ASSERT_EQ(result2.size(), 4);
51 EXPECT_FLOAT_EQ(result2[0], 4);
52 EXPECT_FLOAT_EQ(result2[1], 3);
53 EXPECT_FLOAT_EQ(result2[2], 2.1);
54 EXPECT_FLOAT_EQ(result2[3], 0);
55
56 isolate->Dispose();
57}
058
=== added file 'tests/functions/test.h'
--- tests/functions/test.h 1970-01-01 00:00:00 +0000
+++ tests/functions/test.h 2015-07-02 11:59:44 +0000
@@ -0,0 +1,44 @@
1#pragma once
2
3#include <gtest/gtest.h>
4
5#include <v8-cpp.h>
6
7class TestCaller
8{
9public:
10 explicit TestCaller(v8::Local<v8::Function> const& cb)
11 : cb_(v8::Isolate::GetCurrent(), cb)
12 {
13 }
14
15 std::string call_me() const
16 {
17 v8::Local<v8::Function> cb = v8cpp::to_local<v8::Function>(v8::Isolate::GetCurrent(), cb_);
18 auto result = v8cpp::call_v8(v8::Isolate::GetCurrent(), cb, "hello");
19 return v8cpp::from_v8<std::string>(v8::Isolate::GetCurrent(), result);
20 }
21
22private:
23 v8::Persistent<v8::Function> cb_;
24};
25
26std::string simple_function()
27{
28 return "hello there";
29}
30
31std::vector<float> complex_function(int first, std::string const& second, float third, bool forth)
32{
33 EXPECT_EQ(first, 4);
34 EXPECT_EQ(second, "3");
35 EXPECT_FLOAT_EQ(third, 2.1);
36 EXPECT_EQ(forth, false);
37
38 std::vector<float> num_list;
39 num_list.push_back(first);
40 num_list.push_back(stoi(second));
41 num_list.push_back(third);
42 num_list.push_back(forth);
43 return num_list;
44}
045
=== added directory 'tests/members'
=== added file 'tests/members/CMakeLists.txt'
--- tests/members/CMakeLists.txt 1970-01-01 00:00:00 +0000
+++ tests/members/CMakeLists.txt 2015-07-02 11:59:44 +0000
@@ -0,0 +1,35 @@
1# MODULE
2add_library(
3 test-members-module SHARED
4 module.cpp
5)
6
7# This line sets the output binary name to "<target_name>.node"
8set_target_properties(
9 test-members-module
10 PROPERTIES
11 PREFIX ""
12 SUFFIX ".node"
13)
14
15# TEST
16add_executable(
17 test-members
18 test.h
19 test.cpp
20)
21
22target_link_libraries(
23 test-members
24
25 v8-cpp
26
27 ${GTEST_BOTH_LIBRARIES}
28 ${GMOCK_LIBRARIES}
29 ${TEST_LDFLAGS}
30)
31
32add_test(
33 test-members
34 test-members
35)
036
=== added file 'tests/members/module.cpp'
--- tests/members/module.cpp 1970-01-01 00:00:00 +0000
+++ tests/members/module.cpp 2015-07-02 11:59:44 +0000
@@ -0,0 +1,29 @@
1#include "test.h"
2
3#include <v8-cpp.h>
4
5using namespace v8;
6
7void InitAll(Handle<Object> exports)
8{
9 // Get current isolate
10 Isolate* isolate = Isolate::GetCurrent();
11
12 // Prepare TestStruct binding
13 v8cpp::Class<TestStruct> teststruct(isolate);
14 teststruct
15 .set_constructor()
16 .add_member("bool_value", &TestStruct::bool_value)
17 .add_member("int_value", &TestStruct::int_value)
18 .add_member("float_value", &TestStruct::float_value)
19 .add_member("string_value", &TestStruct::string_value);
20
21 // Prepare module
22 v8cpp::Module module(isolate);
23
24 module.add_class("TestStruct", teststruct);
25
26 exports->SetPrototype(module.create_prototype());
27}
28
29V8CPP_MODULE(addon, InitAll)
030
=== added file 'tests/members/test.cpp'
--- tests/members/test.cpp 1970-01-01 00:00:00 +0000
+++ tests/members/test.cpp 2015-07-02 11:59:44 +0000
@@ -0,0 +1,46 @@
1#include "test.h"
2
3#include <v8-cpp.h>
4
5#include <gtest/gtest.h>
6
7// MEMBERS
8// get/set class/struct members from JS
9
10TEST(Test, get_set_members_from_js)
11{
12 v8::Isolate* isolate = v8::Isolate::New();
13
14 auto test_struct = v8cpp::run_script<TestStruct>(isolate,
15 R"(
16 var module = require("./test-members-module");
17 var test_struct = new module.TestStruct();
18 test_struct;
19 )");
20
21 EXPECT_EQ(test_struct.bool_value, true);
22 EXPECT_EQ(test_struct.int_value, 9);
23 EXPECT_FLOAT_EQ(test_struct.float_value, 0.1);
24 EXPECT_EQ(test_struct.string_value, "hello");
25
26 test_struct = v8cpp::run_script<TestStruct>(isolate,
27 R"(
28 var module = require("./test-members-module");
29 var test_struct = new module.TestStruct();
30 if (test_struct.bool_value)
31 {
32 test_struct.bool_value = false;
33 test_struct.int_value = -1;
34 test_struct.float_value = test_struct.int_value + 0.12;
35 test_struct.string_value = test_struct.string_value + " there";
36 }
37 test_struct;
38 )");
39
40 EXPECT_EQ(test_struct.bool_value, false);
41 EXPECT_EQ(test_struct.int_value, -1);
42 EXPECT_FLOAT_EQ(test_struct.float_value, -0.88);
43 EXPECT_EQ(test_struct.string_value, "hello there");
44
45 isolate->Dispose();
46}
047
=== added file 'tests/members/test.h'
--- tests/members/test.h 1970-01-01 00:00:00 +0000
+++ tests/members/test.h 2015-07-02 11:59:44 +0000
@@ -0,0 +1,11 @@
1#pragma once
2
3#include <gtest/gtest.h>
4
5struct TestStruct
6{
7 bool bool_value = true;
8 int int_value = 9;
9 float float_value = 0.1;
10 std::string string_value = "hello";
11};
012
=== added directory 'tests/methods'
=== added file 'tests/methods/CMakeLists.txt'
--- tests/methods/CMakeLists.txt 1970-01-01 00:00:00 +0000
+++ tests/methods/CMakeLists.txt 2015-07-02 11:59:44 +0000
@@ -0,0 +1,35 @@
1# MODULE
2add_library(
3 test-methods-module SHARED
4 module.cpp
5)
6
7# This line sets the output binary name to "<target_name>.node"
8set_target_properties(
9 test-methods-module
10 PROPERTIES
11 PREFIX ""
12 SUFFIX ".node"
13)
14
15# TEST
16add_executable(
17 test-methods
18 test.h
19 test.cpp
20)
21
22target_link_libraries(
23 test-methods
24
25 v8-cpp
26
27 ${GTEST_BOTH_LIBRARIES}
28 ${GMOCK_LIBRARIES}
29 ${TEST_LDFLAGS}
30)
31
32add_test(
33 test-methods
34 test-methods
35)
036
=== added file 'tests/methods/module.cpp'
--- tests/methods/module.cpp 1970-01-01 00:00:00 +0000
+++ tests/methods/module.cpp 2015-07-02 11:59:44 +0000
@@ -0,0 +1,32 @@
1#include "test.h"
2
3#include <v8-cpp.h>
4
5using namespace v8;
6
7void InitAll(Handle<Object> exports)
8{
9 // Get current isolate
10 Isolate* isolate = Isolate::GetCurrent();
11
12 // Prepare TestClass binding
13 v8cpp::Class<TestClass_OL> testclass(isolate);
14 testclass
15 .add_inheritance<BaseTestClass>()
16 .add_inheritance<TestClass>()
17 .set_constructor()
18 .add_method("regular_method", &TestClass::regular_method)
19 .add_method("static_method", &TestClass::static_method)
20 .add_method("base_method", &BaseTestClass::base_method)
21 .add_method("virtual_method", &TestClass::virtual_method)
22 .add_method("overload_method", &TestClass_OL::overload_method);
23
24 // Prepare module
25 v8cpp::Module module(isolate);
26
27 module.add_class("TestClass", testclass);
28
29 exports->SetPrototype(module.create_prototype());
30}
31
32V8CPP_MODULE(addon, InitAll)
033
=== added file 'tests/methods/test.cpp'
--- tests/methods/test.cpp 1970-01-01 00:00:00 +0000
+++ tests/methods/test.cpp 2015-07-02 11:59:44 +0000
@@ -0,0 +1,100 @@
1#include "test.h"
2
3#include <v8-cpp.h>
4
5#include <gtest/gtest.h>
6
7// METHODS
8// call regular class methods from JS
9// call static class methods from JS
10// call base class methods from JS (inheritance)
11// call overridden class methods from JS (inheritance)
12// call overloaded class methods from JS
13
14TEST(Test, call_regular_method_from_js)
15{
16 v8::Isolate* isolate = v8::Isolate::New();
17
18 auto result = v8cpp::run_script<int>(isolate,
19 R"(
20 var module = require("./test-methods-module");
21 var test_object = new module.TestClass();
22 test_object.regular_method();
23 )");
24
25 EXPECT_EQ(result, 2);
26
27 isolate->Dispose();
28}
29
30TEST(Test, call_static_method_from_js)
31{
32 v8::Isolate* isolate = v8::Isolate::New();
33
34 auto result = v8cpp::run_script<int>(isolate,
35 R"(
36 var module = require("./test-methods-module");
37 module.TestClass.static_method();
38 )");
39
40 EXPECT_EQ(result, 3);
41
42 isolate->Dispose();
43}
44
45TEST(Test, call_base_method_from_js)
46{
47 v8::Isolate* isolate = v8::Isolate::New();
48
49 auto result = v8cpp::run_script<int>(isolate,
50 R"(
51 var module = require("./test-methods-module");
52 var test_object = new module.TestClass();
53 test_object.base_method();
54 )");
55
56 EXPECT_EQ(result, 1);
57
58 isolate->Dispose();
59}
60
61TEST(Test, call_override_method_from_js)
62{
63 v8::Isolate* isolate = v8::Isolate::New();
64
65 auto result = v8cpp::run_script<int>(isolate,
66 R"(
67 var module = require("./test-methods-module");
68 var test_object = new module.TestClass();
69 test_object.virtual_method();
70 )");
71
72 EXPECT_EQ(result, 4);
73
74 isolate->Dispose();
75}
76
77TEST(Test, call_overload_method_from_js)
78{
79 v8::Isolate* isolate = v8::Isolate::New();
80
81 auto result = v8cpp::run_script<int>(isolate,
82 R"(
83 var module = require("./test-methods-module");
84 var test_object = new module.TestClass();
85 test_object.overload_method(5);
86 )");
87
88 EXPECT_EQ(result, 5);
89
90 auto result2 = v8cpp::run_script<std::string>(isolate,
91 R"(
92 var module = require("./test-methods-module");
93 var test_object = new module.TestClass();
94 test_object.overload_method(1, 5);
95 )");
96
97 EXPECT_EQ(result2, "6");
98
99 isolate->Dispose();
100}
0101
=== added file 'tests/methods/test.h'
--- tests/methods/test.h 1970-01-01 00:00:00 +0000
+++ tests/methods/test.h 2015-07-02 11:59:44 +0000
@@ -0,0 +1,71 @@
1#pragma once
2
3#include <gtest/gtest.h>
4
5#include <v8-cpp.h>
6
7class BaseTestClass
8{
9public:
10 virtual ~BaseTestClass() = default;
11
12 int base_method()
13 {
14 return 1;
15 }
16
17 virtual int virtual_method() = 0;
18};
19
20class TestClass : public BaseTestClass
21{
22public:
23 virtual ~TestClass() = default;
24
25 int regular_method()
26 {
27 return 2;
28 }
29
30 static int static_method()
31 {
32 return 3;
33 }
34
35 int virtual_method() override
36 {
37 return 4;
38 }
39
40 int overload_method(int a)
41 {
42 return a;
43 }
44
45 std::string overload_method(int a, int b)
46 {
47 return std::to_string(a + b);
48 }
49};
50
51class TestClass_OL : public TestClass
52{
53public:
54 using TestClass::TestClass;
55
56 v8::Local<v8::Value> overload_method(v8::FunctionCallbackInfo<v8::Value> const& args)
57 {
58 if (args.Length() == 1)
59 {
60 int arg = v8cpp::from_v8<int>(v8::Isolate::GetCurrent(), args[0]);
61 return v8cpp::to_v8(v8::Isolate::GetCurrent(), TestClass::overload_method(arg));
62 }
63 else if (args.Length() == 2)
64 {
65 int arg = v8cpp::from_v8<int>(v8::Isolate::GetCurrent(), args[0]);
66 int arg2 = v8cpp::from_v8<int>(v8::Isolate::GetCurrent(), args[1]);
67 return v8cpp::to_v8(v8::Isolate::GetCurrent(), TestClass::overload_method(arg, arg2));
68 }
69 return v8cpp::to_v8(v8::Isolate::GetCurrent(), 0);
70 }
71};
072
=== added directory 'tests/objects'
=== added file 'tests/objects/CMakeLists.txt'
--- tests/objects/CMakeLists.txt 1970-01-01 00:00:00 +0000
+++ tests/objects/CMakeLists.txt 2015-07-02 11:59:44 +0000
@@ -0,0 +1,35 @@
1# MODULE
2add_library(
3 test-objects-module SHARED
4 module.cpp
5)
6
7# This line sets the output binary name to "<target_name>.node"
8set_target_properties(
9 test-objects-module
10 PROPERTIES
11 PREFIX ""
12 SUFFIX ".node"
13)
14
15# TEST
16add_executable(
17 test-objects
18 test.h
19 test.cpp
20)
21
22target_link_libraries(
23 test-objects
24
25 v8-cpp
26
27 ${GTEST_BOTH_LIBRARIES}
28 ${GMOCK_LIBRARIES}
29 ${TEST_LDFLAGS}
30)
31
32add_test(
33 test-objects
34 test-objects
35)
036
=== added file 'tests/objects/module.cpp'
--- tests/objects/module.cpp 1970-01-01 00:00:00 +0000
+++ tests/objects/module.cpp 2015-07-02 11:59:44 +0000
@@ -0,0 +1,40 @@
1#include "test.h"
2
3#include <v8-cpp.h>
4
5using namespace v8;
6
7void InitAll(Handle<Object> exports)
8{
9 // Get current isolate
10 Isolate* isolate = Isolate::GetCurrent();
11
12 // Prepare TestClass binding
13 v8cpp::Class<TestClass> testclass(isolate);
14 testclass
15 .set_constructor<int, int>()
16 .add_method("i", &TestClass::i)
17 .add_method("embedded_class_ptr", &TestClass::embedded_class_ptr)
18 .add_method("embedded_class_ref", &TestClass::embedded_class_ref)
19 .add_method("embedded_class_copy", &TestClass::embedded_class_copy)
20 .add_method("replace_i", &TestClass::replace_i)
21 .add_method("add_i", &TestClass::add_i);
22
23 // Prepare EmbeddedTestClass binding
24 v8cpp::Class<EmbeddedTestClass> embeddedtestclass(isolate);
25 embeddedtestclass
26 .set_constructor<int, int>()
27 .add_method("i", &EmbeddedTestClass::i);
28
29 // Prepare module
30 v8cpp::Module module(isolate);
31
32 module.add_class("TestClass", testclass);
33 module.add_class("EmbeddedTestClass", embeddedtestclass);
34
35 module.add_function("new_TestClass", &new_TestClass);
36
37 exports->SetPrototype(module.create_prototype());
38}
39
40V8CPP_MODULE(addon, InitAll)
041
=== added file 'tests/objects/test.cpp'
--- tests/objects/test.cpp 1970-01-01 00:00:00 +0000
+++ tests/objects/test.cpp 2015-07-02 11:59:44 +0000
@@ -0,0 +1,108 @@
1#include "test.h"
2
3#include <v8-cpp.h>
4
5#include <gtest/gtest.h>
6
7// OBJECTS
8// construct class via new() from JS
9// construct class via factory methods from JS (constructor overloads)
10// move/ref objects to JS
11// move/ref objects back to C++
12
13TEST(Test, construct_class_via_new)
14{
15 v8::Isolate* isolate = v8::Isolate::New();
16
17 auto test_object = v8cpp::run_script<TestClass>(isolate,
18 R"(
19 var module = require("./test-objects-module");
20 var test_object = new module.TestClass(1, 2);
21 test_object;
22 )");
23
24 EXPECT_EQ(test_object.i(), 3);
25
26 isolate->Dispose();
27}
28
29TEST(Test, construct_class_via_factory)
30{
31 v8::Isolate* isolate = v8::Isolate::New();
32
33 auto test_object = v8cpp::run_script<TestClass>(isolate,
34 R"(
35 var module = require("./test-objects-module");
36 var test_object = module.new_TestClass(1, 2);
37 test_object;
38 )");
39
40 EXPECT_EQ(test_object.i(), 3);
41
42 isolate->Dispose();
43}
44
45TEST(Test, object_to_js)
46{
47 v8::Isolate* isolate = v8::Isolate::New();
48
49 auto test_object = v8cpp::run_script<EmbeddedTestClass*>(isolate,
50 R"(
51 var module = require("./test-objects-module");
52 var test_object = module.new_TestClass(1, 2);
53 test_object.embedded_class_ptr();
54 )");
55
56 EXPECT_EQ(test_object->i(), -1);
57
58 auto test_object2 = v8cpp::run_script<EmbeddedTestClass&>(isolate,
59 R"(
60 var module = require("./test-objects-module");
61 var test_object = module.new_TestClass(1, 2);
62 test_object.embedded_class_ref();
63 )");
64
65 EXPECT_EQ(test_object2.i(), -1);
66
67 auto test_object3 = v8cpp::run_script<EmbeddedTestClass>(isolate,
68 R"(
69 var module = require("./test-objects-module");
70 var test_object = module.new_TestClass(1, 2);
71 test_object.embedded_class_copy();
72 )");
73
74 EXPECT_EQ(test_object3.i(), -1);
75
76 isolate->Dispose();
77}
78
79TEST(Test, object_from_js)
80{
81 v8::Isolate* isolate = v8::Isolate::New();
82
83 auto test_object = v8cpp::run_script<TestClass>(isolate,
84 R"(
85 var module = require("./test-objects-module");
86 var test_object = module.new_TestClass(1, 2);
87 var test_object2 = module.new_TestClass(1, 2);
88
89 test_object.replace_i(test_object2.embedded_class_copy());
90 test_object;
91 )");
92
93 EXPECT_EQ(test_object.i(), -1);
94
95 auto result = v8cpp::run_script<int>(isolate,
96 R"(
97 var module = require("./test-objects-module");
98 var test_object = module.new_TestClass(1, 2);
99 var test_object2 = module.new_TestClass(1, 2);
100
101 test_object.add_i(test_object.embedded_class_ptr(),
102 test_object2.embedded_class_ref());
103 )");
104
105 EXPECT_EQ(result, -2);
106
107 isolate->Dispose();
108}
0109
=== added file 'tests/objects/test.h'
--- tests/objects/test.h 1970-01-01 00:00:00 +0000
+++ tests/objects/test.h 2015-07-02 11:59:44 +0000
@@ -0,0 +1,73 @@
1#pragma once
2
3#include <gtest/gtest.h>
4
5class EmbeddedTestClass
6{
7public:
8 EmbeddedTestClass(int a, int b)
9 {
10 EXPECT_EQ(a, 1);
11 EXPECT_EQ(b, 2);
12 i_ = a - b;
13 }
14
15 int i() const
16 {
17 return i_;
18 }
19
20private:
21 int i_;
22};
23
24class TestClass
25{
26public:
27 TestClass(int a, int b)
28 : embedded_class_(a, b)
29 {
30 EXPECT_EQ(a, 1);
31 EXPECT_EQ(b, 2);
32 i_ = a + b;
33 }
34
35 int i() const
36 {
37 return i_;
38 }
39
40 EmbeddedTestClass* embedded_class_ptr()
41 {
42 return &embedded_class_;
43 }
44
45 EmbeddedTestClass& embedded_class_ref()
46 {
47 return embedded_class_;
48 }
49
50 EmbeddedTestClass embedded_class_copy()
51 {
52 return embedded_class_;
53 }
54
55 void replace_i(EmbeddedTestClass const& other)
56 {
57 i_ = other.i();
58 }
59
60 int add_i(EmbeddedTestClass const& other, EmbeddedTestClass const& other2)
61 {
62 return other.i() + other2.i();
63 }
64
65private:
66 EmbeddedTestClass embedded_class_;
67 int i_;
68};
69
70TestClass* new_TestClass(int a, int b)
71{
72 return new TestClass(a, b);
73}

Subscribers

People subscribed via source and target branches

to all changes: