blob: 3a9dcc12a74fb4c6a1bcd4ea15c2aeeff76cd00b [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>
Vitaly Buka7c82d292015-05-03 18:08:12 -070012#include <gtest/gtest.h>
Alex Vakulenko6201d2d2014-07-16 14:46:48 -070013
Anton Muhin01829452014-11-21 02:16:04 +040014#include "buffet/commands/prop_types.h"
15#include "buffet/commands/prop_values.h"
16
Alex Vakulenko6201d2d2014-07-16 14:46:48 -070017namespace buffet {
18namespace 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 Vakulenko7c36b672014-07-16 14:50:58 -070023std::unique_ptr<base::Value> CreateValue(const char* json);
Alex Vakulenko6201d2d2014-07-16 14:46:48 -070024
25// Helper method to create a JSON dictionary object from a string.
26std::unique_ptr<base::DictionaryValue> CreateDictionaryValue(const char* json);
27
Vitaly Buka7c82d292015-05-03 18:08:12 -070028inline bool IsEqualValue(const base::Value& val1, const base::Value& val2) {
29 return val1.Equals(&val2);
30}
Alex Vakulenko6201d2d2014-07-16 14:46:48 -070031
Alex Vakulenko5ef75792015-03-19 15:50:44 -070032template <typename PropVal, typename T>
33std::unique_ptr<const PropVal> make_prop_value(const T& value) {
34 std::unique_ptr<PropVal> result{
35 new PropVal{PropType::Create(GetValueType<T>())}};
Anton Muhin01829452014-11-21 02:16:04 +040036 result->SetValue(value);
Alex Vakulenko5ef75792015-03-19 15:50:44 -070037 return std::move(result);
Anton Muhin01829452014-11-21 02:16:04 +040038}
39
Alex Vakulenko5ef75792015-03-19 15:50:44 -070040inline std::unique_ptr<const IntValue> make_int_prop_value(int value) {
41 return make_prop_value<IntValue, int>(value);
Anton Muhin01829452014-11-21 02:16:04 +040042}
43
Alex Vakulenko5ef75792015-03-19 15:50:44 -070044inline std::unique_ptr<const DoubleValue> make_double_prop_value(double value) {
45 return make_prop_value<DoubleValue, double>(value);
Anton Muhin01829452014-11-21 02:16:04 +040046}
47
Alex Vakulenko5ef75792015-03-19 15:50:44 -070048inline std::unique_ptr<const BooleanValue> make_bool_prop_value(bool value) {
49 return make_prop_value<BooleanValue, bool>(value);
Anton Muhin01829452014-11-21 02:16:04 +040050}
51
Alex Vakulenko5ef75792015-03-19 15:50:44 -070052inline std::unique_ptr<const StringValue>
Anton Muhin01829452014-11-21 02:16:04 +040053make_string_prop_value(const std::string& value) {
Alex Vakulenko5ef75792015-03-19 15:50:44 -070054 return make_prop_value<StringValue, std::string>(value);
Anton Muhin01829452014-11-21 02:16:04 +040055}
56
Alex Vakulenko6201d2d2014-07-16 14:46:48 -070057} // namespace unittests
58} // namespace buffet
59
Vitaly Buka7c82d292015-05-03 18:08:12 -070060#define EXPECT_JSON_EQ(expected, actual) \
61 EXPECT_PRED2(buffet::unittests::IsEqualValue, \
62 *buffet::unittests::CreateValue(expected), actual)
63
Alex Vakulenko6201d2d2014-07-16 14:46:48 -070064#endif // BUFFET_COMMANDS_UNITTEST_UTILS_H_