| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "testing/gtest/include/gtest/gtest.h" | |
| 6 #include "base/json_writer.h" | |
| 7 #include "base/values.h" | |
| 8 | |
| 9 TEST(JSONWriterTest, Writing) { | |
| 10 // Test null | |
| 11 Value* root = Value::CreateNullValue(); | |
| 12 std::string output_js; | |
| 13 JSONWriter::Write(root, false, &output_js); | |
| 14 ASSERT_EQ("null", output_js); | |
| 15 delete root; | |
| 16 | |
| 17 // Test empty dict | |
| 18 root = new DictionaryValue; | |
| 19 JSONWriter::Write(root, false, &output_js); | |
| 20 ASSERT_EQ("{}", output_js); | |
| 21 delete root; | |
| 22 | |
| 23 // Test empty list | |
| 24 root = new ListValue; | |
| 25 JSONWriter::Write(root, false, &output_js); | |
| 26 ASSERT_EQ("[]", output_js); | |
| 27 delete root; | |
| 28 | |
| 29 // Test Real values should always have a decimal or an 'e'. | |
| 30 root = Value::CreateRealValue(1.0); | |
| 31 JSONWriter::Write(root, false, &output_js); | |
| 32 ASSERT_EQ("1.0", output_js); | |
| 33 delete root; | |
| 34 | |
| 35 // Test Real values in the the range (-1, 1) must have leading zeros | |
| 36 root = Value::CreateRealValue(0.2); | |
| 37 JSONWriter::Write(root, false, &output_js); | |
| 38 ASSERT_EQ("0.2", output_js); | |
| 39 delete root; | |
| 40 | |
| 41 // Test Real values in the the range (-1, 1) must have leading zeros | |
| 42 root = Value::CreateRealValue(-0.8); | |
| 43 JSONWriter::Write(root, false, &output_js); | |
| 44 ASSERT_EQ("-0.8", output_js); | |
| 45 delete root; | |
| 46 | |
| 47 // Writer unittests like empty list/dict nesting, | |
| 48 // list list nesting, etc. | |
| 49 DictionaryValue root_dict; | |
| 50 ListValue* list = new ListValue; | |
| 51 root_dict.Set(L"list", list); | |
| 52 DictionaryValue* inner_dict = new DictionaryValue; | |
| 53 list->Append(inner_dict); | |
| 54 inner_dict->SetInteger(L"inner int", 10); | |
| 55 ListValue* inner_list = new ListValue; | |
| 56 list->Append(inner_list); | |
| 57 list->Append(Value::CreateBooleanValue(true)); | |
| 58 | |
| 59 // Test the pretty-printer. | |
| 60 JSONWriter::Write(&root_dict, false, &output_js); | |
| 61 ASSERT_EQ("{\"list\":[{\"inner int\":10},[],true]}", output_js); | |
| 62 JSONWriter::Write(&root_dict, true, &output_js); | |
| 63 // The pretty-printer uses a different newline style on Windows than on | |
| 64 // other platforms. | |
| 65 #if defined(OS_WIN) | |
| 66 #define JSON_NEWLINE "\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, | |
| 75 output_js); | |
| 76 #undef JSON_NEWLINE | |
| 77 } | |
| OLD | NEW |