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(), '\'', '"'); |
| 17 | return std::unique_ptr<base::Value>(base::JSONReader::Read(json2)); |
| 18 | } |
| 19 | |
| 20 | std::unique_ptr<base::DictionaryValue> CreateDictionaryValue(const char* json) { |
| 21 | std::string json2(json); |
| 22 | std::replace(json2.begin(), json2.end(), '\'', '"'); |
| 23 | base::Value* value = base::JSONReader::Read(json2); |
Alex Vakulenko | 2a17a53 | 2015-02-24 14:51:13 -0800 | [diff] [blame] | 24 | CHECK(value) << "Failed to load JSON: " << json2; |
| 25 | base::DictionaryValue* dict = nullptr; |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 26 | value->GetAsDictionary(&dict); |
| 27 | return std::unique_ptr<base::DictionaryValue>(dict); |
| 28 | } |
| 29 | |
| 30 | std::string ValueToString(const base::Value* value) { |
| 31 | std::string json; |
| 32 | base::JSONWriter::Write(value, &json); |
| 33 | std::replace(json.begin(), json.end(), '"', '\''); |
| 34 | return json; |
| 35 | } |
| 36 | |
| 37 | } // namespace unittests |
| 38 | } // namespace buffet |