Home | History | Annotate | Download | only in brillo
      1 // Copyright 2015 The Android Open Source Project
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //      http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 #include <brillo/value_conversion.h>
     16 
     17 #include <limits>
     18 #include <memory>
     19 #include <string>
     20 #include <vector>
     21 
     22 #include <base/json/json_reader.h>
     23 #include <base/json/json_writer.h>
     24 #include <gtest/gtest.h>
     25 
     26 namespace brillo {
     27 
     28 namespace {
     29 
     30 std::unique_ptr<base::Value> ParseValue(std::string json) {
     31   std::replace(json.begin(), json.end(), '\'', '"');
     32   std::string message;
     33   std::unique_ptr<base::Value> value{
     34       base::JSONReader::ReadAndReturnError(json, base::JSON_PARSE_RFC,nullptr,
     35                                            &message)
     36           .release()};
     37   CHECK(value) << "Failed to load JSON: " << message << ", " << json;
     38   return value;
     39 }
     40 
     41 inline bool IsEqualValue(const base::Value& val1, const base::Value& val2) {
     42   return val1.Equals(&val2);
     43 }
     44 
     45 #define EXPECT_JSON_EQ(expected, actual) \
     46   EXPECT_PRED2(IsEqualValue, *ParseValue(expected), actual)
     47 
     48 }  // namespace
     49 
     50 TEST(ValueConversionTest, FromValueInt) {
     51   int actual;
     52   EXPECT_TRUE(FromValue(*ParseValue("123"), &actual));
     53   EXPECT_EQ(123, actual);
     54 
     55   EXPECT_TRUE(FromValue(*ParseValue("-123"), &actual));
     56   EXPECT_EQ(-123, actual);
     57 
     58   EXPECT_FALSE(FromValue(*ParseValue("true"), &actual));
     59 }
     60 
     61 TEST(ValueConversionTest, FromValueBool) {
     62   bool actual;
     63   EXPECT_TRUE(FromValue(*ParseValue("false"), &actual));
     64   EXPECT_FALSE(actual);
     65 
     66   EXPECT_TRUE(FromValue(*ParseValue("true"), &actual));
     67   EXPECT_TRUE(actual);
     68 
     69   EXPECT_FALSE(FromValue(*ParseValue("0"), &actual));
     70   EXPECT_FALSE(FromValue(*ParseValue("1"), &actual));
     71 }
     72 
     73 TEST(ValueConversionTest, FromValueDouble) {
     74   double actual;
     75   EXPECT_TRUE(FromValue(*ParseValue("12.5"), &actual));
     76   EXPECT_DOUBLE_EQ(12.5, actual);
     77 
     78   EXPECT_TRUE(FromValue(*ParseValue("-0.1"), &actual));
     79   EXPECT_DOUBLE_EQ(-0.1, actual);
     80 
     81   EXPECT_TRUE(FromValue(*ParseValue("17"), &actual));
     82   EXPECT_DOUBLE_EQ(17.0, actual);
     83 
     84   EXPECT_FALSE(FromValue(*ParseValue("'1.0'"), &actual));
     85 }
     86 
     87 TEST(ValueConversionTest, FromValueString) {
     88   std::string actual;
     89   EXPECT_TRUE(FromValue(*ParseValue("'foo'"), &actual));
     90   EXPECT_EQ("foo", actual);
     91 
     92   EXPECT_TRUE(FromValue(*ParseValue("'bar'"), &actual));
     93   EXPECT_EQ("bar", actual);
     94 
     95   EXPECT_TRUE(FromValue(*ParseValue("''"), &actual));
     96   EXPECT_TRUE(actual.empty());
     97 
     98   EXPECT_FALSE(FromValue(*ParseValue("1"), &actual));
     99 }
    100 
    101 TEST(ValueConversionTest, FromValueListValue) {
    102   const base::ListValue* list = nullptr;
    103   auto in_value = ParseValue("[1, 2, 'foo']");
    104   EXPECT_TRUE(FromValue(*in_value, &list));
    105   EXPECT_JSON_EQ("[1, 2, 'foo']", *list);
    106 }
    107 
    108 TEST(ValueConversionTest, FromValueDictValue) {
    109   const base::DictionaryValue* dict = nullptr;
    110   auto in_value = ParseValue("{'foo':'bar','baz': 1}");
    111   EXPECT_TRUE(FromValue(*in_value, &dict));
    112   EXPECT_JSON_EQ("{'foo':'bar','baz': 1}", *dict);
    113 }
    114 
    115 TEST(ValueConversionTest, FromValueListValueUniquePtr) {
    116   std::unique_ptr<base::ListValue> list;
    117   EXPECT_TRUE(FromValue(*ParseValue("[1, 2, 'bar']"), &list));
    118   EXPECT_JSON_EQ("[1, 2, 'bar']", *list);
    119 }
    120 
    121 TEST(ValueConversionTest, FromValueDictValueUniquePtr) {
    122   std::unique_ptr<base::DictionaryValue> dict;
    123   EXPECT_TRUE(FromValue(*ParseValue("{'foo':'bar','baz': 1}"), &dict));
    124   EXPECT_JSON_EQ("{'foo':'bar','baz': 1}", *dict);
    125 }
    126 
    127 TEST(ValueConversionTest, FromValueVectorOfInt) {
    128   std::vector<int> actual;
    129   EXPECT_TRUE(FromValue(*ParseValue("[1, 2, 3, 4]"), &actual));
    130   EXPECT_EQ((std::vector<int>{1, 2, 3, 4}), actual);
    131 
    132   EXPECT_TRUE(FromValue(*ParseValue("[]"), &actual));
    133   EXPECT_TRUE(actual.empty());
    134 
    135   EXPECT_FALSE(FromValue(*ParseValue("[1, 2, 3, '4']"), &actual));
    136 }
    137 
    138 TEST(ValueConversionTest, FromValueVectorOfBool) {
    139   std::vector<bool> actual;
    140   EXPECT_TRUE(FromValue(*ParseValue("[true, true, false]"), &actual));
    141   EXPECT_EQ((std::vector<bool>{true, true, false}), actual);
    142 
    143   EXPECT_TRUE(FromValue(*ParseValue("[]"), &actual));
    144   EXPECT_TRUE(actual.empty());
    145 
    146   EXPECT_FALSE(FromValue(*ParseValue("[true, 0]"), &actual));
    147 }
    148 
    149 TEST(ValueConversionTest, FromValueVectorOfDouble) {
    150   std::vector<double> actual;
    151   EXPECT_TRUE(FromValue(*ParseValue("[1, 2.0, 6.5, -11.2]"), &actual));
    152   EXPECT_EQ((std::vector<double>{1.0, 2.0, 6.5, -11.2}), actual);
    153 
    154   EXPECT_TRUE(FromValue(*ParseValue("[]"), &actual));
    155   EXPECT_TRUE(actual.empty());
    156 
    157   EXPECT_FALSE(FromValue(*ParseValue("['s']"), &actual));
    158 }
    159 
    160 TEST(ValueConversionTest, FromValueVectorOfString) {
    161   std::vector<std::string> actual;
    162   EXPECT_TRUE(FromValue(*ParseValue("['', 'foo', 'bar']"), &actual));
    163   EXPECT_EQ((std::vector<std::string>{"", "foo", "bar"}), actual);
    164 
    165   EXPECT_TRUE(FromValue(*ParseValue("[]"), &actual));
    166   EXPECT_TRUE(actual.empty());
    167 
    168   EXPECT_FALSE(FromValue(*ParseValue("[100]"), &actual));
    169 }
    170 
    171 TEST(ValueConversionTest, FromValueVectorOfVectors) {
    172   std::vector<std::vector<int>> actual;
    173   EXPECT_TRUE(FromValue(*ParseValue("[[1,2], [], [3]]"), &actual));
    174   EXPECT_EQ((std::vector<std::vector<int>>{{1,2}, {}, {3}}), actual);
    175 
    176   EXPECT_TRUE(FromValue(*ParseValue("[]"), &actual));
    177   EXPECT_TRUE(actual.empty());
    178 
    179   EXPECT_FALSE(FromValue(*ParseValue("[100]"), &actual));
    180 }
    181 
    182 TEST(ValueConversionTest, FromValueMap) {
    183   std::map<std::string, int> actual;
    184   EXPECT_TRUE(FromValue(*ParseValue("{'foo':1, 'bar':2, 'baz':3}"), &actual));
    185   EXPECT_EQ((std::map<std::string, int>{{"foo", 1}, {"bar", 2}, {"baz", 3}}),
    186             actual);
    187 
    188   EXPECT_TRUE(FromValue(*ParseValue("{}"), &actual));
    189   EXPECT_TRUE(actual.empty());
    190 
    191   EXPECT_FALSE(FromValue(*ParseValue("{'foo':1, 'bar':'2'}"), &actual));
    192 }
    193 
    194 TEST(ValueConversionTest, FromValueMapOfVectors) {
    195   std::map<std::string, std::vector<int>> actual;
    196   EXPECT_TRUE(FromValue(*ParseValue("{'foo':[1,2], 'bar':[]}"), &actual));
    197   std::map<std::string, std::vector<int>> expected{
    198       {"foo", {1, 2}}, {"bar", {}}};
    199   EXPECT_EQ(expected, actual);
    200 
    201   EXPECT_TRUE(FromValue(*ParseValue("{}"), &actual));
    202   EXPECT_TRUE(actual.empty());
    203 
    204   EXPECT_FALSE(FromValue(*ParseValue("{'foo':[1], 'bar':[2,'3']}"), &actual));
    205 }
    206 
    207 TEST(ValueConversionTest, FromValueVectorOfMaps) {
    208   std::vector<std::map<std::string, int>> actual;
    209   EXPECT_TRUE(FromValue(*ParseValue("[{'foo':1,'bar':2},{'baz':3}]"), &actual));
    210   std::vector<std::map<std::string, int>> expected{
    211       {{"foo", 1}, {"bar", 2}}, {{"baz", 3}}};
    212   EXPECT_EQ(expected, actual);
    213 
    214   EXPECT_TRUE(FromValue(*ParseValue("[]"), &actual));
    215   EXPECT_TRUE(actual.empty());
    216 
    217   EXPECT_FALSE(FromValue(*ParseValue("[{'foo':1}, 'bar']"), &actual));
    218 }
    219 
    220 TEST(ValueConversionTest, FromValueVectorOfLists) {
    221   std::vector<std::unique_ptr<base::ListValue>> actual;
    222   EXPECT_TRUE(FromValue(*ParseValue("[['foo',1],['bar',2],[true]]"), &actual));
    223   ASSERT_EQ(3, actual.size());
    224   EXPECT_JSON_EQ("['foo', 1]", *actual[0]);
    225   EXPECT_JSON_EQ("['bar', 2]", *actual[1]);
    226   EXPECT_JSON_EQ("[true]", *actual[2]);
    227 }
    228 
    229 TEST(ValueConversionTest, FromValueVectorOfDicts) {
    230   std::vector<std::unique_ptr<base::DictionaryValue>> actual;
    231   EXPECT_TRUE(FromValue(*ParseValue("[{'foo': 1}, {'bar': 2}]"), &actual));
    232   ASSERT_EQ(2, actual.size());
    233   EXPECT_JSON_EQ("{'foo': 1}", *actual[0]);
    234   EXPECT_JSON_EQ("{'bar': 2}", *actual[1]);
    235 }
    236 
    237 TEST(ValueConversionTest, ToValueScalar) {
    238   EXPECT_JSON_EQ("1234", *ToValue(1234));
    239   EXPECT_JSON_EQ("true", *ToValue(true));
    240   EXPECT_JSON_EQ("false", *ToValue(false));
    241   EXPECT_JSON_EQ("12.5", *ToValue(12.5));
    242   EXPECT_JSON_EQ("'foobar'", *ToValue("foobar"));
    243 }
    244 
    245 TEST(ValueConversionTest, ToValueVector) {
    246   EXPECT_JSON_EQ("[1, 2, 3]", *ToValue(std::vector<int>{1, 2, 3}));
    247   EXPECT_JSON_EQ("[]", *ToValue(std::vector<int>{}));
    248   EXPECT_JSON_EQ("[true, false]", *ToValue(std::vector<bool>{true, false}));
    249   EXPECT_JSON_EQ("['foo', 'bar']",
    250                  *ToValue(std::vector<std::string>{"foo", "bar"}));
    251   EXPECT_JSON_EQ("[[1,2],[3]]",
    252                  *ToValue(std::vector<std::vector<int>>{{1, 2}, {3}}));
    253 }
    254 
    255 TEST(ValueConversionTest, ToValueMap) {
    256   EXPECT_JSON_EQ("{'foo': 1, 'bar': 2}",
    257                  *ToValue(std::map<std::string, int>{{"foo", 1}, {"bar", 2}}));
    258   EXPECT_JSON_EQ("{}", *ToValue(std::map<std::string, int>{}));
    259   EXPECT_JSON_EQ("{'foo': true}",
    260                  *ToValue(std::map<std::string, bool>{{"foo", true}}));
    261   EXPECT_JSON_EQ("{'foo': 1.1, 'bar': 2.2}",
    262                  *ToValue(std::map<std::string, double>{{"foo", 1.1},
    263                                                         {"bar", 2.2}}));
    264 }
    265 
    266 }  // namespace brillo
    267