aboutsummaryrefslogtreecommitdiff
path: root/src/test/unittest_utils.cc
blob: effadaef7af0e16c07b200e76c3813d96bff3753 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright 2015 The Weave Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <weave/test/unittest_utils.h>

#include <algorithm>

#include <base/json/json_reader.h>
#include <base/json/json_writer.h>
#include <base/logging.h>

namespace weave {
namespace test {

std::unique_ptr<base::Value> CreateValue(const std::string& json) {
  std::string json2(json);
  // Convert apostrophes to double-quotes so JSONReader can parse the string.
  std::replace(json2.begin(), json2.end(), '\'', '"');
  int error = 0;
  std::string message;
  std::unique_ptr<base::Value> value{
      base::JSONReader::ReadAndReturnError(json2, base::JSON_PARSE_RFC, &error,
                                           &message)};
  CHECK(value) << "Failed to load JSON: " << message << ", " << json;
  return value;
}

std::string ValueToString(const base::Value& value) {
  std::string json;
  base::JSONWriter::WriteWithOptions(
      value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
  return json;
}

std::unique_ptr<base::DictionaryValue> CreateDictionaryValue(
    const std::string& json) {
  std::unique_ptr<base::DictionaryValue> dict =
      base::DictionaryValue::From(CreateValue(json));
  CHECK(dict) << "Value is not dictionary: " << json;
  return dict;
}

}  // namespace test
}  // namespace weave