folly copyright 2015 -> copyright 2016
[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 <folly/FBVector.h>
20 #include <folly/FileUtil.h>
21 #include <folly/dynamic.h>
22 #include <folly/json.h>
23 #include <folly/small_vector.h>
24
25 #include <glog/logging.h>
26 #include <gflags/gflags.h>
27 #include <gtest/gtest.h>
28
29 using namespace folly;
30
31 TEST(FormatOther, file) {
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 { closeNoInt(fds[1]); };
38     {
39       FILE* fp = fdopen(fds[1], "wb");
40       PCHECK(fp);
41       SCOPE_EXIT { fclose(fp); };
42       writeTo(fp, format("{} {}", 42, 23));  // <= 512 bytes (PIPE_BUF)
43     }
44
45     char buf[512];
46     ssize_t n = readFull(fds[0], buf, sizeof(buf));
47     CHECK_GE(n, 0);
48
49     EXPECT_EQ("42 23", std::string(buf, n));
50   }
51 }
52
53 TEST(FormatOther, dynamic) {
54   auto dyn = parseJson(
55       "{\n"
56       "  \"hello\": \"world\",\n"
57       "  \"x\": [20, 30],\n"
58       "  \"y\": {\"a\" : 42}\n"
59       "}");
60
61   EXPECT_EQ("world", sformat("{0[hello]}", dyn));
62   EXPECT_THROW(sformat("{0[none]}", dyn), std::out_of_range);
63   EXPECT_EQ("world", sformat("{0[hello]}", defaulted(dyn, "meow")));
64   EXPECT_EQ("meow", sformat("{0[none]}", defaulted(dyn, "meow")));
65
66   EXPECT_EQ("20", sformat("{0[x.0]}", dyn));
67   EXPECT_THROW(sformat("{0[x.2]}", dyn), std::out_of_range);
68
69   // No support for "deep" defaulting (dyn["x"] is not defaulted)
70   auto v = dyn.at("x");
71   EXPECT_EQ("20", sformat("{0[0]}", v));
72   EXPECT_THROW(sformat("{0[2]}", v), std::out_of_range);
73   EXPECT_EQ("20", sformat("{0[0]}", defaulted(v, 42)));
74   EXPECT_EQ("42", sformat("{0[2]}", defaulted(v, 42)));
75
76   EXPECT_EQ("42", sformat("{0[y.a]}", dyn));
77
78   EXPECT_EQ("(null)", sformat("{}", dynamic(nullptr)));
79 }
80
81 namespace {
82
83 template <class T>
84 void testFormatSeq() {
85   T v{10, 20, 30};
86   EXPECT_EQ("30 10", sformat("{0[2]} {0[0]}", v));
87   EXPECT_EQ("0020", sformat("{0[1]:04}", v));
88   EXPECT_EQ("0020", svformat("{1:04}", v));
89   EXPECT_EQ("10 20", svformat("{} {}", v));
90   EXPECT_EQ("10 20 0030", svformat("{} {} {:04}", v));
91 }
92
93 }  // namespace
94
95 TEST(FormatOther, fbvector) {
96   testFormatSeq<fbvector<int>>();
97 }
98
99 TEST(FormatOther, small_vector) {
100   testFormatSeq<small_vector<int, 2>>();
101 }
102
103 int main(int argc, char *argv[]) {
104   testing::InitGoogleTest(&argc, argv);
105   gflags::ParseCommandLineFlags(&argc, &argv, true);
106   return RUN_ALL_TESTS();
107 }