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