| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/basictypes.h" |
| 5 #include "base/json/json_writer.h" | 6 #include "base/json/json_writer.h" |
| 6 #include "base/values.h" | 7 #include "base/values.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 9 |
| 9 namespace base { | 10 namespace base { |
| 10 | 11 |
| 11 TEST(JSONWriterTest, Writing) { | 12 TEST(JSONWriterTest, Writing) { |
| 12 // Test null | 13 // Test null |
| 13 Value* root = Value::CreateNullValue(); | 14 Value* root = Value::CreateNullValue(); |
| 14 std::string output_js; | 15 std::string output_js; |
| 15 JSONWriter::Write(root, false, &output_js); | 16 JSONWriter::Write(root, false, &output_js); |
| 16 ASSERT_EQ("null", output_js); | 17 ASSERT_EQ("null", output_js); |
| 17 delete root; | 18 delete root; |
| 18 | 19 |
| 19 // Test empty dict | 20 // Test empty dict |
| 20 root = new DictionaryValue; | 21 root = new DictionaryValue; |
| 21 JSONWriter::Write(root, false, &output_js); | 22 JSONWriter::Write(root, false, &output_js); |
| 22 ASSERT_EQ("{}", output_js); | 23 ASSERT_EQ("{}", output_js); |
| 23 delete root; | 24 delete root; |
| 24 | 25 |
| 25 // Test empty list | 26 // Test empty list |
| 26 root = new ListValue; | 27 root = new ListValue; |
| 27 JSONWriter::Write(root, false, &output_js); | 28 JSONWriter::Write(root, false, &output_js); |
| 28 ASSERT_EQ("[]", output_js); | 29 ASSERT_EQ("[]", output_js); |
| 29 delete root; | 30 delete root; |
| 30 | 31 |
| 32 // Test 64-bit integers. These should not have an 'e'. |
| 33 root = Value::CreateInteger64Value(kint64max); |
| 34 JSONWriter::Write(root, false, &output_js); |
| 35 ASSERT_EQ("9223372036854775807", output_js); |
| 36 delete root; |
| 37 |
| 31 // Test Real values should always have a decimal or an 'e'. | 38 // Test Real values should always have a decimal or an 'e'. |
| 32 root = Value::CreateDoubleValue(1.0); | 39 root = Value::CreateDoubleValue(1.0); |
| 33 JSONWriter::Write(root, false, &output_js); | 40 JSONWriter::Write(root, false, &output_js); |
| 34 ASSERT_EQ("1.0", output_js); | 41 ASSERT_EQ("1.0", output_js); |
| 35 delete root; | 42 delete root; |
| 36 | 43 |
| 37 // Test Real values in the the range (-1, 1) must have leading zeros | 44 // Test Real values in the the range (-1, 1) must have leading zeros |
| 38 root = Value::CreateDoubleValue(0.2); | 45 root = Value::CreateDoubleValue(0.2); |
| 39 JSONWriter::Write(root, false, &output_js); | 46 JSONWriter::Write(root, false, &output_js); |
| 40 ASSERT_EQ("0.2", output_js); | 47 ASSERT_EQ("0.2", output_js); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 binary_dict.Set("a", Value::CreateIntegerValue(5)); | 122 binary_dict.Set("a", Value::CreateIntegerValue(5)); |
| 116 binary_dict.Set("b", BinaryValue::CreateWithCopiedBuffer("asdf", 4)); | 123 binary_dict.Set("b", BinaryValue::CreateWithCopiedBuffer("asdf", 4)); |
| 117 binary_dict.Set("c", Value::CreateIntegerValue(2)); | 124 binary_dict.Set("c", Value::CreateIntegerValue(2)); |
| 118 JSONWriter::WriteWithOptions(&binary_dict, false, | 125 JSONWriter::WriteWithOptions(&binary_dict, false, |
| 119 JSONWriter::OPTIONS_OMIT_BINARY_VALUES, | 126 JSONWriter::OPTIONS_OMIT_BINARY_VALUES, |
| 120 &output_js); | 127 &output_js); |
| 121 ASSERT_EQ("{\"a\":5,\"c\":2}", output_js); | 128 ASSERT_EQ("{\"a\":5,\"c\":2}", output_js); |
| 122 } | 129 } |
| 123 | 130 |
| 124 } // namespace base | 131 } // namespace base |
| OLD | NEW |