summaryrefslogtreecommitdiffstats
path: root/gin/converter_unittest.cc
blob: 791d7e66ec5bb05278b8425621c53cd6515253b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gin/converter.h"

#include <limits.h>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "gin/public/isolate_holder.h"
#include "gin/test/v8_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "v8/include/v8.h"

using v8::Array;
using v8::Boolean;
using v8::Handle;
using v8::HandleScope;
using v8::Integer;
using v8::Null;
using v8::Number;
using v8::Object;
using v8::String;
using v8::Undefined;
using v8::Value;

namespace gin {

typedef V8Test ConverterTest;

TEST_F(ConverterTest, Bool) {
  HandleScope handle_scope(instance_->isolate());

  EXPECT_TRUE(Converter<bool>::ToV8(instance_->isolate(), true)->StrictEquals(
      Boolean::New(instance_->isolate(), true)));
  EXPECT_TRUE(Converter<bool>::ToV8(instance_->isolate(), false)->StrictEquals(
      Boolean::New(instance_->isolate(), false)));

  struct {
    Handle<Value> input;
    bool expected;
  } test_data[] = {
    { Boolean::New(instance_->isolate(), false).As<Value>(), false },
    { Boolean::New(instance_->isolate(), true).As<Value>(), true },
    { Number::New(instance_->isolate(), 0).As<Value>(), false },
    { Number::New(instance_->isolate(), 1).As<Value>(), true },
    { Number::New(instance_->isolate(), -1).As<Value>(), true },
    { Number::New(instance_->isolate(), 0.1).As<Value>(), true },
    { String::NewFromUtf8(instance_->isolate(), "").As<Value>(), false },
    { String::NewFromUtf8(instance_->isolate(), "foo").As<Value>(), true },
    { Object::New(instance_->isolate()).As<Value>(), true },
    { Null(instance_->isolate()).As<Value>(), false },
    { Undefined(instance_->isolate()).As<Value>(), false },
  };

  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
    bool result = false;
    EXPECT_TRUE(Converter<bool>::FromV8(instance_->isolate(),
                                        test_data[i].input, &result));
    EXPECT_EQ(test_data[i].expected, result);

    result = true;
    EXPECT_TRUE(Converter<bool>::FromV8(instance_->isolate(),
                                        test_data[i].input, &result));
    EXPECT_EQ(test_data[i].expected, result);
  }
}

TEST_F(ConverterTest, Int32) {
  HandleScope handle_scope(instance_->isolate());

  int test_data_to[] = {-1, 0, 1};
  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data_to); ++i) {
    EXPECT_TRUE(Converter<int32_t>::ToV8(instance_->isolate(), test_data_to[i])
                    ->StrictEquals(
                          Integer::New(instance_->isolate(), test_data_to[i])));
  }

  struct {
    v8::Handle<v8::Value> input;
    bool expect_sucess;
    int expected_result;
  } test_data_from[] = {
    { Boolean::New(instance_->isolate(), false).As<Value>(), false, 0 },
    { Boolean::New(instance_->isolate(), true).As<Value>(), false, 0 },
    { Integer::New(instance_->isolate(), -1).As<Value>(), true, -1 },
    { Integer::New(instance_->isolate(), 0).As<Value>(), true, 0 },
    { Integer::New(instance_->isolate(), 1).As<Value>(), true, 1 },
    { Number::New(instance_->isolate(), -1).As<Value>(), true, -1 },
    { Number::New(instance_->isolate(), 1.1).As<Value>(), false, 0 },
    { String::NewFromUtf8(instance_->isolate(), "42").As<Value>(), false, 0 },
    { String::NewFromUtf8(instance_->isolate(), "foo").As<Value>(), false, 0 },
    { Object::New(instance_->isolate()).As<Value>(), false, 0 },
    { Array::New(instance_->isolate()).As<Value>(), false, 0 },
    { v8::Null(instance_->isolate()).As<Value>(), false, 0 },
    { v8::Undefined(instance_->isolate()).As<Value>(), false, 0 },
  };

  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data_from); ++i) {
    int32_t result = std::numeric_limits<int32_t>::min();
    bool success = Converter<int32_t>::FromV8(instance_->isolate(),
                                              test_data_from[i].input, &result);
    EXPECT_EQ(test_data_from[i].expect_sucess, success) << i;
    if (success)
      EXPECT_EQ(test_data_from[i].expected_result, result) << i;
  }
}

TEST_F(ConverterTest, Vector) {
  HandleScope handle_scope(instance_->isolate());

  std::vector<int> expected;
  expected.push_back(-1);
  expected.push_back(0);
  expected.push_back(1);

  Handle<Array> js_array = Handle<Array>::Cast(
      Converter<std::vector<int> >::ToV8(instance_->isolate(), expected));
  ASSERT_FALSE(js_array.IsEmpty());
  EXPECT_EQ(3u, js_array->Length());
  for (size_t i = 0; i < expected.size(); ++i) {
    EXPECT_TRUE(Integer::New(instance_->isolate(), expected[i])
                    ->StrictEquals(js_array->Get(static_cast<int>(i))));
  }

  std::vector<int> actual;
  EXPECT_TRUE(Converter<std::vector<int> >::FromV8(instance_->isolate(),
                                                   js_array, &actual));
  EXPECT_EQ(expected, actual);
}

}  // namespace gin