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 |
(...skipping 11 matching lines...) Expand all Loading... |
22 ASSERT_EQ("{}", output_js); | 22 ASSERT_EQ("{}", output_js); |
23 delete root; | 23 delete root; |
24 | 24 |
25 // Test empty list | 25 // Test empty list |
26 root = new ListValue; | 26 root = new ListValue; |
27 JSONWriter::Write(root, &output_js); | 27 JSONWriter::Write(root, &output_js); |
28 ASSERT_EQ("[]", output_js); | 28 ASSERT_EQ("[]", output_js); |
29 delete root; | 29 delete root; |
30 | 30 |
31 // Test Real values should always have a decimal or an 'e'. | 31 // Test Real values should always have a decimal or an 'e'. |
32 root = Value::CreateDoubleValue(1.0); | 32 root = new FundamentalValue(1.0); |
33 JSONWriter::Write(root, &output_js); | 33 JSONWriter::Write(root, &output_js); |
34 ASSERT_EQ("1.0", output_js); | 34 ASSERT_EQ("1.0", output_js); |
35 delete root; | 35 delete root; |
36 | 36 |
37 // Test Real values in the the range (-1, 1) must have leading zeros | 37 // Test Real values in the the range (-1, 1) must have leading zeros |
38 root = Value::CreateDoubleValue(0.2); | 38 root = new FundamentalValue(0.2); |
39 JSONWriter::Write(root, &output_js); | 39 JSONWriter::Write(root, &output_js); |
40 ASSERT_EQ("0.2", output_js); | 40 ASSERT_EQ("0.2", output_js); |
41 delete root; | 41 delete root; |
42 | 42 |
43 // 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 |
44 root = Value::CreateDoubleValue(-0.8); | 44 root = new FundamentalValue(-0.8); |
45 JSONWriter::Write(root, &output_js); | 45 JSONWriter::Write(root, &output_js); |
46 ASSERT_EQ("-0.8", output_js); | 46 ASSERT_EQ("-0.8", output_js); |
47 delete root; | 47 delete root; |
48 | 48 |
49 // Writer unittests like empty list/dict nesting, | 49 // Writer unittests like empty list/dict nesting, |
50 // list list nesting, etc. | 50 // list list nesting, etc. |
51 DictionaryValue root_dict; | 51 DictionaryValue root_dict; |
52 ListValue* list = new ListValue; | 52 ListValue* list = new ListValue; |
53 root_dict.Set("list", list); | 53 root_dict.Set("list", list); |
54 DictionaryValue* inner_dict = new DictionaryValue; | 54 DictionaryValue* inner_dict = new DictionaryValue; |
55 list->Append(inner_dict); | 55 list->Append(inner_dict); |
56 inner_dict->SetInteger("inner int", 10); | 56 inner_dict->SetInteger("inner int", 10); |
57 ListValue* inner_list = new ListValue; | 57 ListValue* inner_list = new ListValue; |
58 list->Append(inner_list); | 58 list->Append(inner_list); |
59 list->Append(Value::CreateBooleanValue(true)); | 59 list->Append(new FundamentalValue(true)); |
60 | 60 |
61 // Test the pretty-printer. | 61 // Test the pretty-printer. |
62 JSONWriter::Write(&root_dict, &output_js); | 62 JSONWriter::Write(&root_dict, &output_js); |
63 ASSERT_EQ("{\"list\":[{\"inner int\":10},[],true]}", output_js); | 63 ASSERT_EQ("{\"list\":[{\"inner int\":10},[],true]}", output_js); |
64 JSONWriter::WriteWithOptions(&root_dict, JSONWriter::OPTIONS_PRETTY_PRINT, | 64 JSONWriter::WriteWithOptions(&root_dict, JSONWriter::OPTIONS_PRETTY_PRINT, |
65 &output_js); | 65 &output_js); |
66 // The pretty-printer uses a different newline style on Windows than on | 66 // The pretty-printer uses a different newline style on Windows than on |
67 // other platforms. | 67 // other platforms. |
68 #if defined(OS_WIN) | 68 #if defined(OS_WIN) |
69 #define JSON_NEWLINE "\r\n" | 69 #define JSON_NEWLINE "\r\n" |
70 #else | 70 #else |
71 #define JSON_NEWLINE "\n" | 71 #define JSON_NEWLINE "\n" |
72 #endif | 72 #endif |
73 ASSERT_EQ("{" JSON_NEWLINE | 73 ASSERT_EQ("{" JSON_NEWLINE |
74 " \"list\": [ {" JSON_NEWLINE | 74 " \"list\": [ {" JSON_NEWLINE |
75 " \"inner int\": 10" JSON_NEWLINE | 75 " \"inner int\": 10" JSON_NEWLINE |
76 " }, [ ], true ]" JSON_NEWLINE | 76 " }, [ ], true ]" JSON_NEWLINE |
77 "}" JSON_NEWLINE, | 77 "}" JSON_NEWLINE, |
78 output_js); | 78 output_js); |
79 #undef JSON_NEWLINE | 79 #undef JSON_NEWLINE |
80 | 80 |
81 // Test keys with periods | 81 // Test keys with periods |
82 DictionaryValue period_dict; | 82 DictionaryValue period_dict; |
83 period_dict.SetWithoutPathExpansion("a.b", Value::CreateIntegerValue(3)); | 83 period_dict.SetWithoutPathExpansion("a.b", new FundamentalValue(3)); |
84 period_dict.SetWithoutPathExpansion("c", Value::CreateIntegerValue(2)); | 84 period_dict.SetWithoutPathExpansion("c", new FundamentalValue(2)); |
85 DictionaryValue* period_dict2 = new DictionaryValue; | 85 DictionaryValue* period_dict2 = new DictionaryValue; |
86 period_dict2->SetWithoutPathExpansion("g.h.i.j", | 86 period_dict2->SetWithoutPathExpansion("g.h.i.j", new FundamentalValue(1)); |
87 Value::CreateIntegerValue(1)); | |
88 period_dict.SetWithoutPathExpansion("d.e.f", period_dict2); | 87 period_dict.SetWithoutPathExpansion("d.e.f", period_dict2); |
89 JSONWriter::Write(&period_dict, &output_js); | 88 JSONWriter::Write(&period_dict, &output_js); |
90 ASSERT_EQ("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", output_js); | 89 ASSERT_EQ("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", output_js); |
91 | 90 |
92 DictionaryValue period_dict3; | 91 DictionaryValue period_dict3; |
93 period_dict3.Set("a.b", Value::CreateIntegerValue(2)); | 92 period_dict3.Set("a.b", new FundamentalValue(2)); |
94 period_dict3.SetWithoutPathExpansion("a.b", Value::CreateIntegerValue(1)); | 93 period_dict3.SetWithoutPathExpansion("a.b", new FundamentalValue(1)); |
95 JSONWriter::Write(&period_dict3, &output_js); | 94 JSONWriter::Write(&period_dict3, &output_js); |
96 ASSERT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js); | 95 ASSERT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js); |
97 | 96 |
98 // Test omitting binary values. | 97 // Test omitting binary values. |
99 root = BinaryValue::CreateWithCopiedBuffer("asdf", 4); | 98 root = BinaryValue::CreateWithCopiedBuffer("asdf", 4); |
100 JSONWriter::WriteWithOptions(root, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, | 99 JSONWriter::WriteWithOptions(root, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, |
101 &output_js); | 100 &output_js); |
102 ASSERT_TRUE(output_js.empty()); | 101 ASSERT_TRUE(output_js.empty()); |
103 delete root; | 102 delete root; |
104 | 103 |
105 ListValue binary_list; | 104 ListValue binary_list; |
106 binary_list.Append(Value::CreateIntegerValue(5)); | 105 binary_list.Append(new FundamentalValue(5)); |
107 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 106 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
108 binary_list.Append(Value::CreateIntegerValue(2)); | 107 binary_list.Append(new FundamentalValue(2)); |
109 JSONWriter::WriteWithOptions(&binary_list, | 108 JSONWriter::WriteWithOptions(&binary_list, |
110 JSONWriter::OPTIONS_OMIT_BINARY_VALUES, | 109 JSONWriter::OPTIONS_OMIT_BINARY_VALUES, |
111 &output_js); | 110 &output_js); |
112 ASSERT_EQ("[5,2]", output_js); | 111 ASSERT_EQ("[5,2]", output_js); |
113 | 112 |
114 DictionaryValue binary_dict; | 113 DictionaryValue binary_dict; |
115 binary_dict.Set("a", Value::CreateIntegerValue(5)); | 114 binary_dict.Set("a", new FundamentalValue(5)); |
116 binary_dict.Set("b", BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 115 binary_dict.Set("b", BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
117 binary_dict.Set("c", Value::CreateIntegerValue(2)); | 116 binary_dict.Set("c", new FundamentalValue(2)); |
118 JSONWriter::WriteWithOptions(&binary_dict, | 117 JSONWriter::WriteWithOptions(&binary_dict, |
119 JSONWriter::OPTIONS_OMIT_BINARY_VALUES, | 118 JSONWriter::OPTIONS_OMIT_BINARY_VALUES, |
120 &output_js); | 119 &output_js); |
121 ASSERT_EQ("{\"a\":5,\"c\":2}", output_js); | 120 ASSERT_EQ("{\"a\":5,\"c\":2}", output_js); |
122 | 121 |
123 // Test allowing a double with no fractional part to be written as an integer. | 122 // Test allowing a double with no fractional part to be written as an integer. |
124 FundamentalValue double_value(1e10); | 123 FundamentalValue double_value(1e10); |
125 JSONWriter::WriteWithOptions( | 124 JSONWriter::WriteWithOptions( |
126 &double_value, | 125 &double_value, |
127 JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION, | 126 JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION, |
128 &output_js); | 127 &output_js); |
129 ASSERT_EQ("10000000000", output_js); | 128 ASSERT_EQ("10000000000", output_js); |
130 } | 129 } |
131 | 130 |
132 } // namespace base | 131 } // namespace base |
OLD | NEW |