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 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 TEST(JSONWriterTest, NestedTypes) { | 54 TEST(JSONWriterTest, NestedTypes) { |
55 std::string output_js; | 55 std::string output_js; |
56 | 56 |
57 // Writer unittests like empty list/dict nesting, | 57 // Writer unittests like empty list/dict nesting, |
58 // list list nesting, etc. | 58 // list list nesting, etc. |
59 DictionaryValue root_dict; | 59 DictionaryValue root_dict; |
60 std::unique_ptr<ListValue> list(new ListValue()); | 60 std::unique_ptr<ListValue> list(new ListValue()); |
61 std::unique_ptr<DictionaryValue> inner_dict(new DictionaryValue()); | 61 std::unique_ptr<DictionaryValue> inner_dict(new DictionaryValue()); |
62 inner_dict->SetInteger("inner int", 10); | 62 inner_dict->SetInteger("inner int", 10); |
63 list->Append(std::move(inner_dict)); | 63 list->Append(std::move(inner_dict)); |
64 list->Append(WrapUnique(new ListValue())); | 64 list->Append(MakeUnique<ListValue>()); |
65 list->AppendBoolean(true); | 65 list->AppendBoolean(true); |
66 root_dict.Set("list", std::move(list)); | 66 root_dict.Set("list", std::move(list)); |
67 | 67 |
68 // Test the pretty-printer. | 68 // Test the pretty-printer. |
69 EXPECT_TRUE(JSONWriter::Write(root_dict, &output_js)); | 69 EXPECT_TRUE(JSONWriter::Write(root_dict, &output_js)); |
70 EXPECT_EQ("{\"list\":[{\"inner int\":10},[],true]}", output_js); | 70 EXPECT_EQ("{\"list\":[{\"inner int\":10},[],true]}", output_js); |
71 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 71 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
72 root_dict, JSONWriter::OPTIONS_PRETTY_PRINT, &output_js)); | 72 root_dict, JSONWriter::OPTIONS_PRETTY_PRINT, &output_js)); |
73 | 73 |
74 // 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 // Binary values should return errors unless suppressed via the | 112 // Binary values should return errors unless suppressed via the |
113 // OPTIONS_OMIT_BINARY_VALUES flag. | 113 // OPTIONS_OMIT_BINARY_VALUES flag. |
114 std::unique_ptr<Value> root(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 114 std::unique_ptr<Value> root(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
115 EXPECT_FALSE(JSONWriter::Write(*root, &output_js)); | 115 EXPECT_FALSE(JSONWriter::Write(*root, &output_js)); |
116 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 116 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
117 *root, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); | 117 *root, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); |
118 EXPECT_TRUE(output_js.empty()); | 118 EXPECT_TRUE(output_js.empty()); |
119 | 119 |
120 ListValue binary_list; | 120 ListValue binary_list; |
121 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 121 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
122 binary_list.Append(WrapUnique(new FundamentalValue(5))); | 122 binary_list.Append(MakeUnique<FundamentalValue>(5)); |
123 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 123 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
124 binary_list.Append(WrapUnique(new FundamentalValue(2))); | 124 binary_list.Append(MakeUnique<FundamentalValue>(2)); |
125 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 125 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
126 EXPECT_FALSE(JSONWriter::Write(binary_list, &output_js)); | 126 EXPECT_FALSE(JSONWriter::Write(binary_list, &output_js)); |
127 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 127 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
128 binary_list, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); | 128 binary_list, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); |
129 EXPECT_EQ("[5,2]", output_js); | 129 EXPECT_EQ("[5,2]", output_js); |
130 | 130 |
131 DictionaryValue binary_dict; | 131 DictionaryValue binary_dict; |
132 binary_dict.Set("a", BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 132 binary_dict.Set("a", BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
133 binary_dict.SetInteger("b", 5); | 133 binary_dict.SetInteger("b", 5); |
134 binary_dict.Set("c", BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 134 binary_dict.Set("c", BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
(...skipping 10 matching lines...) Expand all 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 |