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

Side by Side Diff: chrome/common/json_value_serializer_unittest.cc

Issue 13169: Add error messages to JSONReader and friends. This required a bit of refactor... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 12 years 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_reader.h"
8 #include "base/json_writer.h" 8 #include "base/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}";
19 Value* root = NULL; 19 Value* root = NULL;
20 JSONStringValueSerializer serializer(original_serialization); 20 JSONStringValueSerializer serializer(original_serialization);
21 ASSERT_TRUE(serializer.Deserialize(&root)); 21 ASSERT_TRUE(serializer.Deserialize(&root, NULL));
22 ASSERT_TRUE(root); 22 ASSERT_TRUE(root);
23 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 23 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
24 24
25 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root); 25 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root);
26 26
27 Value* null_value = NULL; 27 Value* null_value = NULL;
28 ASSERT_TRUE(root_dict->Get(L"null", &null_value)); 28 ASSERT_TRUE(root_dict->Get(L"null", &null_value));
29 ASSERT_TRUE(null_value); 29 ASSERT_TRUE(null_value);
30 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); 30 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL));
31 31
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 std::string expected = "{\"web\":\"\\u7F51\\u9875\"}"; 114 std::string expected = "{\"web\":\"\\u7F51\\u9875\"}";
115 115
116 std::string actual; 116 std::string actual;
117 JSONStringValueSerializer serializer(&actual); 117 JSONStringValueSerializer serializer(&actual);
118 ASSERT_TRUE(serializer.Serialize(root)); 118 ASSERT_TRUE(serializer.Serialize(root));
119 ASSERT_EQ(expected, actual); 119 ASSERT_EQ(expected, actual);
120 120
121 // escaped ascii text -> json 121 // escaped ascii text -> json
122 Value* deserial_root = NULL; 122 Value* deserial_root = NULL;
123 JSONStringValueSerializer deserializer(expected); 123 JSONStringValueSerializer deserializer(expected);
124 ASSERT_TRUE(deserializer.Deserialize(&deserial_root)); 124 ASSERT_TRUE(deserializer.Deserialize(&deserial_root, NULL));
125 DictionaryValue* dict_root = static_cast<DictionaryValue*>(deserial_root); 125 DictionaryValue* dict_root = static_cast<DictionaryValue*>(deserial_root);
126 std::wstring web_value; 126 std::wstring web_value;
127 ASSERT_TRUE(dict_root->GetString(L"web", &web_value)); 127 ASSERT_TRUE(dict_root->GetString(L"web", &web_value));
128 ASSERT_EQ(test, web_value); 128 ASSERT_EQ(test, web_value);
129 delete deserial_root; 129 delete deserial_root;
130 } 130 }
131 131
132 TEST(JSONValueSerializerTest, HexStrings) { 132 TEST(JSONValueSerializerTest, HexStrings) {
133 // hex string json -> escaped ascii text 133 // hex string json -> escaped ascii text
134 DictionaryValue root; 134 DictionaryValue root;
135 std::wstring test(L"\x01\x02"); 135 std::wstring test(L"\x01\x02");
136 root.SetString(L"test", test); 136 root.SetString(L"test", test);
137 137
138 std::string expected = "{\"test\":\"\\x01\\x02\"}"; 138 std::string expected = "{\"test\":\"\\x01\\x02\"}";
139 139
140 std::string actual; 140 std::string actual;
141 JSONStringValueSerializer serializer(&actual); 141 JSONStringValueSerializer serializer(&actual);
142 ASSERT_TRUE(serializer.Serialize(root)); 142 ASSERT_TRUE(serializer.Serialize(root));
143 ASSERT_EQ(expected, actual); 143 ASSERT_EQ(expected, actual);
144 144
145 // escaped ascii text -> json 145 // escaped ascii text -> json
146 Value* deserial_root = NULL; 146 Value* deserial_root = NULL;
147 JSONStringValueSerializer deserializer(expected); 147 JSONStringValueSerializer deserializer(expected);
148 ASSERT_TRUE(deserializer.Deserialize(&deserial_root)); 148 ASSERT_TRUE(deserializer.Deserialize(&deserial_root, NULL));
149 DictionaryValue* dict_root = static_cast<DictionaryValue*>(deserial_root); 149 DictionaryValue* dict_root = static_cast<DictionaryValue*>(deserial_root);
150 std::wstring test_value; 150 std::wstring test_value;
151 ASSERT_TRUE(dict_root->GetString(L"test", &test_value)); 151 ASSERT_TRUE(dict_root->GetString(L"test", &test_value));
152 ASSERT_EQ(test, test_value); 152 ASSERT_EQ(test, test_value);
153 delete deserial_root; 153 delete deserial_root;
154 154
155 // Test converting escaped regular chars 155 // Test converting escaped regular chars
156 deserial_root = NULL; 156 deserial_root = NULL;
157 std::string escaped_chars = "{\"test\":\"\\x67\\x6f\"}"; 157 std::string escaped_chars = "{\"test\":\"\\x67\\x6f\"}";
158 JSONStringValueSerializer deserializer2(escaped_chars); 158 JSONStringValueSerializer deserializer2(escaped_chars);
159 ASSERT_TRUE(deserializer2.Deserialize(&deserial_root)); 159 ASSERT_TRUE(deserializer2.Deserialize(&deserial_root, NULL));
160 dict_root = static_cast<DictionaryValue*>(deserial_root); 160 dict_root = static_cast<DictionaryValue*>(deserial_root);
161 ASSERT_TRUE(dict_root->GetString(L"test", &test_value)); 161 ASSERT_TRUE(dict_root->GetString(L"test", &test_value));
162 ASSERT_EQ(std::wstring(L"go"), test_value); 162 ASSERT_EQ(std::wstring(L"go"), test_value);
163 delete deserial_root; 163 delete deserial_root;
164 } 164 }
165 165
166 TEST(JSONValueSerializerTest, AllowTrailingComma) { 166 TEST(JSONValueSerializerTest, AllowTrailingComma) {
167 Value* root = NULL; 167 Value* root = NULL;
168 Value* root_expected = NULL; 168 Value* root_expected = NULL;
169 std::string test_with_commas("{\"key\": [true,],}"); 169 std::string test_with_commas("{\"key\": [true,],}");
170 std::string test_no_commas("{\"key\": [true]}"); 170 std::string test_no_commas("{\"key\": [true]}");
171 171
172 JSONStringValueSerializer serializer(test_with_commas); 172 JSONStringValueSerializer serializer(test_with_commas);
173 serializer.set_allow_trailing_comma(true); 173 serializer.set_allow_trailing_comma(true);
174 JSONStringValueSerializer serializer_expected(test_no_commas); 174 JSONStringValueSerializer serializer_expected(test_no_commas);
175 ASSERT_TRUE(serializer.Deserialize(&root)); 175 ASSERT_TRUE(serializer.Deserialize(&root, NULL));
176 ASSERT_TRUE(serializer_expected.Deserialize(&root_expected)); 176 ASSERT_TRUE(serializer_expected.Deserialize(&root_expected, NULL));
177 ASSERT_TRUE(root->Equals(root_expected)); 177 ASSERT_TRUE(root->Equals(root_expected));
178 178
179 delete root; 179 delete root;
180 delete root_expected; 180 delete root_expected;
181 } 181 }
182 182
183 namespace { 183 namespace {
184 184
185 void ValidateJsonList(const std::string& json) { 185 void ValidateJsonList(const std::string& json) {
186 Value* root = NULL; 186 Value* root = NULL;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 TEST_F(JSONFileValueSerializerTest, Roundtrip) { 255 TEST_F(JSONFileValueSerializerTest, Roundtrip) {
256 std::wstring original_file_path; 256 std::wstring original_file_path;
257 ASSERT_TRUE( 257 ASSERT_TRUE(
258 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); 258 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path));
259 file_util::AppendToPath(&original_file_path, L"serializer_test.js"); 259 file_util::AppendToPath(&original_file_path, L"serializer_test.js");
260 260
261 ASSERT_TRUE(file_util::PathExists(original_file_path)); 261 ASSERT_TRUE(file_util::PathExists(original_file_path));
262 262
263 JSONFileValueSerializer deserializer(original_file_path); 263 JSONFileValueSerializer deserializer(original_file_path);
264 Value* root; 264 Value* root;
265 ASSERT_TRUE(deserializer.Deserialize(&root)); 265 ASSERT_TRUE(deserializer.Deserialize(&root, NULL));
266 266
267 ASSERT_TRUE(root); 267 ASSERT_TRUE(root);
268 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 268 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
269 269
270 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root); 270 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root);
271 271
272 Value* null_value = NULL; 272 Value* null_value = NULL;
273 ASSERT_TRUE(root_dict->Get(L"null", &null_value)); 273 ASSERT_TRUE(root_dict->Get(L"null", &null_value));
274 ASSERT_TRUE(null_value); 274 ASSERT_TRUE(null_value);
275 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); 275 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL));
(...skipping 29 matching lines...) Expand all
305 TEST_F(JSONFileValueSerializerTest, RoundtripNested) { 305 TEST_F(JSONFileValueSerializerTest, RoundtripNested) {
306 std::wstring original_file_path; 306 std::wstring original_file_path;
307 ASSERT_TRUE( 307 ASSERT_TRUE(
308 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); 308 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path));
309 file_util::AppendToPath(&original_file_path, L"serializer_nested_test.js"); 309 file_util::AppendToPath(&original_file_path, L"serializer_nested_test.js");
310 310
311 ASSERT_TRUE(file_util::PathExists(original_file_path)); 311 ASSERT_TRUE(file_util::PathExists(original_file_path));
312 312
313 JSONFileValueSerializer deserializer(original_file_path); 313 JSONFileValueSerializer deserializer(original_file_path);
314 Value* root; 314 Value* root;
315 ASSERT_TRUE(deserializer.Deserialize(&root)); 315 ASSERT_TRUE(deserializer.Deserialize(&root, NULL));
316 316
317 // Now try writing. 317 // Now try writing.
318 std::wstring written_file_path = test_dir_; 318 std::wstring written_file_path = test_dir_;
319 file_util::AppendToPath(&written_file_path, L"test_output.js"); 319 file_util::AppendToPath(&written_file_path, L"test_output.js");
320 320
321 ASSERT_FALSE(file_util::PathExists(written_file_path)); 321 ASSERT_FALSE(file_util::PathExists(written_file_path));
322 JSONFileValueSerializer serializer(written_file_path); 322 JSONFileValueSerializer serializer(written_file_path);
323 ASSERT_TRUE(serializer.Serialize(*root)); 323 ASSERT_TRUE(serializer.Serialize(*root));
324 ASSERT_TRUE(file_util::PathExists(written_file_path)); 324 ASSERT_TRUE(file_util::PathExists(written_file_path));
325 325
326 // Now compare file contents. 326 // Now compare file contents.
327 EXPECT_TRUE(file_util::ContentsEqual(original_file_path, written_file_path)); 327 EXPECT_TRUE(file_util::ContentsEqual(original_file_path, written_file_path));
328 EXPECT_TRUE(file_util::Delete(written_file_path, false)); 328 EXPECT_TRUE(file_util::Delete(written_file_path, false));
329 329
330 delete root; 330 delete root;
331 } 331 }
332 332
333 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { 333 TEST_F(JSONFileValueSerializerTest, NoWhitespace) {
334 std::wstring source_file_path; 334 std::wstring source_file_path;
335 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); 335 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path));
336 file_util::AppendToPath(&source_file_path, 336 file_util::AppendToPath(&source_file_path,
337 L"serializer_test_nowhitespace.js"); 337 L"serializer_test_nowhitespace.js");
338 ASSERT_TRUE(file_util::PathExists(source_file_path)); 338 ASSERT_TRUE(file_util::PathExists(source_file_path));
339 JSONFileValueSerializer serializer(source_file_path); 339 JSONFileValueSerializer serializer(source_file_path);
340 Value* root; 340 Value* root;
341 ASSERT_TRUE(serializer.Deserialize(&root)); 341 ASSERT_TRUE(serializer.Deserialize(&root, NULL));
342 ASSERT_TRUE(root); 342 ASSERT_TRUE(root);
343 delete root; 343 delete root;
344 } 344 }
345 #endif // defined(OS_WIN) 345 #endif // defined(OS_WIN)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698