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

Unified Diff: base/json/json_value_serializer_unittest.cc

Issue 12481028: base: Move the rest of JSONValueSerializer unit tests from c/common to base/json. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: change the extension to json Created 7 years, 9 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: 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
index c5bef813bb6c8ac92378a2b8e997d4ff42fea6e6..e510500c71f21c6bc91b89ebf93329a53b898ba8 100644
--- a/base/json/json_value_serializer_unittest.cc
+++ b/base/json/json_value_serializer_unittest.cc
@@ -11,6 +11,7 @@
#include "base/json/json_string_value_serializer.h"
#include "base/json/json_writer.h"
#include "base/memory/scoped_ptr.h"
+#include "base/path_service.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
@@ -369,6 +370,104 @@ TEST(JSONValueSerializerTest, JSONReaderComments) {
ASSERT_FALSE(root.get());
}
+class JSONFileValueSerializerTest : public testing::Test {
+ protected:
+ virtual void SetUp() OVERRIDE {
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
+ }
+
+ base::ScopedTempDir temp_dir_;
+};
+
+TEST_F(JSONFileValueSerializerTest, Roundtrip) {
+ base::FilePath original_file_path;
+ ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path));
+ original_file_path =
+ original_file_path.Append(FILE_PATH_LITERAL("serializer_test.json"));
+
+ ASSERT_TRUE(file_util::PathExists(original_file_path));
+
+ JSONFileValueSerializer deserializer(original_file_path);
+ scoped_ptr<Value> root;
+ root.reset(deserializer.Deserialize(NULL, NULL));
+
+ ASSERT_TRUE(root.get());
+ ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
+
+ DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
+
+ Value* null_value = NULL;
+ ASSERT_TRUE(root_dict->Get("null", &null_value));
+ ASSERT_TRUE(null_value);
+ ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL));
+
+ bool bool_value = false;
+ ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value));
+ ASSERT_TRUE(bool_value);
+
+ int int_value = 0;
+ ASSERT_TRUE(root_dict->GetInteger("int", &int_value));
+ ASSERT_EQ(42, int_value);
+
+ std::string string_value;
+ ASSERT_TRUE(root_dict->GetString("string", &string_value));
+ ASSERT_EQ("hello", string_value);
+
+ // Now try writing.
+ const base::FilePath written_file_path =
+ temp_dir_.path().Append(FILE_PATH_LITERAL("test_output.js"));
+
+ ASSERT_FALSE(file_util::PathExists(written_file_path));
+ JSONFileValueSerializer serializer(written_file_path);
+ ASSERT_TRUE(serializer.Serialize(*root));
+ ASSERT_TRUE(file_util::PathExists(written_file_path));
+
+ // Now compare file contents.
+ EXPECT_TRUE(file_util::TextContentsEqual(original_file_path,
+ written_file_path));
+ EXPECT_TRUE(file_util::Delete(written_file_path, false));
+}
+
+TEST_F(JSONFileValueSerializerTest, RoundtripNested) {
+ base::FilePath original_file_path;
+ ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path));
+ original_file_path = original_file_path.Append(
+ FILE_PATH_LITERAL("serializer_nested_test.json"));
+
+ ASSERT_TRUE(file_util::PathExists(original_file_path));
+
+ JSONFileValueSerializer deserializer(original_file_path);
+ scoped_ptr<Value> root;
+ root.reset(deserializer.Deserialize(NULL, NULL));
+ ASSERT_TRUE(root.get());
+
+ // Now try writing.
+ base::FilePath written_file_path = temp_dir_.path().Append(
+ FILE_PATH_LITERAL("test_output.json"));
+
+ ASSERT_FALSE(file_util::PathExists(written_file_path));
+ JSONFileValueSerializer serializer(written_file_path);
+ ASSERT_TRUE(serializer.Serialize(*root));
+ ASSERT_TRUE(file_util::PathExists(written_file_path));
+
+ // Now compare file contents.
+ EXPECT_TRUE(file_util::TextContentsEqual(original_file_path,
+ written_file_path));
+ EXPECT_TRUE(file_util::Delete(written_file_path, false));
+}
+
+TEST_F(JSONFileValueSerializerTest, NoWhitespace) {
+ base::FilePath source_file_path;
+ ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &source_file_path));
+ source_file_path = source_file_path.Append(
+ FILE_PATH_LITERAL("serializer_test_nowhitespace.json"));
+ ASSERT_TRUE(file_util::PathExists(source_file_path));
+ JSONFileValueSerializer serializer(source_file_path);
+ scoped_ptr<Value> root;
+ root.reset(serializer.Deserialize(NULL, NULL));
+ ASSERT_TRUE(root.get());
+}
+
} // namespace
} // namespace base

Powered by Google App Engine
This is Rietveld 408576698