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 |
11 TEST(JSONWriterTest, BasicTypes) { | 11 TEST(JSONWriterTest, BasicTypes) { |
12 std::string output_js; | 12 std::string output_js; |
13 | 13 |
14 // Test null. | 14 // Test null. |
15 Value* root = Value::CreateNullValue(); | 15 scoped_ptr<Value> root = make_scoped_ptr(Value::CreateNullValue()); |
Nico
2015/05/11 18:16:08
Do you need make_scoped_ptr here? Doesn't just
danakj
2015/05/11 18:17:51
Ya I can/should use that constructor instead, done
| |
16 EXPECT_TRUE(JSONWriter::Write(root, &output_js)); | 16 EXPECT_TRUE(JSONWriter::Write(root.get(), &output_js)); |
17 EXPECT_EQ("null", output_js); | 17 EXPECT_EQ("null", output_js); |
18 delete root; | |
19 | 18 |
20 // Test empty dict. | 19 // Test empty dict. |
21 root = new DictionaryValue; | 20 root = make_scoped_ptr(new DictionaryValue); |
22 EXPECT_TRUE(JSONWriter::Write(root, &output_js)); | 21 EXPECT_TRUE(JSONWriter::Write(root.get(), &output_js)); |
23 EXPECT_EQ("{}", output_js); | 22 EXPECT_EQ("{}", output_js); |
24 delete root; | |
25 | 23 |
26 // Test empty list. | 24 // Test empty list. |
27 root = new ListValue; | 25 root = make_scoped_ptr(new ListValue); |
28 EXPECT_TRUE(JSONWriter::Write(root, &output_js)); | 26 EXPECT_TRUE(JSONWriter::Write(root.get(), &output_js)); |
29 EXPECT_EQ("[]", output_js); | 27 EXPECT_EQ("[]", output_js); |
30 delete root; | |
31 | 28 |
32 // Test integer values. | 29 // Test integer values. |
33 root = new FundamentalValue(42); | 30 root = make_scoped_ptr(new FundamentalValue(42)); |
34 EXPECT_TRUE(JSONWriter::Write(root, &output_js)); | 31 EXPECT_TRUE(JSONWriter::Write(root.get(), &output_js)); |
35 EXPECT_EQ("42", output_js); | 32 EXPECT_EQ("42", output_js); |
36 delete root; | |
37 | 33 |
38 // Test boolean values. | 34 // Test boolean values. |
39 root = new FundamentalValue(true); | 35 root = make_scoped_ptr(new FundamentalValue(true)); |
40 EXPECT_TRUE(JSONWriter::Write(root, &output_js)); | 36 EXPECT_TRUE(JSONWriter::Write(root.get(), &output_js)); |
41 EXPECT_EQ("true", output_js); | 37 EXPECT_EQ("true", output_js); |
42 delete root; | |
43 | 38 |
44 // Test Real values should always have a decimal or an 'e'. | 39 // Test Real values should always have a decimal or an 'e'. |
45 root = new FundamentalValue(1.0); | 40 root = make_scoped_ptr(new FundamentalValue(1.0)); |
46 EXPECT_TRUE(JSONWriter::Write(root, &output_js)); | 41 EXPECT_TRUE(JSONWriter::Write(root.get(), &output_js)); |
47 EXPECT_EQ("1.0", output_js); | 42 EXPECT_EQ("1.0", output_js); |
48 delete root; | |
49 | 43 |
50 // Test Real values in the the range (-1, 1) must have leading zeros | 44 // Test Real values in the the range (-1, 1) must have leading zeros |
51 root = new FundamentalValue(0.2); | 45 root = make_scoped_ptr(new FundamentalValue(0.2)); |
52 EXPECT_TRUE(JSONWriter::Write(root, &output_js)); | 46 EXPECT_TRUE(JSONWriter::Write(root.get(), &output_js)); |
53 EXPECT_EQ("0.2", output_js); | 47 EXPECT_EQ("0.2", output_js); |
54 delete root; | |
55 | 48 |
56 // Test Real values in the the range (-1, 1) must have leading zeros | 49 // Test Real values in the the range (-1, 1) must have leading zeros |
57 root = new FundamentalValue(-0.8); | 50 root = make_scoped_ptr(new FundamentalValue(-0.8)); |
58 EXPECT_TRUE(JSONWriter::Write(root, &output_js)); | 51 EXPECT_TRUE(JSONWriter::Write(root.get(), &output_js)); |
59 EXPECT_EQ("-0.8", output_js); | 52 EXPECT_EQ("-0.8", output_js); |
60 delete root; | |
61 | 53 |
62 // Test String values. | 54 // Test String values. |
63 root = new StringValue("foo"); | 55 root = make_scoped_ptr(new StringValue("foo")); |
64 EXPECT_TRUE(JSONWriter::Write(root, &output_js)); | 56 EXPECT_TRUE(JSONWriter::Write(root.get(), &output_js)); |
65 EXPECT_EQ("\"foo\"", output_js); | 57 EXPECT_EQ("\"foo\"", output_js); |
66 delete root; | |
67 } | 58 } |
68 | 59 |
69 | 60 |
70 TEST(JSONWriterTest, NestedTypes) { | 61 TEST(JSONWriterTest, NestedTypes) { |
71 std::string output_js; | 62 std::string output_js; |
72 | 63 |
73 // Writer unittests like empty list/dict nesting, | 64 // Writer unittests like empty list/dict nesting, |
74 // list list nesting, etc. | 65 // list list nesting, etc. |
75 DictionaryValue root_dict; | 66 DictionaryValue root_dict; |
76 scoped_ptr<ListValue> list(new ListValue()); | 67 scoped_ptr<ListValue> list(new ListValue()); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 period_dict3.SetIntegerWithoutPathExpansion("a.b", 1); | 112 period_dict3.SetIntegerWithoutPathExpansion("a.b", 1); |
122 EXPECT_TRUE(JSONWriter::Write(&period_dict3, &output_js)); | 113 EXPECT_TRUE(JSONWriter::Write(&period_dict3, &output_js)); |
123 EXPECT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js); | 114 EXPECT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js); |
124 } | 115 } |
125 | 116 |
126 TEST(JSONWriterTest, BinaryValues) { | 117 TEST(JSONWriterTest, BinaryValues) { |
127 std::string output_js; | 118 std::string output_js; |
128 | 119 |
129 // Binary values should return errors unless suppressed via the | 120 // Binary values should return errors unless suppressed via the |
130 // OPTIONS_OMIT_BINARY_VALUES flag. | 121 // OPTIONS_OMIT_BINARY_VALUES flag. |
131 Value* root = BinaryValue::CreateWithCopiedBuffer("asdf", 4); | 122 scoped_ptr<Value> root = |
132 EXPECT_FALSE(JSONWriter::Write(root, &output_js)); | 123 make_scoped_ptr(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
124 EXPECT_FALSE(JSONWriter::Write(root.get(), &output_js)); | |
133 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 125 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
134 root, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); | 126 root.get(), JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); |
135 EXPECT_TRUE(output_js.empty()); | 127 EXPECT_TRUE(output_js.empty()); |
136 delete root; | |
137 | 128 |
138 ListValue binary_list; | 129 ListValue binary_list; |
139 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 130 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
140 binary_list.Append(new FundamentalValue(5)); | 131 binary_list.Append(make_scoped_ptr(new FundamentalValue(5))); |
141 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 132 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
142 binary_list.Append(new FundamentalValue(2)); | 133 binary_list.Append(make_scoped_ptr(new FundamentalValue(2))); |
143 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 134 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
144 EXPECT_FALSE(JSONWriter::Write(&binary_list, &output_js)); | 135 EXPECT_FALSE(JSONWriter::Write(&binary_list, &output_js)); |
145 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 136 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
146 &binary_list, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); | 137 &binary_list, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); |
147 EXPECT_EQ("[5,2]", output_js); | 138 EXPECT_EQ("[5,2]", output_js); |
148 | 139 |
149 DictionaryValue binary_dict; | 140 DictionaryValue binary_dict; |
150 binary_dict.Set( | 141 binary_dict.Set( |
151 "a", make_scoped_ptr(BinaryValue::CreateWithCopiedBuffer("asdf", 4))); | 142 "a", make_scoped_ptr(BinaryValue::CreateWithCopiedBuffer("asdf", 4))); |
152 binary_dict.SetInteger("b", 5); | 143 binary_dict.SetInteger("b", 5); |
(...skipping 14 matching lines...) Expand all Loading... | |
167 // Test allowing a double with no fractional part to be written as an integer. | 158 // Test allowing a double with no fractional part to be written as an integer. |
168 FundamentalValue double_value(1e10); | 159 FundamentalValue double_value(1e10); |
169 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 160 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
170 &double_value, | 161 &double_value, |
171 JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION, | 162 JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION, |
172 &output_js)); | 163 &output_js)); |
173 EXPECT_EQ("10000000000", output_js); | 164 EXPECT_EQ("10000000000", output_js); |
174 } | 165 } |
175 | 166 |
176 } // namespace base | 167 } // namespace base |
OLD | NEW |