blob: ec543c253d942ba44b4452e5f8dea55b79b6fa92 [file] [log] [blame]
[email protected]a22998a2013-11-10 05:00:501// 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>
avi90e658dd2015-12-21 07:16:198#include <stddef.h>
9#include <stdint.h>
[email protected]a22998a2013-11-10 05:00:5010
[email protected]a22998a2013-11-10 05:00:5011#include "base/compiler_specific.h"
12#include "base/memory/scoped_ptr.h"
[email protected]f04b0e92013-11-22 14:20:5513#include "gin/public/isolate_holder.h"
[email protected]a22998a2013-11-10 05:00:5014#include "gin/test/v8_test.h"
15#include "testing/gtest/include/gtest/gtest.h"
16#include "v8/include/v8.h"
17
ssid93b23d92015-05-07 12:11:0718namespace gin {
19
[email protected]a22998a2013-11-10 05:00:5020using v8::Array;
21using v8::Boolean;
[email protected]a22998a2013-11-10 05:00:5022using v8::HandleScope;
23using v8::Integer;
tfarina777bfee2015-05-04 14:12:2924using v8::Local;
[email protected]a22998a2013-11-10 05:00:5025using v8::Null;
26using v8::Number;
27using v8::Object;
28using v8::String;
29using v8::Undefined;
30using v8::Value;
31
[email protected]a22998a2013-11-10 05:00:5032typedef V8Test ConverterTest;
33
34TEST_F(ConverterTest, Bool) {
[email protected]1b93c232013-11-19 19:25:1235 HandleScope handle_scope(instance_->isolate());
[email protected]a22998a2013-11-10 05:00:5036
[email protected]1b93c232013-11-19 19:25:1237 EXPECT_TRUE(Converter<bool>::ToV8(instance_->isolate(), true)->StrictEquals(
[email protected]91cd4fe2013-11-28 09:31:5838 Boolean::New(instance_->isolate(), true)));
[email protected]1b93c232013-11-19 19:25:1239 EXPECT_TRUE(Converter<bool>::ToV8(instance_->isolate(), false)->StrictEquals(
[email protected]91cd4fe2013-11-28 09:31:5840 Boolean::New(instance_->isolate(), false)));
[email protected]a22998a2013-11-10 05:00:5041
42 struct {
tfarina777bfee2015-05-04 14:12:2943 Local<Value> input;
[email protected]a22998a2013-11-10 05:00:5044 bool expected;
45 } test_data[] = {
[email protected]91cd4fe2013-11-28 09:31:5846 { 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]a22998a2013-11-10 05:00:5057 };
58
viettrungluu2560ad882014-10-17 01:20:4859 for (size_t i = 0; i < arraysize(test_data); ++i) {
[email protected]a22998a2013-11-10 05:00:5060 bool result = false;
[email protected]7618ebbb2013-11-27 03:38:2661 EXPECT_TRUE(Converter<bool>::FromV8(instance_->isolate(),
62 test_data[i].input, &result));
[email protected]a22998a2013-11-10 05:00:5063 EXPECT_EQ(test_data[i].expected, result);
64
65 result = true;
[email protected]7618ebbb2013-11-27 03:38:2666 EXPECT_TRUE(Converter<bool>::FromV8(instance_->isolate(),
67 test_data[i].input, &result));
[email protected]a22998a2013-11-10 05:00:5068 EXPECT_EQ(test_data[i].expected, result);
69 }
70}
71
72TEST_F(ConverterTest, Int32) {
[email protected]1b93c232013-11-19 19:25:1273 HandleScope handle_scope(instance_->isolate());
[email protected]a22998a2013-11-10 05:00:5074
75 int test_data_to[] = {-1, 0, 1};
viettrungluu2560ad882014-10-17 01:20:4876 for (size_t i = 0; i < arraysize(test_data_to); ++i) {
[email protected]add8fb172014-01-03 15:13:4977 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]a22998a2013-11-10 05:00:5080 }
81
82 struct {
deepak.sfaaa1b62015-04-30 07:30:4883 v8::Local<v8::Value> input;
thestig259626c2015-11-23 20:18:1184 bool expect_success;
[email protected]a22998a2013-11-10 05:00:5085 int expected_result;
86 } test_data_from[] = {
[email protected]91cd4fe2013-11-28 09:31:5887 { 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]f53eba122014-04-16 11:30:0193 { Number::New(instance_->isolate(), 1.1).As<Value>(), false, 0 },
[email protected]91cd4fe2013-11-28 09:31:5894 { 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]a22998a2013-11-10 05:00:50100 };
101
viettrungluu2560ad882014-10-17 01:20:48102 for (size_t i = 0; i < arraysize(test_data_from); ++i) {
[email protected]a22998a2013-11-10 05:00:50103 int32_t result = std::numeric_limits<int32_t>::min();
[email protected]7618ebbb2013-11-27 03:38:26104 bool success = Converter<int32_t>::FromV8(instance_->isolate(),
105 test_data_from[i].input, &result);
thestig259626c2015-11-23 20:18:11106 EXPECT_EQ(test_data_from[i].expect_success, success) << i;
[email protected]a22998a2013-11-10 05:00:50107 if (success)
108 EXPECT_EQ(test_data_from[i].expected_result, result) << i;
109 }
110}
111
112TEST_F(ConverterTest, Vector) {
[email protected]1b93c232013-11-19 19:25:12113 HandleScope handle_scope(instance_->isolate());
[email protected]a22998a2013-11-10 05:00:50114
115 std::vector<int> expected;
116 expected.push_back(-1);
117 expected.push_back(0);
118 expected.push_back(1);
119
bashidbd2ef9bb2015-06-02 01:39:32120 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]a22998a2013-11-10 05:00:50126 for (size_t i = 0; i < expected.size(); ++i) {
[email protected]91cd4fe2013-11-28 09:31:58127 EXPECT_TRUE(Integer::New(instance_->isolate(), expected[i])
bashidbd2ef9bb2015-06-02 01:39:32128 ->StrictEquals(js_array2->Get(static_cast<int>(i))));
[email protected]a22998a2013-11-10 05:00:50129 }
[email protected]a22998a2013-11-10 05:00:50130}
131
132} // namespace gin