/*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
// @author Nicholas Ormrod <njormrod@fb.com>
-#include "folly/DynamicConverter.h"
+#include <folly/DynamicConverter.h>
#include <algorithm>
#include <gtest/gtest.h>
-#include <gflags/gflags.h>
-#include "folly/Benchmark.h"
+#include <map>
+#include <vector>
using namespace folly;
using namespace folly::dynamicconverter_detail;
bool c1f = is_container<int>::value;
bool c2f = is_container<std::pair<int, int>>::value;
bool c3f = is_container<A>::value;
+ bool c4f = class_is_container<A>::value;
bool c1t = is_container<std::vector<int>>::value;
bool c2t = is_container<std::set<int>>::value;
bool c3t = is_container<std::map<int, int>>::value;
+ bool c4t = class_is_container<std::vector<A>>::value;
EXPECT_EQ(c1f, false);
EXPECT_EQ(c2f, false);
EXPECT_EQ(c3f, false);
+ EXPECT_EQ(c4f, false);
EXPECT_EQ(c1t, true);
EXPECT_EQ(c2t, true);
EXPECT_EQ(c3t, true);
+ EXPECT_EQ(c4t, true);
+
+
+ bool m1f = is_map<int>::value;
+ bool m2f = is_map<std::set<int>>::value;
+
+ bool m1t = is_map<std::map<int, int>>::value;
+
+ EXPECT_EQ(m1f, false);
+ EXPECT_EQ(m2f, false);
+ EXPECT_EQ(m1t, true);
+
+
+ bool r1f = is_range<int>::value;
+
+ bool r1t = is_range<std::set<int>>::value;
+ bool r2t = is_range<std::vector<int>>::value;
+
+ EXPECT_EQ(r1f, false);
+ EXPECT_EQ(r1t, true);
+ EXPECT_EQ(r2t, true);
}
TEST(DynamicConverter, arithmetic_types) {
auto i2 = convertTo<int64_t>(d2);
EXPECT_EQ(i2, 123456789012345);
- dynamic d3 = 123456789012345;
- auto i3 = convertTo<uint8_t>(d3);
- EXPECT_EQ(i3, 121);
-
dynamic d4 = 3.141;
auto i4 = convertTo<float>(d4);
EXPECT_EQ((int)(i4*100), 314);
dynamic
dv1 = {},
dv2 = { ds1, ds2 },
- dv3 = { ds3 };
+ dv3({ ds3 });
dynamic
dm1 = dynamic::object(true, dv1)(false, dv2),
explicit Token(int kind, const fbstring& lexeme)
: kind_(kind), lexeme_(lexeme) {}
};
+
namespace folly {
template <> struct DynamicConverter<Token> {
static Token convert(const dynamic& d) {
}
};
}
+
TEST(DynamicConverter, example) {
dynamic d1 = dynamic::object("KIND", 2)("LEXEME", "a token");
auto i1 = convertTo<Token>(d1);
EXPECT_EQ(i1.lexeme_, "a token");
}
-int main(int argc, char ** argv) {
- testing::InitGoogleTest(&argc, argv);
- google::ParseCommandLineFlags(&argc, &argv, true);
- if (FLAGS_benchmark) {
- folly::runBenchmarks();
+TEST(DynamicConverter, construct) {
+ using std::vector;
+ using std::map;
+ using std::pair;
+ using std::string;
+ {
+ vector<int> c { 1, 2, 3 };
+ dynamic d = { 1, 2, 3 };
+ EXPECT_EQ(d, toDynamic(c));
+ }
+
+ {
+ map<int, int> c { { 2, 4 }, { 3, 9 } };
+ dynamic d = dynamic::object(2, 4)(3, 9);
+ EXPECT_EQ(d, toDynamic(c));
+ }
+
+ {
+ map<string, string> c { { "a", "b" } };
+ dynamic d = dynamic::object("a", "b");
+ EXPECT_EQ(d, toDynamic(c));
+ }
+
+ {
+ map<string, pair<string, int>> c { { "a", { "b", 3 } } };
+ dynamic d = dynamic::object("a", dynamic { "b", 3 });
+ EXPECT_EQ(d, toDynamic(c));
+ }
+
+ {
+ map<string, pair<string, int>> c { { "a", { "b", 3 } } };
+ dynamic d = dynamic::object("a", dynamic { "b", 3 });
+ EXPECT_EQ(d, toDynamic(c));
+ }
+
+ {
+ vector<int> vi { 2, 3, 4, 5 };
+ auto c = std::make_pair(range(vi.begin(), vi.begin() + 3),
+ range(vi.begin() + 1, vi.begin() + 4));
+ dynamic d = { { 2, 3, 4 }, { 3, 4, 5 } };
+ EXPECT_EQ(d, toDynamic(c));
}
- return RUN_ALL_TESTS();
}
+TEST(DynamicConverter, errors) {
+ const auto int32Over =
+ static_cast<int64_t>(std::numeric_limits<int32_t>().max()) + 1;
+ const auto floatOver =
+ static_cast<double>(std::numeric_limits<float>().max()) * 2;
+
+ dynamic d1 = int32Over;
+ EXPECT_THROW(convertTo<int32_t>(d1), std::range_error);
+
+ dynamic d2 = floatOver;
+ EXPECT_THROW(convertTo<float>(d2), std::range_error);
+}