• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Weave 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 <weave/test/unittest_utils.h>
6 
7 #include <base/json/json_reader.h>
8 #include <base/json/json_writer.h>
9 #include <base/logging.h>
10 
11 namespace weave {
12 namespace test {
13 
CreateValue(const std::string & json)14 std::unique_ptr<base::Value> CreateValue(const std::string& json) {
15   std::string json2(json);
16   // Convert apostrophes to double-quotes so JSONReader can parse the string.
17   std::replace(json2.begin(), json2.end(), '\'', '"');
18   int error = 0;
19   std::string message;
20   std::unique_ptr<base::Value> value{
21       base::JSONReader::ReadAndReturnError(json2, base::JSON_PARSE_RFC, &error,
22                                            &message)
23           .release()};
24   CHECK(value) << "Failed to load JSON: " << message << ", " << json;
25   return value;
26 }
27 
ValueToString(const base::Value & value)28 std::string ValueToString(const base::Value& value) {
29   std::string json;
30   base::JSONWriter::WriteWithOptions(
31       value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
32   return json;
33 }
34 
CreateDictionaryValue(const std::string & json)35 std::unique_ptr<base::DictionaryValue> CreateDictionaryValue(
36     const std::string& json) {
37   std::unique_ptr<base::Value> value = CreateValue(json);
38   base::DictionaryValue* dict = nullptr;
39   value->GetAsDictionary(&dict);
40   CHECK(dict) << "Value is not dictionary: " << json;
41   value.release();
42   return std::unique_ptr<base::DictionaryValue>(dict);
43 }
44 
45 }  // namespace test
46 }  // namespace weave
47