| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/basictypes.h" |
| 5 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 6 #include "base/json_reader.h" | 7 #include "base/json_reader.h" |
| 7 #include "base/json_writer.h" | 8 #include "base/json_writer.h" |
| 8 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 9 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 10 #include "base/values.h" | 11 #include "base/values.h" |
| 11 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
| 12 #include "chrome/common/json_value_serializer.h" | 13 #include "chrome/common/json_value_serializer.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 delete root_expected; | 180 delete root_expected; |
| 180 } | 181 } |
| 181 | 182 |
| 182 namespace { | 183 namespace { |
| 183 | 184 |
| 184 void ValidateJsonList(const std::string& json) { | 185 void ValidateJsonList(const std::string& json) { |
| 185 Value* root = NULL; | 186 Value* root = NULL; |
| 186 ASSERT_TRUE(JSONReader::Read(json, &root, false)); | 187 ASSERT_TRUE(JSONReader::Read(json, &root, false)); |
| 187 ASSERT_TRUE(root && root->IsType(Value::TYPE_LIST)); | 188 ASSERT_TRUE(root && root->IsType(Value::TYPE_LIST)); |
| 188 ListValue* list = static_cast<ListValue*>(root); | 189 ListValue* list = static_cast<ListValue*>(root); |
| 189 ASSERT_EQ(1, list->GetSize()); | 190 ASSERT_EQ(1U, list->GetSize()); |
| 190 Value* elt = NULL; | 191 Value* elt = NULL; |
| 191 ASSERT_TRUE(list->Get(0, &elt)); | 192 ASSERT_TRUE(list->Get(0, &elt)); |
| 192 int value = 0; | 193 int value = 0; |
| 193 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); | 194 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); |
| 194 ASSERT_EQ(1, value); | 195 ASSERT_EQ(1, value); |
| 195 delete root; | 196 delete root; |
| 196 } | 197 } |
| 197 | 198 |
| 198 } // namespace | 199 } // namespace |
| 199 | 200 |
| 200 TEST(JSONValueSerializerTest, JSONReaderComments) { | 201 TEST(JSONValueSerializerTest, JSONReaderComments) { |
| 201 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); | 202 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); |
| 202 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); | 203 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); |
| 203 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); | 204 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); |
| 204 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); | 205 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); |
| 205 ValidateJsonList("[ 1 /* one */ ] /* end */"); | 206 ValidateJsonList("[ 1 /* one */ ] /* end */"); |
| 206 ValidateJsonList("[ 1 //// ,2\r\n ]"); | 207 ValidateJsonList("[ 1 //// ,2\r\n ]"); |
| 207 | 208 |
| 208 Value* root = NULL; | 209 Value* root = NULL; |
| 209 // It's ok to have a comment in a string. | 210 // It's ok to have a comment in a string. |
| 210 ASSERT_TRUE(JSONReader::Read("[\"// ok\\n /* foo */ \"]", &root, false)); | 211 ASSERT_TRUE(JSONReader::Read("[\"// ok\\n /* foo */ \"]", &root, false)); |
| 211 ASSERT_TRUE(root && root->IsType(Value::TYPE_LIST)); | 212 ASSERT_TRUE(root && root->IsType(Value::TYPE_LIST)); |
| 212 ListValue* list = static_cast<ListValue*>(root); | 213 ListValue* list = static_cast<ListValue*>(root); |
| 213 ASSERT_EQ(1, list->GetSize()); | 214 ASSERT_EQ(1U, list->GetSize()); |
| 214 Value* elt = NULL; | 215 Value* elt = NULL; |
| 215 ASSERT_TRUE(list->Get(0, &elt)); | 216 ASSERT_TRUE(list->Get(0, &elt)); |
| 216 std::wstring value; | 217 std::wstring value; |
| 217 ASSERT_TRUE(elt && elt->GetAsString(&value)); | 218 ASSERT_TRUE(elt && elt->GetAsString(&value)); |
| 218 ASSERT_EQ(L"// ok\n /* foo */ ", value); | 219 ASSERT_EQ(L"// ok\n /* foo */ ", value); |
| 219 delete root; | 220 delete root; |
| 220 | 221 |
| 221 root = NULL; | 222 root = NULL; |
| 222 // You can't nest comments. | 223 // You can't nest comments. |
| 223 ASSERT_FALSE(JSONReader::Read("/* /* inner */ outer */ [ 1 ]", &root, false)); | 224 ASSERT_FALSE(JSONReader::Read("/* /* inner */ outer */ [ 1 ]", &root, false)); |
| 224 | 225 |
| 225 // Not a open comment token. | 226 // Not a open comment token. |
| 226 ASSERT_FALSE(JSONReader::Read("/ * * / [1]", &root, false)); | 227 ASSERT_FALSE(JSONReader::Read("/ * * / [1]", &root, false)); |
| 227 } | 228 } |
| 228 | 229 |
| 229 namespace { | 230 namespace { |
| 230 class JSONFileValueSerializerTest : public testing::Test { | 231 class JSONFileValueSerializerTest : public testing::Test { |
| 231 protected: | 232 protected: |
| 232 virtual void SetUp() { | 233 virtual void SetUp() { |
| 233 // Name a subdirectory of the temp directory. | 234 // Name a subdirectory of the temp directory. |
| 234 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); | 235 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); |
| 235 file_util::AppendToPath(&test_dir_, L"JSONFileValueSerializerTest"); | 236 file_util::AppendToPath(&test_dir_, L"JSONFileValueSerializerTest"); |
| 236 | 237 |
| 237 // Create a fresh, empty copy of this directory. | 238 // Create a fresh, empty copy of this directory. |
| 238 file_util::Delete(test_dir_, true); | 239 file_util::Delete(test_dir_, true); |
| 239 CreateDirectory(test_dir_.c_str(), NULL); | 240 file_util::CreateDirectory(test_dir_); |
| 240 } | 241 } |
| 241 virtual void TearDown() { | 242 virtual void TearDown() { |
| 242 // Clean up test directory | 243 // Clean up test directory |
| 243 ASSERT_TRUE(file_util::Delete(test_dir_, false)); | 244 ASSERT_TRUE(file_util::Delete(test_dir_, false)); |
| 244 ASSERT_FALSE(file_util::PathExists(test_dir_)); | 245 ASSERT_FALSE(file_util::PathExists(test_dir_)); |
| 245 } | 246 } |
| 246 | 247 |
| 247 // the path to temporary directory used to contain the test operations | 248 // the path to temporary directory used to contain the test operations |
| 248 std::wstring test_dir_; | 249 std::wstring test_dir_; |
| 249 }; | 250 }; |
| 250 } // anonymous namespace | 251 } // anonymous namespace |
| 251 | 252 |
| 253 // TODO(port): Enable these when PathService::Get with DIR_TEST_DATA is ported. |
| 254 #if defined(OS_WIN) |
| 252 TEST_F(JSONFileValueSerializerTest, Roundtrip) { | 255 TEST_F(JSONFileValueSerializerTest, Roundtrip) { |
| 253 std::wstring original_file_path; | 256 std::wstring original_file_path; |
| 254 ASSERT_TRUE( | 257 ASSERT_TRUE( |
| 255 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); | 258 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); |
| 256 file_util::AppendToPath(&original_file_path, L"serializer_test.js"); | 259 file_util::AppendToPath(&original_file_path, L"serializer_test.js"); |
| 257 | 260 |
| 258 ASSERT_TRUE(file_util::PathExists(original_file_path)); | 261 ASSERT_TRUE(file_util::PathExists(original_file_path)); |
| 259 | 262 |
| 260 JSONFileValueSerializer deserializer(original_file_path); | 263 JSONFileValueSerializer deserializer(original_file_path); |
| 261 Value* root; | 264 Value* root; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); | 335 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); |
| 333 file_util::AppendToPath(&source_file_path, | 336 file_util::AppendToPath(&source_file_path, |
| 334 L"serializer_test_nowhitespace.js"); | 337 L"serializer_test_nowhitespace.js"); |
| 335 ASSERT_TRUE(file_util::PathExists(source_file_path)); | 338 ASSERT_TRUE(file_util::PathExists(source_file_path)); |
| 336 JSONFileValueSerializer serializer(source_file_path); | 339 JSONFileValueSerializer serializer(source_file_path); |
| 337 Value* root; | 340 Value* root; |
| 338 ASSERT_TRUE(serializer.Deserialize(&root)); | 341 ASSERT_TRUE(serializer.Deserialize(&root)); |
| 339 ASSERT_TRUE(root); | 342 ASSERT_TRUE(root); |
| 340 delete root; | 343 delete root; |
| 341 } | 344 } |
| 342 | 345 #endif // defined(OS_WIN) |
| OLD | NEW |