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

Unified Diff: third_party/WebKit/Source/platform/JSONParserTest.cpp

Issue 2205933002: Add JSON parser to Source/platform (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Switch to unique_ptr version of JSONValue classes Created 4 years, 4 months 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/platform/JSONParserTest.cpp
diff --git a/third_party/WebKit/Source/platform/inspector_protocol/ParserTest.cpp b/third_party/WebKit/Source/platform/JSONParserTest.cpp
similarity index 70%
copy from third_party/WebKit/Source/platform/inspector_protocol/ParserTest.cpp
copy to third_party/WebKit/Source/platform/JSONParserTest.cpp
index 1a8f44289f2a7b3f22d517bdcf075f449dcbf31b..2664857c745a9deb636741b73fdec15669d9f450 100644
--- a/third_party/WebKit/Source/platform/inspector_protocol/ParserTest.cpp
+++ b/third_party/WebKit/Source/platform/JSONParserTest.cpp
@@ -2,27 +2,26 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "platform/inspector_protocol/Parser.h"
+#include "platform/JSONParser.h"
-#include "platform/inspector_protocol/String16.h"
-#include "platform/inspector_protocol/Values.h"
+#include "platform/JSONValues.h"
#include "testing/gtest/include/gtest/gtest.h"
+#include "wtf/text/StringBuilder.h"
namespace blink {
-namespace protocol {
-TEST(ParserTest, Reading)
+TEST(JSONParserTest, Reading)
{
- protocol::Value* tmpValue;
- std::unique_ptr<protocol::Value> root;
- std::unique_ptr<protocol::Value> root2;
- String16 strVal;
+ JSONValue* tmpValue;
+ std::unique_ptr<JSONValue> root;
+ std::unique_ptr<JSONValue> root2;
+ String strVal;
int intVal = 0;
// some whitespace checking
root = parseJSON(" null ");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeNull, root->type());
+ EXPECT_EQ(JSONValue::TypeNull, root->getType());
// Invalid JSON string
root = parseJSON("nu");
@@ -31,68 +30,68 @@ TEST(ParserTest, Reading)
// Simple bool
root = parseJSON("true ");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeBoolean, root->type());
+ EXPECT_EQ(JSONValue::TypeBoolean, root->getType());
// Embedded comment
root = parseJSON("40 /*/");
EXPECT_FALSE(root.get());
root = parseJSON("/* comment */null");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeNull, root->type());
+ EXPECT_EQ(JSONValue::TypeNull, root->getType());
root = parseJSON("40 /* comment */");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeInteger, root->type());
- EXPECT_TRUE(root->asInteger(&intVal));
+ EXPECT_EQ(JSONValue::TypeNumber, root->getType());
+ EXPECT_TRUE(root->asNumber(&intVal));
EXPECT_EQ(40, intVal);
root = parseJSON("/**/ 40 /* multi-line\n comment */ // more comment");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeInteger, root->type());
- EXPECT_TRUE(root->asInteger(&intVal));
+ EXPECT_EQ(JSONValue::TypeNumber, root->getType());
+ EXPECT_TRUE(root->asNumber(&intVal));
EXPECT_EQ(40, intVal);
root = parseJSON("true // comment");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeBoolean, root->type());
+ EXPECT_EQ(JSONValue::TypeBoolean, root->getType());
root = parseJSON("/* comment */\"sample string\"");
ASSERT_TRUE(root.get());
EXPECT_TRUE(root->asString(&strVal));
EXPECT_EQ("sample string", strVal);
root = parseJSON("[1, /* comment, 2 ] */ \n 3]");
ASSERT_TRUE(root.get());
- protocol::ListValue* list = ListValue::cast(root.get());
+ JSONArray* list = JSONArray::cast(root.get());
ASSERT_TRUE(list);
- EXPECT_EQ(2u, list->size());
- tmpValue = list->at(0);
+ EXPECT_EQ(2u, list->length());
+ tmpValue = list->get(0);
ASSERT_TRUE(tmpValue);
- EXPECT_TRUE(tmpValue->asInteger(&intVal));
+ EXPECT_TRUE(tmpValue->asNumber(&intVal));
EXPECT_EQ(1, intVal);
- tmpValue = list->at(1);
+ tmpValue = list->get(1);
ASSERT_TRUE(tmpValue);
- EXPECT_TRUE(tmpValue->asInteger(&intVal));
+ EXPECT_TRUE(tmpValue->asNumber(&intVal));
EXPECT_EQ(3, intVal);
root = parseJSON("[1, /*a*/2, 3]");
ASSERT_TRUE(root.get());
- list = ListValue::cast(root.get());
+ list = JSONArray::cast(root.get());
ASSERT_TRUE(list);
- EXPECT_EQ(3u, list->size());
+ EXPECT_EQ(3u, list->length());
root = parseJSON("/* comment **/42");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeInteger, root->type());
- EXPECT_TRUE(root->asInteger(&intVal));
+ EXPECT_EQ(JSONValue::TypeNumber, root->getType());
+ EXPECT_TRUE(root->asNumber(&intVal));
EXPECT_EQ(42, intVal);
root = parseJSON(
"/* comment **/\n"
"// */ 43\n"
"44");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeInteger, root->type());
- EXPECT_TRUE(root->asInteger(&intVal));
+ EXPECT_EQ(JSONValue::TypeNumber, root->getType());
+ EXPECT_TRUE(root->asNumber(&intVal));
EXPECT_EQ(44, intVal);
// Test number formats
root = parseJSON("43");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeInteger, root->type());
- EXPECT_TRUE(root->asInteger(&intVal));
+ EXPECT_EQ(JSONValue::TypeNumber, root->getType());
+ EXPECT_TRUE(root->asNumber(&intVal));
EXPECT_EQ(43, intVal);
// According to RFC4627, oct, hex, and leading zeros are invalid JSON.
@@ -107,9 +106,9 @@ TEST(ParserTest, Reading)
// clause).
root = parseJSON("0");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeInteger, root->type());
+ EXPECT_EQ(JSONValue::TypeNumber, root->getType());
intVal = 1;
- EXPECT_TRUE(root->asInteger(&intVal));
+ EXPECT_TRUE(root->asNumber(&intVal));
EXPECT_EQ(0, intVal);
// Numbers that overflow ints should succeed, being internally promoted to
@@ -117,58 +116,58 @@ TEST(ParserTest, Reading)
root = parseJSON("2147483648");
ASSERT_TRUE(root.get());
double doubleVal;
- EXPECT_EQ(Value::TypeDouble, root->type());
+ EXPECT_EQ(JSONValue::TypeNumber, root->getType());
doubleVal = 0.0;
- EXPECT_TRUE(root->asDouble(&doubleVal));
+ EXPECT_TRUE(root->asNumber(&doubleVal));
EXPECT_DOUBLE_EQ(2147483648.0, doubleVal);
root = parseJSON("-2147483649");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeDouble, root->type());
+ EXPECT_EQ(JSONValue::TypeNumber, root->getType());
doubleVal = 0.0;
- EXPECT_TRUE(root->asDouble(&doubleVal));
+ EXPECT_TRUE(root->asNumber(&doubleVal));
EXPECT_DOUBLE_EQ(-2147483649.0, doubleVal);
// Parse a double
root = parseJSON("43.1");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeDouble, root->type());
+ EXPECT_EQ(JSONValue::TypeNumber, root->getType());
doubleVal = 0.0;
- EXPECT_TRUE(root->asDouble(&doubleVal));
+ EXPECT_TRUE(root->asNumber(&doubleVal));
EXPECT_DOUBLE_EQ(43.1, doubleVal);
root = parseJSON("4.3e-1");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeDouble, root->type());
+ EXPECT_EQ(JSONValue::TypeNumber, root->getType());
doubleVal = 0.0;
- EXPECT_TRUE(root->asDouble(&doubleVal));
+ EXPECT_TRUE(root->asNumber(&doubleVal));
EXPECT_DOUBLE_EQ(.43, doubleVal);
root = parseJSON("2.1e0");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeDouble, root->type());
+ EXPECT_EQ(JSONValue::TypeNumber, root->getType());
doubleVal = 0.0;
- EXPECT_TRUE(root->asDouble(&doubleVal));
+ EXPECT_TRUE(root->asNumber(&doubleVal));
EXPECT_DOUBLE_EQ(2.1, doubleVal);
root = parseJSON("2.1e+0001");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeInteger, root->type());
+ EXPECT_EQ(JSONValue::TypeNumber, root->getType());
doubleVal = 0.0;
- EXPECT_TRUE(root->asDouble(&doubleVal));
+ EXPECT_TRUE(root->asNumber(&doubleVal));
EXPECT_DOUBLE_EQ(21.0, doubleVal);
root = parseJSON("0.01");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeDouble, root->type());
+ EXPECT_EQ(JSONValue::TypeNumber, root->getType());
doubleVal = 0.0;
- EXPECT_TRUE(root->asDouble(&doubleVal));
+ EXPECT_TRUE(root->asNumber(&doubleVal));
EXPECT_DOUBLE_EQ(0.01, doubleVal);
root = parseJSON("1.00");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeInteger, root->type());
+ EXPECT_EQ(JSONValue::TypeNumber, root->getType());
doubleVal = 0.0;
- EXPECT_TRUE(root->asDouble(&doubleVal));
+ EXPECT_TRUE(root->asNumber(&doubleVal));
EXPECT_DOUBLE_EQ(1.0, doubleVal);
// Fractional parts must have a digit before and after the decimal point.
@@ -206,21 +205,21 @@ TEST(ParserTest, Reading)
// Test string parser
root = parseJSON("\"hello world\"");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeString, root->type());
+ EXPECT_EQ(JSONValue::TypeString, root->getType());
EXPECT_TRUE(root->asString(&strVal));
EXPECT_EQ("hello world", strVal);
// Empty string
root = parseJSON("\"\"");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeString, root->type());
+ EXPECT_EQ(JSONValue::TypeString, root->getType());
EXPECT_TRUE(root->asString(&strVal));
EXPECT_EQ("", strVal);
// Test basic string escapes
root = parseJSON("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\"");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeString, root->type());
+ EXPECT_EQ(JSONValue::TypeString, root->getType());
EXPECT_TRUE(root->asString(&strVal));
EXPECT_EQ(" \"\\/\b\f\n\r\t\v", strVal);
@@ -241,26 +240,26 @@ TEST(ParserTest, Reading)
// Basic array
root = parseJSON("[true, false, null]");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeArray, root->type());
- list = ListValue::cast(root.get());
+ EXPECT_EQ(JSONValue::TypeArray, root->getType());
+ list = JSONArray::cast(root.get());
ASSERT_TRUE(list);
- EXPECT_EQ(3U, list->size());
+ EXPECT_EQ(3U, list->length());
// Empty array
root = parseJSON("[]");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeArray, root->type());
- list = ListValue::cast(root.get());
+ EXPECT_EQ(JSONValue::TypeArray, root->getType());
+ list = JSONArray::cast(root.get());
ASSERT_TRUE(list);
- EXPECT_EQ(0U, list->size());
+ EXPECT_EQ(0U, list->length());
// Nested arrays
root = parseJSON("[[true], [], [false, [], [null]], null]");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeArray, root->type());
- list = ListValue::cast(root.get());
+ EXPECT_EQ(JSONValue::TypeArray, root->getType());
+ list = JSONArray::cast(root.get());
ASSERT_TRUE(list);
- EXPECT_EQ(4U, list->size());
+ EXPECT_EQ(4U, list->length());
// Invalid, missing close brace.
root = parseJSON("[[true], [], [false, [], [null]], null");
@@ -280,13 +279,13 @@ TEST(ParserTest, Reading)
root = parseJSON("[true]");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeArray, root->type());
- list = ListValue::cast(root.get());
+ EXPECT_EQ(JSONValue::TypeArray, root->getType());
+ list = JSONArray::cast(root.get());
ASSERT_TRUE(list);
- EXPECT_EQ(1U, list->size());
- tmpValue = list->at(0);
+ EXPECT_EQ(1U, list->length());
+ tmpValue = list->get(0);
ASSERT_TRUE(tmpValue);
- EXPECT_EQ(Value::TypeBoolean, tmpValue->type());
+ EXPECT_EQ(JSONValue::TypeBoolean, tmpValue->getType());
bool boolValue = false;
EXPECT_TRUE(tmpValue->asBoolean(&boolValue));
EXPECT_TRUE(boolValue);
@@ -304,19 +303,19 @@ TEST(ParserTest, Reading)
// Test objects
root = parseJSON("{}");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeObject, root->type());
+ EXPECT_EQ(JSONValue::TypeObject, root->getType());
root = parseJSON("{\"number\":9.87654321, \"null\":null , \"S\" : \"str\" }");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeObject, root->type());
- protocol::DictionaryValue* objectVal = DictionaryValue::cast(root.get());
+ EXPECT_EQ(JSONValue::TypeObject, root->getType());
+ JSONObject* objectVal = JSONObject::cast(root.get());
ASSERT_TRUE(objectVal);
doubleVal = 0.0;
- EXPECT_TRUE(objectVal->getDouble("number", &doubleVal));
+ EXPECT_TRUE(objectVal->getNumber("number", &doubleVal));
EXPECT_DOUBLE_EQ(9.87654321, doubleVal);
- protocol::Value* nullVal = objectVal->get("null");
+ JSONValue* nullVal = objectVal->get("null");
ASSERT_TRUE(nullVal);
- EXPECT_EQ(Value::TypeNull, nullVal->type());
+ EXPECT_EQ(JSONValue::TypeNull, nullVal->getType());
EXPECT_TRUE(objectVal->getString("S", &strVal));
EXPECT_EQ("str", strVal);
@@ -342,14 +341,14 @@ TEST(ParserTest, Reading)
// Test nesting
root = parseJSON("{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeObject, root->type());
- objectVal = DictionaryValue::cast(root.get());
+ EXPECT_EQ(JSONValue::TypeObject, root->getType());
+ objectVal = JSONObject::cast(root.get());
ASSERT_TRUE(objectVal);
- protocol::DictionaryValue* innerObject = objectVal->getObject("inner");
+ JSONObject* innerObject = objectVal->getObject("inner");
ASSERT_TRUE(innerObject);
- protocol::ListValue* innerArray = innerObject->getArray("array");
+ JSONArray* innerArray = innerObject->getArray("array");
ASSERT_TRUE(innerArray);
- EXPECT_EQ(1U, innerArray->size());
+ EXPECT_EQ(1U, innerArray->length());
boolValue = true;
EXPECT_TRUE(objectVal->getBoolean("false", &boolValue));
EXPECT_FALSE(boolValue);
@@ -359,30 +358,30 @@ TEST(ParserTest, Reading)
// Test keys with periods
root = parseJSON("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeObject, root->type());
- objectVal = DictionaryValue::cast(root.get());
+ EXPECT_EQ(JSONValue::TypeObject, root->getType());
+ objectVal = JSONObject::cast(root.get());
ASSERT_TRUE(objectVal);
int integerValue = 0;
- EXPECT_TRUE(objectVal->getInteger("a.b", &integerValue));
+ EXPECT_TRUE(objectVal->getNumber("a.b", &integerValue));
EXPECT_EQ(3, integerValue);
- EXPECT_TRUE(objectVal->getInteger("c", &integerValue));
+ EXPECT_TRUE(objectVal->getNumber("c", &integerValue));
EXPECT_EQ(2, integerValue);
innerObject = objectVal->getObject("d.e.f");
ASSERT_TRUE(innerObject);
- EXPECT_EQ(1U, innerObject->size());
- EXPECT_TRUE(innerObject->getInteger("g.h.i.j", &integerValue));
+ EXPECT_EQ(1, innerObject->size());
+ EXPECT_TRUE(innerObject->getNumber("g.h.i.j", &integerValue));
EXPECT_EQ(1, integerValue);
root = parseJSON("{\"a\":{\"b\":2},\"a.b\":1}");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeObject, root->type());
- objectVal = DictionaryValue::cast(root.get());
+ EXPECT_EQ(JSONValue::TypeObject, root->getType());
+ objectVal = JSONObject::cast(root.get());
ASSERT_TRUE(objectVal);
innerObject = objectVal->getObject("a");
ASSERT_TRUE(innerObject);
- EXPECT_TRUE(innerObject->getInteger("b", &integerValue));
+ EXPECT_TRUE(innerObject->getNumber("b", &integerValue));
EXPECT_EQ(2, integerValue);
- EXPECT_TRUE(objectVal->getInteger("a.b", &integerValue));
+ EXPECT_TRUE(objectVal->getNumber("a.b", &integerValue));
EXPECT_EQ(1, integerValue);
// Invalid, no closing brace
@@ -416,7 +415,7 @@ TEST(ParserTest, Reading)
EXPECT_FALSE(root.get());
// Test stack overflow
- String16Builder evil;
+ StringBuilder evil;
evil.reserveCapacity(2000000);
for (int i = 0; i < 1000000; ++i)
evil.append('[');
@@ -426,7 +425,7 @@ TEST(ParserTest, Reading)
EXPECT_FALSE(root.get());
// A few thousand adjacent lists is fine.
- String16Builder notEvil;
+ StringBuilder notEvil;
notEvil.reserveCapacity(15010);
notEvil.append('[');
for (int i = 0; i < 5000; ++i)
@@ -434,10 +433,10 @@ TEST(ParserTest, Reading)
notEvil.append("[]]");
root = parseJSON(notEvil.toString());
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeArray, root->type());
- list = ListValue::cast(root.get());
+ EXPECT_EQ(JSONValue::TypeArray, root->getType());
+ list = JSONArray::cast(root.get());
ASSERT_TRUE(list);
- EXPECT_EQ(5001U, list->size());
+ EXPECT_EQ(5001U, list->length());
// Test utf8 encoded input
root = parseJSON("\"\\xe7\\xbd\\x91\\xe9\\xa1\\xb5\"");
@@ -446,21 +445,21 @@ TEST(ParserTest, Reading)
// Test utf16 encoded strings.
root = parseJSON("\"\\u20ac3,14\"");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeString, root->type());
+ EXPECT_EQ(JSONValue::TypeString, root->getType());
EXPECT_TRUE(root->asString(&strVal));
UChar tmp2[] = {0x20ac, 0x33, 0x2c, 0x31, 0x34};
- EXPECT_EQ(String16(tmp2, 5), strVal);
+ EXPECT_EQ(String(tmp2, WTF_ARRAY_LENGTH(tmp2)), strVal);
root = parseJSON("\"\\ud83d\\udca9\\ud83d\\udc6c\"");
ASSERT_TRUE(root.get());
- EXPECT_EQ(Value::TypeString, root->type());
+ EXPECT_EQ(JSONValue::TypeString, root->getType());
EXPECT_TRUE(root->asString(&strVal));
UChar tmp3[] = {0xd83d, 0xdca9, 0xd83d, 0xdc6c};
- EXPECT_EQ(String16(tmp3, 4), strVal);
+ EXPECT_EQ(String(tmp3, WTF_ARRAY_LENGTH(tmp3)), strVal);
// Test literal root objects.
root = parseJSON("null");
- EXPECT_EQ(Value::TypeNull, root->type());
+ EXPECT_EQ(JSONValue::TypeNull, root->getType());
root = parseJSON("true");
ASSERT_TRUE(root.get());
@@ -469,7 +468,7 @@ TEST(ParserTest, Reading)
root = parseJSON("10");
ASSERT_TRUE(root.get());
- EXPECT_TRUE(root->asInteger(&integerValue));
+ EXPECT_TRUE(root->asNumber(&integerValue));
EXPECT_EQ(10, integerValue);
root = parseJSON("\"root\"");
@@ -478,7 +477,7 @@ TEST(ParserTest, Reading)
EXPECT_EQ("root", strVal);
}
-TEST(ParserTest, InvalidSanity)
+TEST(JSONParserTest, InvalidSanity)
{
const char* const invalidJson[] = {
"/* test *",
@@ -494,11 +493,10 @@ TEST(ParserTest, InvalidSanity)
"//**/"
};
- for (size_t i = 0; i < 11; ++i) {
- std::unique_ptr<protocol::Value> result = parseJSON(invalidJson[i]);
+ for (size_t i = 0; i < WTF_ARRAY_LENGTH(invalidJson); ++i) {
+ std::unique_ptr<JSONValue> result = parseJSON(invalidJson[i]);
EXPECT_FALSE(result.get());
}
}
-} // namespace protocol
} // namespace blink
« no previous file with comments | « third_party/WebKit/Source/platform/JSONParser.cpp ('k') | third_party/WebKit/Source/platform/blink_platform.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698