summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/bindings/core/v8/v8_object_builder_test.cc
blob: e4cd8cc2844bfb97783f179455a7ed31bfcf6ad0 (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
// Copyright 2016 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 "third_party/blink/renderer/bindings/core/v8/v8_object_builder.h"

#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h"

namespace blink {

namespace {

TEST(V8ObjectBuilderTest, addNull) {
  V8TestingScope scope;
  ScriptState* script_state = scope.GetScriptState();
  V8ObjectBuilder builder(script_state);
  builder.AddNull("null_check");
  ScriptValue json_object = builder.GetScriptValue();
  EXPECT_TRUE(json_object.IsObject());

  String json_string = V8StringToWebCoreString<String>(
      v8::JSON::Stringify(scope.GetContext(),
                          json_object.V8Value().As<v8::Object>())
          .ToLocalChecked(),
      kDoNotExternalize);

  String expected = "{\"null_check\":null}";
  EXPECT_EQ(expected, json_string);
}

TEST(V8ObjectBuilderTest, addBoolean) {
  V8TestingScope scope;
  ScriptState* script_state = scope.GetScriptState();
  V8ObjectBuilder builder(script_state);
  builder.AddBoolean("b1", true);
  builder.AddBoolean("b2", false);
  ScriptValue json_object = builder.GetScriptValue();
  EXPECT_TRUE(json_object.IsObject());

  String json_string = V8StringToWebCoreString<String>(
      v8::JSON::Stringify(scope.GetContext(),
                          json_object.V8Value().As<v8::Object>())
          .ToLocalChecked(),
      kDoNotExternalize);

  String expected = "{\"b1\":true,\"b2\":false}";
  EXPECT_EQ(expected, json_string);
}

TEST(V8ObjectBuilderTest, addNumber) {
  V8TestingScope scope;
  ScriptState* script_state = scope.GetScriptState();
  V8ObjectBuilder builder(script_state);
  builder.AddNumber("n1", 123);
  builder.AddNumber("n2", 123.456);
  ScriptValue json_object = builder.GetScriptValue();
  EXPECT_TRUE(json_object.IsObject());

  String json_string = V8StringToWebCoreString<String>(
      v8::JSON::Stringify(scope.GetContext(),
                          json_object.V8Value().As<v8::Object>())
          .ToLocalChecked(),
      kDoNotExternalize);

  String expected = "{\"n1\":123,\"n2\":123.456}";
  EXPECT_EQ(expected, json_string);
}

TEST(V8ObjectBuilderTest, addString) {
  V8TestingScope scope;
  ScriptState* script_state = scope.GetScriptState();
  V8ObjectBuilder builder(script_state);

  WTF::String test1 = "test1";
  WTF::String test2;
  WTF::String test3 = "test3";
  WTF::String test4;

  builder.AddString("test1", test1);
  builder.AddString("test2", test2);
  builder.AddStringOrNull("test3", test3);
  builder.AddStringOrNull("test4", test4);
  ScriptValue json_object = builder.GetScriptValue();
  EXPECT_TRUE(json_object.IsObject());

  String json_string = V8StringToWebCoreString<String>(
      v8::JSON::Stringify(scope.GetContext(),
                          json_object.V8Value().As<v8::Object>())
          .ToLocalChecked(),
      kDoNotExternalize);

  String expected =
      "{\"test1\":\"test1\",\"test2\":\"\",\"test3\":\"test3\",\"test4\":"
      "null}";
  EXPECT_EQ(expected, json_string);
}

TEST(V8ObjectBuilderTest, add) {
  V8TestingScope scope;
  ScriptState* script_state = scope.GetScriptState();
  V8ObjectBuilder builder(script_state);
  V8ObjectBuilder result(script_state);
  builder.AddNumber("n1", 123);
  builder.AddNumber("n2", 123.456);
  result.Add("builder", builder);
  ScriptValue builder_json_object = builder.GetScriptValue();
  ScriptValue result_json_object = result.GetScriptValue();
  EXPECT_TRUE(builder_json_object.IsObject());
  EXPECT_TRUE(result_json_object.IsObject());

  String json_string = V8StringToWebCoreString<String>(
      v8::JSON::Stringify(scope.GetContext(),
                          result_json_object.V8Value().As<v8::Object>())
          .ToLocalChecked(),
      kDoNotExternalize);

  String expected = "{\"builder\":{\"n1\":123,\"n2\":123.456}}";
  EXPECT_EQ(expected, json_string);
}

}  // namespace

}  // namespace blink