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

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

Issue 3117017: Remove deprecated wstring Get(As)String() methods from Value, etc. (Closed)
Patch Set: fix win Created 10 years, 4 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/json_reader.h" 7 #include "base/json/json_reader.h"
8 #include "base/json/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/string16.h"
10 #include "base/string_util.h" 11 #include "base/string_util.h"
12 #include "base/utf_string_conversions.h"
11 #include "base/values.h" 13 #include "base/values.h"
12 #include "chrome/common/chrome_paths.h" 14 #include "chrome/common/chrome_paths.h"
13 #include "chrome/common/json_value_serializer.h" 15 #include "chrome/common/json_value_serializer.h"
14 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
15 17
16 TEST(JSONValueSerializerTest, Roundtrip) { 18 TEST(JSONValueSerializerTest, Roundtrip) {
17 const std::string original_serialization = 19 const std::string original_serialization =
18 "{\"bool\":true,\"int\":42,\"list\":[1,2],\"null\":null,\"real\":3.14}"; 20 "{\"bool\":true,\"int\":42,\"list\":[1,2],\"null\":null,\"real\":3.14}";
19 JSONStringValueSerializer serializer(original_serialization); 21 JSONStringValueSerializer serializer(original_serialization);
20 scoped_ptr<Value> root(serializer.Deserialize(NULL, NULL)); 22 scoped_ptr<Value> root(serializer.Deserialize(NULL, NULL));
21 ASSERT_TRUE(root.get()); 23 ASSERT_TRUE(root.get());
22 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 24 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
23 25
24 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); 26 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
25 27
26 Value* null_value = NULL; 28 Value* null_value = NULL;
27 ASSERT_TRUE(root_dict->Get(L"null", &null_value)); 29 ASSERT_TRUE(root_dict->Get("null", &null_value));
28 ASSERT_TRUE(null_value); 30 ASSERT_TRUE(null_value);
29 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); 31 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL));
30 32
31 bool bool_value = false; 33 bool bool_value = false;
32 ASSERT_TRUE(root_dict->GetBoolean(L"bool", &bool_value)); 34 ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value));
33 ASSERT_TRUE(bool_value); 35 ASSERT_TRUE(bool_value);
34 36
35 int int_value = 0; 37 int int_value = 0;
36 ASSERT_TRUE(root_dict->GetInteger(L"int", &int_value)); 38 ASSERT_TRUE(root_dict->GetInteger("int", &int_value));
37 ASSERT_EQ(42, int_value); 39 ASSERT_EQ(42, int_value);
38 40
39 double real_value = 0.0; 41 double real_value = 0.0;
40 ASSERT_TRUE(root_dict->GetReal(L"real", &real_value)); 42 ASSERT_TRUE(root_dict->GetReal("real", &real_value));
41 ASSERT_DOUBLE_EQ(3.14, real_value); 43 ASSERT_DOUBLE_EQ(3.14, real_value);
42 44
43 // We shouldn't be able to write using this serializer, since it was 45 // We shouldn't be able to write using this serializer, since it was
44 // initialized with a const string. 46 // initialized with a const string.
45 ASSERT_FALSE(serializer.Serialize(*root_dict)); 47 ASSERT_FALSE(serializer.Serialize(*root_dict));
46 48
47 std::string test_serialization = ""; 49 std::string test_serialization = "";
48 JSONStringValueSerializer mutable_serializer(&test_serialization); 50 JSONStringValueSerializer mutable_serializer(&test_serialization);
49 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); 51 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict));
50 ASSERT_EQ(original_serialization, test_serialization); 52 ASSERT_EQ(original_serialization, test_serialization);
(...skipping 13 matching lines...) Expand all
64 " \"int\": 42," JSON_NEWLINE 66 " \"int\": 42," JSON_NEWLINE
65 " \"list\": [ 1, 2 ]," JSON_NEWLINE 67 " \"list\": [ 1, 2 ]," JSON_NEWLINE
66 " \"null\": null," JSON_NEWLINE 68 " \"null\": null," JSON_NEWLINE
67 " \"real\": 3.14" JSON_NEWLINE 69 " \"real\": 3.14" JSON_NEWLINE
68 "}" JSON_NEWLINE; 70 "}" JSON_NEWLINE;
69 #undef JSON_NEWLINE 71 #undef JSON_NEWLINE
70 ASSERT_EQ(pretty_serialization, test_serialization); 72 ASSERT_EQ(pretty_serialization, test_serialization);
71 } 73 }
72 74
73 TEST(JSONValueSerializerTest, StringEscape) { 75 TEST(JSONValueSerializerTest, StringEscape) {
74 std::wstring all_chars; 76 string16 all_chars;
75 for (int i = 1; i < 256; ++i) { 77 for (int i = 1; i < 256; ++i) {
76 all_chars += static_cast<wchar_t>(i); 78 all_chars += static_cast<char16>(i);
77 } 79 }
78 // Generated in in Firefox using the following js (with an extra backslash for 80 // Generated in in Firefox using the following js (with an extra backslash for
79 // double quote): 81 // double quote):
80 // var s = ''; 82 // var s = '';
81 // for (var i = 1; i < 256; ++i) { s += String.fromCharCode(i); } 83 // for (var i = 1; i < 256; ++i) { s += String.fromCharCode(i); }
82 // uneval(s).replace(/\\/g, "\\\\"); 84 // uneval(s).replace(/\\/g, "\\\\");
83 std::string all_chars_expected = 85 std::string all_chars_expected =
84 "\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000B\\f\\r" 86 "\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000B\\f\\r"
85 "\\u000E\\u000F\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" 87 "\\u000E\\u000F\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017"
86 "\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F !\\\"" 88 "\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F !\\\""
(...skipping 11 matching lines...) Expand all
98 "\\u00DE\\u00DF\\u00E0\\u00E1\\u00E2\\u00E3\\u00E4\\u00E5\\u00E6\\u00E7" 100 "\\u00DE\\u00DF\\u00E0\\u00E1\\u00E2\\u00E3\\u00E4\\u00E5\\u00E6\\u00E7"
99 "\\u00E8\\u00E9\\u00EA\\u00EB\\u00EC\\u00ED\\u00EE\\u00EF\\u00F0\\u00F1" 101 "\\u00E8\\u00E9\\u00EA\\u00EB\\u00EC\\u00ED\\u00EE\\u00EF\\u00F0\\u00F1"
100 "\\u00F2\\u00F3\\u00F4\\u00F5\\u00F6\\u00F7\\u00F8\\u00F9\\u00FA\\u00FB" 102 "\\u00F2\\u00F3\\u00F4\\u00F5\\u00F6\\u00F7\\u00F8\\u00F9\\u00FA\\u00FB"
101 "\\u00FC\\u00FD\\u00FE\\u00FF"; 103 "\\u00FC\\u00FD\\u00FE\\u00FF";
102 104
103 std::string expected_output = "{\"all_chars\":\"" + all_chars_expected + 105 std::string expected_output = "{\"all_chars\":\"" + all_chars_expected +
104 "\"}"; 106 "\"}";
105 // Test JSONWriter interface 107 // Test JSONWriter interface
106 std::string output_js; 108 std::string output_js;
107 DictionaryValue valueRoot; 109 DictionaryValue valueRoot;
108 valueRoot.SetString(L"all_chars", all_chars); 110 valueRoot.SetString("all_chars", all_chars);
109 base::JSONWriter::Write(&valueRoot, false, &output_js); 111 base::JSONWriter::Write(&valueRoot, false, &output_js);
110 ASSERT_EQ(expected_output, output_js); 112 ASSERT_EQ(expected_output, output_js);
111 113
112 // Test JSONValueSerializer interface (uses JSONWriter). 114 // Test JSONValueSerializer interface (uses JSONWriter).
113 JSONStringValueSerializer serializer(&output_js); 115 JSONStringValueSerializer serializer(&output_js);
114 ASSERT_TRUE(serializer.Serialize(valueRoot)); 116 ASSERT_TRUE(serializer.Serialize(valueRoot));
115 ASSERT_EQ(expected_output, output_js); 117 ASSERT_EQ(expected_output, output_js);
116 } 118 }
117 119
118 TEST(JSONValueSerializerTest, UnicodeStrings) { 120 TEST(JSONValueSerializerTest, UnicodeStrings) {
119 // unicode string json -> escaped ascii text 121 // unicode string json -> escaped ascii text
120 DictionaryValue root; 122 DictionaryValue root;
121 std::wstring test(L"\x7F51\x9875"); 123 string16 test(WideToUTF16(L"\x7F51\x9875"));
122 root.SetString(L"web", test); 124 root.SetString("web", test);
123 125
124 std::string expected = "{\"web\":\"\\u7F51\\u9875\"}"; 126 std::string expected = "{\"web\":\"\\u7F51\\u9875\"}";
125 127
126 std::string actual; 128 std::string actual;
127 JSONStringValueSerializer serializer(&actual); 129 JSONStringValueSerializer serializer(&actual);
128 ASSERT_TRUE(serializer.Serialize(root)); 130 ASSERT_TRUE(serializer.Serialize(root));
129 ASSERT_EQ(expected, actual); 131 ASSERT_EQ(expected, actual);
130 132
131 // escaped ascii text -> json 133 // escaped ascii text -> json
132 JSONStringValueSerializer deserializer(expected); 134 JSONStringValueSerializer deserializer(expected);
133 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); 135 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL));
134 ASSERT_TRUE(deserial_root.get()); 136 ASSERT_TRUE(deserial_root.get());
135 DictionaryValue* dict_root = 137 DictionaryValue* dict_root =
136 static_cast<DictionaryValue*>(deserial_root.get()); 138 static_cast<DictionaryValue*>(deserial_root.get());
137 std::wstring web_value; 139 string16 web_value;
138 ASSERT_TRUE(dict_root->GetString(L"web", &web_value)); 140 ASSERT_TRUE(dict_root->GetString("web", &web_value));
139 ASSERT_EQ(test, web_value); 141 ASSERT_EQ(test, web_value);
140 } 142 }
141 143
142 TEST(JSONValueSerializerTest, HexStrings) { 144 TEST(JSONValueSerializerTest, HexStrings) {
143 // hex string json -> escaped ascii text 145 // hex string json -> escaped ascii text
144 DictionaryValue root; 146 DictionaryValue root;
145 std::wstring test(L"\x01\x02"); 147 string16 test(WideToUTF16(L"\x01\x02"));
146 root.SetString(L"test", test); 148 root.SetString("test", test);
147 149
148 std::string expected = "{\"test\":\"\\u0001\\u0002\"}"; 150 std::string expected = "{\"test\":\"\\u0001\\u0002\"}";
149 151
150 std::string actual; 152 std::string actual;
151 JSONStringValueSerializer serializer(&actual); 153 JSONStringValueSerializer serializer(&actual);
152 ASSERT_TRUE(serializer.Serialize(root)); 154 ASSERT_TRUE(serializer.Serialize(root));
153 ASSERT_EQ(expected, actual); 155 ASSERT_EQ(expected, actual);
154 156
155 // escaped ascii text -> json 157 // escaped ascii text -> json
156 JSONStringValueSerializer deserializer(expected); 158 JSONStringValueSerializer deserializer(expected);
157 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); 159 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL));
158 ASSERT_TRUE(deserial_root.get()); 160 ASSERT_TRUE(deserial_root.get());
159 DictionaryValue* dict_root = 161 DictionaryValue* dict_root =
160 static_cast<DictionaryValue*>(deserial_root.get()); 162 static_cast<DictionaryValue*>(deserial_root.get());
161 std::wstring test_value; 163 string16 test_value;
162 ASSERT_TRUE(dict_root->GetString(L"test", &test_value)); 164 ASSERT_TRUE(dict_root->GetString("test", &test_value));
163 ASSERT_EQ(test, test_value); 165 ASSERT_EQ(test, test_value);
164 166
165 // Test converting escaped regular chars 167 // Test converting escaped regular chars
166 std::string escaped_chars = "{\"test\":\"\\u0067\\u006f\"}"; 168 std::string escaped_chars = "{\"test\":\"\\u0067\\u006f\"}";
167 JSONStringValueSerializer deserializer2(escaped_chars); 169 JSONStringValueSerializer deserializer2(escaped_chars);
168 deserial_root.reset(deserializer2.Deserialize(NULL, NULL)); 170 deserial_root.reset(deserializer2.Deserialize(NULL, NULL));
169 ASSERT_TRUE(deserial_root.get()); 171 ASSERT_TRUE(deserial_root.get());
170 dict_root = static_cast<DictionaryValue*>(deserial_root.get()); 172 dict_root = static_cast<DictionaryValue*>(deserial_root.get());
171 ASSERT_TRUE(dict_root->GetString(L"test", &test_value)); 173 ASSERT_TRUE(dict_root->GetString("test", &test_value));
172 ASSERT_EQ(std::wstring(L"go"), test_value); 174 ASSERT_EQ(ASCIIToUTF16("go"), test_value);
173 } 175 }
174 176
175 TEST(JSONValueSerializerTest, AllowTrailingComma) { 177 TEST(JSONValueSerializerTest, AllowTrailingComma) {
176 scoped_ptr<Value> root; 178 scoped_ptr<Value> root;
177 scoped_ptr<Value> root_expected; 179 scoped_ptr<Value> root_expected;
178 std::string test_with_commas("{\"key\": [true,],}"); 180 std::string test_with_commas("{\"key\": [true,],}");
179 std::string test_no_commas("{\"key\": [true]}"); 181 std::string test_no_commas("{\"key\": [true]}");
180 182
181 JSONStringValueSerializer serializer(test_with_commas); 183 JSONStringValueSerializer serializer(test_with_commas);
182 serializer.set_allow_trailing_comma(true); 184 serializer.set_allow_trailing_comma(true);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 216
215 scoped_ptr<Value> root; 217 scoped_ptr<Value> root;
216 218
217 // It's ok to have a comment in a string. 219 // It's ok to have a comment in a string.
218 root.reset(base::JSONReader::Read("[\"// ok\\n /* foo */ \"]", false)); 220 root.reset(base::JSONReader::Read("[\"// ok\\n /* foo */ \"]", false));
219 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); 221 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST));
220 ListValue* list = static_cast<ListValue*>(root.get()); 222 ListValue* list = static_cast<ListValue*>(root.get());
221 ASSERT_EQ(1U, list->GetSize()); 223 ASSERT_EQ(1U, list->GetSize());
222 Value* elt = NULL; 224 Value* elt = NULL;
223 ASSERT_TRUE(list->Get(0, &elt)); 225 ASSERT_TRUE(list->Get(0, &elt));
224 std::wstring value; 226 std::string value;
225 ASSERT_TRUE(elt && elt->GetAsString(&value)); 227 ASSERT_TRUE(elt && elt->GetAsString(&value));
226 ASSERT_EQ(L"// ok\n /* foo */ ", value); 228 ASSERT_EQ("// ok\n /* foo */ ", value);
227 229
228 // You can't nest comments. 230 // You can't nest comments.
229 root.reset(base::JSONReader::Read("/* /* inner */ outer */ [ 1 ]", false)); 231 root.reset(base::JSONReader::Read("/* /* inner */ outer */ [ 1 ]", false));
230 ASSERT_FALSE(root.get()); 232 ASSERT_FALSE(root.get());
231 233
232 // Not a open comment token. 234 // Not a open comment token.
233 root.reset(base::JSONReader::Read("/ * * / [1]", false)); 235 root.reset(base::JSONReader::Read("/ * * / [1]", false));
234 ASSERT_FALSE(root.get()); 236 ASSERT_FALSE(root.get());
235 } 237 }
236 238
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 JSONFileValueSerializer deserializer(original_file_path); 270 JSONFileValueSerializer deserializer(original_file_path);
269 scoped_ptr<Value> root; 271 scoped_ptr<Value> root;
270 root.reset(deserializer.Deserialize(NULL, NULL)); 272 root.reset(deserializer.Deserialize(NULL, NULL));
271 273
272 ASSERT_TRUE(root.get()); 274 ASSERT_TRUE(root.get());
273 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 275 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
274 276
275 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); 277 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
276 278
277 Value* null_value = NULL; 279 Value* null_value = NULL;
278 ASSERT_TRUE(root_dict->Get(L"null", &null_value)); 280 ASSERT_TRUE(root_dict->Get("null", &null_value));
279 ASSERT_TRUE(null_value); 281 ASSERT_TRUE(null_value);
280 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); 282 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL));
281 283
282 bool bool_value = false; 284 bool bool_value = false;
283 ASSERT_TRUE(root_dict->GetBoolean(L"bool", &bool_value)); 285 ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value));
284 ASSERT_TRUE(bool_value); 286 ASSERT_TRUE(bool_value);
285 287
286 int int_value = 0; 288 int int_value = 0;
287 ASSERT_TRUE(root_dict->GetInteger(L"int", &int_value)); 289 ASSERT_TRUE(root_dict->GetInteger("int", &int_value));
288 ASSERT_EQ(42, int_value); 290 ASSERT_EQ(42, int_value);
289 291
290 std::wstring string_value; 292 std::string string_value;
291 ASSERT_TRUE(root_dict->GetString(L"string", &string_value)); 293 ASSERT_TRUE(root_dict->GetString("string", &string_value));
292 ASSERT_EQ(L"hello", string_value); 294 ASSERT_EQ("hello", string_value);
293 295
294 // Now try writing. 296 // Now try writing.
295 const FilePath written_file_path = 297 const FilePath written_file_path =
296 test_dir_.Append(FILE_PATH_LITERAL("test_output.js")); 298 test_dir_.Append(FILE_PATH_LITERAL("test_output.js"));
297 299
298 ASSERT_FALSE(file_util::PathExists(written_file_path)); 300 ASSERT_FALSE(file_util::PathExists(written_file_path));
299 JSONFileValueSerializer serializer(written_file_path); 301 JSONFileValueSerializer serializer(written_file_path);
300 ASSERT_TRUE(serializer.Serialize(*root)); 302 ASSERT_TRUE(serializer.Serialize(*root));
301 ASSERT_TRUE(file_util::PathExists(written_file_path)); 303 ASSERT_TRUE(file_util::PathExists(written_file_path));
302 304
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 FilePath source_file_path; 341 FilePath source_file_path;
340 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); 342 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path));
341 source_file_path = source_file_path.Append( 343 source_file_path = source_file_path.Append(
342 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); 344 FILE_PATH_LITERAL("serializer_test_nowhitespace.js"));
343 ASSERT_TRUE(file_util::PathExists(source_file_path)); 345 ASSERT_TRUE(file_util::PathExists(source_file_path));
344 JSONFileValueSerializer serializer(source_file_path); 346 JSONFileValueSerializer serializer(source_file_path);
345 scoped_ptr<Value> root; 347 scoped_ptr<Value> root;
346 root.reset(serializer.Deserialize(NULL, NULL)); 348 root.reset(serializer.Deserialize(NULL, NULL));
347 ASSERT_TRUE(root.get()); 349 ASSERT_TRUE(root.get());
348 } 350 }
OLDNEW
« no previous file with comments | « chrome/common/extensions/extension_message_bundle_unittest.cc ('k') | chrome/common/plugin_group_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698