OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "bindings/core/v8/V8ObjectBuilder.h" |
| 6 |
| 7 #include "bindings/core/v8/V8Binding.h" |
| 8 #include "bindings/core/v8/V8BindingForTesting.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace blink { |
| 12 |
| 13 namespace { |
| 14 |
| 15 TEST(V8ObjectBuilderTest, addString) { |
| 16 V8TestingScope scope; |
| 17 { |
| 18 ScriptState* scriptState = scope.getScriptState(); |
| 19 V8ObjectBuilder builder(scriptState); |
| 20 |
| 21 WTF::String test1 = "test1"; |
| 22 WTF::String test2; |
| 23 WTF::String test3 = "test3"; |
| 24 WTF::String test4; |
| 25 WTF::String test5 = "test5"; |
| 26 WTF::String test6; |
| 27 |
| 28 builder.addString("test1", test1); |
| 29 builder.addString("test2", test2); |
| 30 builder.addString("test3", test3, true); |
| 31 builder.addString("test4", test4, true); |
| 32 builder.addString("test5", test5, false); |
| 33 builder.addString("test6", test6, false); |
| 34 ScriptValue jsonObject = builder.scriptValue(); |
| 35 EXPECT_TRUE(jsonObject.isObject()); |
| 36 |
| 37 String jsonString = v8StringToWebCoreString<String>( |
| 38 v8::JSON::Stringify(scope.context(), |
| 39 jsonObject.v8Value().As<v8::Object>()) |
| 40 .ToLocalChecked(), |
| 41 DoNotExternalize); |
| 42 |
| 43 String expected = |
| 44 "{\"test1\":\"test1\",\"test2\":\"\",\"test3\":\"test3\",\"test4\":" |
| 45 "null,\"test5\":\"test5\",\"test6\":\"\"}"; |
| 46 EXPECT_EQ(expected, jsonString); |
| 47 } |
| 48 } |
| 49 |
| 50 TEST(V8ObjectBuilderTest, addBoolean) { |
| 51 V8TestingScope scope; |
| 52 { |
| 53 ScriptState* scriptState = scope.getScriptState(); |
| 54 V8ObjectBuilder builder(scriptState); |
| 55 builder.addBoolean("b1", true); |
| 56 builder.addBoolean("b2", false); |
| 57 ScriptValue jsonObject = builder.scriptValue(); |
| 58 EXPECT_TRUE(jsonObject.isObject()); |
| 59 |
| 60 String jsonString = v8StringToWebCoreString<String>( |
| 61 v8::JSON::Stringify(scope.context(), |
| 62 jsonObject.v8Value().As<v8::Object>()) |
| 63 .ToLocalChecked(), |
| 64 DoNotExternalize); |
| 65 |
| 66 String expected = "{\"b1\":true,\"b2\":false}"; |
| 67 EXPECT_EQ(expected, jsonString); |
| 68 } |
| 69 } |
| 70 |
| 71 TEST(V8ObjectBuilderTest, addNumber) { |
| 72 V8TestingScope scope; |
| 73 { |
| 74 ScriptState* scriptState = scope.getScriptState(); |
| 75 V8ObjectBuilder builder(scriptState); |
| 76 builder.addNumber("n1", 123); |
| 77 builder.addNumber("n2", 123.456); |
| 78 ScriptValue jsonObject = builder.scriptValue(); |
| 79 EXPECT_TRUE(jsonObject.isObject()); |
| 80 |
| 81 String jsonString = v8StringToWebCoreString<String>( |
| 82 v8::JSON::Stringify(scope.context(), |
| 83 jsonObject.v8Value().As<v8::Object>()) |
| 84 .ToLocalChecked(), |
| 85 DoNotExternalize); |
| 86 |
| 87 String expected = "{\"n1\":123,\"n2\":123.456}"; |
| 88 EXPECT_EQ(expected, jsonString); |
| 89 } |
| 90 } |
| 91 |
| 92 TEST(V8ObjectBuilderTest, addNull) { |
| 93 V8TestingScope scope; |
| 94 { |
| 95 ScriptState* scriptState = scope.getScriptState(); |
| 96 V8ObjectBuilder builder(scriptState); |
| 97 builder.addNull("null_check"); |
| 98 ScriptValue jsonObject = builder.scriptValue(); |
| 99 EXPECT_TRUE(jsonObject.isObject()); |
| 100 |
| 101 String jsonString = v8StringToWebCoreString<String>( |
| 102 v8::JSON::Stringify(scope.context(), |
| 103 jsonObject.v8Value().As<v8::Object>()) |
| 104 .ToLocalChecked(), |
| 105 DoNotExternalize); |
| 106 |
| 107 String expected = "{\"null_check\":null}"; |
| 108 EXPECT_EQ(expected, jsonString); |
| 109 } |
| 110 } |
| 111 |
| 112 TEST(V8ObjectBuilderTest, add) { |
| 113 V8TestingScope scope; |
| 114 { |
| 115 ScriptState* scriptState = scope.getScriptState(); |
| 116 V8ObjectBuilder builder(scriptState); |
| 117 V8ObjectBuilder result(scriptState); |
| 118 builder.addNumber("n1", 123); |
| 119 builder.addNumber("n2", 123.456); |
| 120 result.add("builder", builder); |
| 121 ScriptValue builderJsonObject = builder.scriptValue(); |
| 122 ScriptValue resultJsonObject = result.scriptValue(); |
| 123 EXPECT_TRUE(builderJsonObject.isObject()); |
| 124 EXPECT_TRUE(resultJsonObject.isObject()); |
| 125 |
| 126 String jsonString = v8StringToWebCoreString<String>( |
| 127 v8::JSON::Stringify(scope.context(), |
| 128 resultJsonObject.v8Value().As<v8::Object>()) |
| 129 .ToLocalChecked(), |
| 130 DoNotExternalize); |
| 131 |
| 132 String expected = "{\"builder\":{\"n1\":123,\"n2\":123.456}}"; |
| 133 EXPECT_EQ(expected, jsonString); |
| 134 } |
| 135 } |
| 136 |
| 137 } // namespace |
| 138 |
| 139 } // namespace blink |
OLD | NEW |