OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 #include "base/json/json_reader.h" | 6 #include "base/json/json_reader.h" |
7 #include "base/scoped_ptr.h" | 7 #include "base/scoped_ptr.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 | 10 |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 root.reset(JSONReader::Read("{}", false)); | 295 root.reset(JSONReader::Read("{}", false)); |
296 ASSERT_TRUE(root.get()); | 296 ASSERT_TRUE(root.get()); |
297 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 297 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
298 | 298 |
299 root.reset(JSONReader::Read( | 299 root.reset(JSONReader::Read( |
300 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }", false)); | 300 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }", false)); |
301 ASSERT_TRUE(root.get()); | 301 ASSERT_TRUE(root.get()); |
302 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 302 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
303 DictionaryValue* dict_val = static_cast<DictionaryValue*>(root.get()); | 303 DictionaryValue* dict_val = static_cast<DictionaryValue*>(root.get()); |
304 real_val = 0.0; | 304 real_val = 0.0; |
305 ASSERT_TRUE(dict_val->GetReal(L"number", &real_val)); | 305 ASSERT_TRUE(dict_val->GetReal("number", &real_val)); |
306 ASSERT_DOUBLE_EQ(9.87654321, real_val); | 306 ASSERT_DOUBLE_EQ(9.87654321, real_val); |
307 Value* null_val = NULL; | 307 Value* null_val = NULL; |
308 ASSERT_TRUE(dict_val->Get(L"null", &null_val)); | 308 ASSERT_TRUE(dict_val->Get("null", &null_val)); |
309 ASSERT_TRUE(null_val->IsType(Value::TYPE_NULL)); | 309 ASSERT_TRUE(null_val->IsType(Value::TYPE_NULL)); |
310 str_val.clear(); | 310 str_val.clear(); |
311 ASSERT_TRUE(dict_val->GetString(L"S", &str_val)); | 311 ASSERT_TRUE(dict_val->GetString(L"S", &str_val)); |
312 ASSERT_EQ(L"str", str_val); | 312 ASSERT_EQ(L"str", str_val); |
313 | 313 |
314 root2.reset(JSONReader::Read( | 314 root2.reset(JSONReader::Read( |
315 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", true)); | 315 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", true)); |
316 ASSERT_TRUE(root2.get()); | 316 ASSERT_TRUE(root2.get()); |
317 EXPECT_TRUE(root->Equals(root2.get())); | 317 EXPECT_TRUE(root->Equals(root2.get())); |
318 | 318 |
(...skipping 16 matching lines...) Expand all Loading... |
335 ASSERT_TRUE(root2.get()); | 335 ASSERT_TRUE(root2.get()); |
336 EXPECT_TRUE(root->Equals(root2.get())); | 336 EXPECT_TRUE(root->Equals(root2.get())); |
337 | 337 |
338 // Test nesting | 338 // Test nesting |
339 root.reset(JSONReader::Read( | 339 root.reset(JSONReader::Read( |
340 "{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}", false)); | 340 "{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}", false)); |
341 ASSERT_TRUE(root.get()); | 341 ASSERT_TRUE(root.get()); |
342 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 342 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
343 dict_val = static_cast<DictionaryValue*>(root.get()); | 343 dict_val = static_cast<DictionaryValue*>(root.get()); |
344 DictionaryValue* inner_dict = NULL; | 344 DictionaryValue* inner_dict = NULL; |
345 ASSERT_TRUE(dict_val->GetDictionary(L"inner", &inner_dict)); | 345 ASSERT_TRUE(dict_val->GetDictionary("inner", &inner_dict)); |
346 ListValue* inner_array = NULL; | 346 ListValue* inner_array = NULL; |
347 ASSERT_TRUE(inner_dict->GetList(L"array", &inner_array)); | 347 ASSERT_TRUE(inner_dict->GetList("array", &inner_array)); |
348 ASSERT_EQ(1U, inner_array->GetSize()); | 348 ASSERT_EQ(1U, inner_array->GetSize()); |
349 bool_value = true; | 349 bool_value = true; |
350 ASSERT_TRUE(dict_val->GetBoolean(L"false", &bool_value)); | 350 ASSERT_TRUE(dict_val->GetBoolean("false", &bool_value)); |
351 ASSERT_FALSE(bool_value); | 351 ASSERT_FALSE(bool_value); |
352 inner_dict = NULL; | 352 inner_dict = NULL; |
353 ASSERT_TRUE(dict_val->GetDictionary(L"d", &inner_dict)); | 353 ASSERT_TRUE(dict_val->GetDictionary("d", &inner_dict)); |
354 | 354 |
355 root2.reset(JSONReader::Read( | 355 root2.reset(JSONReader::Read( |
356 "{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}", true)); | 356 "{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}", true)); |
357 EXPECT_TRUE(root->Equals(root2.get())); | 357 EXPECT_TRUE(root->Equals(root2.get())); |
358 | 358 |
359 // Test keys with periods | 359 // Test keys with periods |
360 root.reset(JSONReader::Read( | 360 root.reset(JSONReader::Read( |
361 "{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", false)); | 361 "{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", false)); |
362 ASSERT_TRUE(root.get()); | 362 ASSERT_TRUE(root.get()); |
363 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 363 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
364 dict_val = static_cast<DictionaryValue*>(root.get()); | 364 dict_val = static_cast<DictionaryValue*>(root.get()); |
365 int integer_value = 0; | 365 int integer_value = 0; |
366 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion(L"a.b", &integer_value)); | 366 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("a.b", &integer_value)); |
367 EXPECT_EQ(3, integer_value); | 367 EXPECT_EQ(3, integer_value); |
368 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion(L"c", &integer_value)); | 368 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("c", &integer_value)); |
369 EXPECT_EQ(2, integer_value); | 369 EXPECT_EQ(2, integer_value); |
370 inner_dict = NULL; | 370 inner_dict = NULL; |
371 ASSERT_TRUE(dict_val->GetDictionaryWithoutPathExpansion(L"d.e.f", | 371 ASSERT_TRUE(dict_val->GetDictionaryWithoutPathExpansion("d.e.f", |
372 &inner_dict)); | 372 &inner_dict)); |
373 ASSERT_EQ(1U, inner_dict->size()); | 373 ASSERT_EQ(1U, inner_dict->size()); |
374 EXPECT_TRUE(inner_dict->GetIntegerWithoutPathExpansion(L"g.h.i.j", | 374 EXPECT_TRUE(inner_dict->GetIntegerWithoutPathExpansion("g.h.i.j", |
375 &integer_value)); | 375 &integer_value)); |
376 EXPECT_EQ(1, integer_value); | 376 EXPECT_EQ(1, integer_value); |
377 | 377 |
378 root.reset(JSONReader::Read("{\"a\":{\"b\":2},\"a.b\":1}", false)); | 378 root.reset(JSONReader::Read("{\"a\":{\"b\":2},\"a.b\":1}", false)); |
379 ASSERT_TRUE(root.get()); | 379 ASSERT_TRUE(root.get()); |
380 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 380 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
381 dict_val = static_cast<DictionaryValue*>(root.get()); | 381 dict_val = static_cast<DictionaryValue*>(root.get()); |
382 EXPECT_TRUE(dict_val->GetInteger(L"a.b", &integer_value)); | 382 EXPECT_TRUE(dict_val->GetInteger("a.b", &integer_value)); |
383 EXPECT_EQ(2, integer_value); | 383 EXPECT_EQ(2, integer_value); |
384 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion(L"a.b", &integer_value)); | 384 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("a.b", &integer_value)); |
385 EXPECT_EQ(1, integer_value); | 385 EXPECT_EQ(1, integer_value); |
386 | 386 |
387 // Invalid, no closing brace | 387 // Invalid, no closing brace |
388 root.reset(JSONReader::Read("{\"a\": true", false)); | 388 root.reset(JSONReader::Read("{\"a\": true", false)); |
389 ASSERT_FALSE(root.get()); | 389 ASSERT_FALSE(root.get()); |
390 | 390 |
391 // Invalid, keys must be quoted | 391 // Invalid, keys must be quoted |
392 root.reset(JSONReader::Read("{foo:true}", false)); | 392 root.reset(JSONReader::Read("{foo:true}", false)); |
393 ASSERT_FALSE(root.get()); | 393 ASSERT_FALSE(root.get()); |
394 | 394 |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 | 555 |
556 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false, | 556 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false, |
557 &error_code, &error_message)); | 557 &error_code, &error_message)); |
558 EXPECT_FALSE(root.get()); | 558 EXPECT_FALSE(root.get()); |
559 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), | 559 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), |
560 error_message); | 560 error_message); |
561 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); | 561 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); |
562 } | 562 } |
563 | 563 |
564 } // namespace base | 564 } // namespace base |
OLD | NEW |