blob: 577405ad9895114233a8e5e93136758b1961057a [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#ifndef BUFFET_COMMANDS_UNITTEST_UTILS_H_
6#define BUFFET_COMMANDS_UNITTEST_UTILS_H_
7
8#include <memory>
9#include <string>
10
11#include <base/values.h>
12
Anton Muhin01829452014-11-21 02:16:04 +040013#include "buffet/commands/prop_types.h"
14#include "buffet/commands/prop_values.h"
15
Alex Vakulenko6201d2d2014-07-16 14:46:48 -070016namespace buffet {
17namespace unittests {
18
19// Helper method to create base::Value from a string as a smart pointer.
20// For ease of definition in C++ code, double-quotes in the source definition
21// are replaced with apostrophes.
Alex Vakulenko7c36b672014-07-16 14:50:58 -070022std::unique_ptr<base::Value> CreateValue(const char* json);
Alex Vakulenko6201d2d2014-07-16 14:46:48 -070023
24// Helper method to create a JSON dictionary object from a string.
25std::unique_ptr<base::DictionaryValue> CreateDictionaryValue(const char* json);
26
27// Converts a JSON value to a string. It also converts double-quotes to
28// apostrophes for easy comparisons in C++ source code.
29std::string ValueToString(const base::Value* value);
30
Anton Muhin01829452014-11-21 02:16:04 +040031template <typename PV, typename T> std::shared_ptr<const PV>
32make_prop_value(const PropType* type, const T& value) {
33 auto result = std::make_shared<PV>(type);
34 result->SetValue(value);
35 return result;
36}
37
38inline std::shared_ptr<const IntValue> make_int_prop_value(int value) {
39 static const PropType* int_prop_type = new IntPropType();
40 return make_prop_value<IntValue, int>(int_prop_type, value);
41}
42
43inline std::shared_ptr<const DoubleValue> make_double_prop_value(double value) {
44 static const PropType* double_prop_type = new DoublePropType();
45 return make_prop_value<DoubleValue, double>(double_prop_type, value);
46}
47
48inline std::shared_ptr<const BooleanValue> make_bool_prop_value(bool value) {
49 static const PropType* boolean_prop_type = new BooleanPropType();
50 return make_prop_value<BooleanValue, bool>(boolean_prop_type, value);
51}
52
53inline std::shared_ptr<const StringValue>
54make_string_prop_value(const std::string& value) {
55 static const PropType* string_prop_type = new StringPropType();
56 return make_prop_value<StringValue, std::string>(string_prop_type, value);
57}
58
Alex Vakulenko6201d2d2014-07-16 14:46:48 -070059} // namespace unittests
60} // namespace buffet
61
62#endif // BUFFET_COMMANDS_UNITTEST_UTILS_H_