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

Proposed by Marcus Tomlinson on 2015-07-02
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 on 2015-07-02
37. By Marcus Tomlinson on 2015-07-02

Merged v8runner

38. By Marcus Tomlinson on 2015-07-02

Merged v8runner

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'CMakeLists.txt'
2--- CMakeLists.txt 2015-07-02 11:59:44 +0000
3+++ CMakeLists.txt 2015-07-02 11:59:44 +0000
4@@ -9,7 +9,7 @@
5 -fno-permissive
6 -pedantic
7 -Wall
8- -Wextra
9+# -Wextra
10 -fPIC
11 )
12
13@@ -21,6 +21,9 @@
14 ${CMAKE_CURRENT_SOURCE_DIR}/deps/v8/out/native/obj.target/tools/gyp
15 )
16
17+add_subdirectory(example)
18 add_subdirectory(src)
19-add_subdirectory(test)
20 add_subdirectory(v8runner)
21+
22+enable_testing()
23+add_subdirectory(tests)
24
25=== renamed directory 'test' => 'example'
26=== modified file 'example/CMakeLists.txt'
27--- test/CMakeLists.txt 2015-07-02 11:59:44 +0000
28+++ example/CMakeLists.txt 2015-07-02 11:59:44 +0000
29@@ -6,20 +6,20 @@
30
31 file(
32 GLOB
33- TEST_FILES
34+ EXAMPLE_FILES
35 *.h *.cpp *.js
36 )
37
38-configure_file(test.js test.js)
39+configure_file(example.js example.js)
40
41 add_library(
42- v8-cpp-test SHARED
43- ${TEST_FILES}
44+ v8-cpp-example SHARED
45+ ${EXAMPLE_FILES}
46 )
47
48-# This line sets the output binary name to "v8-cpp-exampleX.node"
49+# This line sets the output binary name to "v8-cpp-example.node"
50 set_target_properties(
51- v8-cpp-test
52+ v8-cpp-example
53 PROPERTIES
54 PREFIX ""
55 SUFFIX ".node"
56
57=== renamed file 'test/test.js' => 'example/example.js'
58--- test/test.js 2015-06-10 05:57:04 +0000
59+++ example/example.js 2015-07-02 11:59:44 +0000
60@@ -1,4 +1,4 @@
61-var addon = require('./v8-cpp-test');
62+var addon = require('./v8-cpp-example');
63
64 var obj = addon.new_MyObject(10);
65 console.log( obj.plus_one() ); // 11
66
67=== renamed file 'test/addon.cpp' => 'example/module.cpp'
68=== added directory 'tests'
69=== added file 'tests/CMakeLists.txt'
70--- tests/CMakeLists.txt 1970-01-01 00:00:00 +0000
71+++ tests/CMakeLists.txt 2015-07-02 11:59:44 +0000
72@@ -0,0 +1,12 @@
73+include(FindGMock)
74+
75+include_directories(
76+ ${GTEST_INCLUDE_DIRS}
77+ ${GMOCK_INCLUDE_DIRS}
78+ ${TEST_INCLUDE_DIRS}
79+)
80+
81+add_subdirectory(functions)
82+add_subdirectory(members)
83+add_subdirectory(methods)
84+add_subdirectory(objects)
85
86=== added directory 'tests/functions'
87=== added file 'tests/functions/CMakeLists.txt'
88--- tests/functions/CMakeLists.txt 1970-01-01 00:00:00 +0000
89+++ tests/functions/CMakeLists.txt 2015-07-02 11:59:44 +0000
90@@ -0,0 +1,35 @@
91+# MODULE
92+add_library(
93+ test-functions-module SHARED
94+ module.cpp
95+)
96+
97+# This line sets the output binary name to "<target_name>.node"
98+set_target_properties(
99+ test-functions-module
100+ PROPERTIES
101+ PREFIX ""
102+ SUFFIX ".node"
103+)
104+
105+# TEST
106+add_executable(
107+ test-functions
108+ test.h
109+ test.cpp
110+)
111+
112+target_link_libraries(
113+ test-functions
114+
115+ v8-cpp
116+
117+ ${GTEST_BOTH_LIBRARIES}
118+ ${GMOCK_LIBRARIES}
119+ ${TEST_LDFLAGS}
120+)
121+
122+add_test(
123+ test-functions
124+ test-functions
125+)
126
127=== added file 'tests/functions/module.cpp'
128--- tests/functions/module.cpp 1970-01-01 00:00:00 +0000
129+++ tests/functions/module.cpp 2015-07-02 11:59:44 +0000
130@@ -0,0 +1,28 @@
131+#include "test.h"
132+
133+#include <v8-cpp.h>
134+
135+using namespace v8;
136+
137+void InitAll(Handle<Object> exports)
138+{
139+ // Get current isolate
140+ Isolate* isolate = Isolate::GetCurrent();
141+
142+ // Prepare TestClass binding
143+ v8cpp::Class<TestCaller> testcaller(isolate);
144+ testcaller
145+ .set_constructor<Local<Function>>()
146+ .add_method("call_me", &TestCaller::call_me);
147+
148+ // Prepare module
149+ v8cpp::Module module(isolate);
150+
151+ module.add_class("TestCaller", testcaller);
152+ module.add_function("simple_function", &simple_function);
153+ module.add_function("complex_function", &complex_function);
154+
155+ exports->SetPrototype(module.create_prototype());
156+}
157+
158+V8CPP_MODULE(addon, InitAll)
159
160=== added file 'tests/functions/test.cpp'
161--- tests/functions/test.cpp 1970-01-01 00:00:00 +0000
162+++ tests/functions/test.cpp 2015-07-02 11:59:44 +0000
163@@ -0,0 +1,57 @@
164+#include "test.h"
165+
166+#include <v8-cpp.h>
167+
168+#include <gtest/gtest.h>
169+
170+// FUNCTIONS
171+// call JS function from C++ (callback)
172+// call C++ functions from JS
173+
174+TEST(Test, call_to_js)
175+{
176+ v8::Isolate* isolate = v8::Isolate::New();
177+
178+ auto callback_result = v8cpp::run_script<std::string>(isolate,
179+ R"(
180+ var module = require("./test-functions-module");
181+
182+ var caller = new module.TestCaller(function(message)
183+ {
184+ return message + " world" // "hello world"
185+ });
186+
187+ caller.call_me();
188+ )");
189+
190+ EXPECT_EQ(callback_result, "hello world");
191+
192+ isolate->Dispose();
193+}
194+
195+TEST(Test, call_from_js)
196+{
197+ v8::Isolate* isolate = v8::Isolate::New();
198+
199+ auto result = v8cpp::run_script<std::string>(isolate,
200+ R"(
201+ var module = require("./test-functions-module");
202+ module.simple_function();
203+ )");
204+
205+ EXPECT_EQ(result, "hello there");
206+
207+ auto result2 = v8cpp::run_script<std::vector<float>>(isolate,
208+ R"(
209+ var module = require("./test-functions-module");
210+ module.complex_function(4, "3", 2.1, false);
211+ )");
212+
213+ ASSERT_EQ(result2.size(), 4);
214+ EXPECT_FLOAT_EQ(result2[0], 4);
215+ EXPECT_FLOAT_EQ(result2[1], 3);
216+ EXPECT_FLOAT_EQ(result2[2], 2.1);
217+ EXPECT_FLOAT_EQ(result2[3], 0);
218+
219+ isolate->Dispose();
220+}
221
222=== added file 'tests/functions/test.h'
223--- tests/functions/test.h 1970-01-01 00:00:00 +0000
224+++ tests/functions/test.h 2015-07-02 11:59:44 +0000
225@@ -0,0 +1,44 @@
226+#pragma once
227+
228+#include <gtest/gtest.h>
229+
230+#include <v8-cpp.h>
231+
232+class TestCaller
233+{
234+public:
235+ explicit TestCaller(v8::Local<v8::Function> const& cb)
236+ : cb_(v8::Isolate::GetCurrent(), cb)
237+ {
238+ }
239+
240+ std::string call_me() const
241+ {
242+ v8::Local<v8::Function> cb = v8cpp::to_local<v8::Function>(v8::Isolate::GetCurrent(), cb_);
243+ auto result = v8cpp::call_v8(v8::Isolate::GetCurrent(), cb, "hello");
244+ return v8cpp::from_v8<std::string>(v8::Isolate::GetCurrent(), result);
245+ }
246+
247+private:
248+ v8::Persistent<v8::Function> cb_;
249+};
250+
251+std::string simple_function()
252+{
253+ return "hello there";
254+}
255+
256+std::vector<float> complex_function(int first, std::string const& second, float third, bool forth)
257+{
258+ EXPECT_EQ(first, 4);
259+ EXPECT_EQ(second, "3");
260+ EXPECT_FLOAT_EQ(third, 2.1);
261+ EXPECT_EQ(forth, false);
262+
263+ std::vector<float> num_list;
264+ num_list.push_back(first);
265+ num_list.push_back(stoi(second));
266+ num_list.push_back(third);
267+ num_list.push_back(forth);
268+ return num_list;
269+}
270
271=== added directory 'tests/members'
272=== added file 'tests/members/CMakeLists.txt'
273--- tests/members/CMakeLists.txt 1970-01-01 00:00:00 +0000
274+++ tests/members/CMakeLists.txt 2015-07-02 11:59:44 +0000
275@@ -0,0 +1,35 @@
276+# MODULE
277+add_library(
278+ test-members-module SHARED
279+ module.cpp
280+)
281+
282+# This line sets the output binary name to "<target_name>.node"
283+set_target_properties(
284+ test-members-module
285+ PROPERTIES
286+ PREFIX ""
287+ SUFFIX ".node"
288+)
289+
290+# TEST
291+add_executable(
292+ test-members
293+ test.h
294+ test.cpp
295+)
296+
297+target_link_libraries(
298+ test-members
299+
300+ v8-cpp
301+
302+ ${GTEST_BOTH_LIBRARIES}
303+ ${GMOCK_LIBRARIES}
304+ ${TEST_LDFLAGS}
305+)
306+
307+add_test(
308+ test-members
309+ test-members
310+)
311
312=== added file 'tests/members/module.cpp'
313--- tests/members/module.cpp 1970-01-01 00:00:00 +0000
314+++ tests/members/module.cpp 2015-07-02 11:59:44 +0000
315@@ -0,0 +1,29 @@
316+#include "test.h"
317+
318+#include <v8-cpp.h>
319+
320+using namespace v8;
321+
322+void InitAll(Handle<Object> exports)
323+{
324+ // Get current isolate
325+ Isolate* isolate = Isolate::GetCurrent();
326+
327+ // Prepare TestStruct binding
328+ v8cpp::Class<TestStruct> teststruct(isolate);
329+ teststruct
330+ .set_constructor()
331+ .add_member("bool_value", &TestStruct::bool_value)
332+ .add_member("int_value", &TestStruct::int_value)
333+ .add_member("float_value", &TestStruct::float_value)
334+ .add_member("string_value", &TestStruct::string_value);
335+
336+ // Prepare module
337+ v8cpp::Module module(isolate);
338+
339+ module.add_class("TestStruct", teststruct);
340+
341+ exports->SetPrototype(module.create_prototype());
342+}
343+
344+V8CPP_MODULE(addon, InitAll)
345
346=== added file 'tests/members/test.cpp'
347--- tests/members/test.cpp 1970-01-01 00:00:00 +0000
348+++ tests/members/test.cpp 2015-07-02 11:59:44 +0000
349@@ -0,0 +1,46 @@
350+#include "test.h"
351+
352+#include <v8-cpp.h>
353+
354+#include <gtest/gtest.h>
355+
356+// MEMBERS
357+// get/set class/struct members from JS
358+
359+TEST(Test, get_set_members_from_js)
360+{
361+ v8::Isolate* isolate = v8::Isolate::New();
362+
363+ auto test_struct = v8cpp::run_script<TestStruct>(isolate,
364+ R"(
365+ var module = require("./test-members-module");
366+ var test_struct = new module.TestStruct();
367+ test_struct;
368+ )");
369+
370+ EXPECT_EQ(test_struct.bool_value, true);
371+ EXPECT_EQ(test_struct.int_value, 9);
372+ EXPECT_FLOAT_EQ(test_struct.float_value, 0.1);
373+ EXPECT_EQ(test_struct.string_value, "hello");
374+
375+ test_struct = v8cpp::run_script<TestStruct>(isolate,
376+ R"(
377+ var module = require("./test-members-module");
378+ var test_struct = new module.TestStruct();
379+ if (test_struct.bool_value)
380+ {
381+ test_struct.bool_value = false;
382+ test_struct.int_value = -1;
383+ test_struct.float_value = test_struct.int_value + 0.12;
384+ test_struct.string_value = test_struct.string_value + " there";
385+ }
386+ test_struct;
387+ )");
388+
389+ EXPECT_EQ(test_struct.bool_value, false);
390+ EXPECT_EQ(test_struct.int_value, -1);
391+ EXPECT_FLOAT_EQ(test_struct.float_value, -0.88);
392+ EXPECT_EQ(test_struct.string_value, "hello there");
393+
394+ isolate->Dispose();
395+}
396
397=== added file 'tests/members/test.h'
398--- tests/members/test.h 1970-01-01 00:00:00 +0000
399+++ tests/members/test.h 2015-07-02 11:59:44 +0000
400@@ -0,0 +1,11 @@
401+#pragma once
402+
403+#include <gtest/gtest.h>
404+
405+struct TestStruct
406+{
407+ bool bool_value = true;
408+ int int_value = 9;
409+ float float_value = 0.1;
410+ std::string string_value = "hello";
411+};
412
413=== added directory 'tests/methods'
414=== added file 'tests/methods/CMakeLists.txt'
415--- tests/methods/CMakeLists.txt 1970-01-01 00:00:00 +0000
416+++ tests/methods/CMakeLists.txt 2015-07-02 11:59:44 +0000
417@@ -0,0 +1,35 @@
418+# MODULE
419+add_library(
420+ test-methods-module SHARED
421+ module.cpp
422+)
423+
424+# This line sets the output binary name to "<target_name>.node"
425+set_target_properties(
426+ test-methods-module
427+ PROPERTIES
428+ PREFIX ""
429+ SUFFIX ".node"
430+)
431+
432+# TEST
433+add_executable(
434+ test-methods
435+ test.h
436+ test.cpp
437+)
438+
439+target_link_libraries(
440+ test-methods
441+
442+ v8-cpp
443+
444+ ${GTEST_BOTH_LIBRARIES}
445+ ${GMOCK_LIBRARIES}
446+ ${TEST_LDFLAGS}
447+)
448+
449+add_test(
450+ test-methods
451+ test-methods
452+)
453
454=== added file 'tests/methods/module.cpp'
455--- tests/methods/module.cpp 1970-01-01 00:00:00 +0000
456+++ tests/methods/module.cpp 2015-07-02 11:59:44 +0000
457@@ -0,0 +1,32 @@
458+#include "test.h"
459+
460+#include <v8-cpp.h>
461+
462+using namespace v8;
463+
464+void InitAll(Handle<Object> exports)
465+{
466+ // Get current isolate
467+ Isolate* isolate = Isolate::GetCurrent();
468+
469+ // Prepare TestClass binding
470+ v8cpp::Class<TestClass_OL> testclass(isolate);
471+ testclass
472+ .add_inheritance<BaseTestClass>()
473+ .add_inheritance<TestClass>()
474+ .set_constructor()
475+ .add_method("regular_method", &TestClass::regular_method)
476+ .add_method("static_method", &TestClass::static_method)
477+ .add_method("base_method", &BaseTestClass::base_method)
478+ .add_method("virtual_method", &TestClass::virtual_method)
479+ .add_method("overload_method", &TestClass_OL::overload_method);
480+
481+ // Prepare module
482+ v8cpp::Module module(isolate);
483+
484+ module.add_class("TestClass", testclass);
485+
486+ exports->SetPrototype(module.create_prototype());
487+}
488+
489+V8CPP_MODULE(addon, InitAll)
490
491=== added file 'tests/methods/test.cpp'
492--- tests/methods/test.cpp 1970-01-01 00:00:00 +0000
493+++ tests/methods/test.cpp 2015-07-02 11:59:44 +0000
494@@ -0,0 +1,100 @@
495+#include "test.h"
496+
497+#include <v8-cpp.h>
498+
499+#include <gtest/gtest.h>
500+
501+// METHODS
502+// call regular class methods from JS
503+// call static class methods from JS
504+// call base class methods from JS (inheritance)
505+// call overridden class methods from JS (inheritance)
506+// call overloaded class methods from JS
507+
508+TEST(Test, call_regular_method_from_js)
509+{
510+ v8::Isolate* isolate = v8::Isolate::New();
511+
512+ auto result = v8cpp::run_script<int>(isolate,
513+ R"(
514+ var module = require("./test-methods-module");
515+ var test_object = new module.TestClass();
516+ test_object.regular_method();
517+ )");
518+
519+ EXPECT_EQ(result, 2);
520+
521+ isolate->Dispose();
522+}
523+
524+TEST(Test, call_static_method_from_js)
525+{
526+ v8::Isolate* isolate = v8::Isolate::New();
527+
528+ auto result = v8cpp::run_script<int>(isolate,
529+ R"(
530+ var module = require("./test-methods-module");
531+ module.TestClass.static_method();
532+ )");
533+
534+ EXPECT_EQ(result, 3);
535+
536+ isolate->Dispose();
537+}
538+
539+TEST(Test, call_base_method_from_js)
540+{
541+ v8::Isolate* isolate = v8::Isolate::New();
542+
543+ auto result = v8cpp::run_script<int>(isolate,
544+ R"(
545+ var module = require("./test-methods-module");
546+ var test_object = new module.TestClass();
547+ test_object.base_method();
548+ )");
549+
550+ EXPECT_EQ(result, 1);
551+
552+ isolate->Dispose();
553+}
554+
555+TEST(Test, call_override_method_from_js)
556+{
557+ v8::Isolate* isolate = v8::Isolate::New();
558+
559+ auto result = v8cpp::run_script<int>(isolate,
560+ R"(
561+ var module = require("./test-methods-module");
562+ var test_object = new module.TestClass();
563+ test_object.virtual_method();
564+ )");
565+
566+ EXPECT_EQ(result, 4);
567+
568+ isolate->Dispose();
569+}
570+
571+TEST(Test, call_overload_method_from_js)
572+{
573+ v8::Isolate* isolate = v8::Isolate::New();
574+
575+ auto result = v8cpp::run_script<int>(isolate,
576+ R"(
577+ var module = require("./test-methods-module");
578+ var test_object = new module.TestClass();
579+ test_object.overload_method(5);
580+ )");
581+
582+ EXPECT_EQ(result, 5);
583+
584+ auto result2 = v8cpp::run_script<std::string>(isolate,
585+ R"(
586+ var module = require("./test-methods-module");
587+ var test_object = new module.TestClass();
588+ test_object.overload_method(1, 5);
589+ )");
590+
591+ EXPECT_EQ(result2, "6");
592+
593+ isolate->Dispose();
594+}
595
596=== added file 'tests/methods/test.h'
597--- tests/methods/test.h 1970-01-01 00:00:00 +0000
598+++ tests/methods/test.h 2015-07-02 11:59:44 +0000
599@@ -0,0 +1,71 @@
600+#pragma once
601+
602+#include <gtest/gtest.h>
603+
604+#include <v8-cpp.h>
605+
606+class BaseTestClass
607+{
608+public:
609+ virtual ~BaseTestClass() = default;
610+
611+ int base_method()
612+ {
613+ return 1;
614+ }
615+
616+ virtual int virtual_method() = 0;
617+};
618+
619+class TestClass : public BaseTestClass
620+{
621+public:
622+ virtual ~TestClass() = default;
623+
624+ int regular_method()
625+ {
626+ return 2;
627+ }
628+
629+ static int static_method()
630+ {
631+ return 3;
632+ }
633+
634+ int virtual_method() override
635+ {
636+ return 4;
637+ }
638+
639+ int overload_method(int a)
640+ {
641+ return a;
642+ }
643+
644+ std::string overload_method(int a, int b)
645+ {
646+ return std::to_string(a + b);
647+ }
648+};
649+
650+class TestClass_OL : public TestClass
651+{
652+public:
653+ using TestClass::TestClass;
654+
655+ v8::Local<v8::Value> overload_method(v8::FunctionCallbackInfo<v8::Value> const& args)
656+ {
657+ if (args.Length() == 1)
658+ {
659+ int arg = v8cpp::from_v8<int>(v8::Isolate::GetCurrent(), args[0]);
660+ return v8cpp::to_v8(v8::Isolate::GetCurrent(), TestClass::overload_method(arg));
661+ }
662+ else if (args.Length() == 2)
663+ {
664+ int arg = v8cpp::from_v8<int>(v8::Isolate::GetCurrent(), args[0]);
665+ int arg2 = v8cpp::from_v8<int>(v8::Isolate::GetCurrent(), args[1]);
666+ return v8cpp::to_v8(v8::Isolate::GetCurrent(), TestClass::overload_method(arg, arg2));
667+ }
668+ return v8cpp::to_v8(v8::Isolate::GetCurrent(), 0);
669+ }
670+};
671
672=== added directory 'tests/objects'
673=== added file 'tests/objects/CMakeLists.txt'
674--- tests/objects/CMakeLists.txt 1970-01-01 00:00:00 +0000
675+++ tests/objects/CMakeLists.txt 2015-07-02 11:59:44 +0000
676@@ -0,0 +1,35 @@
677+# MODULE
678+add_library(
679+ test-objects-module SHARED
680+ module.cpp
681+)
682+
683+# This line sets the output binary name to "<target_name>.node"
684+set_target_properties(
685+ test-objects-module
686+ PROPERTIES
687+ PREFIX ""
688+ SUFFIX ".node"
689+)
690+
691+# TEST
692+add_executable(
693+ test-objects
694+ test.h
695+ test.cpp
696+)
697+
698+target_link_libraries(
699+ test-objects
700+
701+ v8-cpp
702+
703+ ${GTEST_BOTH_LIBRARIES}
704+ ${GMOCK_LIBRARIES}
705+ ${TEST_LDFLAGS}
706+)
707+
708+add_test(
709+ test-objects
710+ test-objects
711+)
712
713=== added file 'tests/objects/module.cpp'
714--- tests/objects/module.cpp 1970-01-01 00:00:00 +0000
715+++ tests/objects/module.cpp 2015-07-02 11:59:44 +0000
716@@ -0,0 +1,40 @@
717+#include "test.h"
718+
719+#include <v8-cpp.h>
720+
721+using namespace v8;
722+
723+void InitAll(Handle<Object> exports)
724+{
725+ // Get current isolate
726+ Isolate* isolate = Isolate::GetCurrent();
727+
728+ // Prepare TestClass binding
729+ v8cpp::Class<TestClass> testclass(isolate);
730+ testclass
731+ .set_constructor<int, int>()
732+ .add_method("i", &TestClass::i)
733+ .add_method("embedded_class_ptr", &TestClass::embedded_class_ptr)
734+ .add_method("embedded_class_ref", &TestClass::embedded_class_ref)
735+ .add_method("embedded_class_copy", &TestClass::embedded_class_copy)
736+ .add_method("replace_i", &TestClass::replace_i)
737+ .add_method("add_i", &TestClass::add_i);
738+
739+ // Prepare EmbeddedTestClass binding
740+ v8cpp::Class<EmbeddedTestClass> embeddedtestclass(isolate);
741+ embeddedtestclass
742+ .set_constructor<int, int>()
743+ .add_method("i", &EmbeddedTestClass::i);
744+
745+ // Prepare module
746+ v8cpp::Module module(isolate);
747+
748+ module.add_class("TestClass", testclass);
749+ module.add_class("EmbeddedTestClass", embeddedtestclass);
750+
751+ module.add_function("new_TestClass", &new_TestClass);
752+
753+ exports->SetPrototype(module.create_prototype());
754+}
755+
756+V8CPP_MODULE(addon, InitAll)
757
758=== added file 'tests/objects/test.cpp'
759--- tests/objects/test.cpp 1970-01-01 00:00:00 +0000
760+++ tests/objects/test.cpp 2015-07-02 11:59:44 +0000
761@@ -0,0 +1,108 @@
762+#include "test.h"
763+
764+#include <v8-cpp.h>
765+
766+#include <gtest/gtest.h>
767+
768+// OBJECTS
769+// construct class via new() from JS
770+// construct class via factory methods from JS (constructor overloads)
771+// move/ref objects to JS
772+// move/ref objects back to C++
773+
774+TEST(Test, construct_class_via_new)
775+{
776+ v8::Isolate* isolate = v8::Isolate::New();
777+
778+ auto test_object = v8cpp::run_script<TestClass>(isolate,
779+ R"(
780+ var module = require("./test-objects-module");
781+ var test_object = new module.TestClass(1, 2);
782+ test_object;
783+ )");
784+
785+ EXPECT_EQ(test_object.i(), 3);
786+
787+ isolate->Dispose();
788+}
789+
790+TEST(Test, construct_class_via_factory)
791+{
792+ v8::Isolate* isolate = v8::Isolate::New();
793+
794+ auto test_object = v8cpp::run_script<TestClass>(isolate,
795+ R"(
796+ var module = require("./test-objects-module");
797+ var test_object = module.new_TestClass(1, 2);
798+ test_object;
799+ )");
800+
801+ EXPECT_EQ(test_object.i(), 3);
802+
803+ isolate->Dispose();
804+}
805+
806+TEST(Test, object_to_js)
807+{
808+ v8::Isolate* isolate = v8::Isolate::New();
809+
810+ auto test_object = v8cpp::run_script<EmbeddedTestClass*>(isolate,
811+ R"(
812+ var module = require("./test-objects-module");
813+ var test_object = module.new_TestClass(1, 2);
814+ test_object.embedded_class_ptr();
815+ )");
816+
817+ EXPECT_EQ(test_object->i(), -1);
818+
819+ auto test_object2 = v8cpp::run_script<EmbeddedTestClass&>(isolate,
820+ R"(
821+ var module = require("./test-objects-module");
822+ var test_object = module.new_TestClass(1, 2);
823+ test_object.embedded_class_ref();
824+ )");
825+
826+ EXPECT_EQ(test_object2.i(), -1);
827+
828+ auto test_object3 = v8cpp::run_script<EmbeddedTestClass>(isolate,
829+ R"(
830+ var module = require("./test-objects-module");
831+ var test_object = module.new_TestClass(1, 2);
832+ test_object.embedded_class_copy();
833+ )");
834+
835+ EXPECT_EQ(test_object3.i(), -1);
836+
837+ isolate->Dispose();
838+}
839+
840+TEST(Test, object_from_js)
841+{
842+ v8::Isolate* isolate = v8::Isolate::New();
843+
844+ auto test_object = v8cpp::run_script<TestClass>(isolate,
845+ R"(
846+ var module = require("./test-objects-module");
847+ var test_object = module.new_TestClass(1, 2);
848+ var test_object2 = module.new_TestClass(1, 2);
849+
850+ test_object.replace_i(test_object2.embedded_class_copy());
851+ test_object;
852+ )");
853+
854+ EXPECT_EQ(test_object.i(), -1);
855+
856+ auto result = v8cpp::run_script<int>(isolate,
857+ R"(
858+ var module = require("./test-objects-module");
859+ var test_object = module.new_TestClass(1, 2);
860+ var test_object2 = module.new_TestClass(1, 2);
861+
862+ test_object.add_i(test_object.embedded_class_ptr(),
863+ test_object2.embedded_class_ref());
864+ )");
865+
866+ EXPECT_EQ(result, -2);
867+
868+ isolate->Dispose();
869+}
870
871=== added file 'tests/objects/test.h'
872--- tests/objects/test.h 1970-01-01 00:00:00 +0000
873+++ tests/objects/test.h 2015-07-02 11:59:44 +0000
874@@ -0,0 +1,73 @@
875+#pragma once
876+
877+#include <gtest/gtest.h>
878+
879+class EmbeddedTestClass
880+{
881+public:
882+ EmbeddedTestClass(int a, int b)
883+ {
884+ EXPECT_EQ(a, 1);
885+ EXPECT_EQ(b, 2);
886+ i_ = a - b;
887+ }
888+
889+ int i() const
890+ {
891+ return i_;
892+ }
893+
894+private:
895+ int i_;
896+};
897+
898+class TestClass
899+{
900+public:
901+ TestClass(int a, int b)
902+ : embedded_class_(a, b)
903+ {
904+ EXPECT_EQ(a, 1);
905+ EXPECT_EQ(b, 2);
906+ i_ = a + b;
907+ }
908+
909+ int i() const
910+ {
911+ return i_;
912+ }
913+
914+ EmbeddedTestClass* embedded_class_ptr()
915+ {
916+ return &embedded_class_;
917+ }
918+
919+ EmbeddedTestClass& embedded_class_ref()
920+ {
921+ return embedded_class_;
922+ }
923+
924+ EmbeddedTestClass embedded_class_copy()
925+ {
926+ return embedded_class_;
927+ }
928+
929+ void replace_i(EmbeddedTestClass const& other)
930+ {
931+ i_ = other.i();
932+ }
933+
934+ int add_i(EmbeddedTestClass const& other, EmbeddedTestClass const& other2)
935+ {
936+ return other.i() + other2.i();
937+ }
938+
939+private:
940+ EmbeddedTestClass embedded_class_;
941+ int i_;
942+};
943+
944+TestClass* new_TestClass(int a, int b)
945+{
946+ return new TestClass(a, b);
947+}

Subscribers

People subscribed via source and target branches

to all changes: