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" |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 { Number::New(0.1).As<Value>(), true }, | 50 { Number::New(0.1).As<Value>(), true }, |
51 { String::New("").As<Value>(), false }, | 51 { String::New("").As<Value>(), false }, |
52 { String::New("foo").As<Value>(), true }, | 52 { String::New("foo").As<Value>(), true }, |
53 { Object::New().As<Value>(), true }, | 53 { Object::New().As<Value>(), true }, |
54 { Null().As<Value>(), false }, | 54 { Null().As<Value>(), false }, |
55 { Undefined().As<Value>(), false }, | 55 { Undefined().As<Value>(), false }, |
56 }; | 56 }; |
57 | 57 |
58 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | 58 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
59 bool result = false; | 59 bool result = false; |
60 EXPECT_TRUE(Converter<bool>::FromV8(test_data[i].input, &result)); | 60 EXPECT_TRUE(Converter<bool>::FromV8(instance_->isolate(), |
| 61 test_data[i].input, &result)); |
61 EXPECT_EQ(test_data[i].expected, result); | 62 EXPECT_EQ(test_data[i].expected, result); |
62 | 63 |
63 result = true; | 64 result = true; |
64 EXPECT_TRUE(Converter<bool>::FromV8(test_data[i].input, &result)); | 65 EXPECT_TRUE(Converter<bool>::FromV8(instance_->isolate(), |
| 66 test_data[i].input, &result)); |
65 EXPECT_EQ(test_data[i].expected, result); | 67 EXPECT_EQ(test_data[i].expected, result); |
66 } | 68 } |
67 } | 69 } |
68 | 70 |
69 TEST_F(ConverterTest, Int32) { | 71 TEST_F(ConverterTest, Int32) { |
70 HandleScope handle_scope(instance_->isolate()); | 72 HandleScope handle_scope(instance_->isolate()); |
71 | 73 |
72 int test_data_to[] = {-1, 0, 1}; | 74 int test_data_to[] = {-1, 0, 1}; |
73 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data_to); ++i) { | 75 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data_to); ++i) { |
74 EXPECT_TRUE(Converter<int32_t>::ToV8(instance_->isolate(), | 76 EXPECT_TRUE(Converter<int32_t>::ToV8(instance_->isolate(), |
(...skipping 15 matching lines...) Expand all Loading... |
90 { String::New("42").As<Value>(), false, 0 }, | 92 { String::New("42").As<Value>(), false, 0 }, |
91 { String::New("foo").As<Value>(), false, 0 }, | 93 { String::New("foo").As<Value>(), false, 0 }, |
92 { Object::New().As<Value>(), false, 0 }, | 94 { Object::New().As<Value>(), false, 0 }, |
93 { Array::New().As<Value>(), false, 0 }, | 95 { Array::New().As<Value>(), false, 0 }, |
94 { v8::Null().As<Value>(), false, 0 }, | 96 { v8::Null().As<Value>(), false, 0 }, |
95 { v8::Undefined().As<Value>(), false, 0 }, | 97 { v8::Undefined().As<Value>(), false, 0 }, |
96 }; | 98 }; |
97 | 99 |
98 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) { |
99 int32_t result = std::numeric_limits<int32_t>::min(); | 101 int32_t result = std::numeric_limits<int32_t>::min(); |
100 bool success = Converter<int32_t>::FromV8(test_data_from[i].input, &result); | 102 bool success = Converter<int32_t>::FromV8(instance_->isolate(), |
| 103 test_data_from[i].input, &result); |
101 EXPECT_EQ(test_data_from[i].expect_sucess, success) << i; | 104 EXPECT_EQ(test_data_from[i].expect_sucess, success) << i; |
102 if (success) | 105 if (success) |
103 EXPECT_EQ(test_data_from[i].expected_result, result) << i; | 106 EXPECT_EQ(test_data_from[i].expected_result, result) << i; |
104 } | 107 } |
105 } | 108 } |
106 | 109 |
107 TEST_F(ConverterTest, Vector) { | 110 TEST_F(ConverterTest, Vector) { |
108 HandleScope handle_scope(instance_->isolate()); | 111 HandleScope handle_scope(instance_->isolate()); |
109 | 112 |
110 std::vector<int> expected; | 113 std::vector<int> expected; |
111 expected.push_back(-1); | 114 expected.push_back(-1); |
112 expected.push_back(0); | 115 expected.push_back(0); |
113 expected.push_back(1); | 116 expected.push_back(1); |
114 | 117 |
115 Handle<Array> js_array = Handle<Array>::Cast( | 118 Handle<Array> js_array = Handle<Array>::Cast( |
116 Converter<std::vector<int> >::ToV8(instance_->isolate(), expected)); | 119 Converter<std::vector<int> >::ToV8(instance_->isolate(), expected)); |
117 ASSERT_FALSE(js_array.IsEmpty()); | 120 ASSERT_FALSE(js_array.IsEmpty()); |
118 EXPECT_EQ(3u, js_array->Length()); | 121 EXPECT_EQ(3u, js_array->Length()); |
119 for (size_t i = 0; i < expected.size(); ++i) { | 122 for (size_t i = 0; i < expected.size(); ++i) { |
120 EXPECT_TRUE(Integer::New(expected[i])->StrictEquals( | 123 EXPECT_TRUE(Integer::New(expected[i])->StrictEquals( |
121 js_array->Get(static_cast<int>(i)))); | 124 js_array->Get(static_cast<int>(i)))); |
122 } | 125 } |
123 | 126 |
124 std::vector<int> actual; | 127 std::vector<int> actual; |
125 EXPECT_TRUE(Converter<std::vector<int> >::FromV8(js_array, &actual)); | 128 EXPECT_TRUE(Converter<std::vector<int> >::FromV8(instance_->isolate(), |
| 129 js_array, &actual)); |
126 EXPECT_EQ(expected, actual); | 130 EXPECT_EQ(expected, actual); |
127 } | 131 } |
128 | 132 |
129 } // namespace gin | 133 } // namespace gin |
OLD | NEW |