blob: be4d4e7c74c4c4410c921ce09e4772924f8750e8 [file] [log] [blame]
Alex Vakulenko6201d2d2014-07-16 14:46:48 -07001// 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
10namespace buffet {
11namespace unittests {
12
13std::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 Buka7c82d292015-05-03 18:08:12 -070017 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 Vakulenko6201d2d2014-07-16 14:46:48 -070023}
24
25std::unique_ptr<base::DictionaryValue> CreateDictionaryValue(const char* json) {
Vitaly Buka7c82d292015-05-03 18:08:12 -070026 std::unique_ptr<base::Value> value = CreateValue(json);
Alex Vakulenko2a17a532015-02-24 14:51:13 -080027 base::DictionaryValue* dict = nullptr;
Alex Vakulenko6201d2d2014-07-16 14:46:48 -070028 value->GetAsDictionary(&dict);
Vitaly Buka7c82d292015-05-03 18:08:12 -070029 CHECK(dict) << "Value is not dictionary: " << json;
30 value.release();
Alex Vakulenko6201d2d2014-07-16 14:46:48 -070031 return std::unique_ptr<base::DictionaryValue>(dict);
32}
33
Alex Vakulenko6201d2d2014-07-16 14:46:48 -070034} // namespace unittests
35} // namespace buffet