| 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 |
| 7 #include "base/memory/ptr_util.h" |
| 6 #include "base/values.h" | 8 #include "base/values.h" |
| 7 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 11 |
| 10 namespace base { | 12 namespace base { |
| 11 | 13 |
| 12 TEST(JSONWriterTest, BasicTypes) { | 14 TEST(JSONWriterTest, BasicTypes) { |
| 13 std::string output_js; | 15 std::string output_js; |
| 14 | 16 |
| 15 // Test null. | 17 // Test null. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 EXPECT_TRUE(JSONWriter::Write(StringValue("foo"), &output_js)); | 50 EXPECT_TRUE(JSONWriter::Write(StringValue("foo"), &output_js)); |
| 49 EXPECT_EQ("\"foo\"", output_js); | 51 EXPECT_EQ("\"foo\"", output_js); |
| 50 } | 52 } |
| 51 | 53 |
| 52 TEST(JSONWriterTest, NestedTypes) { | 54 TEST(JSONWriterTest, NestedTypes) { |
| 53 std::string output_js; | 55 std::string output_js; |
| 54 | 56 |
| 55 // Writer unittests like empty list/dict nesting, | 57 // Writer unittests like empty list/dict nesting, |
| 56 // list list nesting, etc. | 58 // list list nesting, etc. |
| 57 DictionaryValue root_dict; | 59 DictionaryValue root_dict; |
| 58 scoped_ptr<ListValue> list(new ListValue()); | 60 std::unique_ptr<ListValue> list(new ListValue()); |
| 59 scoped_ptr<DictionaryValue> inner_dict(new DictionaryValue()); | 61 std::unique_ptr<DictionaryValue> inner_dict(new DictionaryValue()); |
| 60 inner_dict->SetInteger("inner int", 10); | 62 inner_dict->SetInteger("inner int", 10); |
| 61 list->Append(std::move(inner_dict)); | 63 list->Append(std::move(inner_dict)); |
| 62 list->Append(make_scoped_ptr(new ListValue())); | 64 list->Append(WrapUnique(new ListValue())); |
| 63 list->AppendBoolean(true); | 65 list->AppendBoolean(true); |
| 64 root_dict.Set("list", std::move(list)); | 66 root_dict.Set("list", std::move(list)); |
| 65 | 67 |
| 66 // Test the pretty-printer. | 68 // Test the pretty-printer. |
| 67 EXPECT_TRUE(JSONWriter::Write(root_dict, &output_js)); | 69 EXPECT_TRUE(JSONWriter::Write(root_dict, &output_js)); |
| 68 EXPECT_EQ("{\"list\":[{\"inner int\":10},[],true]}", output_js); | 70 EXPECT_EQ("{\"list\":[{\"inner int\":10},[],true]}", output_js); |
| 69 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 71 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
| 70 root_dict, JSONWriter::OPTIONS_PRETTY_PRINT, &output_js)); | 72 root_dict, JSONWriter::OPTIONS_PRETTY_PRINT, &output_js)); |
| 71 | 73 |
| 72 // The pretty-printer uses a different newline style on Windows than on | 74 // The pretty-printer uses a different newline style on Windows than on |
| (...skipping 11 matching lines...) Expand all Loading... |
| 84 output_js); | 86 output_js); |
| 85 #undef JSON_NEWLINE | 87 #undef JSON_NEWLINE |
| 86 } | 88 } |
| 87 | 89 |
| 88 TEST(JSONWriterTest, KeysWithPeriods) { | 90 TEST(JSONWriterTest, KeysWithPeriods) { |
| 89 std::string output_js; | 91 std::string output_js; |
| 90 | 92 |
| 91 DictionaryValue period_dict; | 93 DictionaryValue period_dict; |
| 92 period_dict.SetIntegerWithoutPathExpansion("a.b", 3); | 94 period_dict.SetIntegerWithoutPathExpansion("a.b", 3); |
| 93 period_dict.SetIntegerWithoutPathExpansion("c", 2); | 95 period_dict.SetIntegerWithoutPathExpansion("c", 2); |
| 94 scoped_ptr<DictionaryValue> period_dict2(new DictionaryValue()); | 96 std::unique_ptr<DictionaryValue> period_dict2(new DictionaryValue()); |
| 95 period_dict2->SetIntegerWithoutPathExpansion("g.h.i.j", 1); | 97 period_dict2->SetIntegerWithoutPathExpansion("g.h.i.j", 1); |
| 96 period_dict.SetWithoutPathExpansion("d.e.f", std::move(period_dict2)); | 98 period_dict.SetWithoutPathExpansion("d.e.f", std::move(period_dict2)); |
| 97 EXPECT_TRUE(JSONWriter::Write(period_dict, &output_js)); | 99 EXPECT_TRUE(JSONWriter::Write(period_dict, &output_js)); |
| 98 EXPECT_EQ("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", output_js); | 100 EXPECT_EQ("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", output_js); |
| 99 | 101 |
| 100 DictionaryValue period_dict3; | 102 DictionaryValue period_dict3; |
| 101 period_dict3.SetInteger("a.b", 2); | 103 period_dict3.SetInteger("a.b", 2); |
| 102 period_dict3.SetIntegerWithoutPathExpansion("a.b", 1); | 104 period_dict3.SetIntegerWithoutPathExpansion("a.b", 1); |
| 103 EXPECT_TRUE(JSONWriter::Write(period_dict3, &output_js)); | 105 EXPECT_TRUE(JSONWriter::Write(period_dict3, &output_js)); |
| 104 EXPECT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js); | 106 EXPECT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js); |
| 105 } | 107 } |
| 106 | 108 |
| 107 TEST(JSONWriterTest, BinaryValues) { | 109 TEST(JSONWriterTest, BinaryValues) { |
| 108 std::string output_js; | 110 std::string output_js; |
| 109 | 111 |
| 110 // Binary values should return errors unless suppressed via the | 112 // Binary values should return errors unless suppressed via the |
| 111 // OPTIONS_OMIT_BINARY_VALUES flag. | 113 // OPTIONS_OMIT_BINARY_VALUES flag. |
| 112 scoped_ptr<Value> root(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 114 std::unique_ptr<Value> root(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
| 113 EXPECT_FALSE(JSONWriter::Write(*root, &output_js)); | 115 EXPECT_FALSE(JSONWriter::Write(*root, &output_js)); |
| 114 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 116 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
| 115 *root, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); | 117 *root, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); |
| 116 EXPECT_TRUE(output_js.empty()); | 118 EXPECT_TRUE(output_js.empty()); |
| 117 | 119 |
| 118 ListValue binary_list; | 120 ListValue binary_list; |
| 119 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 121 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
| 120 binary_list.Append(make_scoped_ptr(new FundamentalValue(5))); | 122 binary_list.Append(WrapUnique(new FundamentalValue(5))); |
| 121 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 123 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
| 122 binary_list.Append(make_scoped_ptr(new FundamentalValue(2))); | 124 binary_list.Append(WrapUnique(new FundamentalValue(2))); |
| 123 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 125 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
| 124 EXPECT_FALSE(JSONWriter::Write(binary_list, &output_js)); | 126 EXPECT_FALSE(JSONWriter::Write(binary_list, &output_js)); |
| 125 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 127 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
| 126 binary_list, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); | 128 binary_list, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); |
| 127 EXPECT_EQ("[5,2]", output_js); | 129 EXPECT_EQ("[5,2]", output_js); |
| 128 | 130 |
| 129 DictionaryValue binary_dict; | 131 DictionaryValue binary_dict; |
| 130 binary_dict.Set( | 132 binary_dict.Set( |
| 131 "a", make_scoped_ptr(BinaryValue::CreateWithCopiedBuffer("asdf", 4))); | 133 "a", WrapUnique(BinaryValue::CreateWithCopiedBuffer("asdf", 4))); |
| 132 binary_dict.SetInteger("b", 5); | 134 binary_dict.SetInteger("b", 5); |
| 133 binary_dict.Set( | 135 binary_dict.Set( |
| 134 "c", make_scoped_ptr(BinaryValue::CreateWithCopiedBuffer("asdf", 4))); | 136 "c", WrapUnique(BinaryValue::CreateWithCopiedBuffer("asdf", 4))); |
| 135 binary_dict.SetInteger("d", 2); | 137 binary_dict.SetInteger("d", 2); |
| 136 binary_dict.Set( | 138 binary_dict.Set( |
| 137 "e", make_scoped_ptr(BinaryValue::CreateWithCopiedBuffer("asdf", 4))); | 139 "e", WrapUnique(BinaryValue::CreateWithCopiedBuffer("asdf", 4))); |
| 138 EXPECT_FALSE(JSONWriter::Write(binary_dict, &output_js)); | 140 EXPECT_FALSE(JSONWriter::Write(binary_dict, &output_js)); |
| 139 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 141 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
| 140 binary_dict, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); | 142 binary_dict, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); |
| 141 EXPECT_EQ("{\"b\":5,\"d\":2}", output_js); | 143 EXPECT_EQ("{\"b\":5,\"d\":2}", output_js); |
| 142 } | 144 } |
| 143 | 145 |
| 144 TEST(JSONWriterTest, DoublesAsInts) { | 146 TEST(JSONWriterTest, DoublesAsInts) { |
| 145 std::string output_js; | 147 std::string output_js; |
| 146 | 148 |
| 147 // Test allowing a double with no fractional part to be written as an integer. | 149 // Test allowing a double with no fractional part to be written as an integer. |
| 148 FundamentalValue double_value(1e10); | 150 FundamentalValue double_value(1e10); |
| 149 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 151 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
| 150 double_value, JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION, | 152 double_value, JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION, |
| 151 &output_js)); | 153 &output_js)); |
| 152 EXPECT_EQ("10000000000", output_js); | 154 EXPECT_EQ("10000000000", output_js); |
| 153 } | 155 } |
| 154 | 156 |
| 155 } // namespace base | 157 } // namespace base |
| OLD | NEW |