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/gin.h" |
12 #include "gin/test/v8_test.h" | 13 #include "gin/test/v8_test.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "v8/include/v8.h" | 15 #include "v8/include/v8.h" |
15 | 16 |
16 using v8::Array; | 17 using v8::Array; |
17 using v8::Boolean; | 18 using v8::Boolean; |
18 using v8::Handle; | 19 using v8::Handle; |
19 using v8::HandleScope; | 20 using v8::HandleScope; |
20 using v8::Integer; | 21 using v8::Integer; |
21 using v8::Null; | 22 using v8::Null; |
22 using v8::Number; | 23 using v8::Number; |
23 using v8::Object; | 24 using v8::Object; |
24 using v8::String; | 25 using v8::String; |
25 using v8::Undefined; | 26 using v8::Undefined; |
26 using v8::Value; | 27 using v8::Value; |
27 | 28 |
28 namespace gin { | 29 namespace gin { |
29 | 30 |
30 typedef V8Test ConverterTest; | 31 typedef V8Test ConverterTest; |
31 | 32 |
32 TEST_F(ConverterTest, Bool) { | 33 TEST_F(ConverterTest, Bool) { |
33 HandleScope handle_scope(isolate_); | 34 HandleScope handle_scope(instance_->isolate()); |
34 | 35 |
35 EXPECT_TRUE(Converter<bool>::ToV8(isolate_, true)->StrictEquals( | 36 EXPECT_TRUE(Converter<bool>::ToV8(instance_->isolate(), true)->StrictEquals( |
36 Boolean::New(true))); | 37 Boolean::New(true))); |
37 EXPECT_TRUE(Converter<bool>::ToV8(isolate_, false)->StrictEquals( | 38 EXPECT_TRUE(Converter<bool>::ToV8(instance_->isolate(), false)->StrictEquals( |
38 Boolean::New(false))); | 39 Boolean::New(false))); |
39 | 40 |
40 struct { | 41 struct { |
41 Handle<Value> input; | 42 Handle<Value> input; |
42 bool expected; | 43 bool expected; |
43 } test_data[] = { | 44 } test_data[] = { |
44 { Boolean::New(false).As<Value>(), false }, | 45 { Boolean::New(false).As<Value>(), false }, |
45 { Boolean::New(true).As<Value>(), true }, | 46 { Boolean::New(true).As<Value>(), true }, |
46 { Number::New(0).As<Value>(), false }, | 47 { Number::New(0).As<Value>(), false }, |
47 { Number::New(1).As<Value>(), true }, | 48 { Number::New(1).As<Value>(), true }, |
(...skipping 11 matching lines...) Expand all Loading... |
59 EXPECT_TRUE(Converter<bool>::FromV8(test_data[i].input, &result)); | 60 EXPECT_TRUE(Converter<bool>::FromV8(test_data[i].input, &result)); |
60 EXPECT_EQ(test_data[i].expected, result); | 61 EXPECT_EQ(test_data[i].expected, result); |
61 | 62 |
62 result = true; | 63 result = true; |
63 EXPECT_TRUE(Converter<bool>::FromV8(test_data[i].input, &result)); | 64 EXPECT_TRUE(Converter<bool>::FromV8(test_data[i].input, &result)); |
64 EXPECT_EQ(test_data[i].expected, result); | 65 EXPECT_EQ(test_data[i].expected, result); |
65 } | 66 } |
66 } | 67 } |
67 | 68 |
68 TEST_F(ConverterTest, Int32) { | 69 TEST_F(ConverterTest, Int32) { |
69 HandleScope handle_scope(isolate_); | 70 HandleScope handle_scope(instance_->isolate()); |
70 | 71 |
71 int test_data_to[] = {-1, 0, 1}; | 72 int test_data_to[] = {-1, 0, 1}; |
72 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data_to); ++i) { | 73 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data_to); ++i) { |
73 EXPECT_TRUE(Converter<int32_t>::ToV8(isolate_, | 74 EXPECT_TRUE(Converter<int32_t>::ToV8(instance_->isolate(), |
74 test_data_to[i])->StrictEquals(Integer::New(test_data_to[i]))); | 75 test_data_to[i])->StrictEquals(Integer::New(test_data_to[i]))); |
75 } | 76 } |
76 | 77 |
77 struct { | 78 struct { |
78 v8::Handle<v8::Value> input; | 79 v8::Handle<v8::Value> input; |
79 bool expect_sucess; | 80 bool expect_sucess; |
80 int expected_result; | 81 int expected_result; |
81 } test_data_from[] = { | 82 } test_data_from[] = { |
82 { Boolean::New(false).As<Value>(), false, 0 }, | 83 { Boolean::New(false).As<Value>(), false, 0 }, |
83 { Boolean::New(true).As<Value>(), false, 0 }, | 84 { Boolean::New(true).As<Value>(), false, 0 }, |
(...skipping 13 matching lines...) Expand all Loading... |
97 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data_from); ++i) { | 98 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data_from); ++i) { |
98 int32_t result = std::numeric_limits<int32_t>::min(); | 99 int32_t result = std::numeric_limits<int32_t>::min(); |
99 bool success = Converter<int32_t>::FromV8(test_data_from[i].input, &result); | 100 bool success = Converter<int32_t>::FromV8(test_data_from[i].input, &result); |
100 EXPECT_EQ(test_data_from[i].expect_sucess, success) << i; | 101 EXPECT_EQ(test_data_from[i].expect_sucess, success) << i; |
101 if (success) | 102 if (success) |
102 EXPECT_EQ(test_data_from[i].expected_result, result) << i; | 103 EXPECT_EQ(test_data_from[i].expected_result, result) << i; |
103 } | 104 } |
104 } | 105 } |
105 | 106 |
106 TEST_F(ConverterTest, Vector) { | 107 TEST_F(ConverterTest, Vector) { |
107 HandleScope handle_scope(isolate_); | 108 HandleScope handle_scope(instance_->isolate()); |
108 | 109 |
109 std::vector<int> expected; | 110 std::vector<int> expected; |
110 expected.push_back(-1); | 111 expected.push_back(-1); |
111 expected.push_back(0); | 112 expected.push_back(0); |
112 expected.push_back(1); | 113 expected.push_back(1); |
113 | 114 |
114 Handle<Array> js_array = Handle<Array>::Cast( | 115 Handle<Array> js_array = Handle<Array>::Cast( |
115 Converter<std::vector<int> >::ToV8(isolate_, expected)); | 116 Converter<std::vector<int> >::ToV8(instance_->isolate(), expected)); |
116 ASSERT_FALSE(js_array.IsEmpty()); | 117 ASSERT_FALSE(js_array.IsEmpty()); |
117 EXPECT_EQ(3u, js_array->Length()); | 118 EXPECT_EQ(3u, js_array->Length()); |
118 for (size_t i = 0; i < expected.size(); ++i) { | 119 for (size_t i = 0; i < expected.size(); ++i) { |
119 EXPECT_TRUE(Integer::New(expected[i])->StrictEquals( | 120 EXPECT_TRUE(Integer::New(expected[i])->StrictEquals( |
120 js_array->Get(static_cast<int>(i)))); | 121 js_array->Get(static_cast<int>(i)))); |
121 } | 122 } |
122 | 123 |
123 std::vector<int> actual; | 124 std::vector<int> actual; |
124 EXPECT_TRUE(Converter<std::vector<int> >::FromV8(js_array, &actual)); | 125 EXPECT_TRUE(Converter<std::vector<int> >::FromV8(js_array, &actual)); |
125 EXPECT_EQ(expected, actual); | 126 EXPECT_EQ(expected, actual); |
126 } | 127 } |
127 | 128 |
128 } // namespace gin | 129 } // namespace gin |
OLD | NEW |