466d3c6594b0175f69e08d40c9c468d5b547853e
[folly.git] / folly / test / FormatOtherTest.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <folly/Format.h>
18
19 #include <glog/logging.h>
20
21 #include <folly/FBVector.h>
22 #include <folly/FileUtil.h>
23 #include <folly/dynamic.h>
24 #include <folly/json.h>
25 #include <folly/small_vector.h>
26 #include <folly/portability/GTest.h>
27
28 using namespace folly;
29
30 TEST(FormatOther, file) {
31   // Test writing to FILE. I'd use open_memstream but that's not available
32   // outside of Linux (even though it's in POSIX.1-2008).
33   {
34     int fds[2];
35     CHECK_ERR(pipe(fds));
36     SCOPE_EXIT { closeNoInt(fds[1]); };
37     {
38       FILE* fp = fdopen(fds[1], "wb");
39       PCHECK(fp);
40       SCOPE_EXIT { fclose(fp); };
41       writeTo(fp, format("{} {}", 42, 23));  // <= 512 bytes (PIPE_BUF)
42     }
43
44     char buf[512];
45     ssize_t n = readFull(fds[0], buf, sizeof(buf));
46     CHECK_GE(n, 0);
47
48     EXPECT_EQ("42 23", std::string(buf, n));
49   }
50 }
51
52 TEST(FormatOther, dynamic) {
53   auto dyn = parseJson(
54       "{\n"
55       "  \"hello\": \"world\",\n"
56       "  \"x\": [20, 30],\n"
57       "  \"y\": {\"a\" : 42}\n"
58       "}");
59
60   EXPECT_EQ("world", sformat("{0[hello]}", dyn));
61   EXPECT_THROW(sformat("{0[none]}", dyn), std::out_of_range);
62   EXPECT_EQ("world", sformat("{0[hello]}", defaulted(dyn, "meow")));
63   EXPECT_EQ("meow", sformat("{0[none]}", defaulted(dyn, "meow")));
64
65   EXPECT_EQ("20", sformat("{0[x.0]}", dyn));
66   EXPECT_THROW(sformat("{0[x.2]}", dyn), std::out_of_range);
67
68   // No support for "deep" defaulting (dyn["x"] is not defaulted)
69   auto v = dyn.at("x");
70   EXPECT_EQ("20", sformat("{0[0]}", v));
71   EXPECT_THROW(sformat("{0[2]}", v), std::out_of_range);
72   EXPECT_EQ("20", sformat("{0[0]}", defaulted(v, 42)));
73   EXPECT_EQ("42", sformat("{0[2]}", defaulted(v, 42)));
74
75   EXPECT_EQ("42", sformat("{0[y.a]}", dyn));
76
77   EXPECT_EQ("(null)", sformat("{}", dynamic(nullptr)));
78 }
79
80 namespace {
81
82 template <class T>
83 void testFormatSeq() {
84   T v{10, 20, 30};
85   EXPECT_EQ("30 10", sformat("{0[2]} {0[0]}", v));
86   EXPECT_EQ("0020", sformat("{0[1]:04}", v));
87   EXPECT_EQ("0020", svformat("{1:04}", v));
88   EXPECT_EQ("10 20", svformat("{} {}", v));
89   EXPECT_EQ("10 20 0030", svformat("{} {} {:04}", v));
90 }
91
92 }  // namespace
93
94 TEST(FormatOther, fbvector) {
95   testFormatSeq<fbvector<int>>();
96 }
97
98 TEST(FormatOther, small_vector) {
99   testFormatSeq<small_vector<int, 2>>();
100 }
101
102 int main(int argc, char *argv[]) {
103   testing::InitGoogleTest(&argc, argv);
104   gflags::ParseCommandLineFlags(&argc, &argv, true);
105   return RUN_ALL_TESTS();
106 }