proxygen
StringTest.cpp File Reference
#include <folly/String.h>
#include <tuple>
#include <cinttypes>
#include <set>
#include <boost/regex.hpp>
#include <folly/container/Array.h>
#include <folly/portability/GTest.h>
#include <folly/test/TestUtils.h>

Go to the source code of this file.

Classes

struct  PrettyTestCase
 
struct  my::ColorError
 

Namespaces

 my
 

Macros

#define __STDC_FORMAT_MACROS   1
 

Enumerations

enum  my::Color { my::Color::Red, my::Color::Blue }
 
enum  my::ColorErrorCode { my::ColorErrorCode::INVALID_COLOR }
 

Functions

 TEST (StringPrintf, BasicTest)
 
 TEST (StringPrintf, NumericFormats)
 
 TEST (StringPrintf, Appending)
 
void vprintfCheck (const char *expected, const char *fmt,...)
 
void vprintfError (const char *fmt,...)
 
 TEST (StringPrintf, VPrintf)
 
 TEST (StringPrintf, VariousSizes)
 
 TEST (StringPrintf, oldStringPrintfTests)
 
 TEST (StringPrintf, oldStringAppendf)
 
 TEST (Escape, cEscape)
 
 TEST (Escape, cUnescape)
 
 TEST (Escape, uriEscape)
 
 TEST (Escape, uriUnescape)
 
 TEST (Escape, uriEscapeAllCombinations)
 
 TEST (Escape, uriUnescapePercentDecoding)
 
 TEST (PrettyPrint, Basic)
 
 TEST (PrettyToDouble, Basic)
 
 TEST (PrettyPrint, HexDump)
 
 TEST (System, errnoStr)
 
 TEST (Split, split_vector)
 
 TEST (Split, split_fbvector)
 
 TEST (Split, pieces_vector)
 
 TEST (Split, pieces_fbvector)
 
 TEST (Split, fixed)
 
 TEST (Split, std_string_fixed)
 
 TEST (Split, fixed_convert)
 
ColorError my::makeConversionError (ColorErrorCode, StringPiece sp)
 
Expected< StringPiece, ColorErrorCode > my::parseTo (StringPiece in, Color &out) noexcept
 
 TEST (Split, fixed_convert_custom)
 
 TEST (String, join)
 
 TEST (String, hexlify)
 
 TEST (String, unhexlify)
 
 TEST (String, backslashify)
 
 TEST (String, humanify)
 
 TEST (String, toLowerAsciiAligned)
 
 TEST (String, toLowerAsciiUnaligned)
 
 TEST (String, whitespace)
 
 TEST (String, stripLeftMargin_really_empty)
 
 TEST (String, stripLeftMargin_empty)
 
 TEST (String, stripLeftMargin_only_whitespace)
 
 TEST (String, stripLeftMargin_only_uneven_whitespace)
 
 TEST (String, stripLeftMargin_one_line)
 
 TEST (String, stripLeftMargin_two_lines)
 
 TEST (String, stripLeftMargin_three_lines_uneven)
 
 TEST (String, stripLeftMargin_preceding_blank_lines)
 
 TEST (String, stripLeftMargin_succeeding_blank_lines)
 
 TEST (String, stripLeftMargin_interstitial_undented_whiteline)
 
 TEST (String, stripLeftMargin_interstitial_dedented_whiteline)
 
 TEST (String, stripLeftMargin_interstitial_equidented_whiteline)
 
 TEST (String, stripLeftMargin_interstitial_indented_whiteline)
 
 TEST (String, stripLeftMargin_no_pre_whitespace)
 
 TEST (String, stripLeftMargin_no_post_whitespace)
 

Variables

PrettyTestCase prettyTestCases []
 

Macro Definition Documentation

#define __STDC_FORMAT_MACROS   1

Definition at line 18 of file StringTest.cpp.

Function Documentation

TEST ( StringPrintf  ,
BasicTest   
)

Definition at line 36 of file StringTest.cpp.

References EXPECT_EQ, and folly::stringPrintf().

36  {
37  EXPECT_EQ("abc", stringPrintf("%s", "abc"));
38  EXPECT_EQ("abc", stringPrintf("%sbc", "a"));
39  EXPECT_EQ("abc", stringPrintf("a%sc", "b"));
40  EXPECT_EQ("abc", stringPrintf("ab%s", "c"));
41 
42  EXPECT_EQ("abc", stringPrintf("abc"));
43 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stringPrintf(const char *format,...)
Definition: String.cpp:223
TEST ( StringPrintf  ,
NumericFormats   
)

Definition at line 45 of file StringTest.cpp.

References EXPECT_EQ, and folly::stringPrintf().

45  {
46  EXPECT_EQ("12", stringPrintf("%d", 12));
47  EXPECT_EQ("2000000000", stringPrintf("%ld", 2000000000UL));
48  EXPECT_EQ("2000000000", stringPrintf("%ld", 2000000000L));
49  EXPECT_EQ("-2000000000", stringPrintf("%ld", -2000000000L));
50  EXPECT_EQ("5000000000", stringPrintf("%lld", 5000000000ULL));
51  EXPECT_EQ("5000000000", stringPrintf("%lld", 5000000000LL));
52  EXPECT_EQ("-5000000000", stringPrintf("%lld", -5000000000LL));
53  EXPECT_EQ("-1", stringPrintf("%d", 0xffffffff));
54  EXPECT_EQ(
55  "-1",
56  stringPrintf("%" PRId64, static_cast<int64_t>(0xffffffffffffffffLL)));
57  EXPECT_EQ(
58  "-1",
59  stringPrintf("%" PRId64, static_cast<uint64_t>(0xffffffffffffffffULL)));
60 
61  EXPECT_EQ("7.7", stringPrintf("%1.1f", 7.7));
62  EXPECT_EQ("7.7", stringPrintf("%1.1lf", 7.7));
63  EXPECT_EQ("7.70000000000000018", stringPrintf("%.17f", 7.7));
64  EXPECT_EQ("7.70000000000000018", stringPrintf("%.17lf", 7.7));
65 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stringPrintf(const char *format,...)
Definition: String.cpp:223
TEST ( StringPrintf  ,
Appending   
)

Definition at line 67 of file StringTest.cpp.

References EXPECT_EQ, s, and folly::stringAppendf().

67  {
68  string s;
69  stringAppendf(&s, "a%s", "b");
70  stringAppendf(&s, "%c", 'c');
71  EXPECT_EQ(s, "abc");
72  stringAppendf(&s, " %d", 123);
73  EXPECT_EQ(s, "abc 123");
74 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string & stringAppendf(std::string *output, const char *format,...)
Definition: String.cpp:240
static set< string > s
TEST ( StringPrintf  ,
VPrintf   
)

Definition at line 122 of file StringTest.cpp.

References vprintfCheck(), and vprintfError().

122  {
123  vprintfCheck("foo", "%s", "foo");
124  vprintfCheck(
125  "long string requiring reallocation 1 2 3 0x12345678",
126  "%s %s %d %d %d %#x",
127  "long string",
128  "requiring reallocation",
129  1,
130  2,
131  3,
132  0x12345678);
133  vprintfError("bogus%", "foo");
134 }
void vprintfError(const char *fmt,...)
Definition: StringTest.cpp:107
void vprintfCheck(const char *expected, const char *fmt,...)
Definition: StringTest.cpp:76
TEST ( StringPrintf  ,
VariousSizes   
)

Definition at line 136 of file StringTest.cpp.

References EXPECT_EQ, i, and folly::stringPrintf().

136  {
137  // Test a wide variety of output sizes, making sure to cross the
138  // vsnprintf buffer boundary implementation detail.
139  for (int i = 0; i < 4096; ++i) {
140  string expected(i + 1, 'a');
141  expected = "X" + expected + "X";
142  string result = stringPrintf("%s", expected.c_str());
143  EXPECT_EQ(expected.size(), result.size());
144  EXPECT_EQ(expected, result);
145  }
146 
147  // clang-format off
148  EXPECT_EQ("abc12345678910111213141516171819202122232425xyz",
149  stringPrintf("abc%d%d%d%d%d%d%d%d%d%d%d%d%d%d"
150  "%d%d%d%d%d%d%d%d%d%d%dxyz",
151  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
152  17, 18, 19, 20, 21, 22, 23, 24, 25));
153  // clang-format on
154 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stringPrintf(const char *format,...)
Definition: String.cpp:223
TEST ( StringPrintf  ,
oldStringPrintfTests   
)

Definition at line 156 of file StringTest.cpp.

References a, b, EXPECT_EQ, folly::size(), and folly::stringPrintf().

156  {
157  EXPECT_EQ(string("a/b/c/d"), stringPrintf("%s/%s/%s/%s", "a", "b", "c", "d"));
158 
159  EXPECT_EQ(string(" 5 10"), stringPrintf("%5d %5d", 5, 10));
160 
161  // check printing w/ a big buffer
162  for (int size = (1 << 8); size <= (1 << 15); size <<= 1) {
163  string a(size, 'z');
164  string b = stringPrintf("%s", a.c_str());
165  EXPECT_EQ(a.size(), b.size());
166  }
167 }
char b
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stringPrintf(const char *format,...)
Definition: String.cpp:223
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
char a
TEST ( StringPrintf  ,
oldStringAppendf   
)

Definition at line 169 of file StringTest.cpp.

References EXPECT_EQ, s, and folly::stringAppendf().

169  {
170  string s = "hello";
171  stringAppendf(&s, "%s/%s/%s/%s", "a", "b", "c", "d");
172  EXPECT_EQ(string("helloa/b/c/d"), s);
173 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string & stringAppendf(std::string *output, const char *format,...)
Definition: String.cpp:240
static set< string > s
TEST ( Escape  ,
cEscape   
)

Definition at line 175 of file StringTest.cpp.

References EXPECT_EQ.

175  {
176  EXPECT_EQ("hello world", cEscape<std::string>("hello world"));
177  EXPECT_EQ(
178  "hello \\\\world\\\" goodbye",
179  cEscape<std::string>("hello \\world\" goodbye"));
180  EXPECT_EQ("hello\\nworld", cEscape<std::string>("hello\nworld"));
181  EXPECT_EQ("hello\\377\\376", cEscape<std::string>("hello\xff\xfe"));
182 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( Escape  ,
cUnescape   
)

Definition at line 184 of file StringTest.cpp.

References EXPECT_EQ, and EXPECT_THROW_RE.

184  {
185  EXPECT_EQ("hello world", cUnescape<std::string>("hello world"));
186  EXPECT_EQ(
187  "hello \\world\" goodbye",
188  cUnescape<std::string>("hello \\\\world\\\" goodbye"));
189  EXPECT_EQ("hello\nworld", cUnescape<std::string>("hello\\nworld"));
190  EXPECT_EQ("hello\nworld", cUnescape<std::string>("hello\\012world"));
191  EXPECT_EQ("hello\nworld", cUnescape<std::string>("hello\\x0aworld"));
192  EXPECT_EQ("hello\xff\xfe", cUnescape<std::string>("hello\\377\\376"));
193  EXPECT_EQ("hello\xff\xfe", cUnescape<std::string>("hello\\xff\\xfe"));
194  EXPECT_EQ("hello\\", cUnescape<std::string>("hello\\", false));
195 
197  cUnescape<std::string>("hello\\"),
198  std::invalid_argument,
199  "incomplete escape sequence");
201  cUnescape<std::string>("hello\\x"),
202  std::invalid_argument,
203  "incomplete hex escape sequence");
205  cUnescape<std::string>("hello\\q"),
206  std::invalid_argument,
207  "invalid escape sequence");
208 }
#define EXPECT_THROW_RE(statement, exceptionType, pattern)
Definition: TestUtils.h:119
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( Escape  ,
uriEscape   
)

Definition at line 210 of file StringTest.cpp.

References EXPECT_EQ, folly::PATH, and folly::QUERY.

210  {
211  EXPECT_EQ("hello%2c%20%2fworld", uriEscape<std::string>("hello, /world"));
212  EXPECT_EQ(
213  "hello%2c%20/world",
214  uriEscape<std::string>("hello, /world", UriEscapeMode::PATH));
215  EXPECT_EQ(
216  "hello%2c+%2fworld",
217  uriEscape<std::string>("hello, /world", UriEscapeMode::QUERY));
218  EXPECT_EQ(
219  "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.~",
220  uriEscape<std::string>(
221  "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.~"));
222 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( Escape  ,
uriUnescape   
)

Definition at line 224 of file StringTest.cpp.

References c, EXPECT_EQ, EXPECT_GE, EXPECT_LE, EXPECT_THROW, folly::QUERY, and s.

224  {
225  EXPECT_EQ("hello, /world", uriUnescape<std::string>("hello, /world"));
226  EXPECT_EQ("hello, /world", uriUnescape<std::string>("hello%2c%20%2fworld"));
227  EXPECT_EQ("hello,+/world", uriUnescape<std::string>("hello%2c+%2fworld"));
228  EXPECT_EQ(
229  "hello, /world",
230  uriUnescape<std::string>("hello%2c+%2fworld", UriEscapeMode::QUERY));
231  EXPECT_EQ("hello/", uriUnescape<std::string>("hello%2f"));
232  EXPECT_EQ("hello/", uriUnescape<std::string>("hello%2F"));
233  EXPECT_THROW({ uriUnescape<std::string>("hello%"); }, std::invalid_argument);
234  EXPECT_THROW({ uriUnescape<std::string>("hello%2"); }, std::invalid_argument);
235  EXPECT_THROW(
236  { uriUnescape<std::string>("hello%2g"); }, std::invalid_argument);
237 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( Escape  ,
uriEscapeAllCombinations   
)

Definition at line 248 of file StringTest.cpp.

References c, folly::basic_fbstring< E, T, A, Storage >::clear(), EXPECT_EQ, i, folly::uriEscape(), and folly::uriUnescape().

248  {
249  char c[3];
250  c[2] = '\0';
251  StringPiece in(c, 2);
252  fbstring tmp;
253  fbstring out;
254  for (int i = 0; i < 256; ++i) {
255  c[0] = i;
256  for (int j = 0; j < 256; ++j) {
257  c[1] = j;
258  tmp.clear();
259  out.clear();
260  uriEscape(in, tmp);
261  expectPrintable(tmp);
262  uriUnescape(tmp, out);
263  EXPECT_EQ(in, out);
264  }
265  }
266 }
void uriUnescape(StringPiece str, String &out, UriEscapeMode mode)
Definition: String-inl.h:201
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void uriEscape(StringPiece str, String &out, UriEscapeMode mode)
Definition: String-inl.h:166
char c
TEST ( Escape  ,
uriUnescapePercentDecoding   
)

Definition at line 276 of file StringTest.cpp.

References c, folly::basic_fbstring< E, T, A, Storage >::clear(), EXPECT_EQ, EXPECT_THROW, i, int64_t, folly::basic_fbstring< E, T, A, Storage >::size(), and folly::uriUnescape().

276  {
277  char c[4] = {'%', '\0', '\0', '\0'};
278  StringPiece in(c, 3);
279  fbstring out;
280  unsigned int expected = 0;
281  for (int i = 0; i < 256; ++i) {
282  c[1] = i;
283  for (int j = 0; j < 256; ++j) {
284  c[2] = j;
285  if (isHex(i) && isHex(j)) {
286  out.clear();
287  uriUnescape(in, out);
288  EXPECT_EQ(1, out.size());
289  EXPECT_EQ(1, sscanf(c + 1, "%x", &expected));
290  unsigned char v = out[0];
291  EXPECT_EQ(expected, v);
292  } else {
293  EXPECT_THROW({ uriUnescape(in, out); }, std::invalid_argument);
294  }
295  }
296  }
297 }
size_type size() const
Definition: FBString.h:1337
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
void uriUnescape(StringPiece str, String &out, UriEscapeMode mode)
Definition: String-inl.h:201
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char c
TEST ( PrettyPrint  ,
Basic   
)

Definition at line 438 of file StringTest.cpp.

References EXPECT_EQ, i, folly::PRETTY_NUM_TYPES, folly::prettyPrint(), PrettyTestCase::prettyString, PrettyTestCase::prettyType, and PrettyTestCase::realValue.

438  {
439  for (int i = 0; prettyTestCases[i].prettyType != PRETTY_NUM_TYPES; ++i) {
440  const PrettyTestCase& prettyTest = prettyTestCases[i];
441  EXPECT_EQ(
442  prettyTest.prettyString,
443  prettyPrint(prettyTest.realValue, prettyTest.prettyType));
444  }
445 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string prettyPrint(double val, PrettyType type, bool addSpace)
Definition: String.cpp:386
std::string prettyString
Definition: StringTest.cpp:308
PrettyTestCase prettyTestCases[]
Definition: StringTest.cpp:313
PrettyType prettyType
Definition: StringTest.cpp:310
TEST ( PrettyToDouble  ,
Basic   
)

Definition at line 447 of file StringTest.cpp.

References ADD_FAILURE, folly::exceptionStr(), EXPECT_DOUBLE_EQ, EXPECT_EQ, EXPECT_NEAR, EXPECT_THROW, i, folly::PRETTY_NUM_TYPES, folly::PRETTY_SI, folly::PRETTY_UNITS_METRIC, folly::prettyPrint(), PrettyTestCase::prettyString, folly::prettyToDouble(), PrettyTestCase::prettyType, PrettyTestCase::realValue, string, and x.

447  {
448  // check manually created tests
449  for (int i = 0; prettyTestCases[i].prettyType != PRETTY_NUM_TYPES; ++i) {
450  PrettyTestCase testCase = prettyTestCases[i];
451  PrettyType formatType = testCase.prettyType;
452  double x = testCase.realValue;
453  std::string testString = testCase.prettyString;
454  double recoveredX = 0;
455  try {
456  recoveredX = prettyToDouble(testString, formatType);
457  } catch (const std::exception& ex) {
458  ADD_FAILURE() << testCase.prettyString << " -> " << ex.what();
459  }
460  double relativeError =
461  fabs(x) < 1e-5 ? (x - recoveredX) : (x - recoveredX) / x;
462  EXPECT_NEAR(0, relativeError, 1e-3);
463  }
464 
465  // checks for compatibility with prettyPrint over the whole parameter space
466  for (int i = 0; i < PRETTY_NUM_TYPES; ++i) {
467  PrettyType formatType = static_cast<PrettyType>(i);
468  for (double x = 1e-18; x < 1e40; x *= 1.9) {
469  bool addSpace = static_cast<PrettyType>(i) == PRETTY_SI;
470  for (int it = 0; it < 2; ++it, addSpace = true) {
471  double recoveredX = 0;
472  try {
473  recoveredX =
474  prettyToDouble(prettyPrint(x, formatType, addSpace), formatType);
475  } catch (const std::exception& ex) {
477  }
478  double relativeError = (x - recoveredX) / x;
479  EXPECT_NEAR(0, relativeError, 1e-3);
480  }
481  }
482  }
483 
484  // check for incorrect values
485  EXPECT_THROW(prettyToDouble("10Mx", PRETTY_SI), std::range_error);
486  EXPECT_THROW(prettyToDouble("10 Mx", PRETTY_SI), std::range_error);
487  EXPECT_THROW(prettyToDouble("10 M x", PRETTY_SI), std::range_error);
488 
489  StringPiece testString = "10Mx";
491  EXPECT_EQ(testString, "x");
492 }
Definition: InvokeTest.cpp:58
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
fbstring exceptionStr(const std::exception &e)
const int x
std::string prettyPrint(double val, PrettyType type, bool addSpace)
Definition: String.cpp:386
std::string prettyString
Definition: StringTest.cpp:308
double prettyToDouble(folly::StringPiece *const prettyString, const PrettyType type)
Definition: String.cpp:416
#define EXPECT_NEAR(val1, val2, abs_error)
Definition: gtest.h:2043
#define EXPECT_DOUBLE_EQ(val1, val2)
Definition: gtest.h:2031
PrettyTestCase prettyTestCases[]
Definition: StringTest.cpp:313
const char * string
Definition: Conv.cpp:212
PrettyType prettyType
Definition: StringTest.cpp:310
#define ADD_FAILURE()
Definition: gtest.h:1808
TEST ( PrettyPrint  ,
HexDump   
)

Definition at line 494 of file StringTest.cpp.

References a, EXPECT_EQ, folly::hexDump(), and string.

494  {
495  std::string a("abc\x00\x02\xa0", 6); // embedded NUL
496  EXPECT_EQ(
497  "00000000 61 62 63 00 02 a0 "
498  "|abc... |\n",
499  hexDump(a.data(), a.size()));
500 
501  a = "abcdefghijklmnopqrstuvwxyz";
502  EXPECT_EQ(
503  "00000000 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 "
504  "|abcdefghijklmnop|\n"
505  "00000010 71 72 73 74 75 76 77 78 79 7a "
506  "|qrstuvwxyz |\n",
507  hexDump(a.data(), a.size()));
508 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void hexDump(const void *ptr, size_t size, OutIt out)
Definition: String-inl.h:645
char a
const char * string
Definition: Conv.cpp:212
TEST ( System  ,
errnoStr   
)

Definition at line 510 of file StringTest.cpp.

References folly::errnoStr(), EXPECT_EQ, folly::split(), and start.

510  {
511  errno = EACCES;
512  EXPECT_EQ(EACCES, errno);
513  EXPECT_EQ(EACCES, errno); // twice to make sure EXPECT_EQ doesn't change it
514 
515  fbstring expected = strerror(ENOENT);
516 
517  errno = EACCES;
518  EXPECT_EQ(expected, errnoStr(ENOENT));
519  // Ensure that errno isn't changed
520  EXPECT_EQ(EACCES, errno);
521 
522  // Per POSIX, all errno values are positive, so -1 is invalid
523  errnoStr(-1);
524 
525  // Ensure that errno isn't changed
526  EXPECT_EQ(EACCES, errno);
527 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
fbstring errnoStr(int err)
Definition: String.cpp:463
TEST ( Split  ,
split_vector   
)

Definition at line 832 of file StringTest.cpp.

832  {
833  splitTest<std::vector>();
834 }
TEST ( Split  ,
split_fbvector   
)

Definition at line 835 of file StringTest.cpp.

835  {
836  splitTest<folly::fbvector>();
837 }
TEST ( Split  ,
pieces_vector   
)

Definition at line 838 of file StringTest.cpp.

838  {
839  piecesTest<std::vector>();
840 }
TEST ( Split  ,
pieces_fbvector   
)

Definition at line 841 of file StringTest.cpp.

841  {
842  piecesTest<folly::fbvector>();
843 }
TEST ( Split  ,
fixed   
)

Definition at line 845 of file StringTest.cpp.

References a, b, c, EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, and folly::split().

845  {
846  StringPiece a, b, c, d;
847 
848  EXPECT_TRUE(folly::split<false>('.', "a.b.c.d", a, b, c, d));
849  EXPECT_TRUE(folly::split<false>('.', "a.b.c", a, b, c));
850  EXPECT_TRUE(folly::split<false>('.', "a.b", a, b));
851  EXPECT_TRUE(folly::split<false>('.', "a", a));
852 
853  EXPECT_TRUE(folly::split('.', "a.b.c.d", a, b, c, d));
854  EXPECT_TRUE(folly::split('.', "a.b.c", a, b, c));
855  EXPECT_TRUE(folly::split('.', "a.b", a, b));
856  EXPECT_TRUE(folly::split('.', "a", a));
857 
858  EXPECT_TRUE(folly::split<false>('.', "a.b.c", a, b, c));
859  EXPECT_EQ("a", a);
860  EXPECT_EQ("b", b);
861  EXPECT_EQ("c", c);
862  EXPECT_FALSE(folly::split<false>('.', "a.b", a, b, c));
863  EXPECT_TRUE(folly::split<false>('.', "a.b.c", a, b));
864  EXPECT_EQ("a", a);
865  EXPECT_EQ("b.c", b);
866 
867  EXPECT_TRUE(folly::split('.', "a.b.c", a, b, c));
868  EXPECT_EQ("a", a);
869  EXPECT_EQ("b", b);
870  EXPECT_EQ("c", c);
871  EXPECT_FALSE(folly::split('.', "a.b.c", a, b));
872  EXPECT_FALSE(folly::split('.', "a.b", a, b, c));
873 
874  EXPECT_TRUE(folly::split<false>('.', "a.b", a, b));
875  EXPECT_EQ("a", a);
876  EXPECT_EQ("b", b);
877  EXPECT_FALSE(folly::split<false>('.', "a", a, b));
878  EXPECT_TRUE(folly::split<false>('.', "a.b", a));
879  EXPECT_EQ("a.b", a);
880 
881  EXPECT_TRUE(folly::split('.', "a.b", a, b));
882  EXPECT_EQ("a", a);
883  EXPECT_EQ("b", b);
884  EXPECT_FALSE(folly::split('.', "a", a, b));
885  EXPECT_FALSE(folly::split('.', "a.b", a));
886 }
char b
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void split(const Delim &delimiter, const String &input, std::vector< OutputType > &out, bool ignoreEmpty)
Definition: String-inl.h:382
char a
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
char c
TEST ( Split  ,
std_string_fixed   
)

Definition at line 888 of file StringTest.cpp.

References a, b, c, EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, folly::split(), and string.

888  {
889  std::string a, b, c, d;
890 
891  EXPECT_TRUE(folly::split<false>('.', "a.b.c.d", a, b, c, d));
892  EXPECT_TRUE(folly::split<false>('.', "a.b.c", a, b, c));
893  EXPECT_TRUE(folly::split<false>('.', "a.b", a, b));
894  EXPECT_TRUE(folly::split<false>('.', "a", a));
895 
896  EXPECT_TRUE(folly::split('.', "a.b.c.d", a, b, c, d));
897  EXPECT_TRUE(folly::split('.', "a.b.c", a, b, c));
898  EXPECT_TRUE(folly::split('.', "a.b", a, b));
899  EXPECT_TRUE(folly::split('.', "a", a));
900 
901  EXPECT_TRUE(folly::split<false>('.', "a.b.c", a, b, c));
902  EXPECT_EQ("a", a);
903  EXPECT_EQ("b", b);
904  EXPECT_EQ("c", c);
905  EXPECT_FALSE(folly::split<false>('.', "a.b", a, b, c));
906  EXPECT_TRUE(folly::split<false>('.', "a.b.c", a, b));
907  EXPECT_EQ("a", a);
908  EXPECT_EQ("b.c", b);
909 
910  EXPECT_TRUE(folly::split('.', "a.b.c", a, b, c));
911  EXPECT_EQ("a", a);
912  EXPECT_EQ("b", b);
913  EXPECT_EQ("c", c);
914  EXPECT_FALSE(folly::split('.', "a.b.c", a, b));
915  EXPECT_FALSE(folly::split('.', "a.b", a, b, c));
916 
917  EXPECT_TRUE(folly::split<false>('.', "a.b", a, b));
918  EXPECT_EQ("a", a);
919  EXPECT_EQ("b", b);
920  EXPECT_FALSE(folly::split<false>('.', "a", a, b));
921  EXPECT_TRUE(folly::split<false>('.', "a.b", a));
922  EXPECT_EQ("a.b", a);
923 
924  EXPECT_TRUE(folly::split('.', "a.b", a, b));
925  EXPECT_EQ("a", a);
926  EXPECT_EQ("b", b);
927  EXPECT_FALSE(folly::split('.', "a", a, b));
928  EXPECT_FALSE(folly::split('.', "a.b", a));
929 }
char b
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void split(const Delim &delimiter, const String &input, std::vector< OutputType > &out, bool ignoreEmpty)
Definition: String-inl.h:382
char a
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
const char * string
Definition: Conv.cpp:212
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
char c
TEST ( Split  ,
fixed_convert   
)

Definition at line 931 of file StringTest.cpp.

References a, b, c, EXPECT_EQ, EXPECT_FALSE, EXPECT_NEAR, EXPECT_TRUE, and folly::split().

931  {
932  StringPiece a, d;
933  int b;
934  double c = 0;
935 
936  EXPECT_TRUE(folly::split(':', "a:13:14.7:b", a, b, c, d));
937  EXPECT_EQ("a", a);
938  EXPECT_EQ(13, b);
939  EXPECT_NEAR(14.7, c, 1e-10);
940  EXPECT_EQ("b", d);
941 
942  EXPECT_TRUE(folly::split<false>(':', "b:14:15.3:c", a, b, c, d));
943  EXPECT_EQ("b", a);
944  EXPECT_EQ(14, b);
945  EXPECT_NEAR(15.3, c, 1e-10);
946  EXPECT_EQ("c", d);
947 
948  EXPECT_FALSE(folly::split(':', "a:13:14.7:b", a, b, d));
949 
950  EXPECT_TRUE(folly::split<false>(':', "a:13:14.7:b", a, b, d));
951  EXPECT_EQ("a", a);
952  EXPECT_EQ(13, b);
953  EXPECT_EQ("14.7:b", d);
954 
955  // Enable verifying that a line only contains one field
956  EXPECT_TRUE(folly::split(' ', "hello", a));
957  EXPECT_FALSE(folly::split(' ', "hello world", a));
958 
959  // Test cases with std::ignore.
960  EXPECT_TRUE(folly::split(':', "a:13:14.7:b", std::ignore, b, c, d));
961  EXPECT_EQ(13, b);
962  EXPECT_NEAR(14.7, c, 1e-10);
963  EXPECT_EQ("b", d);
964 
965  EXPECT_TRUE(folly::split(':', "a:13:14.7:b", std::ignore, b, c, std::ignore));
966  EXPECT_EQ(13, b);
967  EXPECT_NEAR(14.7, c, 1e-10);
968 
969  EXPECT_TRUE(folly::split<false>(':', "a:13:14.7:b", a, b, std::ignore));
970  EXPECT_EQ("a", a);
971  EXPECT_EQ(13, b);
972 
973  EXPECT_FALSE(folly::split<false>(':', "a:13", std::ignore, b, std::ignore));
974  EXPECT_TRUE(folly::split<false>(':', ":13:", std::ignore, b, std::ignore));
975  EXPECT_EQ(13, b);
976 }
char b
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void split(const Delim &delimiter, const String &input, std::vector< OutputType > &out, bool ignoreEmpty)
Definition: String-inl.h:382
char a
#define EXPECT_NEAR(val1, val2, abs_error)
Definition: gtest.h:2043
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
char c
TEST ( Split  ,
fixed_convert_custom   
)

Definition at line 1009 of file StringTest.cpp.

References my::Blue, EXPECT_EQ, EXPECT_THROW, EXPECT_TRUE, my::Red, and folly::split().

1009  {
1010  my::Color c1, c2;
1011 
1012  EXPECT_TRUE(folly::split(',', "R,B", c1, c2));
1015 
1016  EXPECT_THROW(folly::split(',', "B,G", c1, c2), my::ColorError);
1017 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void split(const Delim &delimiter, const String &input, std::vector< OutputType > &out, bool ignoreEmpty)
Definition: String-inl.h:382
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( String  ,
join   
)

Definition at line 1019 of file StringTest.cpp.

References folly::empty(), EXPECT_EQ, EXPECT_TRUE, folly::join(), and gmock_output_test::output.

1019  {
1020  string output;
1021 
1022  std::vector<int> empty = {};
1023  join(":", empty, output);
1024  EXPECT_TRUE(output.empty());
1025 
1026  std::vector<std::string> input1 = {"1", "23", "456", ""};
1027  join(':', input1, output);
1028  EXPECT_EQ(output, "1:23:456:");
1029  output = join(':', input1);
1030  EXPECT_EQ(output, "1:23:456:");
1031 
1032  auto input2 = {1, 23, 456};
1033  join("-*-", input2, output);
1034  EXPECT_EQ(output, "1-*-23-*-456");
1035  output = join("-*-", input2);
1036  EXPECT_EQ(output, "1-*-23-*-456");
1037 
1038  auto input3 = {'f', 'a', 'c', 'e', 'b', 'o', 'o', 'k'};
1039  join("", input3, output);
1040  EXPECT_EQ(output, "facebook");
1041 
1042  join("_", {"", "f", "a", "c", "e", "b", "o", "o", "k", ""}, output);
1043  EXPECT_EQ(output, "_f_a_c_e_b_o_o_k_");
1044 
1045  output = join("", input3.begin(), input3.end());
1046  EXPECT_EQ(output, "facebook");
1047 
1048  std::multiset<char> input4(input3);
1049  output = join("", input4);
1050  EXPECT_EQ("abcefkoo", output);
1051  output = join("", input4.begin(), input4.end());
1052  EXPECT_EQ("abcefkoo", output);
1053 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr auto empty(C const &c) -> decltype(c.empty())
Definition: Access.h:55
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define join
TEST ( String  ,
hexlify   
)

Definition at line 1055 of file StringTest.cpp.

References folly::Range< Iter >::data(), EXPECT_EQ, EXPECT_TRUE, and folly::hexlify().

1055  {
1056  string input1 = "0123";
1057  string output1;
1058  EXPECT_TRUE(hexlify(input1, output1));
1059  EXPECT_EQ("30313233", output1);
1060 
1061  fbstring input2 = "abcdefg";
1062  input2[1] = 0;
1063  input2[3] = 0xff;
1064  input2[5] = 0xb6;
1065  fbstring output2;
1066  EXPECT_TRUE(hexlify(input2, output2));
1067  EXPECT_EQ("610063ff65b667", output2);
1068 
1069  EXPECT_EQ("666f6f626172", hexlify("foobar"));
1070  auto bytes = folly::make_array<uint8_t>(1, 2, 3, 4);
1071  EXPECT_EQ("01020304", hexlify(ByteRange{bytes.data(), bytes.size()}));
1072 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr Iter data() const
Definition: Range.h:446
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool hexlify(const InputString &input, OutputString &output, bool append_output)
Definition: String-inl.h:596
TEST ( String  ,
unhexlify   
)

Definition at line 1074 of file StringTest.cpp.

References EXPECT_EQ, EXPECT_FALSE, EXPECT_THROW, EXPECT_TRUE, folly::basic_fbstring< E, T, A, Storage >::size(), and folly::unhexlify().

1074  {
1075  string input1 = "30313233";
1076  string output1;
1077  EXPECT_TRUE(unhexlify(input1, output1));
1078  EXPECT_EQ(output1, "0123");
1079 
1080  fbstring input2 = "610063ff65b667";
1081  fbstring output2;
1082  EXPECT_TRUE(unhexlify(input2, output2));
1083  EXPECT_EQ(output2.size(), 7);
1084  EXPECT_EQ(output2[0], 'a');
1085  EXPECT_EQ(output2[1], 0);
1086  EXPECT_EQ(output2[2], 'c');
1087  EXPECT_EQ(output2[3] & 0xff, 0xff);
1088  EXPECT_EQ(output2[4], 'e');
1089  EXPECT_EQ(output2[5] & 0xff, 0xb6);
1090  EXPECT_EQ(output2[6], 'g');
1091 
1092  string input3 = "x";
1093  string output3;
1094  EXPECT_FALSE(unhexlify(input3, output3));
1095 
1096  string input4 = "xy";
1097  string output4;
1098  EXPECT_FALSE(unhexlify(input4, output4));
1099 
1100  EXPECT_EQ("foobar", unhexlify("666f6f626172"));
1101  EXPECT_EQ(StringPiece("foo\0bar", 7), unhexlify("666f6f00626172"));
1102  EXPECT_THROW(unhexlify("666f6fzz626172"), std::domain_error);
1103 }
size_type size() const
Definition: FBString.h:1337
bool unhexlify(const InputString &input, OutputString &output)
Definition: String-inl.h:616
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
Range< const char * > StringPiece
TEST ( String  ,
backslashify   
)

Definition at line 1105 of file StringTest.cpp.

References folly::backslashify(), EXPECT_EQ, and string.

1105  {
1106  EXPECT_EQ("abc", string("abc"));
1107  EXPECT_EQ("abc", backslashify(string("abc")));
1108  EXPECT_EQ("abc\\r", backslashify(string("abc\r")));
1109  EXPECT_EQ("abc\\x0d", backslashify(string("abc\r"), true));
1110  EXPECT_EQ("\\0\\0", backslashify(string(2, '\0')));
1111 
1112  StringPiece input1 = "abc\r";
1113  std::string output1 = backslashify(input1);
1114  EXPECT_EQ("abc\\r", output1);
1115 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void backslashify(folly::StringPiece input, OutputString &output, bool hex_style)
Definition: String-inl.h:507
const char * string
Definition: Conv.cpp:212
TEST ( String  ,
humanify   
)

Definition at line 1117 of file StringTest.cpp.

References folly::Range< Iter >::begin(), EXPECT_EQ, folly::humanify(), i, folly::Range< Iter >::size(), and folly::toLowerAscii().

1117  {
1118  // Simple cases; output is obvious.
1119  EXPECT_EQ("abc", humanify(string("abc")));
1120  EXPECT_EQ("abc\\\\r", humanify(string("abc\\r")));
1121  EXPECT_EQ("0xff", humanify(string("\xff")));
1122  EXPECT_EQ("abc\\xff", humanify(string("abc\xff")));
1123  EXPECT_EQ("abc\\b", humanify(string("abc\b")));
1124  EXPECT_EQ("0x00", humanify(string(1, '\0')));
1125  EXPECT_EQ("0x0000", humanify(string(2, '\0')));
1126 
1127  // Mostly printable, so backslash! 80, 60, and 40% printable, respectively
1128  EXPECT_EQ("aaaa\\xff", humanify(string("aaaa\xff")));
1129  EXPECT_EQ("aaa\\xff\\xff", humanify(string("aaa\xff\xff")));
1130  EXPECT_EQ("aa\\xff\\xff\\xff", humanify(string("aa\xff\xff\xff")));
1131 
1132  // 20% printable, and the printable portion isn't the prefix; hexify!
1133  EXPECT_EQ(
1134  "0xff61ffffff",
1135  humanify(string("\xff"
1136  "a\xff\xff\xff")));
1137 
1138  // Same as previous, except swap first two chars; prefix is
1139  // printable and within the threshold, so backslashify.
1140  EXPECT_EQ("a\\xff\\xff\\xff\\xff", humanify(string("a\xff\xff\xff\xff")));
1141 
1142  // Just too much unprintable; hex, despite prefix.
1143  EXPECT_EQ("0x61ffffffffff", humanify(string("a\xff\xff\xff\xff\xff")));
1144 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void humanify(const String1 &input, String2 &output)
Definition: String-inl.h:552
TEST ( String  ,
toLowerAsciiAligned   
)

Definition at line 1191 of file StringTest.cpp.

References i.

1191  {
1192  static const size_t kSize = 256;
1193  char input[kSize];
1194  for (size_t i = 0; i < kSize; i++) {
1195  input[i] = (char)(i & 0xff);
1196  }
1197  testToLowerAscii(Range<const char*>(input, kSize));
1198 }
TEST ( String  ,
toLowerAsciiUnaligned   
)

Definition at line 1200 of file StringTest.cpp.

References i.

1200  {
1201  static const size_t kSize = 256;
1202  char input[kSize];
1203  for (size_t i = 0; i < kSize; i++) {
1204  input[i] = (char)(i & 0xff);
1205  }
1206  // Test input buffers of several lengths to exercise all the
1207  // cases: buffer at the start/middle/end of an aligned block, plus
1208  // buffers that span multiple aligned blocks. The longest test input
1209  // is 3 unaligned bytes + 4 32-bit aligned bytes + 8 64-bit aligned
1210  // + 4 32-bit aligned + 3 unaligned = 22 bytes.
1211  for (size_t length = 1; length < 23; length++) {
1212  for (size_t offset = 0; offset + length <= kSize; offset++) {
1213  testToLowerAscii(Range<const char*>(input + offset, length));
1214  }
1215  }
1216 }
TEST ( String  ,
whitespace   
)

Definition at line 1218 of file StringTest.cpp.

References EXPECT_EQ, folly::ltrimWhitespace(), folly::rtrimWhitespace(), and folly::trimWhitespace().

1218  {
1219  // trimWhitespace:
1220  EXPECT_EQ("kavabanga", trimWhitespace("kavabanga"));
1221  EXPECT_EQ("kavabanga", trimWhitespace("kavabanga \t \n "));
1222  EXPECT_EQ("kavabanga", trimWhitespace(" \t \r \n \n kavabanga"));
1223  EXPECT_EQ("kavabanga", trimWhitespace("\t \r \n kavabanga \t \n "));
1224  EXPECT_EQ("kavabanga", trimWhitespace(" \t \r \n \n kavabanga"));
1225  EXPECT_EQ("kavabanga", trimWhitespace("\t \r \n kavabanga \t \n "));
1226  EXPECT_EQ(
1227  ltrimWhitespace(rtrimWhitespace("kavabanga")),
1228  rtrimWhitespace(ltrimWhitespace("kavabanga")));
1229  EXPECT_EQ(
1230  ltrimWhitespace(rtrimWhitespace("kavabanga \r\t\n")),
1231  rtrimWhitespace(ltrimWhitespace("kavabanga \r\t\n")));
1232  EXPECT_EQ("", trimWhitespace("\t \r \n \t \n "));
1233  EXPECT_EQ("", trimWhitespace(""));
1234  EXPECT_EQ("", trimWhitespace("\t"));
1235  EXPECT_EQ("", trimWhitespace("\r"));
1236  EXPECT_EQ("", trimWhitespace("\n"));
1237  EXPECT_EQ("", trimWhitespace("\t "));
1238  EXPECT_EQ("", trimWhitespace("\r "));
1239  EXPECT_EQ("", trimWhitespace("\n "));
1240  EXPECT_EQ("", trimWhitespace(" \t"));
1241  EXPECT_EQ("", trimWhitespace(" \r"));
1242  EXPECT_EQ("", trimWhitespace(" \n"));
1243 
1244  // ltrimWhitespace:
1245  EXPECT_EQ("kavabanga", ltrimWhitespace("\t kavabanga"));
1246  EXPECT_EQ("kavabanga \r\n", ltrimWhitespace("\t kavabanga \r\n"));
1247  EXPECT_EQ("", ltrimWhitespace("\r "));
1248  EXPECT_EQ("", ltrimWhitespace("\n "));
1249  EXPECT_EQ("", ltrimWhitespace("\r "));
1250 
1251  // rtrimWhitespace:
1252  EXPECT_EQ("\t kavabanga", rtrimWhitespace("\t kavabanga"));
1253  EXPECT_EQ("\t kavabanga", rtrimWhitespace("\t kavabanga \r\n"));
1254  EXPECT_EQ("", rtrimWhitespace("\r "));
1255  EXPECT_EQ("", rtrimWhitespace("\n "));
1256  EXPECT_EQ("", rtrimWhitespace("\r "));
1257 }
StringPiece ltrimWhitespace(StringPiece sp)
Definition: String.cpp:131
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
StringPiece rtrimWhitespace(StringPiece sp)
Definition: String.cpp:149
StringPiece trimWhitespace(StringPiece sp)
Definition: String.h:568
TEST ( String  ,
stripLeftMargin_really_empty   
)

Definition at line 1259 of file StringTest.cpp.

References EXPECT_EQ, and folly::stripLeftMargin().

1259  {
1260  auto input = "";
1261  auto expected = "";
1262  EXPECT_EQ(expected, stripLeftMargin(input));
1263 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stripLeftMargin(std::string s)
Definition: String.cpp:704
TEST ( String  ,
stripLeftMargin_empty   
)

Definition at line 1265 of file StringTest.cpp.

References EXPECT_EQ, and folly::stripLeftMargin().

1265  {
1266  auto input = R"TEXT(
1267  )TEXT";
1268  auto expected = "";
1269  EXPECT_EQ(expected, stripLeftMargin(input));
1270 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stripLeftMargin(std::string s)
Definition: String.cpp:704
TEST ( String  ,
stripLeftMargin_only_whitespace   
)

Definition at line 1272 of file StringTest.cpp.

References EXPECT_EQ, regex, and folly::stripLeftMargin().

1272  {
1273  // using ~ as a marker
1274  string input = R"TEXT(
1275  ~
1276  )TEXT";
1277  input = boost::regex_replace(input, boost::regex("~"), "");
1278  EXPECT_EQ("\n \n ", input);
1279  auto expected = "\n";
1280  EXPECT_EQ(expected, stripLeftMargin(input));
1281 }
regex
Definition: CMakeCache.txt:563
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stripLeftMargin(std::string s)
Definition: String.cpp:704
TEST ( String  ,
stripLeftMargin_only_uneven_whitespace   
)

Definition at line 1283 of file StringTest.cpp.

References EXPECT_EQ, regex, and folly::stripLeftMargin().

1283  {
1284  // using ~ as a marker1
1285  string input = R"TEXT(
1286  ~
1287  ~
1288  )TEXT";
1289  input = boost::regex_replace(input, boost::regex("~"), "");
1290  EXPECT_EQ("\n \n \n ", input);
1291  auto expected = "\n\n";
1292 
1293  EXPECT_EQ(expected, stripLeftMargin(input));
1294 }
regex
Definition: CMakeCache.txt:563
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stripLeftMargin(std::string s)
Definition: String.cpp:704
TEST ( String  ,
stripLeftMargin_one_line   
)

Definition at line 1296 of file StringTest.cpp.

References EXPECT_EQ, and folly::stripLeftMargin().

1296  {
1297  auto input = R"TEXT(
1298  hi there bob!
1299  )TEXT";
1300  auto expected = "hi there bob!\n";
1301  EXPECT_EQ(expected, stripLeftMargin(input));
1302 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stripLeftMargin(std::string s)
Definition: String.cpp:704
TEST ( String  ,
stripLeftMargin_two_lines   
)

Definition at line 1304 of file StringTest.cpp.

References EXPECT_EQ, and folly::stripLeftMargin().

1304  {
1305  auto input = R"TEXT(
1306  hi there bob!
1307  nice weather today!
1308  )TEXT";
1309  auto expected = "hi there bob!\nnice weather today!\n";
1310  EXPECT_EQ(expected, stripLeftMargin(input));
1311 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stripLeftMargin(std::string s)
Definition: String.cpp:704
TEST ( String  ,
stripLeftMargin_three_lines_uneven   
)

Definition at line 1313 of file StringTest.cpp.

References EXPECT_EQ, and folly::stripLeftMargin().

1313  {
1314  auto input = R"TEXT(
1315  hi there bob!
1316  nice weather today!
1317  so long!
1318  )TEXT";
1319  auto expected = " hi there bob!\nnice weather today!\n so long!\n";
1320  EXPECT_EQ(expected, stripLeftMargin(input));
1321 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stripLeftMargin(std::string s)
Definition: String.cpp:704
TEST ( String  ,
stripLeftMargin_preceding_blank_lines   
)

Definition at line 1323 of file StringTest.cpp.

References EXPECT_EQ, and folly::stripLeftMargin().

1323  {
1324  auto input = R"TEXT(
1325 
1326 
1327  hi there bob!
1328  )TEXT";
1329  auto expected = "\n\nhi there bob!\n";
1330  EXPECT_EQ(expected, stripLeftMargin(input));
1331 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stripLeftMargin(std::string s)
Definition: String.cpp:704
TEST ( String  ,
stripLeftMargin_succeeding_blank_lines   
)

Definition at line 1333 of file StringTest.cpp.

References EXPECT_EQ, and folly::stripLeftMargin().

1333  {
1334  auto input = R"TEXT(
1335  hi there bob!
1336 
1337 
1338  )TEXT";
1339  auto expected = "hi there bob!\n\n\n";
1340  EXPECT_EQ(expected, stripLeftMargin(input));
1341 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stripLeftMargin(std::string s)
Definition: String.cpp:704
TEST ( String  ,
stripLeftMargin_interstitial_undented_whiteline   
)

Definition at line 1343 of file StringTest.cpp.

References EXPECT_EQ, regex, and folly::stripLeftMargin().

1343  {
1344  // using ~ as a marker
1345  string input = R"TEXT(
1346  hi there bob!
1347  ~
1348  so long!
1349  )TEXT";
1350  input = boost::regex_replace(input, boost::regex(" +~"), "");
1351  EXPECT_EQ("\n hi there bob!\n\n so long!\n ", input);
1352  auto expected = "hi there bob!\n\nso long!\n";
1353  EXPECT_EQ(expected, stripLeftMargin(input));
1354 }
regex
Definition: CMakeCache.txt:563
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stripLeftMargin(std::string s)
Definition: String.cpp:704
TEST ( String  ,
stripLeftMargin_interstitial_dedented_whiteline   
)

Definition at line 1356 of file StringTest.cpp.

References EXPECT_EQ, regex, and folly::stripLeftMargin().

1356  {
1357  // using ~ as a marker
1358  string input = R"TEXT(
1359  hi there bob!
1360  ~
1361  so long!
1362  )TEXT";
1363  input = boost::regex_replace(input, boost::regex("~"), "");
1364  EXPECT_EQ("\n hi there bob!\n \n so long!\n ", input);
1365  auto expected = "hi there bob!\n\nso long!\n";
1366  EXPECT_EQ(expected, stripLeftMargin(input));
1367 }
regex
Definition: CMakeCache.txt:563
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stripLeftMargin(std::string s)
Definition: String.cpp:704
TEST ( String  ,
stripLeftMargin_interstitial_equidented_whiteline   
)

Definition at line 1369 of file StringTest.cpp.

References EXPECT_EQ, regex, and folly::stripLeftMargin().

1369  {
1370  // using ~ as a marker
1371  string input = R"TEXT(
1372  hi there bob!
1373  ~
1374  so long!
1375  )TEXT";
1376  input = boost::regex_replace(input, boost::regex("~"), "");
1377  EXPECT_EQ("\n hi there bob!\n \n so long!\n ", input);
1378  auto expected = "hi there bob!\n\nso long!\n";
1379  EXPECT_EQ(expected, stripLeftMargin(input));
1380 }
regex
Definition: CMakeCache.txt:563
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stripLeftMargin(std::string s)
Definition: String.cpp:704
TEST ( String  ,
stripLeftMargin_interstitial_indented_whiteline   
)

Definition at line 1382 of file StringTest.cpp.

References EXPECT_EQ, regex, and folly::stripLeftMargin().

1382  {
1383  // using ~ as a marker
1384  string input = R"TEXT(
1385  hi there bob!
1386  ~
1387  so long!
1388  )TEXT";
1389  input = boost::regex_replace(input, boost::regex("~"), "");
1390  EXPECT_EQ("\n hi there bob!\n \n so long!\n ", input);
1391  auto expected = "hi there bob!\n \nso long!\n";
1392  EXPECT_EQ(expected, stripLeftMargin(input));
1393 }
regex
Definition: CMakeCache.txt:563
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stripLeftMargin(std::string s)
Definition: String.cpp:704
TEST ( String  ,
stripLeftMargin_no_pre_whitespace   
)

Definition at line 1395 of file StringTest.cpp.

References EXPECT_EQ, regex, and folly::stripLeftMargin().

1395  {
1396  // using ~ as a marker
1397  string input = R"TEXT( hi there bob!
1398  ~
1399  so long!
1400  )TEXT";
1401  input = boost::regex_replace(input, boost::regex("~"), "");
1402  EXPECT_EQ(" hi there bob!\n \n so long!\n ", input);
1403  auto expected = "hi there bob!\n \nso long!\n";
1404  EXPECT_EQ(expected, stripLeftMargin(input));
1405 }
regex
Definition: CMakeCache.txt:563
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stripLeftMargin(std::string s)
Definition: String.cpp:704
TEST ( String  ,
stripLeftMargin_no_post_whitespace   
)

Definition at line 1407 of file StringTest.cpp.

References EXPECT_EQ, regex, and folly::stripLeftMargin().

1407  {
1408  // using ~ as a marker
1409  string input = R"TEXT(
1410  hi there bob!
1411  ~
1412  so long! )TEXT";
1413  input = boost::regex_replace(input, boost::regex("~"), "");
1414  EXPECT_EQ("\n hi there bob!\n \n so long! ", input);
1415  auto expected = "hi there bob!\n \nso long! ";
1416  EXPECT_EQ(expected, stripLeftMargin(input));
1417 }
regex
Definition: CMakeCache.txt:563
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stripLeftMargin(std::string s)
Definition: String.cpp:704
void vprintfCheck ( const char *  expected,
const char *  fmt,
  ... 
)

Definition at line 76 of file StringTest.cpp.

References EXPECT_EQ, prefix(), SCOPE_EXIT, string, folly::stringVAppendf(), and folly::stringVPrintf().

Referenced by TEST().

76  {
77  va_list apOrig;
78  va_start(apOrig, fmt);
79  SCOPE_EXIT {
80  va_end(apOrig);
81  };
82  va_list ap;
83  va_copy(ap, apOrig);
84  SCOPE_EXIT {
85  va_end(ap);
86  };
87 
88  // Check both APIs for calling stringVPrintf()
89  EXPECT_EQ(expected, stringVPrintf(fmt, ap));
90  va_end(ap);
91  va_copy(ap, apOrig);
92 
93  std::string out;
94  stringVPrintf(&out, fmt, ap);
95  va_end(ap);
96  va_copy(ap, apOrig);
97  EXPECT_EQ(expected, out);
98 
99  // Check stringVAppendf() as well
100  std::string prefix = "foobar";
101  out = prefix;
102  EXPECT_EQ(prefix + expected, stringVAppendf(&out, fmt, ap));
103  va_end(ap);
104  va_copy(ap, apOrig);
105 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string stringVPrintf(const char *format, va_list ap)
Definition: String.cpp:232
#define SCOPE_EXIT
Definition: ScopeGuard.h:274
std::string & stringVAppendf(std::string *output, const char *format, va_list ap)
Definition: String.cpp:250
bool prefix(Cursor &c, uint32_t expected)
const char * string
Definition: Conv.cpp:212
void vprintfError ( const char *  fmt,
  ... 
)

Definition at line 107 of file StringTest.cpp.

References EXPECT_THROW, SCOPE_EXIT, and folly::stringVPrintf().

Referenced by TEST().

107  {
108  va_list ap;
109  va_start(ap, fmt);
110  SCOPE_EXIT {
111  va_end(ap);
112  };
113 
114 #ifdef HAVE_VSNPRINTF_ERRORS
115  // OSX's sprintf family does not return a negative number on a bad format
116  // string, but Linux does. It's unclear to me which behavior is more
117  // correct.
118  EXPECT_THROW({ stringVPrintf(fmt, ap); }, std::runtime_error);
119 #endif
120 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
std::string stringVPrintf(const char *format, va_list ap)
Definition: String.cpp:232
#define SCOPE_EXIT
Definition: ScopeGuard.h:274

Variable Documentation

PrettyTestCase prettyTestCases[]

Definition at line 313 of file StringTest.cpp.