Move UpdateShortcutInfoAndIconForApp from web_app_ui to web_app
[chromium-blink-merge.git] / base / json / json_writer_unittest.cc
blobae46800dde686f9a3255f57dc8a99bc5b28fd445
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
3 // found in the LICENSE file.
5 #include "base/json/json_writer.h"
6 #include "base/values.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace base {
11 TEST(JSONWriterTest, BasicTypes) {
12 std::string output_js;
14 // Test null.
15 Value* root = Value::CreateNullValue();
16 EXPECT_TRUE(JSONWriter::Write(root, &output_js));
17 EXPECT_EQ("null", output_js);
18 delete root;
20 // Test empty dict.
21 root = new DictionaryValue;
22 EXPECT_TRUE(JSONWriter::Write(root, &output_js));
23 EXPECT_EQ("{}", output_js);
24 delete root;
26 // Test empty list.
27 root = new ListValue;
28 EXPECT_TRUE(JSONWriter::Write(root, &output_js));
29 EXPECT_EQ("[]", output_js);
30 delete root;
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;
38 // Test boolean values.
39 root = new FundamentalValue(true);
40 EXPECT_TRUE(JSONWriter::Write(root, &output_js));
41 EXPECT_EQ("true", output_js);
42 delete root;
44 // Test Real values should always have a decimal or an 'e'.
45 root = new FundamentalValue(1.0);
46 EXPECT_TRUE(JSONWriter::Write(root, &output_js));
47 EXPECT_EQ("1.0", output_js);
48 delete root;
50 // Test Real values in the the range (-1, 1) must have leading zeros
51 root = new FundamentalValue(0.2);
52 EXPECT_TRUE(JSONWriter::Write(root, &output_js));
53 EXPECT_EQ("0.2", output_js);
54 delete root;
56 // Test Real values in the the range (-1, 1) must have leading zeros
57 root = new FundamentalValue(-0.8);
58 EXPECT_TRUE(JSONWriter::Write(root, &output_js));
59 EXPECT_EQ("-0.8", output_js);
60 delete root;
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;
70 TEST(JSONWriterTest, NestedTypes) {
71 std::string output_js;
73 // Writer unittests like empty list/dict nesting,
74 // list list nesting, etc.
75 DictionaryValue root_dict;
76 ListValue* list = new ListValue;
77 root_dict.Set("list", list);
78 DictionaryValue* inner_dict = new DictionaryValue;
79 list->Append(inner_dict);
80 inner_dict->SetInteger("inner int", 10);
81 ListValue* inner_list = new ListValue;
82 list->Append(inner_list);
83 list->Append(new FundamentalValue(true));
85 // Test the pretty-printer.
86 EXPECT_TRUE(JSONWriter::Write(&root_dict, &output_js));
87 EXPECT_EQ("{\"list\":[{\"inner int\":10},[],true]}", output_js);
88 EXPECT_TRUE(JSONWriter::WriteWithOptions(&root_dict,
89 JSONWriter::OPTIONS_PRETTY_PRINT,
90 &output_js));
92 // The pretty-printer uses a different newline style on Windows than on
93 // other platforms.
94 #if defined(OS_WIN)
95 #define JSON_NEWLINE "\r\n"
96 #else
97 #define JSON_NEWLINE "\n"
98 #endif
99 EXPECT_EQ("{" JSON_NEWLINE
100 " \"list\": [ {" JSON_NEWLINE
101 " \"inner int\": 10" JSON_NEWLINE
102 " }, [ ], true ]" JSON_NEWLINE
103 "}" JSON_NEWLINE,
104 output_js);
105 #undef JSON_NEWLINE
108 TEST(JSONWriterTest, KeysWithPeriods) {
109 std::string output_js;
111 DictionaryValue period_dict;
112 period_dict.SetWithoutPathExpansion("a.b", new FundamentalValue(3));
113 period_dict.SetWithoutPathExpansion("c", new FundamentalValue(2));
114 DictionaryValue* period_dict2 = new DictionaryValue;
115 period_dict2->SetWithoutPathExpansion("g.h.i.j", new FundamentalValue(1));
116 period_dict.SetWithoutPathExpansion("d.e.f", period_dict2);
117 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);
120 DictionaryValue period_dict3;
121 period_dict3.Set("a.b", new FundamentalValue(2));
122 period_dict3.SetWithoutPathExpansion("a.b", new FundamentalValue(1));
123 EXPECT_TRUE(JSONWriter::Write(&period_dict3, &output_js));
124 EXPECT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js);
127 TEST(JSONWriterTest, BinaryValues) {
128 std::string output_js;
130 // Binary values should return errors unless suppressed via the
131 // OPTIONS_OMIT_BINARY_VALUES flag.
132 Value* root = BinaryValue::CreateWithCopiedBuffer("asdf", 4);
133 EXPECT_FALSE(JSONWriter::Write(root, &output_js));
134 EXPECT_TRUE(JSONWriter::WriteWithOptions(
135 root, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js));
136 EXPECT_TRUE(output_js.empty());
137 delete root;
139 ListValue binary_list;
140 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4));
141 binary_list.Append(new FundamentalValue(5));
142 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4));
143 binary_list.Append(new FundamentalValue(2));
144 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4));
145 EXPECT_FALSE(JSONWriter::Write(&binary_list, &output_js));
146 EXPECT_TRUE(JSONWriter::WriteWithOptions(
147 &binary_list, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js));
148 EXPECT_EQ("[5,2]", output_js);
150 DictionaryValue binary_dict;
151 binary_dict.Set("a", BinaryValue::CreateWithCopiedBuffer("asdf", 4));
152 binary_dict.Set("b", new FundamentalValue(5));
153 binary_dict.Set("c", BinaryValue::CreateWithCopiedBuffer("asdf", 4));
154 binary_dict.Set("d", new FundamentalValue(2));
155 binary_dict.Set("e", BinaryValue::CreateWithCopiedBuffer("asdf", 4));
156 EXPECT_FALSE(JSONWriter::Write(&binary_dict, &output_js));
157 EXPECT_TRUE(JSONWriter::WriteWithOptions(
158 &binary_dict, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js));
159 EXPECT_EQ("{\"b\":5,\"d\":2}", output_js);
162 TEST(JSONWriterTest, DoublesAsInts) {
163 std::string output_js;
165 // Test allowing a double with no fractional part to be written as an integer.
166 FundamentalValue double_value(1e10);
167 EXPECT_TRUE(JSONWriter::WriteWithOptions(
168 &double_value,
169 JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION,
170 &output_js));
171 EXPECT_EQ("10000000000", output_js);
174 } // namespace base