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 | |
Alex Vakulenko | 8a05beb | 2015-11-24 17:13:20 -0800 | [diff] [blame] | 5 | #include <weave/test/unittest_utils.h> |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 6 | |
| 7 | #include <base/json/json_reader.h> |
Vitaly Buka | 11b2f23 | 2015-08-20 13:55:41 -0700 | [diff] [blame] | 8 | #include <base/json/json_writer.h> |
Vitaly Buka | 0d50107 | 2015-08-18 18:09:46 -0700 | [diff] [blame] | 9 | #include <base/logging.h> |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 10 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 11 | namespace weave { |
Vitaly Buka | 0f6b2ec | 2015-08-20 15:35:19 -0700 | [diff] [blame] | 12 | namespace test { |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 13 | |
Vitaly Buka | fc42b31 | 2015-07-24 14:04:31 -0700 | [diff] [blame] | 14 | std::unique_ptr<base::Value> CreateValue(const std::string& json) { |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 15 | std::string json2(json); |
| 16 | // Convert apostrophes to double-quotes so JSONReader can parse the string. |
| 17 | std::replace(json2.begin(), json2.end(), '\'', '"'); |
Vitaly Buka | 7c82d29 | 2015-05-03 18:08:12 -0700 | [diff] [blame] | 18 | int error = 0; |
| 19 | std::string message; |
Alex Vakulenko | ae1ffbc | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 20 | std::unique_ptr<base::Value> value{ |
| 21 | base::JSONReader::ReadAndReturnError(json2, base::JSON_PARSE_RFC, &error, |
| 22 | &message) |
| 23 | .release()}; |
Vitaly Buka | 7c82d29 | 2015-05-03 18:08:12 -0700 | [diff] [blame] | 24 | CHECK(value) << "Failed to load JSON: " << message << ", " << json; |
| 25 | return 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 |