OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "gin/converter.h" | 5 #include "gin/converter.h" |
6 | 6 |
7 #include <limits.h> | 7 #include <limits.h> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "gin/public/isolate_holder.h" | 12 #include "gin/public/isolate_holder.h" |
13 #include "gin/test/v8_test.h" | 13 #include "gin/test/v8_test.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "v8/include/v8.h" | 15 #include "v8/include/v8.h" |
16 | 16 |
17 using v8::Array; | 17 using v8::Array; |
18 using v8::Boolean; | |
19 using v8::Handle; | |
20 using v8::HandleScope; | 18 using v8::HandleScope; |
21 using v8::Integer; | 19 using v8::Integer; |
22 using v8::Null; | 20 using v8::Null; |
23 using v8::Number; | 21 using v8::Number; |
24 using v8::Object; | 22 using v8::Object; |
25 using v8::String; | 23 using v8::String; |
26 using v8::Undefined; | 24 using v8::Undefined; |
27 using v8::Value; | |
rmcilroy
2014/10/06 15:41:03
I'm not sure why these changes are required? Can
baixo
2014/10/07 14:53:28
Done.
| |
28 | 25 |
29 namespace gin { | 26 namespace gin { |
30 | 27 |
31 typedef V8Test ConverterTest; | 28 typedef V8Test ConverterTest; |
32 | 29 |
33 TEST_F(ConverterTest, Bool) { | 30 TEST_F(ConverterTest, Bool) { |
34 HandleScope handle_scope(instance_->isolate()); | 31 HandleScope handle_scope(instance_->isolate()); |
35 | 32 |
36 EXPECT_TRUE(Converter<bool>::ToV8(instance_->isolate(), true)->StrictEquals( | 33 EXPECT_TRUE(Converter<bool>::ToV8(instance_->isolate(), true)->StrictEquals( |
37 Boolean::New(instance_->isolate(), true))); | 34 v8::Boolean::New(instance_->isolate(), true))); |
38 EXPECT_TRUE(Converter<bool>::ToV8(instance_->isolate(), false)->StrictEquals( | 35 EXPECT_TRUE(Converter<bool>::ToV8(instance_->isolate(), false)->StrictEquals( |
39 Boolean::New(instance_->isolate(), false))); | 36 v8::Boolean::New(instance_->isolate(), false))); |
40 | 37 |
41 struct { | 38 struct { |
42 Handle<Value> input; | 39 v8::Handle<v8::Value> input; |
43 bool expected; | 40 bool expected; |
44 } test_data[] = { | 41 } test_data[] = { |
45 { Boolean::New(instance_->isolate(), false).As<Value>(), false }, | 42 { v8::Boolean::New(instance_->isolate(), false).As<v8::Value>(), false }, |
46 { Boolean::New(instance_->isolate(), true).As<Value>(), true }, | 43 { v8::Boolean::New(instance_->isolate(), true).As<v8::Value>(), true }, |
47 { Number::New(instance_->isolate(), 0).As<Value>(), false }, | 44 { Number::New(instance_->isolate(), 0).As<v8::Value>(), false }, |
48 { Number::New(instance_->isolate(), 1).As<Value>(), true }, | 45 { Number::New(instance_->isolate(), 1).As<v8::Value>(), true }, |
49 { Number::New(instance_->isolate(), -1).As<Value>(), true }, | 46 { Number::New(instance_->isolate(), -1).As<v8::Value>(), true }, |
50 { Number::New(instance_->isolate(), 0.1).As<Value>(), true }, | 47 { Number::New(instance_->isolate(), 0.1).As<v8::Value>(), true }, |
51 { String::NewFromUtf8(instance_->isolate(), "").As<Value>(), false }, | 48 { String::NewFromUtf8(instance_->isolate(), "").As<v8::Value>(), false }, |
52 { String::NewFromUtf8(instance_->isolate(), "foo").As<Value>(), true }, | 49 { String::NewFromUtf8(instance_->isolate(), "foo").As<v8::Value>(), true }, |
53 { Object::New(instance_->isolate()).As<Value>(), true }, | 50 { Object::New(instance_->isolate()).As<v8::Value>(), true }, |
54 { Null(instance_->isolate()).As<Value>(), false }, | 51 { Null(instance_->isolate()).As<v8::Value>(), false }, |
55 { Undefined(instance_->isolate()).As<Value>(), false }, | 52 { Undefined(instance_->isolate()).As<v8::Value>(), false }, |
56 }; | 53 }; |
57 | 54 |
58 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | 55 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
59 bool result = false; | 56 bool result = false; |
60 EXPECT_TRUE(Converter<bool>::FromV8(instance_->isolate(), | 57 EXPECT_TRUE(Converter<bool>::FromV8(instance_->isolate(), |
61 test_data[i].input, &result)); | 58 test_data[i].input, &result)); |
62 EXPECT_EQ(test_data[i].expected, result); | 59 EXPECT_EQ(test_data[i].expected, result); |
63 | 60 |
64 result = true; | 61 result = true; |
65 EXPECT_TRUE(Converter<bool>::FromV8(instance_->isolate(), | 62 EXPECT_TRUE(Converter<bool>::FromV8(instance_->isolate(), |
(...skipping 10 matching lines...) Expand all Loading... | |
76 EXPECT_TRUE(Converter<int32_t>::ToV8(instance_->isolate(), test_data_to[i]) | 73 EXPECT_TRUE(Converter<int32_t>::ToV8(instance_->isolate(), test_data_to[i]) |
77 ->StrictEquals( | 74 ->StrictEquals( |
78 Integer::New(instance_->isolate(), test_data_to[i]))); | 75 Integer::New(instance_->isolate(), test_data_to[i]))); |
79 } | 76 } |
80 | 77 |
81 struct { | 78 struct { |
82 v8::Handle<v8::Value> input; | 79 v8::Handle<v8::Value> input; |
83 bool expect_sucess; | 80 bool expect_sucess; |
84 int expected_result; | 81 int expected_result; |
85 } test_data_from[] = { | 82 } test_data_from[] = { |
86 { Boolean::New(instance_->isolate(), false).As<Value>(), false, 0 }, | 83 { v8::Boolean::New(instance_->isolate(), false).As<v8::Value>(), false, 0 }, |
87 { Boolean::New(instance_->isolate(), true).As<Value>(), false, 0 }, | 84 { v8::Boolean::New(instance_->isolate(), true).As<v8::Value>(), false, 0 }, |
88 { Integer::New(instance_->isolate(), -1).As<Value>(), true, -1 }, | 85 { Integer::New(instance_->isolate(), -1).As<v8::Value>(), true, -1 }, |
89 { Integer::New(instance_->isolate(), 0).As<Value>(), true, 0 }, | 86 { Integer::New(instance_->isolate(), 0).As<v8::Value>(), true, 0 }, |
90 { Integer::New(instance_->isolate(), 1).As<Value>(), true, 1 }, | 87 { Integer::New(instance_->isolate(), 1).As<v8::Value>(), true, 1 }, |
91 { Number::New(instance_->isolate(), -1).As<Value>(), true, -1 }, | 88 { Number::New(instance_->isolate(), -1).As<v8::Value>(), true, -1 }, |
92 { Number::New(instance_->isolate(), 1.1).As<Value>(), false, 0 }, | 89 { Number::New(instance_->isolate(), 1.1).As<v8::Value>(), false, 0 }, |
93 { String::NewFromUtf8(instance_->isolate(), "42").As<Value>(), false, 0 }, | 90 { String::NewFromUtf8(instance_->isolate(), "42").As<v8::Value>(), |
94 { String::NewFromUtf8(instance_->isolate(), "foo").As<Value>(), false, 0 }, | 91 false, 0 }, |
95 { Object::New(instance_->isolate()).As<Value>(), false, 0 }, | 92 { String::NewFromUtf8(instance_->isolate(), "foo").As<v8::Value>(), |
96 { Array::New(instance_->isolate()).As<Value>(), false, 0 }, | 93 false, 0 }, |
97 { v8::Null(instance_->isolate()).As<Value>(), false, 0 }, | 94 { Object::New(instance_->isolate()).As<v8::Value>(), false, 0 }, |
98 { v8::Undefined(instance_->isolate()).As<Value>(), false, 0 }, | 95 { Array::New(instance_->isolate()).As<v8::Value>(), false, 0 }, |
96 { v8::Null(instance_->isolate()).As<v8::Value>(), false, 0 }, | |
97 { v8::Undefined(instance_->isolate()).As<v8::Value>(), false, 0 }, | |
99 }; | 98 }; |
100 | 99 |
101 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data_from); ++i) { | 100 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data_from); ++i) { |
102 int32_t result = std::numeric_limits<int32_t>::min(); | 101 int32_t result = std::numeric_limits<int32_t>::min(); |
103 bool success = Converter<int32_t>::FromV8(instance_->isolate(), | 102 bool success = Converter<int32_t>::FromV8(instance_->isolate(), |
104 test_data_from[i].input, &result); | 103 test_data_from[i].input, &result); |
105 EXPECT_EQ(test_data_from[i].expect_sucess, success) << i; | 104 EXPECT_EQ(test_data_from[i].expect_sucess, success) << i; |
106 if (success) | 105 if (success) |
107 EXPECT_EQ(test_data_from[i].expected_result, result) << i; | 106 EXPECT_EQ(test_data_from[i].expected_result, result) << i; |
108 } | 107 } |
109 } | 108 } |
110 | 109 |
111 TEST_F(ConverterTest, Vector) { | 110 TEST_F(ConverterTest, Vector) { |
112 HandleScope handle_scope(instance_->isolate()); | 111 HandleScope handle_scope(instance_->isolate()); |
113 | 112 |
114 std::vector<int> expected; | 113 std::vector<int> expected; |
115 expected.push_back(-1); | 114 expected.push_back(-1); |
116 expected.push_back(0); | 115 expected.push_back(0); |
117 expected.push_back(1); | 116 expected.push_back(1); |
118 | 117 |
119 Handle<Array> js_array = Handle<Array>::Cast( | 118 v8::Handle<Array> js_array = v8::Handle<Array>::Cast( |
120 Converter<std::vector<int> >::ToV8(instance_->isolate(), expected)); | 119 Converter<std::vector<int> >::ToV8(instance_->isolate(), expected)); |
121 ASSERT_FALSE(js_array.IsEmpty()); | 120 ASSERT_FALSE(js_array.IsEmpty()); |
122 EXPECT_EQ(3u, js_array->Length()); | 121 EXPECT_EQ(3u, js_array->Length()); |
123 for (size_t i = 0; i < expected.size(); ++i) { | 122 for (size_t i = 0; i < expected.size(); ++i) { |
124 EXPECT_TRUE(Integer::New(instance_->isolate(), expected[i]) | 123 EXPECT_TRUE(Integer::New(instance_->isolate(), expected[i]) |
125 ->StrictEquals(js_array->Get(static_cast<int>(i)))); | 124 ->StrictEquals(js_array->Get(static_cast<int>(i)))); |
126 } | 125 } |
127 | 126 |
128 std::vector<int> actual; | 127 std::vector<int> actual; |
129 EXPECT_TRUE(Converter<std::vector<int> >::FromV8(instance_->isolate(), | 128 EXPECT_TRUE(Converter<std::vector<int> >::FromV8(instance_->isolate(), |
130 js_array, &actual)); | 129 js_array, &actual)); |
131 EXPECT_EQ(expected, actual); | 130 EXPECT_EQ(expected, actual); |
132 } | 131 } |
133 | 132 |
134 } // namespace gin | 133 } // namespace gin |
OLD | NEW |