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 EXPECT_TRUE(JSONWriter::Write(Value::CreateNullValue().get(), &output_js)); |
16 EXPECT_TRUE(JSONWriter::Write(root.get(), &output_js)); | |
17 EXPECT_EQ("null", output_js); | 16 EXPECT_EQ("null", output_js); |
18 | 17 |
19 // Test empty dict. | 18 // Test empty dict. |
20 DictionaryValue dict; | 19 DictionaryValue dict; |
21 EXPECT_TRUE(JSONWriter::Write(&dict, &output_js)); | 20 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 ListValue list; |
26 EXPECT_TRUE(JSONWriter::Write(&list, &output_js)); | 25 EXPECT_TRUE(JSONWriter::Write(&list, &output_js)); |
(...skipping 23 matching lines...) Expand all Loading... |
50 FundamentalValue double_value3(-0.8); | 49 FundamentalValue double_value3(-0.8); |
51 EXPECT_TRUE(JSONWriter::Write(&double_value3, &output_js)); | 50 EXPECT_TRUE(JSONWriter::Write(&double_value3, &output_js)); |
52 EXPECT_EQ("-0.8", output_js); | 51 EXPECT_EQ("-0.8", output_js); |
53 | 52 |
54 // Test String values. | 53 // Test String values. |
55 StringValue string_value("foo"); | 54 StringValue string_value("foo"); |
56 EXPECT_TRUE(JSONWriter::Write(&string_value, &output_js)); | 55 EXPECT_TRUE(JSONWriter::Write(&string_value, &output_js)); |
57 EXPECT_EQ("\"foo\"", output_js); | 56 EXPECT_EQ("\"foo\"", output_js); |
58 } | 57 } |
59 | 58 |
60 | |
61 TEST(JSONWriterTest, NestedTypes) { | 59 TEST(JSONWriterTest, NestedTypes) { |
62 std::string output_js; | 60 std::string output_js; |
63 | 61 |
64 // Writer unittests like empty list/dict nesting, | 62 // Writer unittests like empty list/dict nesting, |
65 // list list nesting, etc. | 63 // list list nesting, etc. |
66 DictionaryValue root_dict; | 64 DictionaryValue root_dict; |
67 scoped_ptr<ListValue> list(new ListValue()); | 65 scoped_ptr<ListValue> list(new ListValue()); |
68 scoped_ptr<DictionaryValue> inner_dict(new DictionaryValue()); | 66 scoped_ptr<DictionaryValue> inner_dict(new DictionaryValue()); |
69 inner_dict->SetInteger("inner int", 10); | 67 inner_dict->SetInteger("inner int", 10); |
70 list->Append(inner_dict.Pass()); | 68 list->Append(inner_dict.Pass()); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 // Test allowing a double with no fractional part to be written as an integer. | 155 // Test allowing a double with no fractional part to be written as an integer. |
158 FundamentalValue double_value(1e10); | 156 FundamentalValue double_value(1e10); |
159 EXPECT_TRUE(JSONWriter::WriteWithOptions( | 157 EXPECT_TRUE(JSONWriter::WriteWithOptions( |
160 &double_value, | 158 &double_value, |
161 JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION, | 159 JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION, |
162 &output_js)); | 160 &output_js)); |
163 EXPECT_EQ("10000000000", output_js); | 161 EXPECT_EQ("10000000000", output_js); |
164 } | 162 } |
165 | 163 |
166 } // namespace base | 164 } // namespace base |
OLD | NEW |