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

Unified Diff: base/json/json_value_serializer_unittest.cc

Issue 8423079: Allow trailing comma in JSON policy files. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added unit testing for the whole JSONValueSerializer suite. Created 9 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
« no previous file with comments | « base/json/json_value_serializer.cc ('k') | chrome/browser/policy/config_dir_policy_provider.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/json/json_value_serializer_unittest.cc
diff --git a/base/json/json_value_serializer_unittest.cc b/base/json/json_value_serializer_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..7a33fa1a06419ca14c41cc500e06d2bea818bb89
--- /dev/null
+++ b/base/json/json_value_serializer_unittest.cc
@@ -0,0 +1,151 @@
+// Copyright (c) 2011 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 <string>
+
+#include "testing/gtest/include/gtest/gtest.h"
willchan no longer on Chromium 2011/11/15 02:36:22 This one should get sorted among the rest below.
pastarmovj 2011/11/17 10:35:35 Done.
+
+#include "base/file_util.h"
+#include "base/json/json_reader.h"
+#include "base/json/json_value_serializer.h"
willchan no longer on Chromium 2011/11/15 02:36:22 This one is supposed to go first, even ahead of th
pastarmovj 2011/11/17 10:35:35 Done.
+#include "base/memory/scoped_ptr.h"
+#include "base/scoped_temp_dir.h"
+#include "base/values.h"
+
+namespace {
+
+// Some proper JSON to test with:
+std::string proper_json =
willchan no longer on Chromium 2011/11/15 02:36:22 This is creating a static intiializer. Please use
pastarmovj 2011/11/17 10:35:35 Done.
+ "{\n"
+ " \"compound\": {\n"
+ " \"a\": 1,\n"
+ " \"b\": 2\n"
+ " },\n"
+ " \"some_String\": \"1337\",\n"
+ " \"some_int\": 42,\n"
+ " \"the_list\": [ \"val1\", \"val2\" ]\n"
+ "}\n";
+
+// Some proper JSON with trailing commas:
+std::string proper_json_with_commas =
+ "{\n"
+ "\t\"some_int\": 42,\n"
+ "\t\"some_String\": \"1337\",\n"
+ "\t\"the_list\": [\"val1\", \"val2\", ],\n"
+ "\t\"compound\": { \"a\": 1, \"b\": 2, },\n"
+ "}\n";
+
+// Some broken JSON:
+std::string broken_json =
+ "{\n"
+ "\t\"some_int\": a42,\n"
+ "\tsome_String: \"1337\",\n"
+ "\t\"the_list: [\"val1\", \"val2\", ],\n"
+ "\t\"compound\": { \"a\" 1, \"b\": 2, },\n"
+ "},\n";
+
+void CheckJSONIsStillTheSame(base::Value& value) {
+ // Serialize back the output.
+ std::string serialized_json;
+ JSONStringValueSerializer str_serializer(&serialized_json);
+ str_serializer.set_pretty_print(true);
+ ASSERT_TRUE(str_serializer.Serialize(value));
+ // Now compare the input with the output.
+ ASSERT_EQ(proper_json, serialized_json);
+}
+
+} // namespace
+
+namespace base {
willchan no longer on Chromium 2011/11/15 02:36:22 You should probably just put everything within the
pastarmovj 2011/11/17 10:35:35 Done.
+
+TEST(JSONValueSerializerTest, ReadProperJSONFromString) {
+ // Try to deserialize it through the serializer.
+ JSONStringValueSerializer str_deserializer(proper_json);
+
+ int error_code = 0;
+ std::string error_message;
+ scoped_ptr<Value> value(
+ str_deserializer.Deserialize(&error_code, &error_message));
+ ASSERT_TRUE(value.get());
+ ASSERT_EQ(0, error_code);
+ ASSERT_TRUE(error_message.empty());
+ // Verify if the same JSON is still there.
+ CheckJSONIsStillTheSame(*value);
+}
+
+TEST(JSONValueSerializerTest, ReadJSONWithTrailingCommasFromString) {
+ // Try to deserialize it through the serializer.
+ JSONStringValueSerializer str_deserializer(proper_json_with_commas);
+
+ int error_code = 0;
+ std::string error_message;
+ scoped_ptr<Value> value(
+ str_deserializer.Deserialize(&error_code, &error_message));
+ ASSERT_FALSE(value.get());
+ ASSERT_NE(0, error_code);
+ ASSERT_FALSE(error_message.empty());
+ // Now the flag is set and it must pass.
+ str_deserializer.set_allow_trailing_comma(true);
+ value.reset(str_deserializer.Deserialize(&error_code, &error_message));
+ ASSERT_TRUE(value.get());
+ ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
+ // Verify if the same JSON is still there.
+ CheckJSONIsStillTheSame(*value);
+}
+
+TEST(JSONValueSerializerTest, ReadProperJSONFromFile) {
+ ScopedTempDir tempdir;
+ ASSERT_TRUE(tempdir.CreateUniqueTempDir());
+ // Write it down in the file.
+ FilePath temp_file(tempdir.path().AppendASCII("test.json"));
+ ASSERT_EQ(static_cast<int>(proper_json.length()),
+ file_util::WriteFile(temp_file,
+ proper_json.data(),
+ proper_json.length()));
+
+ // Try to deserialize it through the serializer.
+ JSONFileValueSerializer file_deserializer(temp_file);
+
+ int error_code = 0;
+ std::string error_message;
+ scoped_ptr<Value> value(
+ file_deserializer.Deserialize(&error_code, &error_message));
+ ASSERT_TRUE(value.get());
+ ASSERT_EQ(0, error_code);
+ ASSERT_TRUE(error_message.empty());
+ // Verify if the same JSON is still there.
+ CheckJSONIsStillTheSame(*value);
+}
+
+TEST(JSONValueSerializerTest, ReadJSONWithCommasFromFile) {
+ ScopedTempDir tempdir;
+ ASSERT_TRUE(tempdir.CreateUniqueTempDir());
+ // Write it down in the file.
+ FilePath temp_file(tempdir.path().AppendASCII("test.json"));
+ ASSERT_EQ(static_cast<int>(proper_json_with_commas.length()),
+ file_util::WriteFile(temp_file,
+ proper_json_with_commas.data(),
+ proper_json_with_commas.length()));
+
+ // Try to deserialize it through the serializer.
+ JSONFileValueSerializer file_deserializer(temp_file);
+ // This must fail without the proper flag.
+ int error_code = 0;
+ std::string error_message;
+ scoped_ptr<Value> value(
+ file_deserializer.Deserialize(&error_code, &error_message));
+ ASSERT_FALSE(value.get());
+ ASSERT_NE(0, error_code);
+ ASSERT_FALSE(error_message.empty());
+ // Now the flag is set and it must pass.
+ file_deserializer.set_allow_trailing_comma(true);
+ value.reset(file_deserializer.Deserialize(&error_code, &error_message));
+ ASSERT_TRUE(value.get());
+ ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
+ // Verify if the same JSON is still there.
+ CheckJSONIsStillTheSame(*value);
+}
+
+} // namespace base
+
« no previous file with comments | « base/json/json_value_serializer.cc ('k') | chrome/browser/policy/config_dir_policy_provider.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698