blob: cddfd4ca59d94100e90e4cdd474ead23cbd56b20 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// 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.
initial.commit3f4a7322008-07-27 06:49:38 +09004
5#include "testing/gtest/include/gtest/gtest.h"
6#include "base/json_writer.h"
7#include "base/values.h"
8
9TEST(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 // Writer unittests like empty list/dict nesting,
36 // list list nesting, etc.
37 DictionaryValue root_dict;
38 ListValue* list = new ListValue;
39 root_dict.Set(L"list", list);
40 DictionaryValue* inner_dict = new DictionaryValue;
41 list->Append(inner_dict);
42 inner_dict->SetInteger(L"inner int", 10);
43 ListValue* inner_list = new ListValue;
44 list->Append(inner_list);
45 list->Append(Value::CreateBooleanValue(true));
46
47 JSONWriter::Write(&root_dict, false, &output_js);
48 ASSERT_EQ("{\"list\":[{\"inner int\":10},[],true]}", output_js);
49 JSONWriter::Write(&root_dict, true, &output_js);
50 ASSERT_EQ("{\r\n"
51 " \"list\": [ {\r\n"
52 " \"inner int\": 10\r\n"
53 " }, [ ], true ]\r\n"
54 "}\r\n",
55 output_js);
56}
57
license.botf003cfe2008-08-24 09:55:55 +090058