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
5
#include <
openvpn/common/size.hpp
>
6
#include <
openvpn/common/exception.hpp
>
7
8
#include <
openvpn/common/format.hpp
>
9
#include <
openvpn/common/ostream.hpp
>
10
11
using namespace
openvpn
;
12
13
class
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
30
OPENVPN_OSTREAM
(
MyObj
, to_string);
31
32
const
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
55
TEST
(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
108
template
<
typename
... Args>
109
inline
std::string
pfmt
(
const
std::string &fmt, Args... args)
110
{
111
#if 1
112
PrintFormatted<std::string>
pf(fmt, 256);
113
#else
114
PrintFormatted<std::ostringstream>
pf(fmt, 256);
115
#endif
116
pf.
process
(args...);
117
return
pf.
str
();
118
}
MyObj
Definition
test_format.cpp:14
MyObj::value
int value
Definition
test_format.cpp:27
MyObj::to_string
std::string to_string() const
Definition
test_format.cpp:21
MyObj::MyObj
MyObj(int v)
Definition
test_format.cpp:16
openvpn::Exception
Definition
exception.hpp:37
openvpn::PrintFormatted
Definition
format.hpp:214
openvpn::PrintFormatted::str
std::string str()
Definition
format.hpp:243
openvpn::PrintFormatted::process
void process()
Definition
format.hpp:224
exception.hpp
format.hpp
openvpn
Definition
ovpncli.cpp:97
openvpn::prints
std::string prints(Args... args)
Definition
format.hpp:64
openvpn::printfmt
std::string printfmt(const std::string &fmt, Args... args)
Definition
format.hpp:313
ostream.hpp
OPENVPN_OSTREAM
#define OPENVPN_OSTREAM(TYPE, METH)
Definition
ostream.hpp:21
size.hpp
os
std::ostringstream os
Definition
test_capture.cpp:1099
test_common.hpp
TEST
TEST(Misc, Format)
Definition
test_format.cpp:55
expected
const std::string expected
Definition
test_format.cpp:32
pfmt
std::string pfmt(const std::string &fmt, Args... args)
Definition
test_format.cpp:109
test
unittests
test_format.cpp
Generated by
1.9.8