• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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 "util/json/json_helpers.h"
6 
7 #include "gmock/gmock.h"
8 #include "gtest/gtest.h"
9 #include "util/chrono_helpers.h"
10 
11 namespace openscreen {
12 namespace json {
13 namespace {
14 
15 using ::testing::ElementsAre;
16 
17 const Json::Value kNone;
18 const Json::Value kEmptyString = "";
19 const Json::Value kEmptyArray(Json::arrayValue);
20 
21 struct Dummy {
22   int value;
23 
operator ==openscreen::json::__anon08d430ae0111::Dummy24   constexpr bool operator==(const Dummy& other) const {
25     return other.value == value;
26   }
27 };
28 
ParseAndValidateDummy(const Json::Value & value,Dummy * out)29 bool ParseAndValidateDummy(const Json::Value& value, Dummy* out) {
30   int value_out;
31   if (!ParseAndValidateInt(value, &value_out)) {
32     return false;
33   }
34   *out = Dummy{value_out};
35   return true;
36 }
37 
38 }  // namespace
39 
TEST(ParsingHelpersTest,ParseAndValidateDouble)40 TEST(ParsingHelpersTest, ParseAndValidateDouble) {
41   const Json::Value kValid = 13.37;
42   const Json::Value kNotDouble = "coffee beans";
43   const Json::Value kNegativeDouble = -4.2;
44   const Json::Value kZeroDouble = 0.0;
45   const Json::Value kNanDouble = std::nan("");
46 
47   double out;
48   EXPECT_TRUE(ParseAndValidateDouble(kValid, &out));
49   EXPECT_DOUBLE_EQ(13.37, out);
50   EXPECT_TRUE(ParseAndValidateDouble(kZeroDouble, &out));
51   EXPECT_DOUBLE_EQ(0.0, out);
52   EXPECT_FALSE(ParseAndValidateDouble(kNotDouble, &out));
53   EXPECT_FALSE(ParseAndValidateDouble(kNegativeDouble, &out));
54   EXPECT_FALSE(ParseAndValidateDouble(kNone, &out));
55   EXPECT_FALSE(ParseAndValidateDouble(kNanDouble, &out));
56 }
57 
TEST(ParsingHelpersTest,ParseAndValidateInt)58 TEST(ParsingHelpersTest, ParseAndValidateInt) {
59   const Json::Value kValid = 1337;
60   const Json::Value kNotInt = "cold brew";
61   const Json::Value kNegativeInt = -42;
62   const Json::Value kZeroInt = 0;
63 
64   int out;
65   EXPECT_TRUE(ParseAndValidateInt(kValid, &out));
66   EXPECT_EQ(1337, out);
67   EXPECT_TRUE(ParseAndValidateInt(kZeroInt, &out));
68   EXPECT_EQ(0, out);
69   EXPECT_FALSE(ParseAndValidateInt(kNone, &out));
70   EXPECT_FALSE(ParseAndValidateInt(kNotInt, &out));
71   EXPECT_FALSE(ParseAndValidateInt(kNegativeInt, &out));
72 }
73 
TEST(ParsingHelpersTest,ParseAndValidateUint)74 TEST(ParsingHelpersTest, ParseAndValidateUint) {
75   const Json::Value kValid = 1337u;
76   const Json::Value kNotUint = "espresso";
77   const Json::Value kZeroUint = 0u;
78 
79   uint32_t out;
80   EXPECT_TRUE(ParseAndValidateUint(kValid, &out));
81   EXPECT_EQ(1337u, out);
82   EXPECT_TRUE(ParseAndValidateUint(kZeroUint, &out));
83   EXPECT_EQ(0u, out);
84   EXPECT_FALSE(ParseAndValidateUint(kNone, &out));
85   EXPECT_FALSE(ParseAndValidateUint(kNotUint, &out));
86 }
87 
TEST(ParsingHelpersTest,ParseAndValidateString)88 TEST(ParsingHelpersTest, ParseAndValidateString) {
89   const Json::Value kValid = "macchiato";
90   const Json::Value kNotString = 42;
91 
92   std::string out;
93   EXPECT_TRUE(ParseAndValidateString(kValid, &out));
94   EXPECT_EQ("macchiato", out);
95   EXPECT_TRUE(ParseAndValidateString(kEmptyString, &out));
96   EXPECT_EQ("", out);
97   EXPECT_FALSE(ParseAndValidateString(kNone, &out));
98   EXPECT_FALSE(ParseAndValidateString(kNotString, &out));
99 }
100 
101 // Simple fraction validity is tested extensively in its unit tests, so we
102 // just check the major cases here.
TEST(ParsingHelpersTest,ParseAndValidateSimpleFraction)103 TEST(ParsingHelpersTest, ParseAndValidateSimpleFraction) {
104   const Json::Value kValid = "42/30";
105   const Json::Value kValidNumber = "42";
106   const Json::Value kUndefined = "5/0";
107   const Json::Value kNegative = "10/-2";
108   const Json::Value kInvalidNumber = "-1";
109   const Json::Value kNotSimpleFraction = "latte";
110   const Json::Value kInteger = 123;
111   const Json::Value kNegativeInteger = -5000;
112 
113   SimpleFraction out;
114   EXPECT_TRUE(ParseAndValidateSimpleFraction(kValid, &out));
115   EXPECT_EQ((SimpleFraction{42, 30}), out);
116   EXPECT_TRUE(ParseAndValidateSimpleFraction(kValidNumber, &out));
117   EXPECT_EQ((SimpleFraction{42, 1}), out);
118   EXPECT_TRUE(ParseAndValidateSimpleFraction(kInteger, &out));
119   EXPECT_EQ((SimpleFraction{123, 1}), out);
120   EXPECT_FALSE(ParseAndValidateSimpleFraction(kUndefined, &out));
121   EXPECT_FALSE(ParseAndValidateSimpleFraction(kNegative, &out));
122   EXPECT_FALSE(ParseAndValidateSimpleFraction(kInvalidNumber, &out));
123   EXPECT_FALSE(ParseAndValidateSimpleFraction(kNotSimpleFraction, &out));
124   EXPECT_FALSE(ParseAndValidateSimpleFraction(kNone, &out));
125   EXPECT_FALSE(ParseAndValidateSimpleFraction(kEmptyString, &out));
126   EXPECT_FALSE(ParseAndValidateSimpleFraction(kNegativeInteger, &out));
127 }
128 
TEST(ParsingHelpersTest,ParseAndValidateMilliseconds)129 TEST(ParsingHelpersTest, ParseAndValidateMilliseconds) {
130   const Json::Value kValid = 1000;
131   const Json::Value kValidFloat = 500.0;
132   const Json::Value kNegativeNumber = -120;
133   const Json::Value kZeroNumber = 0;
134   const Json::Value kNotNumber = "affogato";
135 
136   milliseconds out;
137   EXPECT_TRUE(ParseAndValidateMilliseconds(kValid, &out));
138   EXPECT_EQ(milliseconds(1000), out);
139   EXPECT_TRUE(ParseAndValidateMilliseconds(kValidFloat, &out));
140   EXPECT_EQ(milliseconds(500), out);
141   EXPECT_TRUE(ParseAndValidateMilliseconds(kZeroNumber, &out));
142   EXPECT_EQ(milliseconds(0), out);
143   EXPECT_FALSE(ParseAndValidateMilliseconds(kNone, &out));
144   EXPECT_FALSE(ParseAndValidateMilliseconds(kNegativeNumber, &out));
145   EXPECT_FALSE(ParseAndValidateMilliseconds(kNotNumber, &out));
146 }
147 
TEST(ParsingHelpersTest,ParseAndValidateArray)148 TEST(ParsingHelpersTest, ParseAndValidateArray) {
149   Json::Value valid_dummy_array;
150   valid_dummy_array[0] = 123;
151   valid_dummy_array[1] = 456;
152 
153   Json::Value invalid_dummy_array;
154   invalid_dummy_array[0] = "iced coffee";
155   invalid_dummy_array[1] = 456;
156 
157   std::vector<Dummy> out;
158   EXPECT_TRUE(ParseAndValidateArray<Dummy>(valid_dummy_array,
159                                            ParseAndValidateDummy, &out));
160   EXPECT_THAT(out, ElementsAre(Dummy{123}, Dummy{456}));
161   EXPECT_FALSE(ParseAndValidateArray<Dummy>(invalid_dummy_array,
162                                             ParseAndValidateDummy, &out));
163   EXPECT_FALSE(
164       ParseAndValidateArray<Dummy>(kEmptyArray, ParseAndValidateDummy, &out));
165 }
166 
TEST(ParsingHelpersTest,ParseAndValidateIntArray)167 TEST(ParsingHelpersTest, ParseAndValidateIntArray) {
168   Json::Value valid_int_array;
169   valid_int_array[0] = 123;
170   valid_int_array[1] = 456;
171 
172   Json::Value invalid_int_array;
173   invalid_int_array[0] = "iced coffee";
174   invalid_int_array[1] = 456;
175 
176   std::vector<int> out;
177   EXPECT_TRUE(ParseAndValidateIntArray(valid_int_array, &out));
178   EXPECT_THAT(out, ElementsAre(123, 456));
179   EXPECT_FALSE(ParseAndValidateIntArray(invalid_int_array, &out));
180   EXPECT_FALSE(ParseAndValidateIntArray(kEmptyArray, &out));
181 }
182 
TEST(ParsingHelpersTest,ParseAndValidateUintArray)183 TEST(ParsingHelpersTest, ParseAndValidateUintArray) {
184   Json::Value valid_uint_array;
185   valid_uint_array[0] = 123u;
186   valid_uint_array[1] = 456u;
187 
188   Json::Value invalid_uint_array;
189   invalid_uint_array[0] = "breve";
190   invalid_uint_array[1] = 456u;
191 
192   std::vector<uint32_t> out;
193   EXPECT_TRUE(ParseAndValidateUintArray(valid_uint_array, &out));
194   EXPECT_THAT(out, ElementsAre(123u, 456u));
195   EXPECT_FALSE(ParseAndValidateUintArray(invalid_uint_array, &out));
196   EXPECT_FALSE(ParseAndValidateUintArray(kEmptyArray, &out));
197 }
198 
TEST(ParsingHelpersTest,ParseAndValidateStringArray)199 TEST(ParsingHelpersTest, ParseAndValidateStringArray) {
200   Json::Value valid_string_array;
201   valid_string_array[0] = "nitro cold brew";
202   valid_string_array[1] = "doppio espresso";
203 
204   Json::Value invalid_string_array;
205   invalid_string_array[0] = "mocha latte";
206   invalid_string_array[1] = 456;
207 
208   std::vector<std::string> out;
209   EXPECT_TRUE(ParseAndValidateStringArray(valid_string_array, &out));
210   EXPECT_THAT(out, ElementsAre("nitro cold brew", "doppio espresso"));
211   EXPECT_FALSE(ParseAndValidateStringArray(invalid_string_array, &out));
212   EXPECT_FALSE(ParseAndValidateStringArray(kEmptyArray, &out));
213 }
214 
215 }  // namespace json
216 }  // namespace openscreen
217