Home | History | Annotate | Download | only in test
      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 "base/json/json_reader.h"
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/strings/string_number_conversions.h"
     10 #include "base/values.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 namespace base {
     14 
     15 void ExpectDictBooleanValue(bool expected_value,
     16                             const DictionaryValue& value,
     17                             const std::string& key) {
     18   bool boolean_value = false;
     19   EXPECT_TRUE(value.GetBoolean(key, &boolean_value)) << key;
     20   EXPECT_EQ(expected_value, boolean_value) << key;
     21 }
     22 
     23 void ExpectDictDictionaryValue(const DictionaryValue& expected_value,
     24                                const DictionaryValue& value,
     25                                const std::string& key) {
     26   const DictionaryValue* dict_value = NULL;
     27   EXPECT_TRUE(value.GetDictionary(key, &dict_value)) << key;
     28   EXPECT_TRUE(Value::Equals(dict_value, &expected_value)) << key;
     29 }
     30 
     31 void ExpectDictIntegerValue(int expected_value,
     32                             const DictionaryValue& value,
     33                             const std::string& key) {
     34   int integer_value = 0;
     35   EXPECT_TRUE(value.GetInteger(key, &integer_value)) << key;
     36   EXPECT_EQ(expected_value, integer_value) << key;
     37 }
     38 
     39 void ExpectDictListValue(const ListValue& expected_value,
     40                          const DictionaryValue& value,
     41                          const std::string& key) {
     42   const ListValue* list_value = NULL;
     43   EXPECT_TRUE(value.GetList(key, &list_value)) << key;
     44   EXPECT_TRUE(Value::Equals(list_value, &expected_value)) << key;
     45 }
     46 
     47 void ExpectDictStringValue(const std::string& expected_value,
     48                            const DictionaryValue& value,
     49                            const std::string& key) {
     50   std::string string_value;
     51   EXPECT_TRUE(value.GetString(key, &string_value)) << key;
     52   EXPECT_EQ(expected_value, string_value) << key;
     53 }
     54 
     55 void ExpectStringValue(const std::string& expected_str,
     56                        StringValue* actual) {
     57   scoped_ptr<StringValue> scoped_actual(actual);
     58   std::string actual_str;
     59   EXPECT_TRUE(scoped_actual->GetAsString(&actual_str));
     60   EXPECT_EQ(expected_str, actual_str);
     61 }
     62 
     63 namespace test {
     64 
     65 scoped_ptr<Value> ParseJson(base::StringPiece json) {
     66   std::string error_msg;
     67   scoped_ptr<Value> result(base::JSONReader::ReadAndReturnError(
     68       json, base::JSON_ALLOW_TRAILING_COMMAS,
     69       NULL, &error_msg));
     70   if (!result) {
     71     ADD_FAILURE() << "Failed to parse \"" << json << "\": " << error_msg;
     72     result.reset(Value::CreateNullValue());
     73   }
     74   return result.Pass();
     75 }
     76 
     77 }  // namespace test
     78 }  // namespace base
     79