Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS 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 "buffet/commands/unittest_utils.h" |
| 6 | |
| 7 | #include <base/json/json_reader.h> |
| 8 | #include <base/json/json_writer.h> |
| 9 | |
| 10 | namespace buffet { |
| 11 | namespace unittests { |
| 12 | |
| 13 | std::unique_ptr<base::Value> CreateValue(const char* json) { |
| 14 | std::string json2(json); |
| 15 | // Convert apostrophes to double-quotes so JSONReader can parse the string. |
| 16 | std::replace(json2.begin(), json2.end(), '\'', '"'); |
Vitaly Buka | 7c82d29 | 2015-05-03 18:08:12 -0700 | [diff] [blame] | 17 | int error = 0; |
| 18 | std::string message; |
| 19 | std::unique_ptr<base::Value> value{base::JSONReader::ReadAndReturnError( |
| 20 | json2, base::JSON_PARSE_RFC, &error, &message)}; |
| 21 | CHECK(value) << "Failed to load JSON: " << message << ", " << json; |
| 22 | return value; |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | std::unique_ptr<base::DictionaryValue> CreateDictionaryValue(const char* json) { |
Vitaly Buka | 7c82d29 | 2015-05-03 18:08:12 -0700 | [diff] [blame] | 26 | std::unique_ptr<base::Value> value = CreateValue(json); |
Alex Vakulenko | 2a17a53 | 2015-02-24 14:51:13 -0800 | [diff] [blame] | 27 | base::DictionaryValue* dict = nullptr; |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 28 | value->GetAsDictionary(&dict); |
Vitaly Buka | 7c82d29 | 2015-05-03 18:08:12 -0700 | [diff] [blame] | 29 | CHECK(dict) << "Value is not dictionary: " << json; |
| 30 | value.release(); |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 31 | return std::unique_ptr<base::DictionaryValue>(dict); |
| 32 | } |
| 33 | |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 34 | } // namespace unittests |
| 35 | } // namespace buffet |