xref: /aosp_15_r20/external/libchrome/base/test/values_test_util.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1 // Copyright (c) 2012 The Chromium 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 "base/test/values_test_util.h"
6 
7 #include <memory>
8 
9 #include "base/json/json_reader.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/values.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 namespace base {
16 
ExpectDictBooleanValue(bool expected_value,const DictionaryValue & value,const std::string & key)17 void ExpectDictBooleanValue(bool expected_value,
18                             const DictionaryValue& value,
19                             const std::string& key) {
20   bool boolean_value = false;
21   EXPECT_TRUE(value.GetBoolean(key, &boolean_value)) << key;
22   EXPECT_EQ(expected_value, boolean_value) << key;
23 }
24 
ExpectDictDictionaryValue(const DictionaryValue & expected_value,const DictionaryValue & value,const std::string & key)25 void ExpectDictDictionaryValue(const DictionaryValue& expected_value,
26                                const DictionaryValue& value,
27                                const std::string& key) {
28   const DictionaryValue* dict_value = nullptr;
29   EXPECT_TRUE(value.GetDictionary(key, &dict_value)) << key;
30   EXPECT_EQ(expected_value, *dict_value) << key;
31 }
32 
ExpectDictIntegerValue(int expected_value,const DictionaryValue & value,const std::string & key)33 void ExpectDictIntegerValue(int expected_value,
34                             const DictionaryValue& value,
35                             const std::string& key) {
36   int integer_value = 0;
37   EXPECT_TRUE(value.GetInteger(key, &integer_value)) << key;
38   EXPECT_EQ(expected_value, integer_value) << key;
39 }
40 
ExpectDictListValue(const ListValue & expected_value,const DictionaryValue & value,const std::string & key)41 void ExpectDictListValue(const ListValue& expected_value,
42                          const DictionaryValue& value,
43                          const std::string& key) {
44   const ListValue* list_value = nullptr;
45   EXPECT_TRUE(value.GetList(key, &list_value)) << key;
46   EXPECT_EQ(expected_value, *list_value) << key;
47 }
48 
ExpectDictStringValue(const std::string & expected_value,const DictionaryValue & value,const std::string & key)49 void ExpectDictStringValue(const std::string& expected_value,
50                            const DictionaryValue& value,
51                            const std::string& key) {
52   std::string string_value;
53   EXPECT_TRUE(value.GetString(key, &string_value)) << key;
54   EXPECT_EQ(expected_value, string_value) << key;
55 }
56 
ExpectStringValue(const std::string & expected_str,const Value & actual)57 void ExpectStringValue(const std::string& expected_str, const Value& actual) {
58   EXPECT_EQ(Value::Type::STRING, actual.type());
59   EXPECT_EQ(expected_str, actual.GetString());
60 }
61 
62 namespace test {
63 
ParseJson(base::StringPiece json)64 std::unique_ptr<Value> ParseJson(base::StringPiece json) {
65   std::string error_msg;
66   std::unique_ptr<Value> result = base::JSONReader::ReadAndReturnError(
67       json, base::JSON_ALLOW_TRAILING_COMMAS, nullptr, &error_msg);
68   if (!result) {
69     ADD_FAILURE() << "Failed to parse \"" << json << "\": " << error_msg;
70     result = std::make_unique<Value>();
71   }
72   return result;
73 }
74 
75 }  // namespace test
76 }  // namespace base
77