| OLD | NEW |
| 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 "base/json/json_writer.h" | 5 #include "base/json/json_writer.h" |
| 6 #include "base/values.h" | 6 #include "base/values.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 8 |
| 9 namespace base { | 9 namespace base { |
| 10 | 10 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 | 50 |
| 51 TEST(JSONWriterTest, NestedTypes) { | 51 TEST(JSONWriterTest, NestedTypes) { |
| 52 std::string output_js; | 52 std::string output_js; |
| 53 | 53 |
| 54 // Writer unittests like empty list/dict nesting, | 54 // Writer unittests like empty list/dict nesting, |
| 55 // list list nesting, etc. | 55 // list list nesting, etc. |
| 56 DictionaryValue root_dict; | 56 DictionaryValue root_dict; |
| 57 scoped_ptr<ListValue> list(new ListValue()); | 57 scoped_ptr<ListValue> list(new ListValue()); |
| 58 scoped_ptr<DictionaryValue> inner_dict(new DictionaryValue()); | 58 scoped_ptr<DictionaryValue> inner_dict(new DictionaryValue()); |
| 59 inner_dict->SetInteger("inner int", 10); | 59 inner_dict->SetInteger("inner int", 10); |
| 60 list->Append(inner_dict.Pass()); | 60 list->Append(std::move(inner_dict)); |
| 61 list->Append(make_scoped_ptr(new ListValue())); | 61 list->Append(make_scoped_ptr(new ListValue())); |
| 62 list->AppendBoolean(true); | 62 list->AppendBoolean(true); |
| 63 root_dict.Set("list", list.Pass()); | 63 root_dict.Set("list", std::move(list)); |
| 64 | 64 |
| 65 // Test the pretty-printer. | 65 // Test the pretty-printer. |
| 66 EXPECT_TRUE(JSONWriter::Write(root_dict, &output_js)); | 66 EXPECT_TRUE(JSONWriter::Write(root_dict, &output_js)); |
| 67 EXPECT_EQ("{\"list\":[{\"inner int\":10},[],true]}", output_js); | 67 EXPECT_EQ("{\"list\":[{\"inner int\":10},[],true]}", output_js); |
| 68 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 68 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
| 69 root_dict, JSONWriter::OPTIONS_PRETTY_PRINT, &output_js)); | 69 root_dict, JSONWriter::OPTIONS_PRETTY_PRINT, &output_js)); |
| 70 | 70 |
| 71 // The pretty-printer uses a different newline style on Windows than on | 71 // The pretty-printer uses a different newline style on Windows than on |
| 72 // other platforms. | 72 // other platforms. |
| 73 #if defined(OS_WIN) | 73 #if defined(OS_WIN) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 85 } | 85 } |
| 86 | 86 |
| 87 TEST(JSONWriterTest, KeysWithPeriods) { | 87 TEST(JSONWriterTest, KeysWithPeriods) { |
| 88 std::string output_js; | 88 std::string output_js; |
| 89 | 89 |
| 90 DictionaryValue period_dict; | 90 DictionaryValue period_dict; |
| 91 period_dict.SetIntegerWithoutPathExpansion("a.b", 3); | 91 period_dict.SetIntegerWithoutPathExpansion("a.b", 3); |
| 92 period_dict.SetIntegerWithoutPathExpansion("c", 2); | 92 period_dict.SetIntegerWithoutPathExpansion("c", 2); |
| 93 scoped_ptr<DictionaryValue> period_dict2(new DictionaryValue()); | 93 scoped_ptr<DictionaryValue> period_dict2(new DictionaryValue()); |
| 94 period_dict2->SetIntegerWithoutPathExpansion("g.h.i.j", 1); | 94 period_dict2->SetIntegerWithoutPathExpansion("g.h.i.j", 1); |
| 95 period_dict.SetWithoutPathExpansion("d.e.f", period_dict2.Pass()); | 95 period_dict.SetWithoutPathExpansion("d.e.f", std::move(period_dict2)); |
| 96 EXPECT_TRUE(JSONWriter::Write(period_dict, &output_js)); | 96 EXPECT_TRUE(JSONWriter::Write(period_dict, &output_js)); |
| 97 EXPECT_EQ("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", output_js); | 97 EXPECT_EQ("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", output_js); |
| 98 | 98 |
| 99 DictionaryValue period_dict3; | 99 DictionaryValue period_dict3; |
| 100 period_dict3.SetInteger("a.b", 2); | 100 period_dict3.SetInteger("a.b", 2); |
| 101 period_dict3.SetIntegerWithoutPathExpansion("a.b", 1); | 101 period_dict3.SetIntegerWithoutPathExpansion("a.b", 1); |
| 102 EXPECT_TRUE(JSONWriter::Write(period_dict3, &output_js)); | 102 EXPECT_TRUE(JSONWriter::Write(period_dict3, &output_js)); |
| 103 EXPECT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js); | 103 EXPECT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js); |
| 104 } | 104 } |
| 105 | 105 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 | 145 |
| 146 // Test allowing a double with no fractional part to be written as an integer. | 146 // Test allowing a double with no fractional part to be written as an integer. |
| 147 FundamentalValue double_value(1e10); | 147 FundamentalValue double_value(1e10); |
| 148 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 148 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
| 149 double_value, JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION, | 149 double_value, JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION, |
| 150 &output_js)); | 150 &output_js)); |
| 151 EXPECT_EQ("10000000000", output_js); | 151 EXPECT_EQ("10000000000", output_js); |
| 152 } | 152 } |
| 153 | 153 |
| 154 } // namespace base | 154 } // namespace base |
| OLD | NEW |