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 | |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 9 | #include <base/json/json_reader.h> |
Vitaly Buka | 11b2f23 | 2015-08-20 13:55:41 -0700 | [diff] [blame] | 10 | #include <base/json/json_writer.h> |
Vitaly Buka | 0d50107 | 2015-08-18 18:09:46 -0700 | [diff] [blame] | 11 | #include <base/logging.h> |
hscham | 82802cc | 2020-07-08 13:46:48 +0900 | [diff] [blame^] | 12 | #include <base/values.h> |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 13 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 14 | namespace weave { |
Vitaly Buka | 0f6b2ec | 2015-08-20 15:35:19 -0700 | [diff] [blame] | 15 | namespace test { |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 16 | |
Vitaly Buka | fc42b31 | 2015-07-24 14:04:31 -0700 | [diff] [blame] | 17 | std::unique_ptr<base::Value> CreateValue(const std::string& json) { |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 18 | std::string json2(json); |
| 19 | // Convert apostrophes to double-quotes so JSONReader can parse the string. |
| 20 | std::replace(json2.begin(), json2.end(), '\'', '"'); |
hscham | 82802cc | 2020-07-08 13:46:48 +0900 | [diff] [blame^] | 21 | auto result = base::JSONReader::ReadAndReturnValueWithError( |
| 22 | json2, base::JSON_PARSE_RFC); |
| 23 | CHECK(result.value) << "Failed to load JSON: " << result.error_message << ", " |
| 24 | << json; |
| 25 | return base::Value::ToUniquePtrValue(std::move(*result.value)); |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Vitaly Buka | 11b2f23 | 2015-08-20 13:55:41 -0700 | [diff] [blame] | 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 | |
Vitaly Buka | fc42b31 | 2015-07-24 14:04:31 -0700 | [diff] [blame] | 35 | std::unique_ptr<base::DictionaryValue> CreateDictionaryValue( |
| 36 | const std::string& json) { |
Vitaly Buka | 7c82d29 | 2015-05-03 18:08:12 -0700 | [diff] [blame] | 37 | std::unique_ptr<base::Value> value = CreateValue(json); |
Alex Vakulenko | 2a17a53 | 2015-02-24 14:51:13 -0800 | [diff] [blame] | 38 | base::DictionaryValue* dict = nullptr; |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 39 | value->GetAsDictionary(&dict); |
Vitaly Buka | 7c82d29 | 2015-05-03 18:08:12 -0700 | [diff] [blame] | 40 | CHECK(dict) << "Value is not dictionary: " << json; |
| 41 | value.release(); |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 42 | return std::unique_ptr<base::DictionaryValue>(dict); |
| 43 | } |
| 44 | |
Vitaly Buka | 0f6b2ec | 2015-08-20 15:35:19 -0700 | [diff] [blame] | 45 | } // namespace test |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 46 | } // namespace weave |