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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 period_dict3.SetIntegerWithoutPathExpansion("a.b", 1); | 104 period_dict3.SetIntegerWithoutPathExpansion("a.b", 1); |
105 EXPECT_TRUE(JSONWriter::Write(period_dict3, &output_js)); | 105 EXPECT_TRUE(JSONWriter::Write(period_dict3, &output_js)); |
106 EXPECT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js); | 106 EXPECT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js); |
107 } | 107 } |
108 | 108 |
109 TEST(JSONWriterTest, BinaryValues) { | 109 TEST(JSONWriterTest, BinaryValues) { |
110 std::string output_js; | 110 std::string output_js; |
111 | 111 |
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(Value::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(Value::CreateWithCopiedBuffer("asdf", 4)); |
122 binary_list.Append(MakeUnique<Value>(5)); | 122 binary_list.Append(MakeUnique<Value>(5)); |
123 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 123 binary_list.Append(Value::CreateWithCopiedBuffer("asdf", 4)); |
124 binary_list.Append(MakeUnique<Value>(2)); | 124 binary_list.Append(MakeUnique<Value>(2)); |
125 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 125 binary_list.Append(Value::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", Value::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", Value::CreateWithCopiedBuffer("asdf", 4)); |
135 binary_dict.SetInteger("d", 2); | 135 binary_dict.SetInteger("d", 2); |
136 binary_dict.Set("e", BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 136 binary_dict.Set("e", Value::CreateWithCopiedBuffer("asdf", 4)); |
137 EXPECT_FALSE(JSONWriter::Write(binary_dict, &output_js)); | 137 EXPECT_FALSE(JSONWriter::Write(binary_dict, &output_js)); |
138 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 138 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
139 binary_dict, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); | 139 binary_dict, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); |
140 EXPECT_EQ("{\"b\":5,\"d\":2}", output_js); | 140 EXPECT_EQ("{\"b\":5,\"d\":2}", output_js); |
141 } | 141 } |
142 | 142 |
143 TEST(JSONWriterTest, DoublesAsInts) { | 143 TEST(JSONWriterTest, DoublesAsInts) { |
144 std::string output_js; | 144 std::string output_js; |
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 Value double_value(1e10); | 147 Value 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 |