2 * Copyright 2012 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // @author Nicholas Ormrod <njormrod@fb.com>
19 #include "folly/DynamicConverter.h"
20 #include <gtest/gtest.h>
21 #include <gflags/gflags.h>
22 #include "folly/Benchmark.h"
24 using namespace folly;
25 using namespace folly::dynamicconverter_detail;
27 TEST(DynamicConverter, template_metaprogramming) {
30 bool c1f = is_container<int>::value;
31 bool c2f = is_container<std::pair<int, int>>::value;
32 bool c3f = is_container<A>::value;
34 bool c1t = is_container<std::vector<int>>::value;
35 bool c2t = is_container<std::set<int>>::value;
36 bool c3t = is_container<std::map<int, int>>::value;
38 EXPECT_EQ(c1f, false);
39 EXPECT_EQ(c2f, false);
40 EXPECT_EQ(c3f, false);
45 bool m1f = is_map_container<int>::value;
46 bool m2f = is_map_container<std::vector<int>>::value;
47 bool m3f = is_map_container<std::set<int>>::value;
49 bool m1t = is_map_container<std::map<int, int>>::value;
50 bool m2t = is_map_container<std::unordered_map<int, int>>::value;
52 EXPECT_EQ(m1f, false);
53 EXPECT_EQ(m2f, false);
54 EXPECT_EQ(m3f, false);
59 TEST(DynamicConverter, arithmetic_types) {
61 auto i1 = convertTo<int>(d1);
64 dynamic d2 = 123456789012345;
65 auto i2 = convertTo<int64_t>(d2);
66 EXPECT_EQ(i2, 123456789012345);
68 dynamic d3 = 123456789012345;
69 auto i3 = convertTo<uint8_t>(d3);
73 auto i4 = convertTo<float>(d4);
74 EXPECT_EQ((int)(i4*100), 314);
77 auto i5 = convertTo<bool>(d5);
81 const auto i6 = convertTo<const int>(d6);
85 auto i7 = convertTo<int>(d7);
89 auto i8 = convertTo<bool>(d8);
93 TEST(DynamicConverter, simple_builtins) {
94 dynamic d1 = "Haskell";
95 auto i1 = convertTo<folly::fbstring>(d1);
96 EXPECT_EQ(i1, "Haskell");
99 auto i2 = convertTo<std::string>(d2);
102 dynamic d3 = { 12, "Scala" };
103 auto i3 = convertTo<std::pair<int, std::string>>(d3);
104 EXPECT_EQ(i3.first, 12);
105 EXPECT_EQ(i3.second, "Scala");
107 dynamic d4 = dynamic::object("C", "C++");
108 auto i4 = convertTo<std::pair<std::string, folly::fbstring>>(d4);
109 EXPECT_EQ(i4.first, "C");
110 EXPECT_EQ(i4.second, "C++");
113 TEST(DynamicConverter, simple_fbvector) {
114 dynamic d1 = { 1, 2, 3 };
115 auto i1 = convertTo<folly::fbvector<int>>(d1);
116 decltype(i1) i1b = { 1, 2, 3 };
120 TEST(DynamicConverter, simple_container) {
121 dynamic d1 = { 1, 2, 3 };
122 auto i1 = convertTo<std::vector<int>>(d1);
123 decltype(i1) i1b = { 1, 2, 3 };
126 dynamic d2 = { 1, 3, 5, 2, 4 };
127 auto i2 = convertTo<std::set<int>>(d2);
128 decltype(i2) i2b = { 1, 2, 3, 5, 4 };
132 TEST(DynamicConverter, simple_map) {
133 dynamic d1 = dynamic::object(1, "one")(2, "two");
134 auto i1 = convertTo<std::map<int, std::string>>(d1);
135 decltype(i1) i1b = { { 1, "one" }, { 2, "two" } };
138 dynamic d2 = { { 3, "three" }, { 4, "four" } };
139 auto i2 = convertTo<std::unordered_map<int, std::string>>(d2);
140 decltype(i2) i2b = { { 3, "three" }, { 4, "four" } };
144 TEST(DynamicConverter, map_keyed_by_string) {
145 dynamic d1 = dynamic::object("1", "one")("2", "two");
146 auto i1 = convertTo<std::map<std::string, std::string>>(d1);
147 decltype(i1) i1b = { { "1", "one" }, { "2", "two" } };
150 dynamic d2 = { { "3", "three" }, { "4", "four" } };
151 auto i2 = convertTo<std::unordered_map<std::string, std::string>>(d2);
152 decltype(i2) i2b = { { "3", "three" }, { "4", "four" } };
156 TEST(DynamicConverter, nested_containers) {
157 dynamic d1 = { { 1 }, { }, { 2, 3 } };
158 auto i1 = convertTo<folly::fbvector<std::vector<uint8_t>>>(d1);
159 decltype(i1) i1b = { { 1 }, { }, { 2, 3 } };
162 dynamic h2a = { "3", ".", "1", "4" };
163 dynamic h2b = { "2", ".", "7", "2" };
164 dynamic d2 = dynamic::object(3.14, h2a)(2.72, h2b);
165 auto i2 = convertTo<std::map<double, std::vector<folly::fbstring>>>(d2);
167 { { 3.14, { "3", ".", "1", "4" } },
168 { 2.72, { "2", ".", "7", "2" } } };
174 bool operator==(const A & o) const { return i == o.i; }
177 template <> struct DynamicConverter<A> {
178 static A convert(const dynamic & d) {
179 return { convertTo<int>(d["i"]) };
183 TEST(DynamicConverter, custom_class) {
184 dynamic d1 = dynamic::object("i", 17);
185 auto i1 = convertTo<A>(d1);
188 dynamic d2 = { dynamic::object("i", 18), dynamic::object("i", 19) };
189 auto i2 = convertTo<std::vector<A>>(d2);
190 decltype(i2) i2b = { { 18 }, { 19 } };
194 TEST(DynamicConverter, crazy) {
195 // we are going to create a vector<unordered_map<bool, T>>
196 // we will construct some of the maps from dynamic objects,
197 // some from a vector of KV pairs.
198 // T will be vector<set<string>>
200 std::set<std::string>
201 s1 = { "a", "e", "i", "o", "u" },
202 s2 = { "2", "3", "5", "7" },
203 s3 = { "Hello", "World" };
205 std::vector<std::set<std::string>>
210 std::unordered_map<bool, std::vector<std::set<std::string>>>
211 m1 = { { true, v1 }, { false, v2 } },
212 m2 = { { true, v3 } };
214 std::vector<std::unordered_map<bool, std::vector<std::set<std::string>>>>
219 ds1 = { "a", "e", "i", "o", "u" },
220 ds2 = { "2", "3", "5", "7" },
221 ds3 = { "Hello", "World" };
229 dm1 = dynamic::object(true, dv1)(false, dv2),
230 dm2 = { { true, dv3 } };
236 auto i = convertTo<std::vector<std::unordered_map<bool, std::vector<
237 std::set<std::string>>>>>(df1); // yes, that is 5 close-chevrons
242 TEST(DynamicConverter, consts) {
244 auto i1 = convertTo<const double>(d1);
247 dynamic d2 = "Hello";
248 auto i2 = convertTo<const std::string>(d2);
249 decltype(i2) i2b = "Hello";
253 auto i3 = convertTo<const bool>(d3);
257 auto i4 = convertTo<const bool>(d4);
260 dynamic d5 = { 1, 2 };
261 auto i5 = convertTo<const std::pair<const int, const int>>(d5);
262 decltype(i5) i5b = { 1, 2 };
270 explicit Token(int kind, const fbstring& lexeme)
271 : kind_(kind), lexeme_(lexeme) {}
274 template <> struct DynamicConverter<Token> {
275 static Token convert(const dynamic& d) {
276 int k = convertTo<int>(d["KIND"]);
277 fbstring lex = convertTo<fbstring>(d["LEXEME"]);
278 return Token(k, lex);
282 TEST(DynamicConverter, example) {
283 dynamic d1 = dynamic::object("KIND", 2)("LEXEME", "a token");
284 auto i1 = convertTo<Token>(d1);
285 EXPECT_EQ(i1.kind_, 2);
286 EXPECT_EQ(i1.lexeme_, "a token");
289 int main(int argc, char ** argv) {
290 testing::InitGoogleTest(&argc, argv);
291 google::ParseCommandLineFlags(&argc, &argv, true);
292 if (FLAGS_benchmark) {
293 folly::runBenchmarks();
295 return RUN_ALL_TESTS();