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 EXPECT_TRUE(JSONWriter::Write(Value::CreateNullValue().get(), &output_js)); |
16 EXPECT_TRUE(JSONWriter::Write(root, &output_js)); | |
17 EXPECT_EQ("null", output_js); | 16 EXPECT_EQ("null", output_js); |
18 delete root; | |
19 | 17 |
20 // Test empty dict. | 18 // Test empty dict. |
21 root = new DictionaryValue; | 19 DictionaryValue dict; |
22 EXPECT_TRUE(JSONWriter::Write(root, &output_js)); | 20 EXPECT_TRUE(JSONWriter::Write(&dict, &output_js)); |
23 EXPECT_EQ("{}", output_js); | 21 EXPECT_EQ("{}", output_js); |
24 delete root; | |
25 | 22 |
26 // Test empty list. | 23 // Test empty list. |
27 root = new ListValue; | 24 ListValue list; |
28 EXPECT_TRUE(JSONWriter::Write(root, &output_js)); | 25 EXPECT_TRUE(JSONWriter::Write(&list, &output_js)); |
29 EXPECT_EQ("[]", output_js); | 26 EXPECT_EQ("[]", output_js); |
30 delete root; | |
31 | 27 |
32 // Test integer values. | 28 // Test integer values. |
33 root = new FundamentalValue(42); | 29 FundamentalValue int_value(42); |
34 EXPECT_TRUE(JSONWriter::Write(root, &output_js)); | 30 EXPECT_TRUE(JSONWriter::Write(&int_value, &output_js)); |
35 EXPECT_EQ("42", output_js); | 31 EXPECT_EQ("42", output_js); |
36 delete root; | |
37 | 32 |
38 // Test boolean values. | 33 // Test boolean values. |
39 root = new FundamentalValue(true); | 34 FundamentalValue bool_value(true); |
40 EXPECT_TRUE(JSONWriter::Write(root, &output_js)); | 35 EXPECT_TRUE(JSONWriter::Write(&bool_value, &output_js)); |
41 EXPECT_EQ("true", output_js); | 36 EXPECT_EQ("true", output_js); |
42 delete root; | |
43 | 37 |
44 // Test Real values should always have a decimal or an 'e'. | 38 // Test Real values should always have a decimal or an 'e'. |
45 root = new FundamentalValue(1.0); | 39 FundamentalValue double_value(1.0); |
46 EXPECT_TRUE(JSONWriter::Write(root, &output_js)); | 40 EXPECT_TRUE(JSONWriter::Write(&double_value, &output_js)); |
47 EXPECT_EQ("1.0", output_js); | 41 EXPECT_EQ("1.0", output_js); |
48 delete root; | |
49 | 42 |
50 // Test Real values in the the range (-1, 1) must have leading zeros | 43 // Test Real values in the the range (-1, 1) must have leading zeros |
51 root = new FundamentalValue(0.2); | 44 FundamentalValue double_value2(0.2); |
52 EXPECT_TRUE(JSONWriter::Write(root, &output_js)); | 45 EXPECT_TRUE(JSONWriter::Write(&double_value2, &output_js)); |
53 EXPECT_EQ("0.2", output_js); | 46 EXPECT_EQ("0.2", output_js); |
54 delete root; | |
55 | 47 |
56 // Test Real values in the the range (-1, 1) must have leading zeros | 48 // Test Real values in the the range (-1, 1) must have leading zeros |
57 root = new FundamentalValue(-0.8); | 49 FundamentalValue double_value3(-0.8); |
58 EXPECT_TRUE(JSONWriter::Write(root, &output_js)); | 50 EXPECT_TRUE(JSONWriter::Write(&double_value3, &output_js)); |
59 EXPECT_EQ("-0.8", output_js); | 51 EXPECT_EQ("-0.8", output_js); |
60 delete root; | |
61 | 52 |
62 // Test String values. | 53 // Test String values. |
63 root = new StringValue("foo"); | 54 StringValue string_value("foo"); |
64 EXPECT_TRUE(JSONWriter::Write(root, &output_js)); | 55 EXPECT_TRUE(JSONWriter::Write(&string_value, &output_js)); |
65 EXPECT_EQ("\"foo\"", output_js); | 56 EXPECT_EQ("\"foo\"", output_js); |
66 delete root; | |
67 } | 57 } |
68 | 58 |
69 | |
70 TEST(JSONWriterTest, NestedTypes) { | 59 TEST(JSONWriterTest, NestedTypes) { |
71 std::string output_js; | 60 std::string output_js; |
72 | 61 |
73 // Writer unittests like empty list/dict nesting, | 62 // Writer unittests like empty list/dict nesting, |
74 // list list nesting, etc. | 63 // list list nesting, etc. |
75 DictionaryValue root_dict; | 64 DictionaryValue root_dict; |
76 ListValue* list = new ListValue; | 65 scoped_ptr<ListValue> list(new ListValue()); |
77 root_dict.Set("list", list); | 66 scoped_ptr<DictionaryValue> inner_dict(new DictionaryValue()); |
78 DictionaryValue* inner_dict = new DictionaryValue; | |
79 list->Append(inner_dict); | |
80 inner_dict->SetInteger("inner int", 10); | 67 inner_dict->SetInteger("inner int", 10); |
81 ListValue* inner_list = new ListValue; | 68 list->Append(inner_dict.Pass()); |
82 list->Append(inner_list); | 69 list->Append(make_scoped_ptr(new ListValue())); |
83 list->Append(new FundamentalValue(true)); | 70 list->AppendBoolean(true); |
| 71 root_dict.Set("list", list.Pass()); |
84 | 72 |
85 // Test the pretty-printer. | 73 // Test the pretty-printer. |
86 EXPECT_TRUE(JSONWriter::Write(&root_dict, &output_js)); | 74 EXPECT_TRUE(JSONWriter::Write(&root_dict, &output_js)); |
87 EXPECT_EQ("{\"list\":[{\"inner int\":10},[],true]}", output_js); | 75 EXPECT_EQ("{\"list\":[{\"inner int\":10},[],true]}", output_js); |
88 EXPECT_TRUE(JSONWriter::WriteWithOptions(&root_dict, | 76 EXPECT_TRUE(JSONWriter::WriteWithOptions(&root_dict, |
89 JSONWriter::OPTIONS_PRETTY_PRINT, | 77 JSONWriter::OPTIONS_PRETTY_PRINT, |
90 &output_js)); | 78 &output_js)); |
91 | 79 |
92 // The pretty-printer uses a different newline style on Windows than on | 80 // The pretty-printer uses a different newline style on Windows than on |
93 // other platforms. | 81 // other platforms. |
94 #if defined(OS_WIN) | 82 #if defined(OS_WIN) |
95 #define JSON_NEWLINE "\r\n" | 83 #define JSON_NEWLINE "\r\n" |
96 #else | 84 #else |
97 #define JSON_NEWLINE "\n" | 85 #define JSON_NEWLINE "\n" |
98 #endif | 86 #endif |
99 EXPECT_EQ("{" JSON_NEWLINE | 87 EXPECT_EQ("{" JSON_NEWLINE |
100 " \"list\": [ {" JSON_NEWLINE | 88 " \"list\": [ {" JSON_NEWLINE |
101 " \"inner int\": 10" JSON_NEWLINE | 89 " \"inner int\": 10" JSON_NEWLINE |
102 " }, [ ], true ]" JSON_NEWLINE | 90 " }, [ ], true ]" JSON_NEWLINE |
103 "}" JSON_NEWLINE, | 91 "}" JSON_NEWLINE, |
104 output_js); | 92 output_js); |
105 #undef JSON_NEWLINE | 93 #undef JSON_NEWLINE |
106 } | 94 } |
107 | 95 |
108 TEST(JSONWriterTest, KeysWithPeriods) { | 96 TEST(JSONWriterTest, KeysWithPeriods) { |
109 std::string output_js; | 97 std::string output_js; |
110 | 98 |
111 DictionaryValue period_dict; | 99 DictionaryValue period_dict; |
112 period_dict.SetWithoutPathExpansion("a.b", new FundamentalValue(3)); | 100 period_dict.SetIntegerWithoutPathExpansion("a.b", 3); |
113 period_dict.SetWithoutPathExpansion("c", new FundamentalValue(2)); | 101 period_dict.SetIntegerWithoutPathExpansion("c", 2); |
114 DictionaryValue* period_dict2 = new DictionaryValue; | 102 scoped_ptr<DictionaryValue> period_dict2(new DictionaryValue()); |
115 period_dict2->SetWithoutPathExpansion("g.h.i.j", new FundamentalValue(1)); | 103 period_dict2->SetIntegerWithoutPathExpansion("g.h.i.j", 1); |
116 period_dict.SetWithoutPathExpansion("d.e.f", period_dict2); | 104 period_dict.SetWithoutPathExpansion("d.e.f", period_dict2.Pass()); |
117 EXPECT_TRUE(JSONWriter::Write(&period_dict, &output_js)); | 105 EXPECT_TRUE(JSONWriter::Write(&period_dict, &output_js)); |
118 EXPECT_EQ("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", output_js); | 106 EXPECT_EQ("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", output_js); |
119 | 107 |
120 DictionaryValue period_dict3; | 108 DictionaryValue period_dict3; |
121 period_dict3.Set("a.b", new FundamentalValue(2)); | 109 period_dict3.SetInteger("a.b", 2); |
122 period_dict3.SetWithoutPathExpansion("a.b", new FundamentalValue(1)); | 110 period_dict3.SetIntegerWithoutPathExpansion("a.b", 1); |
123 EXPECT_TRUE(JSONWriter::Write(&period_dict3, &output_js)); | 111 EXPECT_TRUE(JSONWriter::Write(&period_dict3, &output_js)); |
124 EXPECT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js); | 112 EXPECT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js); |
125 } | 113 } |
126 | 114 |
127 TEST(JSONWriterTest, BinaryValues) { | 115 TEST(JSONWriterTest, BinaryValues) { |
128 std::string output_js; | 116 std::string output_js; |
129 | 117 |
130 // Binary values should return errors unless suppressed via the | 118 // Binary values should return errors unless suppressed via the |
131 // OPTIONS_OMIT_BINARY_VALUES flag. | 119 // OPTIONS_OMIT_BINARY_VALUES flag. |
132 Value* root = BinaryValue::CreateWithCopiedBuffer("asdf", 4); | 120 scoped_ptr<Value> root(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
133 EXPECT_FALSE(JSONWriter::Write(root, &output_js)); | 121 EXPECT_FALSE(JSONWriter::Write(root.get(), &output_js)); |
134 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 122 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
135 root, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); | 123 root.get(), JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); |
136 EXPECT_TRUE(output_js.empty()); | 124 EXPECT_TRUE(output_js.empty()); |
137 delete root; | |
138 | 125 |
139 ListValue binary_list; | 126 ListValue binary_list; |
140 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 127 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
141 binary_list.Append(new FundamentalValue(5)); | 128 binary_list.Append(make_scoped_ptr(new FundamentalValue(5))); |
142 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 129 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
143 binary_list.Append(new FundamentalValue(2)); | 130 binary_list.Append(make_scoped_ptr(new FundamentalValue(2))); |
144 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 131 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
145 EXPECT_FALSE(JSONWriter::Write(&binary_list, &output_js)); | 132 EXPECT_FALSE(JSONWriter::Write(&binary_list, &output_js)); |
146 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 133 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
147 &binary_list, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); | 134 &binary_list, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); |
148 EXPECT_EQ("[5,2]", output_js); | 135 EXPECT_EQ("[5,2]", output_js); |
149 | 136 |
150 DictionaryValue binary_dict; | 137 DictionaryValue binary_dict; |
151 binary_dict.Set("a", BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 138 binary_dict.Set( |
152 binary_dict.Set("b", new FundamentalValue(5)); | 139 "a", make_scoped_ptr(BinaryValue::CreateWithCopiedBuffer("asdf", 4))); |
153 binary_dict.Set("c", BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 140 binary_dict.SetInteger("b", 5); |
154 binary_dict.Set("d", new FundamentalValue(2)); | 141 binary_dict.Set( |
155 binary_dict.Set("e", BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 142 "c", make_scoped_ptr(BinaryValue::CreateWithCopiedBuffer("asdf", 4))); |
| 143 binary_dict.SetInteger("d", 2); |
| 144 binary_dict.Set( |
| 145 "e", make_scoped_ptr(BinaryValue::CreateWithCopiedBuffer("asdf", 4))); |
156 EXPECT_FALSE(JSONWriter::Write(&binary_dict, &output_js)); | 146 EXPECT_FALSE(JSONWriter::Write(&binary_dict, &output_js)); |
157 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 147 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
158 &binary_dict, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); | 148 &binary_dict, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js)); |
159 EXPECT_EQ("{\"b\":5,\"d\":2}", output_js); | 149 EXPECT_EQ("{\"b\":5,\"d\":2}", output_js); |
160 } | 150 } |
161 | 151 |
162 TEST(JSONWriterTest, DoublesAsInts) { | 152 TEST(JSONWriterTest, DoublesAsInts) { |
163 std::string output_js; | 153 std::string output_js; |
164 | 154 |
165 // Test allowing a double with no fractional part to be written as an integer. | 155 // Test allowing a double with no fractional part to be written as an integer. |
166 FundamentalValue double_value(1e10); | 156 FundamentalValue double_value(1e10); |
167 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 157 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
168 &double_value, | 158 &double_value, |
169 JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION, | 159 JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION, |
170 &output_js)); | 160 &output_js)); |
171 EXPECT_EQ("10000000000", output_js); | 161 EXPECT_EQ("10000000000", output_js); |
172 } | 162 } |
173 | 163 |
174 } // namespace base | 164 } // namespace base |
OLD | NEW |