[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "gin/converter.h" |
| 6 | |
| 7 | #include <limits.h> |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 10 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
| 12 | #include "base/memory/scoped_ptr.h" |
[email protected] | f04b0e9 | 2013-11-22 14:20:55 | [diff] [blame] | 13 | #include "gin/public/isolate_holder.h" |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 14 | #include "gin/test/v8_test.h" |
| 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | #include "v8/include/v8.h" |
| 17 | |
ssid | 93b23d9 | 2015-05-07 12:11:07 | [diff] [blame] | 18 | namespace gin { |
| 19 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 20 | using v8::Array; |
| 21 | using v8::Boolean; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 22 | using v8::HandleScope; |
| 23 | using v8::Integer; |
tfarina | 777bfee | 2015-05-04 14:12:29 | [diff] [blame] | 24 | using v8::Local; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 25 | using v8::Null; |
| 26 | using v8::Number; |
| 27 | using v8::Object; |
| 28 | using v8::String; |
| 29 | using v8::Undefined; |
| 30 | using v8::Value; |
| 31 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 32 | typedef V8Test ConverterTest; |
| 33 | |
| 34 | TEST_F(ConverterTest, Bool) { |
[email protected] | 1b93c23 | 2013-11-19 19:25:12 | [diff] [blame] | 35 | HandleScope handle_scope(instance_->isolate()); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 36 | |
[email protected] | 1b93c23 | 2013-11-19 19:25:12 | [diff] [blame] | 37 | EXPECT_TRUE(Converter<bool>::ToV8(instance_->isolate(), true)->StrictEquals( |
[email protected] | 91cd4fe | 2013-11-28 09:31:58 | [diff] [blame] | 38 | Boolean::New(instance_->isolate(), true))); |
[email protected] | 1b93c23 | 2013-11-19 19:25:12 | [diff] [blame] | 39 | EXPECT_TRUE(Converter<bool>::ToV8(instance_->isolate(), false)->StrictEquals( |
[email protected] | 91cd4fe | 2013-11-28 09:31:58 | [diff] [blame] | 40 | Boolean::New(instance_->isolate(), false))); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 41 | |
| 42 | struct { |
tfarina | 777bfee | 2015-05-04 14:12:29 | [diff] [blame] | 43 | Local<Value> input; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 44 | bool expected; |
| 45 | } test_data[] = { |
[email protected] | 91cd4fe | 2013-11-28 09:31:58 | [diff] [blame] | 46 | { Boolean::New(instance_->isolate(), false).As<Value>(), false }, |
| 47 | { Boolean::New(instance_->isolate(), true).As<Value>(), true }, |
| 48 | { Number::New(instance_->isolate(), 0).As<Value>(), false }, |
| 49 | { Number::New(instance_->isolate(), 1).As<Value>(), true }, |
| 50 | { Number::New(instance_->isolate(), -1).As<Value>(), true }, |
| 51 | { Number::New(instance_->isolate(), 0.1).As<Value>(), true }, |
| 52 | { String::NewFromUtf8(instance_->isolate(), "").As<Value>(), false }, |
| 53 | { String::NewFromUtf8(instance_->isolate(), "foo").As<Value>(), true }, |
| 54 | { Object::New(instance_->isolate()).As<Value>(), true }, |
| 55 | { Null(instance_->isolate()).As<Value>(), false }, |
| 56 | { Undefined(instance_->isolate()).As<Value>(), false }, |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 57 | }; |
| 58 | |
viettrungluu | 2560ad88 | 2014-10-17 01:20:48 | [diff] [blame] | 59 | for (size_t i = 0; i < arraysize(test_data); ++i) { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 60 | bool result = false; |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 61 | EXPECT_TRUE(Converter<bool>::FromV8(instance_->isolate(), |
| 62 | test_data[i].input, &result)); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 63 | EXPECT_EQ(test_data[i].expected, result); |
| 64 | |
| 65 | result = true; |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 66 | EXPECT_TRUE(Converter<bool>::FromV8(instance_->isolate(), |
| 67 | test_data[i].input, &result)); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 68 | EXPECT_EQ(test_data[i].expected, result); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | TEST_F(ConverterTest, Int32) { |
[email protected] | 1b93c23 | 2013-11-19 19:25:12 | [diff] [blame] | 73 | HandleScope handle_scope(instance_->isolate()); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 74 | |
| 75 | int test_data_to[] = {-1, 0, 1}; |
viettrungluu | 2560ad88 | 2014-10-17 01:20:48 | [diff] [blame] | 76 | for (size_t i = 0; i < arraysize(test_data_to); ++i) { |
[email protected] | add8fb17 | 2014-01-03 15:13:49 | [diff] [blame] | 77 | EXPECT_TRUE(Converter<int32_t>::ToV8(instance_->isolate(), test_data_to[i]) |
| 78 | ->StrictEquals( |
| 79 | Integer::New(instance_->isolate(), test_data_to[i]))); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | struct { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 83 | v8::Local<v8::Value> input; |
thestig | 259626c | 2015-11-23 20:18:11 | [diff] [blame] | 84 | bool expect_success; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 85 | int expected_result; |
| 86 | } test_data_from[] = { |
[email protected] | 91cd4fe | 2013-11-28 09:31:58 | [diff] [blame] | 87 | { Boolean::New(instance_->isolate(), false).As<Value>(), false, 0 }, |
| 88 | { Boolean::New(instance_->isolate(), true).As<Value>(), false, 0 }, |
| 89 | { Integer::New(instance_->isolate(), -1).As<Value>(), true, -1 }, |
| 90 | { Integer::New(instance_->isolate(), 0).As<Value>(), true, 0 }, |
| 91 | { Integer::New(instance_->isolate(), 1).As<Value>(), true, 1 }, |
| 92 | { Number::New(instance_->isolate(), -1).As<Value>(), true, -1 }, |
[email protected] | f53eba12 | 2014-04-16 11:30:01 | [diff] [blame] | 93 | { Number::New(instance_->isolate(), 1.1).As<Value>(), false, 0 }, |
[email protected] | 91cd4fe | 2013-11-28 09:31:58 | [diff] [blame] | 94 | { String::NewFromUtf8(instance_->isolate(), "42").As<Value>(), false, 0 }, |
| 95 | { String::NewFromUtf8(instance_->isolate(), "foo").As<Value>(), false, 0 }, |
| 96 | { Object::New(instance_->isolate()).As<Value>(), false, 0 }, |
| 97 | { Array::New(instance_->isolate()).As<Value>(), false, 0 }, |
| 98 | { v8::Null(instance_->isolate()).As<Value>(), false, 0 }, |
| 99 | { v8::Undefined(instance_->isolate()).As<Value>(), false, 0 }, |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 100 | }; |
| 101 | |
viettrungluu | 2560ad88 | 2014-10-17 01:20:48 | [diff] [blame] | 102 | for (size_t i = 0; i < arraysize(test_data_from); ++i) { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 103 | int32_t result = std::numeric_limits<int32_t>::min(); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 104 | bool success = Converter<int32_t>::FromV8(instance_->isolate(), |
| 105 | test_data_from[i].input, &result); |
thestig | 259626c | 2015-11-23 20:18:11 | [diff] [blame] | 106 | EXPECT_EQ(test_data_from[i].expect_success, success) << i; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 107 | if (success) |
| 108 | EXPECT_EQ(test_data_from[i].expected_result, result) << i; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | TEST_F(ConverterTest, Vector) { |
[email protected] | 1b93c23 | 2013-11-19 19:25:12 | [diff] [blame] | 113 | HandleScope handle_scope(instance_->isolate()); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 114 | |
| 115 | std::vector<int> expected; |
| 116 | expected.push_back(-1); |
| 117 | expected.push_back(0); |
| 118 | expected.push_back(1); |
| 119 | |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 120 | auto maybe = Converter<std::vector<int>>::ToV8( |
| 121 | instance_->isolate()->GetCurrentContext(), expected); |
| 122 | Local<Value> js_value; |
| 123 | EXPECT_TRUE(maybe.ToLocal(&js_value)); |
| 124 | Local<Array> js_array2 = Local<Array>::Cast(js_value); |
| 125 | EXPECT_EQ(3u, js_array2->Length()); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 126 | for (size_t i = 0; i < expected.size(); ++i) { |
[email protected] | 91cd4fe | 2013-11-28 09:31:58 | [diff] [blame] | 127 | EXPECT_TRUE(Integer::New(instance_->isolate(), expected[i]) |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 128 | ->StrictEquals(js_array2->Get(static_cast<int>(i)))); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 129 | } |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | } // namespace gin |