OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 #include "base/json_writer.h" | 6 #include "base/json_writer.h" |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 | 8 |
9 TEST(JSONWriterTest, Writing) { | 9 TEST(JSONWriterTest, Writing) { |
10 // Test null | 10 // Test null |
(...skipping 25 matching lines...) Expand all Loading... |
36 root = Value::CreateRealValue(0.2); | 36 root = Value::CreateRealValue(0.2); |
37 JSONWriter::Write(root, false, &output_js); | 37 JSONWriter::Write(root, false, &output_js); |
38 ASSERT_EQ("0.2", output_js); | 38 ASSERT_EQ("0.2", output_js); |
39 delete root; | 39 delete root; |
40 | 40 |
41 // Test Real values in the the range (-1, 1) must have leading zeros | 41 // Test Real values in the the range (-1, 1) must have leading zeros |
42 root = Value::CreateRealValue(-0.8); | 42 root = Value::CreateRealValue(-0.8); |
43 JSONWriter::Write(root, false, &output_js); | 43 JSONWriter::Write(root, false, &output_js); |
44 ASSERT_EQ("-0.8", output_js); | 44 ASSERT_EQ("-0.8", output_js); |
45 delete root; | 45 delete root; |
46 | 46 |
47 // Writer unittests like empty list/dict nesting, | 47 // Writer unittests like empty list/dict nesting, |
48 // list list nesting, etc. | 48 // list list nesting, etc. |
49 DictionaryValue root_dict; | 49 DictionaryValue root_dict; |
50 ListValue* list = new ListValue; | 50 ListValue* list = new ListValue; |
51 root_dict.Set(L"list", list); | 51 root_dict.Set(L"list", list); |
52 DictionaryValue* inner_dict = new DictionaryValue; | 52 DictionaryValue* inner_dict = new DictionaryValue; |
53 list->Append(inner_dict); | 53 list->Append(inner_dict); |
54 inner_dict->SetInteger(L"inner int", 10); | 54 inner_dict->SetInteger(L"inner int", 10); |
55 ListValue* inner_list = new ListValue; | 55 ListValue* inner_list = new ListValue; |
56 list->Append(inner_list); | 56 list->Append(inner_list); |
57 list->Append(Value::CreateBooleanValue(true)); | 57 list->Append(Value::CreateBooleanValue(true)); |
58 | 58 |
| 59 // Test the pretty-printer. |
59 JSONWriter::Write(&root_dict, false, &output_js); | 60 JSONWriter::Write(&root_dict, false, &output_js); |
60 ASSERT_EQ("{\"list\":[{\"inner int\":10},[],true]}", output_js); | 61 ASSERT_EQ("{\"list\":[{\"inner int\":10},[],true]}", output_js); |
61 JSONWriter::Write(&root_dict, true, &output_js); | 62 JSONWriter::Write(&root_dict, true, &output_js); |
62 ASSERT_EQ("{\r\n" | 63 // The pretty-printer uses a different newline style on Windows than on |
63 " \"list\": [ {\r\n" | 64 // other platforms. |
64 " \"inner int\": 10\r\n" | 65 #if defined(OS_WIN) |
65 " }, [ ], true ]\r\n" | 66 #define JSON_NEWLINE "\r\n" |
66 "}\r\n", | 67 #else |
| 68 #define JSON_NEWLINE "\n" |
| 69 #endif |
| 70 ASSERT_EQ("{" JSON_NEWLINE |
| 71 " \"list\": [ {" JSON_NEWLINE |
| 72 " \"inner int\": 10" JSON_NEWLINE |
| 73 " }, [ ], true ]" JSON_NEWLINE |
| 74 "}" JSON_NEWLINE, |
67 output_js); | 75 output_js); |
| 76 #undef JSON_NEWLINE |
68 } | 77 } |
OLD | NEW |