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