Chromium Code Reviews| 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) { | |
|
Yuki
2016/11/09 12:17:18
nit: I'd suggest to sort the test cases in the sam
| |
| 16 V8TestingScope scope; | |
| 17 { | |
|
Yuki
2016/11/09 12:17:18
nit: You don't need to create a new block.
Without
| |
| 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 | |
| 26 builder.addString("test1", test1); | |
| 27 builder.addString("test2", test2); | |
| 28 builder.addStringOrNull("test3", test3); | |
| 29 builder.addStringOrNull("test4", test4); | |
| 30 ScriptValue jsonObject = builder.scriptValue(); | |
| 31 EXPECT_TRUE(jsonObject.isObject()); | |
| 32 | |
| 33 String jsonString = v8StringToWebCoreString<String>( | |
| 34 v8::JSON::Stringify(scope.context(), | |
| 35 jsonObject.v8Value().As<v8::Object>()) | |
| 36 .ToLocalChecked(), | |
| 37 DoNotExternalize); | |
| 38 | |
| 39 String expected = | |
| 40 "{\"test1\":\"test1\",\"test2\":\"\",\"test3\":\"test3\",\"test4\":" | |
| 41 "null}"; | |
| 42 EXPECT_EQ(expected, jsonString); | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 TEST(V8ObjectBuilderTest, addBoolean) { | |
| 47 V8TestingScope scope; | |
| 48 { | |
| 49 ScriptState* scriptState = scope.getScriptState(); | |
| 50 V8ObjectBuilder builder(scriptState); | |
| 51 builder.addBoolean("b1", true); | |
| 52 builder.addBoolean("b2", false); | |
| 53 ScriptValue jsonObject = builder.scriptValue(); | |
| 54 EXPECT_TRUE(jsonObject.isObject()); | |
| 55 | |
| 56 String jsonString = v8StringToWebCoreString<String>( | |
| 57 v8::JSON::Stringify(scope.context(), | |
| 58 jsonObject.v8Value().As<v8::Object>()) | |
| 59 .ToLocalChecked(), | |
| 60 DoNotExternalize); | |
| 61 | |
| 62 String expected = "{\"b1\":true,\"b2\":false}"; | |
| 63 EXPECT_EQ(expected, jsonString); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 TEST(V8ObjectBuilderTest, addNumber) { | |
| 68 V8TestingScope scope; | |
| 69 { | |
| 70 ScriptState* scriptState = scope.getScriptState(); | |
| 71 V8ObjectBuilder builder(scriptState); | |
| 72 builder.addNumber("n1", 123); | |
| 73 builder.addNumber("n2", 123.456); | |
| 74 ScriptValue jsonObject = builder.scriptValue(); | |
| 75 EXPECT_TRUE(jsonObject.isObject()); | |
| 76 | |
| 77 String jsonString = v8StringToWebCoreString<String>( | |
| 78 v8::JSON::Stringify(scope.context(), | |
| 79 jsonObject.v8Value().As<v8::Object>()) | |
| 80 .ToLocalChecked(), | |
| 81 DoNotExternalize); | |
| 82 | |
| 83 String expected = "{\"n1\":123,\"n2\":123.456}"; | |
| 84 EXPECT_EQ(expected, jsonString); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 TEST(V8ObjectBuilderTest, addNull) { | |
| 89 V8TestingScope scope; | |
| 90 { | |
| 91 ScriptState* scriptState = scope.getScriptState(); | |
| 92 V8ObjectBuilder builder(scriptState); | |
| 93 builder.addNull("null_check"); | |
| 94 ScriptValue jsonObject = builder.scriptValue(); | |
| 95 EXPECT_TRUE(jsonObject.isObject()); | |
| 96 | |
| 97 String jsonString = v8StringToWebCoreString<String>( | |
| 98 v8::JSON::Stringify(scope.context(), | |
| 99 jsonObject.v8Value().As<v8::Object>()) | |
| 100 .ToLocalChecked(), | |
| 101 DoNotExternalize); | |
| 102 | |
| 103 String expected = "{\"null_check\":null}"; | |
| 104 EXPECT_EQ(expected, jsonString); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 TEST(V8ObjectBuilderTest, add) { | |
| 109 V8TestingScope scope; | |
| 110 { | |
| 111 ScriptState* scriptState = scope.getScriptState(); | |
| 112 V8ObjectBuilder builder(scriptState); | |
| 113 V8ObjectBuilder result(scriptState); | |
| 114 builder.addNumber("n1", 123); | |
| 115 builder.addNumber("n2", 123.456); | |
| 116 result.add("builder", builder); | |
| 117 ScriptValue builderJsonObject = builder.scriptValue(); | |
| 118 ScriptValue resultJsonObject = result.scriptValue(); | |
| 119 EXPECT_TRUE(builderJsonObject.isObject()); | |
| 120 EXPECT_TRUE(resultJsonObject.isObject()); | |
| 121 | |
| 122 String jsonString = v8StringToWebCoreString<String>( | |
| 123 v8::JSON::Stringify(scope.context(), | |
| 124 resultJsonObject.v8Value().As<v8::Object>()) | |
| 125 .ToLocalChecked(), | |
| 126 DoNotExternalize); | |
| 127 | |
| 128 String expected = "{\"builder\":{\"n1\":123,\"n2\":123.456}}"; | |
| 129 EXPECT_EQ(expected, jsonString); | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 } // namespace | |
| 134 | |
| 135 } // namespace blink | |
| OLD | NEW |