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/basictypes.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
13 #include "chrome/common/json_value_serializer.h" | 13 #include "chrome/common/json_value_serializer.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 TEST(JSONValueSerializerTest, Roundtrip) { | 16 TEST(JSONValueSerializerTest, Roundtrip) { |
17 const std::string original_serialization = | 17 const std::string original_serialization = |
18 "{\"bool\":true,\"int\":42,\"list\":[1,2],\"null\":null,\"real\":3.14}"; | 18 "{\"bool\":true,\"int\":42,\"list\":[1,2],\"null\":null,\"real\":3.14}"; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 "\\u00E8\\u00E9\\u00EA\\u00EB\\u00EC\\u00ED\\u00EE\\u00EF\\u00F0\\u00F1" | 99 "\\u00E8\\u00E9\\u00EA\\u00EB\\u00EC\\u00ED\\u00EE\\u00EF\\u00F0\\u00F1" |
100 "\\u00F2\\u00F3\\u00F4\\u00F5\\u00F6\\u00F7\\u00F8\\u00F9\\u00FA\\u00FB" | 100 "\\u00F2\\u00F3\\u00F4\\u00F5\\u00F6\\u00F7\\u00F8\\u00F9\\u00FA\\u00FB" |
101 "\\u00FC\\u00FD\\u00FE\\u00FF"; | 101 "\\u00FC\\u00FD\\u00FE\\u00FF"; |
102 | 102 |
103 std::string expected_output = "{\"all_chars\":\"" + all_chars_expected + | 103 std::string expected_output = "{\"all_chars\":\"" + all_chars_expected + |
104 "\"}"; | 104 "\"}"; |
105 // Test JSONWriter interface | 105 // Test JSONWriter interface |
106 std::string output_js; | 106 std::string output_js; |
107 DictionaryValue valueRoot; | 107 DictionaryValue valueRoot; |
108 valueRoot.SetString(L"all_chars", all_chars); | 108 valueRoot.SetString(L"all_chars", all_chars); |
109 JSONWriter::Write(&valueRoot, false, &output_js); | 109 base::JSONWriter::Write(&valueRoot, false, &output_js); |
110 ASSERT_EQ(expected_output, output_js); | 110 ASSERT_EQ(expected_output, output_js); |
111 | 111 |
112 // Test JSONValueSerializer interface (uses JSONWriter). | 112 // Test JSONValueSerializer interface (uses JSONWriter). |
113 JSONStringValueSerializer serializer(&output_js); | 113 JSONStringValueSerializer serializer(&output_js); |
114 ASSERT_TRUE(serializer.Serialize(valueRoot)); | 114 ASSERT_TRUE(serializer.Serialize(valueRoot)); |
115 ASSERT_EQ(expected_output, output_js); | 115 ASSERT_EQ(expected_output, output_js); |
116 } | 116 } |
117 | 117 |
118 TEST(JSONValueSerializerTest, UnicodeStrings) { | 118 TEST(JSONValueSerializerTest, UnicodeStrings) { |
119 // unicode string json -> escaped ascii text | 119 // unicode string json -> escaped ascii text |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 root.reset(serializer.Deserialize(NULL)); | 184 root.reset(serializer.Deserialize(NULL)); |
185 ASSERT_TRUE(root.get()); | 185 ASSERT_TRUE(root.get()); |
186 root_expected.reset(serializer_expected.Deserialize(NULL)); | 186 root_expected.reset(serializer_expected.Deserialize(NULL)); |
187 ASSERT_TRUE(root_expected.get()); | 187 ASSERT_TRUE(root_expected.get()); |
188 ASSERT_TRUE(root->Equals(root_expected.get())); | 188 ASSERT_TRUE(root->Equals(root_expected.get())); |
189 } | 189 } |
190 | 190 |
191 namespace { | 191 namespace { |
192 | 192 |
193 void ValidateJsonList(const std::string& json) { | 193 void ValidateJsonList(const std::string& json) { |
194 scoped_ptr<Value> root(JSONReader::Read(json, false)); | 194 scoped_ptr<Value> root(base::JSONReader::Read(json, false)); |
195 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); | 195 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); |
196 ListValue* list = static_cast<ListValue*>(root.get()); | 196 ListValue* list = static_cast<ListValue*>(root.get()); |
197 ASSERT_EQ(1U, list->GetSize()); | 197 ASSERT_EQ(1U, list->GetSize()); |
198 Value* elt = NULL; | 198 Value* elt = NULL; |
199 ASSERT_TRUE(list->Get(0, &elt)); | 199 ASSERT_TRUE(list->Get(0, &elt)); |
200 int value = 0; | 200 int value = 0; |
201 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); | 201 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); |
202 ASSERT_EQ(1, value); | 202 ASSERT_EQ(1, value); |
203 } | 203 } |
204 | 204 |
205 } // namespace | 205 } // namespace |
206 | 206 |
207 TEST(JSONValueSerializerTest, JSONReaderComments) { | 207 TEST(JSONValueSerializerTest, JSONReaderComments) { |
208 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); | 208 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); |
209 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); | 209 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); |
210 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); | 210 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); |
211 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); | 211 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); |
212 ValidateJsonList("[ 1 /* one */ ] /* end */"); | 212 ValidateJsonList("[ 1 /* one */ ] /* end */"); |
213 ValidateJsonList("[ 1 //// ,2\r\n ]"); | 213 ValidateJsonList("[ 1 //// ,2\r\n ]"); |
214 | 214 |
215 scoped_ptr<Value> root; | 215 scoped_ptr<Value> root; |
216 | 216 |
217 // It's ok to have a comment in a string. | 217 // It's ok to have a comment in a string. |
218 root.reset(JSONReader::Read("[\"// ok\\n /* foo */ \"]", false)); | 218 root.reset(base::JSONReader::Read("[\"// ok\\n /* foo */ \"]", false)); |
219 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); | 219 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); |
220 ListValue* list = static_cast<ListValue*>(root.get()); | 220 ListValue* list = static_cast<ListValue*>(root.get()); |
221 ASSERT_EQ(1U, list->GetSize()); | 221 ASSERT_EQ(1U, list->GetSize()); |
222 Value* elt = NULL; | 222 Value* elt = NULL; |
223 ASSERT_TRUE(list->Get(0, &elt)); | 223 ASSERT_TRUE(list->Get(0, &elt)); |
224 std::wstring value; | 224 std::wstring value; |
225 ASSERT_TRUE(elt && elt->GetAsString(&value)); | 225 ASSERT_TRUE(elt && elt->GetAsString(&value)); |
226 ASSERT_EQ(L"// ok\n /* foo */ ", value); | 226 ASSERT_EQ(L"// ok\n /* foo */ ", value); |
227 | 227 |
228 // You can't nest comments. | 228 // You can't nest comments. |
229 root.reset(JSONReader::Read("/* /* inner */ outer */ [ 1 ]", false)); | 229 root.reset(base::JSONReader::Read("/* /* inner */ outer */ [ 1 ]", false)); |
230 ASSERT_FALSE(root.get()); | 230 ASSERT_FALSE(root.get()); |
231 | 231 |
232 // Not a open comment token. | 232 // Not a open comment token. |
233 root.reset(JSONReader::Read("/ * * / [1]", false)); | 233 root.reset(base::JSONReader::Read("/ * * / [1]", false)); |
234 ASSERT_FALSE(root.get()); | 234 ASSERT_FALSE(root.get()); |
235 } | 235 } |
236 | 236 |
237 class JSONFileValueSerializerTest : public testing::Test { | 237 class JSONFileValueSerializerTest : public testing::Test { |
238 protected: | 238 protected: |
239 virtual void SetUp() { | 239 virtual void SetUp() { |
240 // Name a subdirectory of the temp directory. | 240 // Name a subdirectory of the temp directory. |
241 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); | 241 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); |
242 test_dir_ = | 242 test_dir_ = |
243 test_dir_.Append(FILE_PATH_LITERAL("JSONFileValueSerializerTest")); | 243 test_dir_.Append(FILE_PATH_LITERAL("JSONFileValueSerializerTest")); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 FilePath source_file_path; | 339 FilePath source_file_path; |
340 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); | 340 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); |
341 source_file_path = source_file_path.Append( | 341 source_file_path = source_file_path.Append( |
342 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); | 342 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); |
343 ASSERT_TRUE(file_util::PathExists(source_file_path)); | 343 ASSERT_TRUE(file_util::PathExists(source_file_path)); |
344 JSONFileValueSerializer serializer(source_file_path); | 344 JSONFileValueSerializer serializer(source_file_path); |
345 scoped_ptr<Value> root; | 345 scoped_ptr<Value> root; |
346 root.reset(serializer.Deserialize(NULL)); | 346 root.reset(serializer.Deserialize(NULL)); |
347 ASSERT_TRUE(root.get()); | 347 ASSERT_TRUE(root.get()); |
348 } | 348 } |
OLD | NEW |