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

Side by Side Diff: base/json/json_reader_unittest.cc

Issue 1120006: detect preferences errors (Closed)
Patch Set: changes from review Created 10 years, 8 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
« no previous file with comments | « base/json/json_reader.cc ('k') | base/values.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 450 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 ASSERT_FALSE(root.get()); 461 ASSERT_FALSE(root.get());
462 root.reset(JSONReader::Read("10", false)); 462 root.reset(JSONReader::Read("10", false));
463 ASSERT_FALSE(root.get()); 463 ASSERT_FALSE(root.get());
464 root.reset(JSONReader::Read("\"root\"", false)); 464 root.reset(JSONReader::Read("\"root\"", false));
465 ASSERT_FALSE(root.get()); 465 ASSERT_FALSE(root.get());
466 } 466 }
467 467
468 TEST(JSONReaderTest, ErrorMessages) { 468 TEST(JSONReaderTest, ErrorMessages) {
469 // Error strings should not be modified in case of success. 469 // Error strings should not be modified in case of success.
470 std::string error_message; 470 std::string error_message;
471 int error_code = 0;
471 scoped_ptr<Value> root; 472 scoped_ptr<Value> root;
472 root.reset(JSONReader::ReadAndReturnError("[42]", false, &error_message)); 473 root.reset(JSONReader::ReadAndReturnError("[42]", false,
474 &error_code, &error_message));
473 EXPECT_TRUE(error_message.empty()); 475 EXPECT_TRUE(error_message.empty());
476 EXPECT_EQ(0, error_code);
474 477
475 // Test line and column counting 478 // Test line and column counting
476 const char* big_json = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]"; 479 const char* big_json = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]";
477 // error here --------------------------------^ 480 // error here --------------------------------^
478 root.reset(JSONReader::ReadAndReturnError(big_json, false, &error_message)); 481 root.reset(JSONReader::ReadAndReturnError(big_json, false,
482 &error_code, &error_message));
479 EXPECT_FALSE(root.get()); 483 EXPECT_FALSE(root.get());
480 EXPECT_EQ(JSONReader::FormatErrorMessage(5, 9, JSONReader::kSyntaxError), 484 EXPECT_EQ(JSONReader::FormatErrorMessage(5, 9, JSONReader::kSyntaxError),
481 error_message); 485 error_message);
486 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
482 487
483 // Test each of the error conditions 488 // Test each of the error conditions
484 root.reset(JSONReader::ReadAndReturnError("{},{}", false, &error_message)); 489 root.reset(JSONReader::ReadAndReturnError("{},{}", false,
490 &error_code, &error_message));
485 EXPECT_FALSE(root.get()); 491 EXPECT_FALSE(root.get());
486 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 3, 492 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 3,
487 JSONReader::kUnexpectedDataAfterRoot), error_message); 493 JSONReader::kUnexpectedDataAfterRoot), error_message);
494 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, error_code);
488 495
489 std::string nested_json; 496 std::string nested_json;
490 for (int i = 0; i < 101; ++i) { 497 for (int i = 0; i < 101; ++i) {
491 nested_json.insert(nested_json.begin(), '['); 498 nested_json.insert(nested_json.begin(), '[');
492 nested_json.append(1, ']'); 499 nested_json.append(1, ']');
493 } 500 }
494 root.reset(JSONReader::ReadAndReturnError(nested_json, false, 501 root.reset(JSONReader::ReadAndReturnError(nested_json, false,
495 &error_message)); 502 &error_code, &error_message));
496 EXPECT_FALSE(root.get()); 503 EXPECT_FALSE(root.get());
497 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 101, JSONReader::kTooMuchNesting), 504 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 101, JSONReader::kTooMuchNesting),
498 error_message); 505 error_message);
506 EXPECT_EQ(JSONReader::JSON_TOO_MUCH_NESTING, error_code);
499 507
500 root.reset(JSONReader::ReadAndReturnError("42", false, &error_message)); 508 root.reset(JSONReader::ReadAndReturnError("42", false,
509 &error_code, &error_message));
501 EXPECT_FALSE(root.get()); 510 EXPECT_FALSE(root.get());
502 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 1, 511 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 1,
503 JSONReader::kBadRootElementType), error_message); 512 JSONReader::kBadRootElementType), error_message);
513 EXPECT_EQ(JSONReader::JSON_BAD_ROOT_ELEMENT_TYPE, error_code);
504 514
505 root.reset(JSONReader::ReadAndReturnError("[1,]", false, &error_message)); 515 root.reset(JSONReader::ReadAndReturnError("[1,]", false,
516 &error_code, &error_message));
506 EXPECT_FALSE(root.get()); 517 EXPECT_FALSE(root.get());
507 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 4, JSONReader::kTrailingComma), 518 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 4, JSONReader::kTrailingComma),
508 error_message); 519 error_message);
520 EXPECT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
509 521
510 root.reset(JSONReader::ReadAndReturnError("{foo:\"bar\"}", false, 522 root.reset(JSONReader::ReadAndReturnError("{foo:\"bar\"}", false,
511 &error_message)); 523 &error_code, &error_message));
512 EXPECT_FALSE(root.get()); 524 EXPECT_FALSE(root.get());
513 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2, 525 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2,
514 JSONReader::kUnquotedDictionaryKey), error_message); 526 JSONReader::kUnquotedDictionaryKey), error_message);
527 EXPECT_EQ(JSONReader::JSON_UNQUOTED_DICTIONARY_KEY, error_code);
515 528
516 root.reset(JSONReader::ReadAndReturnError("{\"foo\":\"bar\",}", false, 529 root.reset(JSONReader::ReadAndReturnError("{\"foo\":\"bar\",}", false,
517 &error_message)); 530 &error_code, &error_message));
518 EXPECT_FALSE(root.get()); 531 EXPECT_FALSE(root.get());
519 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 14, JSONReader::kTrailingComma), 532 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 14, JSONReader::kTrailingComma),
520 error_message); 533 error_message);
521 534
522 root.reset(JSONReader::ReadAndReturnError("[nu]", false, &error_message)); 535 root.reset(JSONReader::ReadAndReturnError("[nu]", false,
536 &error_code, &error_message));
523 EXPECT_FALSE(root.get()); 537 EXPECT_FALSE(root.get());
524 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2, JSONReader::kSyntaxError), 538 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2, JSONReader::kSyntaxError),
525 error_message); 539 error_message);
540 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
526 541
527 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\xq\"]", false, 542 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\xq\"]", false,
528 &error_message)); 543 &error_code, &error_message));
529 EXPECT_FALSE(root.get()); 544 EXPECT_FALSE(root.get());
530 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), 545 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
531 error_message); 546 error_message);
547 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
532 548
533 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\uq\"]", false, 549 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\uq\"]", false,
534 &error_message)); 550 &error_code, &error_message));
535 EXPECT_FALSE(root.get()); 551 EXPECT_FALSE(root.get());
536 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), 552 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
537 error_message); 553 error_message);
554 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
538 555
539 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false, 556 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false,
540 &error_message)); 557 &error_code, &error_message));
541 EXPECT_FALSE(root.get()); 558 EXPECT_FALSE(root.get());
542 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), 559 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
543 error_message); 560 error_message);
544 561 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
545 } 562 }
546 563
547 } // namespace base 564 } // namespace base
OLDNEW
« no previous file with comments | « base/json/json_reader.cc ('k') | base/values.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698