test_util.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
1// Copyright 2013 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 "tools/json_schema_compiler/test/test_util.h"
6
7#include <string>
8
9#include "base/json/json_reader.h"
10#include "base/logging.h"
11
12namespace json_schema_compiler {
13namespace test_util {
14
15scoped_ptr<base::Value> ReadJson(const base::StringPiece& json) {
16  int error_code;
17  std::string error_msg;
18  scoped_ptr<base::Value> result(base::JSONReader::ReadAndReturnError(
19      json,
20      base::JSON_ALLOW_TRAILING_COMMAS,
21      &error_code,
22      &error_msg));
23  // CHECK not ASSERT since passing invalid |json| is a test error.
24  CHECK(result) << error_msg;
25  return result.Pass();
26}
27
28scoped_ptr<base::ListValue> List(base::Value* a) {
29  scoped_ptr<base::ListValue> list(new base::ListValue());
30  list->Append(a);
31  return list.Pass();
32}
33scoped_ptr<base::ListValue> List(base::Value* a, base::Value* b) {
34  scoped_ptr<base::ListValue> list = List(a);
35  list->Append(b);
36  return list.Pass();
37}
38scoped_ptr<base::ListValue> List(base::Value* a,
39                                 base::Value* b,
40                                 base::Value* c) {
41  scoped_ptr<base::ListValue> list = List(a, b);
42  list->Append(c);
43  return list.Pass();
44}
45
46scoped_ptr<base::DictionaryValue> Dictionary(
47    const std::string& ak, base::Value* av) {
48  scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
49  dict->SetWithoutPathExpansion(ak, av);
50  return dict.Pass();
51}
52scoped_ptr<base::DictionaryValue> Dictionary(
53    const std::string& ak, base::Value* av,
54    const std::string& bk, base::Value* bv) {
55  scoped_ptr<base::DictionaryValue> dict = Dictionary(ak, av);
56  dict->SetWithoutPathExpansion(bk, bv);
57  return dict.Pass();
58}
59scoped_ptr<base::DictionaryValue> Dictionary(
60    const std::string& ak, base::Value* av,
61    const std::string& bk, base::Value* bv,
62    const std::string& ck, base::Value* cv) {
63  scoped_ptr<base::DictionaryValue> dict = Dictionary(ak, av, bk, bv);
64  dict->SetWithoutPathExpansion(ck, cv);
65  return dict.Pass();
66}
67
68}  // namespace test_util
69}  // namespace json_schema_compiler
70