Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: gin/converter_unittest.cc

Issue 67763002: Add gin, a lightweight bindings system for V8 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address Aaron's comments Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « gin/converter.cc ('k') | gin/gin.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "gin/converter.h"
6
7 #include <limits.h>
8
9 #include "base/basictypes.h"
10 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "gin/test/v8_test.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "v8/include/v8.h"
15
16 using v8::Array;
17 using v8::Boolean;
18 using v8::Handle;
19 using v8::HandleScope;
20 using v8::Integer;
21 using v8::Null;
22 using v8::Number;
23 using v8::Object;
24 using v8::String;
25 using v8::Undefined;
26 using v8::Value;
27
28 namespace gin {
29
30 typedef V8Test ConverterTest;
31
32 TEST_F(ConverterTest, Bool) {
33 HandleScope handle_scope(isolate_);
34
35 EXPECT_TRUE(Converter<bool>::ToV8(isolate_, true)->StrictEquals(
36 Boolean::New(true)));
37 EXPECT_TRUE(Converter<bool>::ToV8(isolate_, false)->StrictEquals(
38 Boolean::New(false)));
39
40 struct {
41 Handle<Value> input;
42 bool expected;
43 } test_data[] = {
44 { Boolean::New(false).As<Value>(), false },
45 { Boolean::New(true).As<Value>(), true },
46 { Number::New(0).As<Value>(), false },
47 { Number::New(1).As<Value>(), true },
48 { Number::New(-1).As<Value>(), true },
49 { Number::New(0.1).As<Value>(), true },
50 { String::New("").As<Value>(), false },
51 { String::New("foo").As<Value>(), true },
52 { Object::New().As<Value>(), true },
53 { Null().As<Value>(), false },
54 { Undefined().As<Value>(), false },
55 };
56
57 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
58 bool result = false;
59 EXPECT_TRUE(Converter<bool>::FromV8(test_data[i].input, &result));
60 EXPECT_EQ(test_data[i].expected, result);
61
62 result = true;
63 EXPECT_TRUE(Converter<bool>::FromV8(test_data[i].input, &result));
64 EXPECT_EQ(test_data[i].expected, result);
65 }
66 }
67
68 TEST_F(ConverterTest, Int32) {
69 HandleScope handle_scope(isolate_);
70
71 int test_data_to[] = {-1, 0, 1};
72 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data_to); ++i) {
73 EXPECT_TRUE(Converter<int32_t>::ToV8(isolate_,
74 test_data_to[i])->StrictEquals(Integer::New(test_data_to[i])));
75 }
76
77 struct {
78 v8::Handle<v8::Value> input;
79 bool expect_sucess;
80 int expected_result;
81 } test_data_from[] = {
82 { Boolean::New(false).As<Value>(), false, 0 },
83 { Boolean::New(true).As<Value>(), false, 0 },
84 { Integer::New(-1).As<Value>(), true, -1 },
85 { Integer::New(0).As<Value>(), true, 0 },
86 { Integer::New(1).As<Value>(), true, 1 },
87 { Number::New(-1).As<Value>(), true, -1 },
88 { Number::New(1.1).As<Value>(), false, 0 },
89 { String::New("42").As<Value>(), false, 0 },
90 { String::New("foo").As<Value>(), false, 0 },
91 { Object::New().As<Value>(), false, 0 },
92 { Array::New().As<Value>(), false, 0 },
93 { v8::Null().As<Value>(), false, 0 },
94 { v8::Undefined().As<Value>(), false, 0 },
95 };
96
97 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data_from); ++i) {
98 int32_t result = std::numeric_limits<int32_t>::min();
99 bool success = Converter<int32_t>::FromV8(test_data_from[i].input, &result);
100 EXPECT_EQ(test_data_from[i].expect_sucess, success) << i;
101 if (success)
102 EXPECT_EQ(test_data_from[i].expected_result, result) << i;
103 }
104 }
105
106 TEST_F(ConverterTest, Vector) {
107 HandleScope handle_scope(isolate_);
108
109 std::vector<int> expected;
110 expected.push_back(-1);
111 expected.push_back(0);
112 expected.push_back(1);
113
114 Handle<Array> js_array = Handle<Array>::Cast(
115 Converter<std::vector<int> >::ToV8(isolate_, expected));
116 ASSERT_FALSE(js_array.IsEmpty());
117 EXPECT_EQ(3u, js_array->Length());
118 for (size_t i = 0; i < expected.size(); ++i) {
119 EXPECT_TRUE(Integer::New(expected[i])->StrictEquals(
120 js_array->Get(static_cast<int>(i))));
121 }
122
123 std::vector<int> actual;
124 EXPECT_TRUE(Converter<std::vector<int> >::FromV8(js_array, &actual));
125 EXPECT_EQ(expected, actual);
126 }
127
128 } // namespace gin
OLDNEW
« no previous file with comments | « gin/converter.cc ('k') | gin/gin.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698