proxygen
DynamicOtherTest.cpp File Reference
#include <folly/dynamic.h>
#include <folly/gen/Base.h>
#include <folly/json.h>
#include <folly/portability/GFlags.h>
#include <folly/portability/GTest.h>
#include <iostream>

Go to the source code of this file.

Functions

 TEST (Dynamic, ArrayGenerator)
 
 TEST (Dynamic, StringPtrs)
 
 TEST (Dynamic, Getters)
 
 TEST (Dynamic, FormattedIO)
 
int main (int argc, char **argv)
 

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 120 of file DynamicOtherTest.cpp.

References testing::InitGoogleTest(), and RUN_ALL_TESTS().

120  {
122  gflags::ParseCommandLineFlags(&argc, &argv, true);
123  return RUN_ALL_TESTS();
124 }
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2232
char ** argv
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5370
TEST ( Dynamic  ,
ArrayGenerator   
)

Definition at line 29 of file DynamicOtherTest.cpp.

References folly::dynamic::array(), folly::dynamic::asInt(), EXPECT_EQ, folly::gen::from(), folly::gen::member(), folly::gen::sum, and folly::gen::take().

29  {
30  // Make sure arrays can be used with folly::gen.
31  using namespace folly::gen;
32  dynamic arr = dynamic::array(1, 2, 3, 4);
33  EXPECT_EQ(from(arr) | take(3) | member(&dynamic::asInt) | sum, 6);
34 }
From from(Container &source)
Definition: Base.h:438
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Sum sum
Definition: Base-inl.h:2549
int64_t asInt() const
Definition: dynamic-inl.h:524
std::enable_if< ExprIsConst< Constness >::value, Map >::type member(Return(Class::*member)() const)
Definition: Base.h:605
static void array(EmptyArrayTag)
Definition: dynamic-inl.h:233
detail::Take take(Number count)
Definition: Base-inl.h:2582
TEST ( Dynamic  ,
StringPtrs   
)

Definition at line 36 of file DynamicOtherTest.cpp.

References folly::dynamic::asString(), folly::dynamic::c_str(), folly::dynamic::data(), EXPECT_EQ, EXPECT_THROW, folly::dynamic::getString(), folly::parseJson(), and folly::dynamic::stringPiece().

36  {
37  dynamic str = "12.0";
38  dynamic num = 12.0;
39  dynamic nullStr = folly::parseJson("\"foo\\u0000bar\"");
40 
41  EXPECT_EQ(0, strcmp(str.c_str(), "12.0"));
42  EXPECT_EQ(0, strncmp(str.data(), "12.0", str.asString().length()));
43  EXPECT_EQ(str.stringPiece(), "12.0");
44 
46  EXPECT_THROW(num.data(), TypeError);
48 
49  EXPECT_EQ(nullStr.stringPiece(), folly::StringPiece("foo\0bar", 7));
50 
51  nullStr.getString()[3] = '|';
52  EXPECT_EQ(nullStr.stringPiece(), "foo|bar");
53 }
const char * data() const &
Definition: dynamic-inl.h:570
StringPiece stringPiece() const
Definition: dynamic-inl.h:576
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
dynamic parseJson(StringPiece range)
Definition: json.cpp:900
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const std::string & getString() const &
Definition: dynamic-inl.h:531
std::string asString() const
Definition: dynamic-inl.h:518
Range< const char * > StringPiece
const char * c_str() const &
Definition: dynamic-inl.h:573
TEST ( Dynamic  ,
Getters   
)

Definition at line 55 of file DynamicOtherTest.cpp.

References EXPECT_EQ, EXPECT_THROW, folly::dynamic::getBool(), folly::dynamic::getDouble(), folly::dynamic::getInt(), folly::dynamic::getString(), folly::parseJson(), and string.

55  {
56  dynamic dStr = folly::parseJson("\"foo\\u0000bar\"");
57  dynamic dInt = 1;
58  dynamic dDouble = 0.5;
59  dynamic dBool = true;
60 
61  EXPECT_EQ(dStr.getString(), std::string("foo\0bar", 7));
62  EXPECT_EQ(dInt.getInt(), 1);
63  EXPECT_EQ(dDouble.getDouble(), 0.5);
64  EXPECT_EQ(dBool.getBool(), true);
65 
66  dStr.getString()[3] = '|';
67  EXPECT_EQ(dStr.getString(), "foo|bar");
68 
69  dInt.getInt() = 2;
70  EXPECT_EQ(dInt.getInt(), 2);
71 
72  dDouble.getDouble() = 0.7;
73  EXPECT_EQ(dDouble.getDouble(), 0.7);
74 
75  dBool.getBool() = false;
76  EXPECT_EQ(dBool.getBool(), false);
77 
78  EXPECT_THROW(dStr.getInt(), TypeError);
81 
85 
86  EXPECT_THROW(dDouble.getString(), TypeError);
87  EXPECT_THROW(dDouble.getInt(), TypeError);
88  EXPECT_THROW(dDouble.getBool(), TypeError);
89 
91  EXPECT_THROW(dBool.getInt(), TypeError);
93 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
dynamic parseJson(StringPiece range)
Definition: json.cpp:900
double getDouble() const &
Definition: dynamic-inl.h:534
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const std::string & getString() const &
Definition: dynamic-inl.h:531
int64_t getInt() const &
Definition: dynamic-inl.h:537
const char * string
Definition: Conv.cpp:212
bool getBool() const &
Definition: dynamic-inl.h:540
TEST ( Dynamic  ,
FormattedIO   
)

Definition at line 95 of file DynamicOtherTest.cpp.

References folly::dynamic::array(), EXPECT_EQ, and folly::dynamic::object().

95  {
96  std::ostringstream out;
97  dynamic doubl = 123.33;
98  dynamic dint = 12;
99  out << "0x" << std::hex << ++dint << ' ' << std::setprecision(1) << doubl
100  << '\n';
101  EXPECT_EQ(out.str(), "0xd 1e+02\n");
102 
103  out.str("");
104  dynamic arrr = dynamic::array(1, 2, 3);
105  out << arrr;
106  EXPECT_EQ(out.str(), "[1,2,3]");
107 
108  out.str("");
109  dynamic objy = dynamic::object("a", 12);
110  out << objy;
111  EXPECT_EQ(out.str(), R"({"a":12})");
112 
113  out.str("");
114  dynamic objy2 = dynamic::array(
115  objy, dynamic::object(12, "str"), dynamic::object(true, false));
116  out << objy2;
117  EXPECT_EQ(out.str(), R"([{"a":12},{12:"str"},{true:false}])");
118 }
void * object
Definition: AtFork.cpp:32
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922