Line data Source code
1 : #include <wlog/formatter/formatter.hpp>
2 : #include "test.hpp"
3 : #include <sstream>
4 : #include <chrono>
5 :
6 : namespace
7 : {
8 :
9 : int test1();
10 : int test2();
11 : int test3();
12 :
13 : static const time_t test_time = 1512672862;
14 : static const char* ru_date = "Чт 07 дек 2017 21:54:22";
15 : static const char* ru_date2 = "Чтв 07 Дек 2017 21:54:22";
16 : static const char* ru_date3 = "Чт. 07 дек. 2017 21:54:22";
17 :
18 : static const char* en_date = "Thu 07 Dec 2017 09:54:22 PM MSK";
19 : static wlog::time_point tp;
20 :
21 1 : int test1()
22 : {
23 1 : std::stringstream ss;
24 2 : wlog::formatter_options opt;
25 2 : wlog::formatter_handlers hdr;
26 1 : opt.colorized = wlog::colorized_flags::date;
27 1 : wlog::formatter::date(ss, tp, opt, hdr);
28 1 : std::cout << ss.str() << std::endl;
29 1 : TEST( ss.str() == "\033[32m2017-12-07\033[0m" );
30 :
31 1 : ss.str(""); ss.clear();
32 :
33 1 : opt.locale="en_US.UTF-8";
34 1 : opt.colorized = wlog::colorized_flags::none;
35 1 : wlog::formatter::date(ss, tp, opt, hdr);
36 1 : std::cout << ss.str() << std::endl;
37 1 : TEST( ss.str() == en_date );
38 :
39 1 : ss.str(""); ss.clear();
40 1 : opt.locale="ru_RU.UTF-8";
41 1 : wlog::formatter::date(ss, tp, opt, hdr);
42 1 : std::cout << ss.str() << std::endl;
43 1 : TEST( ss.str() == ru_date || ss.str() == ru_date2 || ss.str() == ru_date3);
44 :
45 1 : ss.str(""); ss.clear();
46 1 : opt.locale="ru_RU.UTF-8";
47 1 : opt.hide = wlog::hide_flags::year;
48 1 : opt.resolution = wlog::resolutions::month;
49 1 : wlog::formatter::date(ss, tp, opt, hdr);
50 1 : std::cout << ss.str() << std::endl;
51 1 : TEST( ss.str() == "дек" || ss.str() == "Дек" || ss.str() == "дек.");
52 :
53 1 : ss.str(""); ss.clear();
54 1 : opt.locale="ru_RU.UTF-8";
55 1 : opt.hide = wlog::hide_flags::none;
56 1 : opt.resolution = wlog::resolutions::year;
57 1 : wlog::formatter::date(ss, tp, opt, hdr);
58 1 : std::cout << ss.str() << std::endl;
59 1 : TEST( ss.str() == "2017" );
60 :
61 2 : return 0;
62 : }
63 :
64 1 : int test2()
65 : {
66 1 : std::stringstream ss;
67 2 : wlog::formatter_options opt;
68 2 : wlog::formatter_handlers hdr;
69 1 : wlog::formatter::time(ss, tp, opt, hdr);
70 1 : std::cout << ss.str() << std::endl;
71 1 : TEST( ss.str() == "21:54:22" );
72 :
73 1 : ss.str(""); ss.clear();
74 1 : opt.locale="en_US.UTF-8";
75 1 : wlog::formatter::time(ss, tp, opt, hdr);
76 1 : std::cout << ss.str() << std::endl;
77 1 : TEST( ss.str() == "09:54:22 PM" );
78 :
79 1 : ss.str(""); ss.clear();
80 1 : opt.locale.clear();//="ru_RU.UTF-8";
81 1 : opt.hide = wlog::hide_flags::hours;
82 1 : opt.resolution = wlog::resolutions::minutes;
83 1 : wlog::formatter::time(ss, tp, opt, hdr);
84 1 : std::cout << "[" << ss.str() << "]" << std::endl;
85 1 : TEST( ss.str() == "54m" );
86 :
87 2 : return 0;
88 : }
89 :
90 1 : int test3()
91 : {
92 1 : std::stringstream ss;
93 2 : wlog::formatter_options opt;
94 2 : wlog::formatter_handlers hdr;
95 1 : opt.locale="ru_RU.UTF-8";
96 1 : opt.colorized = wlog::colorized_flags::none;
97 1 : opt.resolution = wlog::resolutions::deciseconds;
98 1 : wlog::formatter::fraction(ss, tp, opt, hdr);
99 1 : std::cout << ss.str() << std::endl;
100 1 : TEST( ss.str() == ".1" );
101 :
102 1 : ss.str(""); ss.clear();
103 1 : opt.resolution = wlog::resolutions::centiseconds;
104 1 : wlog::formatter::fraction(ss, tp, opt, hdr);
105 1 : std::cout << ss.str() << std::endl;
106 1 : TEST( ss.str() == ".12" );
107 :
108 1 : ss.str(""); ss.clear();
109 1 : opt.resolution = wlog::resolutions::milliseconds;
110 1 : wlog::formatter::fraction(ss, tp, opt, hdr);
111 1 : std::cout << ss.str() << std::endl;
112 1 : TEST( ss.str() == ".123" );
113 :
114 1 : ss.str(""); ss.clear();
115 1 : opt.resolution = wlog::resolutions::microseconds;
116 1 : wlog::formatter::fraction(ss, tp, opt, hdr);
117 1 : std::cout << ss.str() << std::endl;
118 1 : TEST( ss.str() == ".123456" );
119 :
120 1 : ss.str(""); ss.clear();
121 1 : opt.resolution = wlog::resolutions::nanoseconds;
122 1 : wlog::formatter::fraction(ss, tp, opt, hdr);
123 1 : std::cout << ss.str() << std::endl;
124 1 : TEST( ss.str() == ".123456000" );
125 :
126 2 : return 0;
127 : }
128 :
129 1 : int test4()
130 : {
131 1 : wlog::formatter_handlers hdr;
132 2 : wlog::formatter_options opt;
133 1 : opt.resolution = wlog::resolutions::microseconds;
134 1 : opt.colorized = wlog::colorized_flags::none;
135 :
136 : {
137 1 : std::stringstream ss;
138 2 : wlog::formatter fmt(opt, wlog::formatter_handlers() );
139 1 : fmt(ss, tp, "hello", "world", "!");
140 1 : std::cout << "[" << ss.str() << "]" << std::endl;
141 2 : std::string str1="2017-12-07 21:54:22.123456 hello world !";
142 2 : std::string str2=ss.str();
143 2 : TEST( str1 == str2 );
144 :
145 : }
146 :
147 1 : opt.resolution = wlog::resolutions::seconds;
148 : {
149 1 : std::stringstream ss;
150 2 : wlog::formatter fmt(opt, wlog::formatter_handlers());
151 1 : fmt(ss, tp, "hello", "world", "!");
152 1 : std::cout << ss.str() << std::endl;
153 2 : TEST( ss.str() == "2017-12-07 21:54:22 hello world !" );
154 : }
155 :
156 1 : opt.resolution = wlog::resolutions::minutes;
157 1 : opt.hide = wlog::hide_flags::year;
158 : {
159 1 : std::stringstream ss;
160 2 : wlog::formatter fmt(opt, wlog::formatter_handlers());
161 1 : fmt(ss, tp, "hello", "world", "!");
162 1 : std::cout << ss.str() << std::endl;
163 2 : TEST( ss.str() == "Thu Dec 07 21:54 hello world !" );
164 : }
165 :
166 1 : opt.resolution = wlog::resolutions::minutes;
167 1 : opt.hide = wlog::hide_flags::year | wlog::hide_flags::weekday;
168 : {
169 1 : std::stringstream ss;
170 2 : wlog::formatter fmt(opt, wlog::formatter_handlers());
171 1 : fmt(ss, tp, "hello", "world", "!");
172 1 : std::cout << ss.str() << std::endl;
173 2 : TEST( ss.str() == "Dec 07 21:54 hello world !" );
174 : }
175 :
176 1 : opt.resolution = wlog::resolutions::minutes;
177 1 : opt.colorized = wlog::colorized_flags::name | wlog::colorized_flags::ident | /*wlog::colorized_flags::message |*/ wlog::colorized_flags::time;
178 : //opt.color_map["hello1"] = "\033[94m";
179 1 : opt.hide = wlog::hide_flags::year | wlog::hide_flags::weekday;
180 : {
181 1 : std::stringstream ss;
182 2 : wlog::formatter fmt(opt, wlog::formatter_handlers());
183 1 : fmt(ss, tp, "hello", "world", "!");
184 2 : std::cout << ss.str() << std::endl;
185 : //TEST( ss.str() == "Dec 07 21:54 hello world !" );
186 : }
187 :
188 2 : return 0;
189 : }
190 :
191 :
192 : }
193 :
194 1 : int main()
195 : {
196 1 : setenv("TZ", "MSK-3", 1);
197 1 : tzset();
198 1 : tp = wlog::time_point::clock::from_time_t(test_time);
199 1 : tp += std::chrono::microseconds(123456);
200 :
201 1 : if ( int res = test1() )
202 0 : return res;
203 1 : if ( int res = test2() )
204 0 : return res;
205 1 : if ( int res = test3() )
206 0 : return res;
207 1 : if ( int res = test4() )
208 0 : return res;
209 :
210 1 : return 0;
211 3 : }
|