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