OpenVPN 3 Core Library
Loading...
Searching...
No Matches
test_format.cpp
Go to the documentation of this file.
1#include "test_common.hpp"
2#include <iostream>
3
4
7
10
11using namespace openvpn;
12
13class MyObj
14{
15 public:
16 MyObj(int v)
17 : value(v)
18 {
19 }
20
21 std::string to_string() const
22 {
23 return std::to_string(value);
24 }
25
26 private:
27 int value;
28};
29
31
32const std::string expected = "7\n"
33 "foo\n"
34 "bar\n"
35 "3.141593\n"
36 "3\n"
37 "1\n"
38 "0\n"
39 "pi is not 3 nor is it 7 ; it is 3.14159 ...\n"
40 "pi is 'not' 3 nor is it 7 ; it is 3.141593... (and has 99% less fat!)\n"
41 "the year is 2015 and the weather is \"partly cloudy\"\n"
42 "where am I? is it still 2015?\n"
43 "no, it's 1666... bring out yer dedd?\n"
44 "save 20%!\n"
45 "no wait... save? 99.9999%!\n"
46 "extra argument is here\n"
47 "is the question true or false?\n"
48 "more extra arguments are here\n"
49 "null string ''\n"
50 "nullptr 'nullptr'\n"
51 "foo=bar non const\n"
52 "EX1: bad foo\n"
53 "EX2: this prog is done 4 U\n";
54
55TEST(Misc, Format)
56{
57 std::ostringstream os;
58
59 const MyObj seven(7);
60 const std::string foo = "foo";
61 const char *const bar = "bar";
62 const double pi = 3.14159265;
63 const int three = 3;
64 const std::string weather = "partly cloudy";
65 char *nc = const_cast<char *>("non const");
66
67 os << to_string(seven) << '\n';
68 os << to_string(foo) << '\n';
69 os << to_string(bar) << '\n';
70 os << to_string(pi) << '\n';
71 os << to_string(three) << '\n';
72 os << to_string(true) << '\n';
73 os << to_string(false) << '\n';
74 os << prints("pi", "is", std::string("not"), 3, "nor is it", seven, ';', "it", "is", pi, "...") << '\n';
75 os << printfmt("pi is %r %s nor is it %s ; it is %s... (and has %s%% less %s!)", "not", 3, seven, pi, 99, std::string("fat")) << '\n';
76 os << printfmt("the year is %s and the weather is %R", 2015, weather) << '\n';
77 os << printfmt("where am %s? is it still %s?", 'I', 2015) << '\n';
78 os << printfmt("no, it's %s... bring out yer dedd%s", 1666) << '\n';
79 os << printfmt("save 20%%!") << '\n';
80 os << printfmt("no wait... save%s 99.9999%%!") << '\n';
81 os << printfmt("extra argument is here", 1) << '\n';
82 os << printfmt("is the question %s or %s?", true, false) << '\n';
83 os << printfmt("more extra arguments are here", 1, 2, 3, 4) << '\n';
84 os << printfmt("null string '%s'", static_cast<const char *>(nullptr)) << '\n';
85 os << printfmt("nullptr '%s'", nullptr) << '\n';
86 os << printfmt("%s=%s %s", foo, bar, nc) << '\n';
87 try
88 {
89 const std::string exstr = "bad foo";
90 throw Exception(exstr);
91 }
92 catch (const std::exception &e)
93 {
94 os << prints("EX1:", e.what()) << '\n';
95 }
96 try
97 {
98 throw Exception(prints("this", "prog", "is", "done", 4, 'U'));
99 }
100 catch (const std::exception &e)
101 {
102 os << prints("EX2:", e.what()) << '\n';
103 }
104 const std::string actual = os.str();
105 ASSERT_EQ(expected, actual);
106}
107
108template <typename... Args>
109inline std::string pfmt(const std::string &fmt, Args... args)
110{
111#if 1
112 PrintFormatted<std::string> pf(fmt, 256);
113#else
115#endif
116 pf.process(args...);
117 return pf.str();
118}
int value
std::string to_string() const
MyObj(int v)
std::string str()
Definition format.hpp:243
std::string prints(Args... args)
Definition format.hpp:64
std::string printfmt(const std::string &fmt, Args... args)
Definition format.hpp:313
#define OPENVPN_OSTREAM(TYPE, METH)
Definition ostream.hpp:21
std::ostringstream os
TEST(Misc, Format)
const std::string expected
std::string pfmt(const std::string &fmt, Args... args)