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> |
Vitaly Buka | 7c82d29 | 2015-05-03 18:08:12 -0700 | [diff] [blame] | 12 | #include <gtest/gtest.h> |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 13 | |
Anton Muhin | 0182945 | 2014-11-21 02:16:04 +0400 | [diff] [blame] | 14 | #include "buffet/commands/prop_types.h" |
| 15 | #include "buffet/commands/prop_values.h" |
| 16 | |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 17 | namespace buffet { |
| 18 | namespace unittests { |
| 19 | |
| 20 | // Helper method to create base::Value from a string as a smart pointer. |
| 21 | // For ease of definition in C++ code, double-quotes in the source definition |
| 22 | // are replaced with apostrophes. |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 23 | std::unique_ptr<base::Value> CreateValue(const char* json); |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 24 | |
| 25 | // Helper method to create a JSON dictionary object from a string. |
| 26 | std::unique_ptr<base::DictionaryValue> CreateDictionaryValue(const char* json); |
| 27 | |
Vitaly Buka | 7c82d29 | 2015-05-03 18:08:12 -0700 | [diff] [blame] | 28 | inline bool IsEqualValue(const base::Value& val1, const base::Value& val2) { |
| 29 | return val1.Equals(&val2); |
| 30 | } |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 31 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 32 | template <typename PropVal, typename T> |
| 33 | std::unique_ptr<const PropVal> make_prop_value(const T& value) { |
| 34 | std::unique_ptr<PropVal> result{ |
| 35 | new PropVal{PropType::Create(GetValueType<T>())}}; |
Anton Muhin | 0182945 | 2014-11-21 02:16:04 +0400 | [diff] [blame] | 36 | result->SetValue(value); |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 37 | return std::move(result); |
Anton Muhin | 0182945 | 2014-11-21 02:16:04 +0400 | [diff] [blame] | 38 | } |
| 39 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 40 | inline std::unique_ptr<const IntValue> make_int_prop_value(int value) { |
| 41 | return make_prop_value<IntValue, int>(value); |
Anton Muhin | 0182945 | 2014-11-21 02:16:04 +0400 | [diff] [blame] | 42 | } |
| 43 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 44 | inline std::unique_ptr<const DoubleValue> make_double_prop_value(double value) { |
| 45 | return make_prop_value<DoubleValue, double>(value); |
Anton Muhin | 0182945 | 2014-11-21 02:16:04 +0400 | [diff] [blame] | 46 | } |
| 47 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 48 | inline std::unique_ptr<const BooleanValue> make_bool_prop_value(bool value) { |
| 49 | return make_prop_value<BooleanValue, bool>(value); |
Anton Muhin | 0182945 | 2014-11-21 02:16:04 +0400 | [diff] [blame] | 50 | } |
| 51 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 52 | inline std::unique_ptr<const StringValue> |
Anton Muhin | 0182945 | 2014-11-21 02:16:04 +0400 | [diff] [blame] | 53 | make_string_prop_value(const std::string& value) { |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 54 | return make_prop_value<StringValue, std::string>(value); |
Anton Muhin | 0182945 | 2014-11-21 02:16:04 +0400 | [diff] [blame] | 55 | } |
| 56 | |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 57 | } // namespace unittests |
| 58 | } // namespace buffet |
| 59 | |
Vitaly Buka | 7c82d29 | 2015-05-03 18:08:12 -0700 | [diff] [blame] | 60 | #define EXPECT_JSON_EQ(expected, actual) \ |
| 61 | EXPECT_PRED2(buffet::unittests::IsEqualValue, \ |
| 62 | *buffet::unittests::CreateValue(expected), actual) |
| 63 | |
Alex Vakulenko | 6201d2d | 2014-07-16 14:46:48 -0700 | [diff] [blame] | 64 | #endif // BUFFET_COMMANDS_UNITTEST_UTILS_H_ |