Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(468)

Unified Diff: third_party/WebKit/Source/bindings/core/v8/V8ObjectBuilderTest.cpp

Issue 2476393003: V8ObjectBuilder::addString support Nullable. (Closed)
Patch Set: Add V8ObjectBuilderTest Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/bindings/core/v8/V8ObjectBuilderTest.cpp
diff --git a/third_party/WebKit/Source/bindings/core/v8/V8ObjectBuilderTest.cpp b/third_party/WebKit/Source/bindings/core/v8/V8ObjectBuilderTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..247ddb2eabe0af0dc2395eeb5dc4052e2470b875
--- /dev/null
+++ b/third_party/WebKit/Source/bindings/core/v8/V8ObjectBuilderTest.cpp
@@ -0,0 +1,139 @@
+// 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 "bindings/core/v8/V8ObjectBuilder.h"
+
+#include "bindings/core/v8/V8Binding.h"
+#include "bindings/core/v8/V8BindingForTesting.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace blink {
+
+namespace {
+
+TEST(V8ObjectBuilderTest, addString) {
+ V8TestingScope scope;
+ {
+ ScriptState* scriptState = scope.getScriptState();
+ V8ObjectBuilder builder(scriptState);
+
+ WTF::String test1 = "test1";
+ WTF::String test2;
+ WTF::String test3 = "test3";
+ WTF::String test4;
+ WTF::String test5 = "test5";
+ WTF::String test6;
+
+ builder.addString("test1", test1);
+ builder.addString("test2", test2);
+ builder.addString("test3", test3, true);
+ builder.addString("test4", test4, true);
+ builder.addString("test5", test5, false);
+ builder.addString("test6", test6, false);
+ ScriptValue jsonObject = builder.scriptValue();
+ EXPECT_TRUE(jsonObject.isObject());
+
+ String jsonString = v8StringToWebCoreString<String>(
+ v8::JSON::Stringify(scope.context(),
+ jsonObject.v8Value().As<v8::Object>())
+ .ToLocalChecked(),
+ DoNotExternalize);
+
+ String expected =
+ "{\"test1\":\"test1\",\"test2\":\"\",\"test3\":\"test3\",\"test4\":"
+ "null,\"test5\":\"test5\",\"test6\":\"\"}";
+ EXPECT_EQ(expected, jsonString);
+ }
+}
+
+TEST(V8ObjectBuilderTest, addBoolean) {
+ V8TestingScope scope;
+ {
+ ScriptState* scriptState = scope.getScriptState();
+ V8ObjectBuilder builder(scriptState);
+ builder.addBoolean("b1", true);
+ builder.addBoolean("b2", false);
+ ScriptValue jsonObject = builder.scriptValue();
+ EXPECT_TRUE(jsonObject.isObject());
+
+ String jsonString = v8StringToWebCoreString<String>(
+ v8::JSON::Stringify(scope.context(),
+ jsonObject.v8Value().As<v8::Object>())
+ .ToLocalChecked(),
+ DoNotExternalize);
+
+ String expected = "{\"b1\":true,\"b2\":false}";
+ EXPECT_EQ(expected, jsonString);
+ }
+}
+
+TEST(V8ObjectBuilderTest, addNumber) {
+ V8TestingScope scope;
+ {
+ ScriptState* scriptState = scope.getScriptState();
+ V8ObjectBuilder builder(scriptState);
+ builder.addNumber("n1", 123);
+ builder.addNumber("n2", 123.456);
+ ScriptValue jsonObject = builder.scriptValue();
+ EXPECT_TRUE(jsonObject.isObject());
+
+ String jsonString = v8StringToWebCoreString<String>(
+ v8::JSON::Stringify(scope.context(),
+ jsonObject.v8Value().As<v8::Object>())
+ .ToLocalChecked(),
+ DoNotExternalize);
+
+ String expected = "{\"n1\":123,\"n2\":123.456}";
+ EXPECT_EQ(expected, jsonString);
+ }
+}
+
+TEST(V8ObjectBuilderTest, addNull) {
+ V8TestingScope scope;
+ {
+ ScriptState* scriptState = scope.getScriptState();
+ V8ObjectBuilder builder(scriptState);
+ builder.addNull("null_check");
+ ScriptValue jsonObject = builder.scriptValue();
+ EXPECT_TRUE(jsonObject.isObject());
+
+ String jsonString = v8StringToWebCoreString<String>(
+ v8::JSON::Stringify(scope.context(),
+ jsonObject.v8Value().As<v8::Object>())
+ .ToLocalChecked(),
+ DoNotExternalize);
+
+ String expected = "{\"null_check\":null}";
+ EXPECT_EQ(expected, jsonString);
+ }
+}
+
+TEST(V8ObjectBuilderTest, add) {
+ V8TestingScope scope;
+ {
+ ScriptState* scriptState = scope.getScriptState();
+ V8ObjectBuilder builder(scriptState);
+ V8ObjectBuilder result(scriptState);
+ builder.addNumber("n1", 123);
+ builder.addNumber("n2", 123.456);
+ result.add("builder", builder);
+ ScriptValue builderJsonObject = builder.scriptValue();
+ ScriptValue resultJsonObject = result.scriptValue();
+ EXPECT_TRUE(builderJsonObject.isObject());
+ EXPECT_TRUE(resultJsonObject.isObject());
+
+ String jsonString = v8StringToWebCoreString<String>(
+ v8::JSON::Stringify(scope.context(),
+ resultJsonObject.v8Value().As<v8::Object>())
+ .ToLocalChecked(),
+ DoNotExternalize);
+
+ String expected = "{\"builder\":{\"n1\":123,\"n2\":123.456}}";
+ EXPECT_EQ(expected, jsonString);
+ }
+}
+
+} // namespace
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698