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 | #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 Muhin | 0182945 | 2014-11-21 02:16:04 +0400 | [diff] [blame] | 13 | #include "buffet/commands/prop_types.h" |
| 14 | #include "buffet/commands/prop_values.h" |
| 15 | |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 16 | namespace buffet { |
| 17 | namespace 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 Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 22 | std::unique_ptr<base::Value> CreateValue(const char* json); |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 23 | |
| 24 | // Helper method to create a JSON dictionary object from a string. |
| 25 | std::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. |
| 29 | std::string ValueToString(const base::Value* value); |
| 30 | |
Anton Muhin | 0182945 | 2014-11-21 02:16:04 +0400 | [diff] [blame] | 31 | template <typename PV, typename T> std::shared_ptr<const PV> |
| 32 | make_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 | |
| 38 | inline 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 | |
| 43 | inline 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 | |
| 48 | inline 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 | |
| 53 | inline std::shared_ptr<const StringValue> |
| 54 | make_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 Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 59 | } // namespace unittests |
| 60 | } // namespace buffet |
| 61 | |
| 62 | #endif // BUFFET_COMMANDS_UNITTEST_UTILS_H_ |