proxygen
FormatOtherTest.cpp File Reference
#include <folly/Format.h>
#include <glog/logging.h>
#include <folly/FBVector.h>
#include <folly/FileUtil.h>
#include <folly/Portability.h>
#include <folly/dynamic.h>
#include <folly/json.h>
#include <folly/portability/GTest.h>
#include <folly/small_vector.h>

Go to the source code of this file.

Functions

 TEST (FormatOther, file)
 
 TEST (FormatOther, dynamic)
 
 TEST (FormatOther, fbvector)
 
 TEST (FormatOther, small_vector)
 
int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 111 of file FormatOtherTest.cpp.

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

111  {
113  gflags::ParseCommandLineFlags(&argc, &argv, true);
114  return RUN_ALL_TESTS();
115 }
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 ( FormatOther  ,
file   
)

Definition at line 31 of file FormatOtherTest.cpp.

References folly::closeNoInt(), EXPECT_EQ, FILE, folly::format(), folly::kIsWindows, pipe(), folly::readFull(), SCOPE_EXIT, string, and folly::writeTo().

31  {
32  // Test writing to FILE. I'd use open_memstream but that's not available
33  // outside of Linux (even though it's in POSIX.1-2008).
34  {
35  int fds[2];
36  CHECK_ERR(pipe(fds));
37  SCOPE_EXIT {
38  // fclose on Windows automatically closes the underlying
39  // file descriptor.
40  if (!kIsWindows) {
41  closeNoInt(fds[1]);
42  }
43  };
44  {
45  FILE* fp = fdopen(fds[1], "wb");
46  PCHECK(fp);
47  SCOPE_EXIT {
48  fclose(fp);
49  };
50  writeTo(fp, format("{} {}", 42, 23)); // <= 512 bytes (PIPE_BUF)
51  }
52 
53  char buf[512];
54  ssize_t n = readFull(fds[0], buf, sizeof(buf));
55  CHECK_GE(n, 0);
56 
57  EXPECT_EQ("42 23", std::string(buf, n));
58  }
59 }
int closeNoInt(int fd)
Definition: FileUtil.cpp:56
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
ssize_t readFull(int fd, void *buf, size_t count)
Definition: FileUtil.cpp:126
#define SCOPE_EXIT
Definition: ScopeGuard.h:274
void writeTo(FILE *fp, const BaseFormatter< Derived, containerMode, Args... > &formatter)
Definition: Format-inl.h:283
const char * string
Definition: Conv.cpp:212
Formatter< false, Args... > format(StringPiece fmt, Args &&...args)
Definition: Format.h:271
constexpr auto kIsWindows
Definition: Portability.h:367
GMockOutputTest ExpectedCall FILE
void pipe(CPUExecutor cpu, IOExecutor io)
TEST ( FormatOther  ,
dynamic   
)

Definition at line 61 of file FormatOtherTest.cpp.

References folly::defaulted(), EXPECT_EQ, EXPECT_THROW, folly::parseJson(), folly::sformat(), folly::svformat(), and folly::T.

61  {
62  auto dyn = parseJson(
63  "{\n"
64  " \"hello\": \"world\",\n"
65  " \"x\": [20, 30],\n"
66  " \"y\": {\"a\" : 42}\n"
67  "}");
68 
69  EXPECT_EQ("world", sformat("{0[hello]}", dyn));
70  EXPECT_THROW(sformat("{0[none]}", dyn), std::out_of_range);
71  EXPECT_EQ("world", sformat("{0[hello]}", defaulted(dyn, "meow")));
72  EXPECT_EQ("meow", sformat("{0[none]}", defaulted(dyn, "meow")));
73 
74  EXPECT_EQ("20", sformat("{0[x.0]}", dyn));
75  EXPECT_THROW(sformat("{0[x.2]}", dyn), std::out_of_range);
76 
77  // No support for "deep" defaulting (dyn["x"] is not defaulted)
78  auto v = dyn.at("x");
79  EXPECT_EQ("20", sformat("{0[0]}", v));
80  EXPECT_THROW(sformat("{0[2]}", v), std::out_of_range);
81  EXPECT_EQ("20", sformat("{0[0]}", defaulted(v, 42)));
82  EXPECT_EQ("42", sformat("{0[2]}", defaulted(v, 42)));
83 
84  EXPECT_EQ("42", sformat("{0[y.a]}", dyn));
85 
86  EXPECT_EQ("(null)", sformat("{}", dynamic(nullptr)));
87 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
dynamic parseJson(StringPiece range)
Definition: json.cpp:900
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
detail::DefaultValueWrapper< Container, Value > defaulted(const Container &c, const Value &v)
Definition: Format.h:353
TEST ( FormatOther  ,
fbvector   
)

Definition at line 103 of file FormatOtherTest.cpp.

103  {
104  testFormatSeq<fbvector<int>>();
105 }
TEST ( FormatOther  ,
small_vector   
)

Definition at line 107 of file FormatOtherTest.cpp.

107  {
108  testFormatSeq<small_vector<int, 2>>();
109 }