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

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

Issue 1180693002: Update from https://crrev.com/333737 (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: rebased Created 5 years, 6 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_value_converter_unittest.cc ('k') | base/json/json_writer.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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <string> 5 #include <string>
6 6
7 #include "base/files/file_util.h" 7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/json/json_file_value_serializer.h" 9 #include "base/json/json_file_value_serializer.h"
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 str_serializer.set_pretty_print(true); 68 str_serializer.set_pretty_print(true);
69 ASSERT_TRUE(str_serializer.Serialize(value)); 69 ASSERT_TRUE(str_serializer.Serialize(value));
70 // Unify line endings between platforms. 70 // Unify line endings between platforms.
71 ReplaceSubstringsAfterOffset(&serialized_json, 0, 71 ReplaceSubstringsAfterOffset(&serialized_json, 0,
72 kWinLineEnds, kLinuxLineEnds); 72 kWinLineEnds, kLinuxLineEnds);
73 // Now compare the input with the output. 73 // Now compare the input with the output.
74 ASSERT_EQ(kProperJSON, serialized_json); 74 ASSERT_EQ(kProperJSON, serialized_json);
75 } 75 }
76 76
77 void ValidateJsonList(const std::string& json) { 77 void ValidateJsonList(const std::string& json) {
78 scoped_ptr<Value> root(JSONReader::Read(json)); 78 scoped_ptr<Value> root = JSONReader::Read(json);
79 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); 79 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST));
80 ListValue* list = static_cast<ListValue*>(root.get()); 80 ListValue* list = static_cast<ListValue*>(root.get());
81 ASSERT_EQ(1U, list->GetSize()); 81 ASSERT_EQ(1U, list->GetSize());
82 Value* elt = NULL; 82 Value* elt = NULL;
83 ASSERT_TRUE(list->Get(0, &elt)); 83 ASSERT_TRUE(list->Get(0, &elt));
84 int value = 0; 84 int value = 0;
85 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); 85 ASSERT_TRUE(elt && elt->GetAsInteger(&value));
86 ASSERT_EQ(1, value); 86 ASSERT_EQ(1, value);
87 } 87 }
88 88
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 "\xC3\xA8\xC3\xA9\xC3\xAA\xC3\xAB\xC3\xAC\xC3\xAD\xC3\xAE\xC3\xAF\xC3\xB0" 295 "\xC3\xA8\xC3\xA9\xC3\xAA\xC3\xAB\xC3\xAC\xC3\xAD\xC3\xAE\xC3\xAF\xC3\xB0"
296 "\xC3\xB1\xC3\xB2\xC3\xB3\xC3\xB4\xC3\xB5\xC3\xB6\xC3\xB7\xC3\xB8\xC3\xB9" 296 "\xC3\xB1\xC3\xB2\xC3\xB3\xC3\xB4\xC3\xB5\xC3\xB6\xC3\xB7\xC3\xB8\xC3\xB9"
297 "\xC3\xBA\xC3\xBB\xC3\xBC\xC3\xBD\xC3\xBE\xC3\xBF"; 297 "\xC3\xBA\xC3\xBB\xC3\xBC\xC3\xBD\xC3\xBE\xC3\xBF";
298 298
299 std::string expected_output = "{\"all_chars\":\"" + all_chars_expected + 299 std::string expected_output = "{\"all_chars\":\"" + all_chars_expected +
300 "\"}"; 300 "\"}";
301 // Test JSONWriter interface 301 // Test JSONWriter interface
302 std::string output_js; 302 std::string output_js;
303 DictionaryValue valueRoot; 303 DictionaryValue valueRoot;
304 valueRoot.SetString("all_chars", all_chars); 304 valueRoot.SetString("all_chars", all_chars);
305 JSONWriter::Write(&valueRoot, &output_js); 305 JSONWriter::Write(valueRoot, &output_js);
306 ASSERT_EQ(expected_output, output_js); 306 ASSERT_EQ(expected_output, output_js);
307 307
308 // Test JSONValueSerializer interface (uses JSONWriter). 308 // Test JSONValueSerializer interface (uses JSONWriter).
309 JSONStringValueSerializer serializer(&output_js); 309 JSONStringValueSerializer serializer(&output_js);
310 ASSERT_TRUE(serializer.Serialize(valueRoot)); 310 ASSERT_TRUE(serializer.Serialize(valueRoot));
311 ASSERT_EQ(expected_output, output_js); 311 ASSERT_EQ(expected_output, output_js);
312 } 312 }
313 313
314 TEST(JSONValueSerializerTest, UnicodeStrings) { 314 TEST(JSONValueSerializerTest, UnicodeStrings) {
315 // unicode string json -> escaped ascii text 315 // unicode string json -> escaped ascii text
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); 372 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]");
373 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); 373 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]");
374 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); 374 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer");
375 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); 375 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]");
376 ValidateJsonList("[ 1 /* one */ ] /* end */"); 376 ValidateJsonList("[ 1 /* one */ ] /* end */");
377 ValidateJsonList("[ 1 //// ,2\r\n ]"); 377 ValidateJsonList("[ 1 //// ,2\r\n ]");
378 378
379 scoped_ptr<Value> root; 379 scoped_ptr<Value> root;
380 380
381 // It's ok to have a comment in a string. 381 // It's ok to have a comment in a string.
382 root.reset(JSONReader::Read("[\"// ok\\n /* foo */ \"]")); 382 root.reset(JSONReader::DeprecatedRead("[\"// ok\\n /* foo */ \"]"));
383 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); 383 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST));
384 ListValue* list = static_cast<ListValue*>(root.get()); 384 ListValue* list = static_cast<ListValue*>(root.get());
385 ASSERT_EQ(1U, list->GetSize()); 385 ASSERT_EQ(1U, list->GetSize());
386 Value* elt = NULL; 386 Value* elt = NULL;
387 ASSERT_TRUE(list->Get(0, &elt)); 387 ASSERT_TRUE(list->Get(0, &elt));
388 std::string value; 388 std::string value;
389 ASSERT_TRUE(elt && elt->GetAsString(&value)); 389 ASSERT_TRUE(elt && elt->GetAsString(&value));
390 ASSERT_EQ("// ok\n /* foo */ ", value); 390 ASSERT_EQ("// ok\n /* foo */ ", value);
391 391
392 // You can't nest comments. 392 // You can't nest comments.
393 root.reset(JSONReader::Read("/* /* inner */ outer */ [ 1 ]")); 393 root.reset(JSONReader::DeprecatedRead("/* /* inner */ outer */ [ 1 ]"));
394 ASSERT_FALSE(root.get()); 394 ASSERT_FALSE(root.get());
395 395
396 // Not a open comment token. 396 // Not a open comment token.
397 root.reset(JSONReader::Read("/ * * / [1]")); 397 root.reset(JSONReader::DeprecatedRead("/ * * / [1]"));
398 ASSERT_FALSE(root.get()); 398 ASSERT_FALSE(root.get());
399 } 399 }
400 400
401 class JSONFileValueSerializerTest : public testing::Test { 401 class JSONFileValueSerializerTest : public testing::Test {
402 protected: 402 protected:
403 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } 403 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
404 404
405 base::ScopedTempDir temp_dir_; 405 base::ScopedTempDir temp_dir_;
406 }; 406 };
407 407
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 ASSERT_TRUE(PathExists(source_file_path)); 488 ASSERT_TRUE(PathExists(source_file_path));
489 JSONFileValueDeserializer deserializer(source_file_path); 489 JSONFileValueDeserializer deserializer(source_file_path);
490 scoped_ptr<Value> root; 490 scoped_ptr<Value> root;
491 root.reset(deserializer.Deserialize(NULL, NULL)); 491 root.reset(deserializer.Deserialize(NULL, NULL));
492 ASSERT_TRUE(root.get()); 492 ASSERT_TRUE(root.get());
493 } 493 }
494 494
495 } // namespace 495 } // namespace
496 496
497 } // namespace base 497 } // namespace base
OLDNEW
« no previous file with comments | « base/json/json_value_converter_unittest.cc ('k') | base/json/json_writer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698