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