Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 5 | #include "src/commands/unittest_utils.h" |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 6 | |
Eric Caruso | 3b7b67e | 2017-01-10 15:42:06 -0800 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
Qijiang Fan | 029236e | 2021-06-24 15:17:53 +0900 | [diff] [blame^] | 9 | #include <base/check.h> |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 10 | #include <base/json/json_reader.h> |
Vitaly Buka | 11b2f23 | 2015-08-20 13:55:41 -0700 | [diff] [blame] | 11 | #include <base/json/json_writer.h> |
Vitaly Buka | 0d50107 | 2015-08-18 18:09:46 -0700 | [diff] [blame] | 12 | #include <base/logging.h> |
hscham | 82802cc | 2020-07-08 13:46:48 +0900 | [diff] [blame] | 13 | #include <base/values.h> |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 14 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 15 | namespace weave { |
Vitaly Buka | 0f6b2ec | 2015-08-20 15:35:19 -0700 | [diff] [blame] | 16 | namespace test { |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 17 | |
Vitaly Buka | fc42b31 | 2015-07-24 14:04:31 -0700 | [diff] [blame] | 18 | std::unique_ptr<base::Value> CreateValue(const std::string& json) { |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 19 | std::string json2(json); |
| 20 | // Convert apostrophes to double-quotes so JSONReader can parse the string. |
| 21 | std::replace(json2.begin(), json2.end(), '\'', '"'); |
hscham | 82802cc | 2020-07-08 13:46:48 +0900 | [diff] [blame] | 22 | auto result = base::JSONReader::ReadAndReturnValueWithError( |
| 23 | json2, base::JSON_PARSE_RFC); |
| 24 | CHECK(result.value) << "Failed to load JSON: " << result.error_message << ", " |
| 25 | << json; |
| 26 | return base::Value::ToUniquePtrValue(std::move(*result.value)); |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Vitaly Buka | 11b2f23 | 2015-08-20 13:55:41 -0700 | [diff] [blame] | 29 | std::string ValueToString(const base::Value& value) { |
| 30 | std::string json; |
| 31 | base::JSONWriter::WriteWithOptions( |
| 32 | value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
| 33 | return json; |
| 34 | } |
| 35 | |
Vitaly Buka | fc42b31 | 2015-07-24 14:04:31 -0700 | [diff] [blame] | 36 | std::unique_ptr<base::DictionaryValue> CreateDictionaryValue( |
| 37 | const std::string& json) { |
Vitaly Buka | 7c82d29 | 2015-05-03 18:08:12 -0700 | [diff] [blame] | 38 | std::unique_ptr<base::Value> value = CreateValue(json); |
Alex Vakulenko | 2a17a53 | 2015-02-24 14:51:13 -0800 | [diff] [blame] | 39 | base::DictionaryValue* dict = nullptr; |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 40 | value->GetAsDictionary(&dict); |
Vitaly Buka | 7c82d29 | 2015-05-03 18:08:12 -0700 | [diff] [blame] | 41 | CHECK(dict) << "Value is not dictionary: " << json; |
| 42 | value.release(); |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 43 | return std::unique_ptr<base::DictionaryValue>(dict); |
| 44 | } |
| 45 | |
Vitaly Buka | 0f6b2ec | 2015-08-20 15:35:19 -0700 | [diff] [blame] | 46 | } // namespace test |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 47 | } // namespace weave |