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

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

Issue 7661009: base: Add Is* functions to Value class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: tony review Created 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/scoped_temp_dir.h" 10 #include "base/scoped_temp_dir.h"
11 #include "base/string16.h" 11 #include "base/string16.h"
12 #include "base/string_util.h" 12 #include "base/string_util.h"
13 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "chrome/common/chrome_paths.h" 15 #include "chrome/common/chrome_paths.h"
16 #include "content/common/json_value_serializer.h" 16 #include "content/common/json_value_serializer.h"
17 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
18 18
19 TEST(JSONValueSerializerTest, Roundtrip) { 19 TEST(JSONValueSerializerTest, Roundtrip) {
20 const std::string original_serialization = 20 const std::string original_serialization =
21 "{\"bool\":true,\"double\":3.14,\"int\":42,\"list\":[1,2],\"null\":null}"; 21 "{\"bool\":true,\"double\":3.14,\"int\":42,\"list\":[1,2],\"null\":null}";
22 JSONStringValueSerializer serializer(original_serialization); 22 JSONStringValueSerializer serializer(original_serialization);
23 scoped_ptr<Value> root(serializer.Deserialize(NULL, NULL)); 23 scoped_ptr<Value> root(serializer.Deserialize(NULL, NULL));
24 ASSERT_TRUE(root.get()); 24 ASSERT_TRUE(root.get());
25 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 25 ASSERT_TRUE(root->IsDictionary());
26 26
27 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); 27 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
28 28
29 Value* null_value = NULL; 29 Value* null_value = NULL;
30 ASSERT_TRUE(root_dict->Get("null", &null_value)); 30 ASSERT_TRUE(root_dict->Get("null", &null_value));
31 ASSERT_TRUE(null_value); 31 ASSERT_TRUE(null_value);
32 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); 32 ASSERT_TRUE(null_value->IsNull());
33 33
34 bool bool_value = false; 34 bool bool_value = false;
35 ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value)); 35 ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value));
36 ASSERT_TRUE(bool_value); 36 ASSERT_TRUE(bool_value);
37 37
38 int int_value = 0; 38 int int_value = 0;
39 ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); 39 ASSERT_TRUE(root_dict->GetInteger("int", &int_value));
40 ASSERT_EQ(42, int_value); 40 ASSERT_EQ(42, int_value);
41 41
42 double double_value = 0.0; 42 double double_value = 0.0;
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 ASSERT_TRUE(root.get()); 188 ASSERT_TRUE(root.get());
189 root_expected.reset(serializer_expected.Deserialize(NULL, NULL)); 189 root_expected.reset(serializer_expected.Deserialize(NULL, NULL));
190 ASSERT_TRUE(root_expected.get()); 190 ASSERT_TRUE(root_expected.get());
191 ASSERT_TRUE(root->Equals(root_expected.get())); 191 ASSERT_TRUE(root->Equals(root_expected.get()));
192 } 192 }
193 193
194 namespace { 194 namespace {
195 195
196 void ValidateJsonList(const std::string& json) { 196 void ValidateJsonList(const std::string& json) {
197 scoped_ptr<Value> root(base::JSONReader::Read(json, false)); 197 scoped_ptr<Value> root(base::JSONReader::Read(json, false));
198 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); 198 ASSERT_TRUE(root.get() && root->IsList());
199 ListValue* list = static_cast<ListValue*>(root.get()); 199 ListValue* list = static_cast<ListValue*>(root.get());
200 ASSERT_EQ(1U, list->GetSize()); 200 ASSERT_EQ(1U, list->GetSize());
201 Value* elt = NULL; 201 Value* elt = NULL;
202 ASSERT_TRUE(list->Get(0, &elt)); 202 ASSERT_TRUE(list->Get(0, &elt));
203 int value = 0; 203 int value = 0;
204 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); 204 ASSERT_TRUE(elt && elt->GetAsInteger(&value));
205 ASSERT_EQ(1, value); 205 ASSERT_EQ(1, value);
206 } 206 }
207 207
208 } // namespace 208 } // namespace
209 209
210 TEST(JSONValueSerializerTest, JSONReaderComments) { 210 TEST(JSONValueSerializerTest, JSONReaderComments) {
211 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); 211 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]");
212 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); 212 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]");
213 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); 213 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer");
214 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); 214 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]");
215 ValidateJsonList("[ 1 /* one */ ] /* end */"); 215 ValidateJsonList("[ 1 /* one */ ] /* end */");
216 ValidateJsonList("[ 1 //// ,2\r\n ]"); 216 ValidateJsonList("[ 1 //// ,2\r\n ]");
217 217
218 scoped_ptr<Value> root; 218 scoped_ptr<Value> root;
219 219
220 // It's ok to have a comment in a string. 220 // It's ok to have a comment in a string.
221 root.reset(base::JSONReader::Read("[\"// ok\\n /* foo */ \"]", false)); 221 root.reset(base::JSONReader::Read("[\"// ok\\n /* foo */ \"]", false));
222 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); 222 ASSERT_TRUE(root.get() && root->IsList());
223 ListValue* list = static_cast<ListValue*>(root.get()); 223 ListValue* list = static_cast<ListValue*>(root.get());
224 ASSERT_EQ(1U, list->GetSize()); 224 ASSERT_EQ(1U, list->GetSize());
225 Value* elt = NULL; 225 Value* elt = NULL;
226 ASSERT_TRUE(list->Get(0, &elt)); 226 ASSERT_TRUE(list->Get(0, &elt));
227 std::string value; 227 std::string value;
228 ASSERT_TRUE(elt && elt->GetAsString(&value)); 228 ASSERT_TRUE(elt && elt->GetAsString(&value));
229 ASSERT_EQ("// ok\n /* foo */ ", value); 229 ASSERT_EQ("// ok\n /* foo */ ", value);
230 230
231 // You can't nest comments. 231 // You can't nest comments.
232 root.reset(base::JSONReader::Read("/* /* inner */ outer */ [ 1 ]", false)); 232 root.reset(base::JSONReader::Read("/* /* inner */ outer */ [ 1 ]", false));
(...skipping 20 matching lines...) Expand all
253 original_file_path = 253 original_file_path =
254 original_file_path.Append(FILE_PATH_LITERAL("serializer_test.js")); 254 original_file_path.Append(FILE_PATH_LITERAL("serializer_test.js"));
255 255
256 ASSERT_TRUE(file_util::PathExists(original_file_path)); 256 ASSERT_TRUE(file_util::PathExists(original_file_path));
257 257
258 JSONFileValueSerializer deserializer(original_file_path); 258 JSONFileValueSerializer deserializer(original_file_path);
259 scoped_ptr<Value> root; 259 scoped_ptr<Value> root;
260 root.reset(deserializer.Deserialize(NULL, NULL)); 260 root.reset(deserializer.Deserialize(NULL, NULL));
261 261
262 ASSERT_TRUE(root.get()); 262 ASSERT_TRUE(root.get());
263 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 263 ASSERT_TRUE(root->IsDictionary());
264 264
265 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); 265 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
266 266
267 Value* null_value = NULL; 267 Value* null_value = NULL;
268 ASSERT_TRUE(root_dict->Get("null", &null_value)); 268 ASSERT_TRUE(root_dict->Get("null", &null_value));
269 ASSERT_TRUE(null_value); 269 ASSERT_TRUE(null_value);
270 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); 270 ASSERT_TRUE(null_value->IsNull());
271 271
272 bool bool_value = false; 272 bool bool_value = false;
273 ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value)); 273 ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value));
274 ASSERT_TRUE(bool_value); 274 ASSERT_TRUE(bool_value);
275 275
276 int int_value = 0; 276 int int_value = 0;
277 ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); 277 ASSERT_TRUE(root_dict->GetInteger("int", &int_value));
278 ASSERT_EQ(42, int_value); 278 ASSERT_EQ(42, int_value);
279 279
280 std::string string_value; 280 std::string string_value;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 FilePath source_file_path; 329 FilePath source_file_path;
330 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); 330 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path));
331 source_file_path = source_file_path.Append( 331 source_file_path = source_file_path.Append(
332 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); 332 FILE_PATH_LITERAL("serializer_test_nowhitespace.js"));
333 ASSERT_TRUE(file_util::PathExists(source_file_path)); 333 ASSERT_TRUE(file_util::PathExists(source_file_path));
334 JSONFileValueSerializer serializer(source_file_path); 334 JSONFileValueSerializer serializer(source_file_path);
335 scoped_ptr<Value> root; 335 scoped_ptr<Value> root;
336 root.reset(serializer.Deserialize(NULL, NULL)); 336 root.reset(serializer.Deserialize(NULL, NULL));
337 ASSERT_TRUE(root.get()); 337 ASSERT_TRUE(root.get());
338 } 338 }
OLDNEW
« no previous file with comments | « chrome/common/json_schema_validator_unittest_base.cc ('k') | chrome/common/net/gaia/gaia_oauth_client.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698