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