1*11be35a1SLionel Sambuc // Copyright 2011 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc // documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc // may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc // without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc #include "utils/cmdline/ui.hpp"
30*11be35a1SLionel Sambuc
31*11be35a1SLionel Sambuc extern "C" {
32*11be35a1SLionel Sambuc #include <sys/ioctl.h>
33*11be35a1SLionel Sambuc
34*11be35a1SLionel Sambuc #include <fcntl.h>
35*11be35a1SLionel Sambuc #include <unistd.h>
36*11be35a1SLionel Sambuc }
37*11be35a1SLionel Sambuc
38*11be35a1SLionel Sambuc #include <cerrno>
39*11be35a1SLionel Sambuc #include <cstring>
40*11be35a1SLionel Sambuc
41*11be35a1SLionel Sambuc #include <atf-c++.hpp>
42*11be35a1SLionel Sambuc
43*11be35a1SLionel Sambuc #include "utils/cmdline/globals.hpp"
44*11be35a1SLionel Sambuc #include "utils/cmdline/ui_mock.hpp"
45*11be35a1SLionel Sambuc #include "utils/env.hpp"
46*11be35a1SLionel Sambuc #include "utils/format/macros.hpp"
47*11be35a1SLionel Sambuc #include "utils/optional.ipp"
48*11be35a1SLionel Sambuc #include "utils/text/table.hpp"
49*11be35a1SLionel Sambuc
50*11be35a1SLionel Sambuc namespace cmdline = utils::cmdline;
51*11be35a1SLionel Sambuc namespace text = utils::text;
52*11be35a1SLionel Sambuc
53*11be35a1SLionel Sambuc using utils::none;
54*11be35a1SLionel Sambuc using utils::optional;
55*11be35a1SLionel Sambuc
56*11be35a1SLionel Sambuc
57*11be35a1SLionel Sambuc namespace {
58*11be35a1SLionel Sambuc
59*11be35a1SLionel Sambuc
60*11be35a1SLionel Sambuc /// Reopens stdout as a tty and returns its width.
61*11be35a1SLionel Sambuc ///
62*11be35a1SLionel Sambuc /// \return The width of the tty in columns. If the width is wider than 80, the
63*11be35a1SLionel Sambuc /// result is 5 columns narrower to match the screen_width() algorithm.
64*11be35a1SLionel Sambuc static std::size_t
reopen_stdout(void)65*11be35a1SLionel Sambuc reopen_stdout(void)
66*11be35a1SLionel Sambuc {
67*11be35a1SLionel Sambuc const int fd = ::open("/dev/tty", O_WRONLY);
68*11be35a1SLionel Sambuc if (fd == -1)
69*11be35a1SLionel Sambuc ATF_SKIP(F("Cannot open tty for test: %s") % ::strerror(errno));
70*11be35a1SLionel Sambuc struct ::winsize ws;
71*11be35a1SLionel Sambuc if (::ioctl(fd, TIOCGWINSZ, &ws) == -1)
72*11be35a1SLionel Sambuc ATF_SKIP(F("Cannot determine size of tty: %s") % ::strerror(errno));
73*11be35a1SLionel Sambuc
74*11be35a1SLionel Sambuc if (fd != STDOUT_FILENO) {
75*11be35a1SLionel Sambuc if (::dup2(fd, STDOUT_FILENO) == -1)
76*11be35a1SLionel Sambuc ATF_SKIP(F("Failed to redirect stdout: %s") % ::strerror(errno));
77*11be35a1SLionel Sambuc ::close(fd);
78*11be35a1SLionel Sambuc }
79*11be35a1SLionel Sambuc
80*11be35a1SLionel Sambuc return ws.ws_col >= 80 ? ws.ws_col - 5 : ws.ws_col;
81*11be35a1SLionel Sambuc }
82*11be35a1SLionel Sambuc
83*11be35a1SLionel Sambuc
84*11be35a1SLionel Sambuc } // anonymous namespace
85*11be35a1SLionel Sambuc
86*11be35a1SLionel Sambuc
87*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ui__screen_width__columns_set__no_tty);
ATF_TEST_CASE_BODY(ui__screen_width__columns_set__no_tty)88*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ui__screen_width__columns_set__no_tty)
89*11be35a1SLionel Sambuc {
90*11be35a1SLionel Sambuc utils::setenv("COLUMNS", "4321");
91*11be35a1SLionel Sambuc ::close(STDOUT_FILENO);
92*11be35a1SLionel Sambuc
93*11be35a1SLionel Sambuc cmdline::ui ui;
94*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(4321 - 5, ui.screen_width().get());
95*11be35a1SLionel Sambuc }
96*11be35a1SLionel Sambuc
97*11be35a1SLionel Sambuc
98*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ui__screen_width__columns_set__tty);
ATF_TEST_CASE_BODY(ui__screen_width__columns_set__tty)99*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ui__screen_width__columns_set__tty)
100*11be35a1SLionel Sambuc {
101*11be35a1SLionel Sambuc utils::setenv("COLUMNS", "4321");
102*11be35a1SLionel Sambuc (void)reopen_stdout();
103*11be35a1SLionel Sambuc
104*11be35a1SLionel Sambuc cmdline::ui ui;
105*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(4321 - 5, ui.screen_width().get());
106*11be35a1SLionel Sambuc }
107*11be35a1SLionel Sambuc
108*11be35a1SLionel Sambuc
109*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ui__screen_width__columns_empty__no_tty);
ATF_TEST_CASE_BODY(ui__screen_width__columns_empty__no_tty)110*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ui__screen_width__columns_empty__no_tty)
111*11be35a1SLionel Sambuc {
112*11be35a1SLionel Sambuc utils::setenv("COLUMNS", "");
113*11be35a1SLionel Sambuc ::close(STDOUT_FILENO);
114*11be35a1SLionel Sambuc
115*11be35a1SLionel Sambuc cmdline::ui ui;
116*11be35a1SLionel Sambuc ATF_REQUIRE(!ui.screen_width());
117*11be35a1SLionel Sambuc }
118*11be35a1SLionel Sambuc
119*11be35a1SLionel Sambuc
120*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ui__screen_width__columns_empty__tty);
ATF_TEST_CASE_BODY(ui__screen_width__columns_empty__tty)121*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ui__screen_width__columns_empty__tty)
122*11be35a1SLionel Sambuc {
123*11be35a1SLionel Sambuc utils::setenv("COLUMNS", "");
124*11be35a1SLionel Sambuc const std::size_t columns = reopen_stdout();
125*11be35a1SLionel Sambuc
126*11be35a1SLionel Sambuc cmdline::ui ui;
127*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(columns, ui.screen_width().get());
128*11be35a1SLionel Sambuc }
129*11be35a1SLionel Sambuc
130*11be35a1SLionel Sambuc
131*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ui__screen_width__columns_invalid__no_tty);
ATF_TEST_CASE_BODY(ui__screen_width__columns_invalid__no_tty)132*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ui__screen_width__columns_invalid__no_tty)
133*11be35a1SLionel Sambuc {
134*11be35a1SLionel Sambuc utils::setenv("COLUMNS", "foo bar");
135*11be35a1SLionel Sambuc ::close(STDOUT_FILENO);
136*11be35a1SLionel Sambuc
137*11be35a1SLionel Sambuc cmdline::ui ui;
138*11be35a1SLionel Sambuc ATF_REQUIRE(!ui.screen_width());
139*11be35a1SLionel Sambuc }
140*11be35a1SLionel Sambuc
141*11be35a1SLionel Sambuc
142*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ui__screen_width__columns_invalid__tty);
ATF_TEST_CASE_BODY(ui__screen_width__columns_invalid__tty)143*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ui__screen_width__columns_invalid__tty)
144*11be35a1SLionel Sambuc {
145*11be35a1SLionel Sambuc utils::setenv("COLUMNS", "foo bar");
146*11be35a1SLionel Sambuc const std::size_t columns = reopen_stdout();
147*11be35a1SLionel Sambuc
148*11be35a1SLionel Sambuc cmdline::ui ui;
149*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(columns, ui.screen_width().get());
150*11be35a1SLionel Sambuc }
151*11be35a1SLionel Sambuc
152*11be35a1SLionel Sambuc
153*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ui__screen_width__tty_is_file);
ATF_TEST_CASE_BODY(ui__screen_width__tty_is_file)154*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ui__screen_width__tty_is_file)
155*11be35a1SLionel Sambuc {
156*11be35a1SLionel Sambuc utils::unsetenv("COLUMNS");
157*11be35a1SLionel Sambuc const int fd = ::open("test.txt", O_WRONLY | O_CREAT | O_TRUNC, 0755);
158*11be35a1SLionel Sambuc ATF_REQUIRE(fd != -1);
159*11be35a1SLionel Sambuc if (fd != STDOUT_FILENO) {
160*11be35a1SLionel Sambuc ATF_REQUIRE(::dup2(fd, STDOUT_FILENO) != -1);
161*11be35a1SLionel Sambuc ::close(fd);
162*11be35a1SLionel Sambuc }
163*11be35a1SLionel Sambuc
164*11be35a1SLionel Sambuc cmdline::ui ui;
165*11be35a1SLionel Sambuc ATF_REQUIRE(!ui.screen_width());
166*11be35a1SLionel Sambuc }
167*11be35a1SLionel Sambuc
168*11be35a1SLionel Sambuc
169*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ui__screen_width__cached);
ATF_TEST_CASE_BODY(ui__screen_width__cached)170*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ui__screen_width__cached)
171*11be35a1SLionel Sambuc {
172*11be35a1SLionel Sambuc cmdline::ui ui;
173*11be35a1SLionel Sambuc
174*11be35a1SLionel Sambuc utils::setenv("COLUMNS", "100");
175*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(100 - 5, ui.screen_width().get());
176*11be35a1SLionel Sambuc
177*11be35a1SLionel Sambuc utils::setenv("COLUMNS", "80");
178*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(100 - 5, ui.screen_width().get());
179*11be35a1SLionel Sambuc
180*11be35a1SLionel Sambuc utils::unsetenv("COLUMNS");
181*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(100 - 5, ui.screen_width().get());
182*11be35a1SLionel Sambuc }
183*11be35a1SLionel Sambuc
184*11be35a1SLionel Sambuc
185*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ui__err);
ATF_TEST_CASE_BODY(ui__err)186*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ui__err)
187*11be35a1SLionel Sambuc {
188*11be35a1SLionel Sambuc cmdline::ui_mock ui(10); // Keep shorter than message.
189*11be35a1SLionel Sambuc ui.err("This is a short message");
190*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1, ui.err_log().size());
191*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("This is a short message", ui.err_log()[0]);
192*11be35a1SLionel Sambuc ATF_REQUIRE(ui.out_log().empty());
193*11be35a1SLionel Sambuc }
194*11be35a1SLionel Sambuc
195*11be35a1SLionel Sambuc
196*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ui__out);
ATF_TEST_CASE_BODY(ui__out)197*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ui__out)
198*11be35a1SLionel Sambuc {
199*11be35a1SLionel Sambuc cmdline::ui_mock ui(10); // Keep shorter than message.
200*11be35a1SLionel Sambuc ui.out("This is a short message");
201*11be35a1SLionel Sambuc ATF_REQUIRE(ui.err_log().empty());
202*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1, ui.out_log().size());
203*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("This is a short message", ui.out_log()[0]);
204*11be35a1SLionel Sambuc }
205*11be35a1SLionel Sambuc
206*11be35a1SLionel Sambuc
207*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ui__out_wrap__no_refill);
ATF_TEST_CASE_BODY(ui__out_wrap__no_refill)208*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ui__out_wrap__no_refill)
209*11be35a1SLionel Sambuc {
210*11be35a1SLionel Sambuc cmdline::ui_mock ui(100);
211*11be35a1SLionel Sambuc ui.out_wrap("This is a short message");
212*11be35a1SLionel Sambuc ATF_REQUIRE(ui.err_log().empty());
213*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1, ui.out_log().size());
214*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("This is a short message", ui.out_log()[0]);
215*11be35a1SLionel Sambuc }
216*11be35a1SLionel Sambuc
217*11be35a1SLionel Sambuc
218*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ui__out_wrap__refill);
ATF_TEST_CASE_BODY(ui__out_wrap__refill)219*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ui__out_wrap__refill)
220*11be35a1SLionel Sambuc {
221*11be35a1SLionel Sambuc cmdline::ui_mock ui(16);
222*11be35a1SLionel Sambuc ui.out_wrap("This is a short message");
223*11be35a1SLionel Sambuc ATF_REQUIRE(ui.err_log().empty());
224*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(2, ui.out_log().size());
225*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("This is a short", ui.out_log()[0]);
226*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("message", ui.out_log()[1]);
227*11be35a1SLionel Sambuc }
228*11be35a1SLionel Sambuc
229*11be35a1SLionel Sambuc
230*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ui__out_tag_wrap__no_refill);
ATF_TEST_CASE_BODY(ui__out_tag_wrap__no_refill)231*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ui__out_tag_wrap__no_refill)
232*11be35a1SLionel Sambuc {
233*11be35a1SLionel Sambuc cmdline::ui_mock ui(100);
234*11be35a1SLionel Sambuc ui.out_tag_wrap("Some long tag: ", "This is a short message");
235*11be35a1SLionel Sambuc ATF_REQUIRE(ui.err_log().empty());
236*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1, ui.out_log().size());
237*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("Some long tag: This is a short message", ui.out_log()[0]);
238*11be35a1SLionel Sambuc }
239*11be35a1SLionel Sambuc
240*11be35a1SLionel Sambuc
241*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ui__out_tag_wrap__refill__repeat);
ATF_TEST_CASE_BODY(ui__out_tag_wrap__refill__repeat)242*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ui__out_tag_wrap__refill__repeat)
243*11be35a1SLionel Sambuc {
244*11be35a1SLionel Sambuc cmdline::ui_mock ui(32);
245*11be35a1SLionel Sambuc ui.out_tag_wrap("Some long tag: ", "This is a short message");
246*11be35a1SLionel Sambuc ATF_REQUIRE(ui.err_log().empty());
247*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(2, ui.out_log().size());
248*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("Some long tag: This is a short", ui.out_log()[0]);
249*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("Some long tag: message", ui.out_log()[1]);
250*11be35a1SLionel Sambuc }
251*11be35a1SLionel Sambuc
252*11be35a1SLionel Sambuc
253*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ui__out_tag_wrap__refill__no_repeat);
ATF_TEST_CASE_BODY(ui__out_tag_wrap__refill__no_repeat)254*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ui__out_tag_wrap__refill__no_repeat)
255*11be35a1SLionel Sambuc {
256*11be35a1SLionel Sambuc cmdline::ui_mock ui(32);
257*11be35a1SLionel Sambuc ui.out_tag_wrap("Some long tag: ", "This is a short message", false);
258*11be35a1SLionel Sambuc ATF_REQUIRE(ui.err_log().empty());
259*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(2, ui.out_log().size());
260*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("Some long tag: This is a short", ui.out_log()[0]);
261*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(" message", ui.out_log()[1]);
262*11be35a1SLionel Sambuc }
263*11be35a1SLionel Sambuc
264*11be35a1SLionel Sambuc
265*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ui__out_tag_wrap__tag_too_long);
ATF_TEST_CASE_BODY(ui__out_tag_wrap__tag_too_long)266*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ui__out_tag_wrap__tag_too_long)
267*11be35a1SLionel Sambuc {
268*11be35a1SLionel Sambuc cmdline::ui_mock ui(5);
269*11be35a1SLionel Sambuc ui.out_tag_wrap("Some long tag: ", "This is a short message");
270*11be35a1SLionel Sambuc ATF_REQUIRE(ui.err_log().empty());
271*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1, ui.out_log().size());
272*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("Some long tag: This is a short message", ui.out_log()[0]);
273*11be35a1SLionel Sambuc }
274*11be35a1SLionel Sambuc
275*11be35a1SLionel Sambuc
276*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ui__out_table__empty);
ATF_TEST_CASE_BODY(ui__out_table__empty)277*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ui__out_table__empty)
278*11be35a1SLionel Sambuc {
279*11be35a1SLionel Sambuc const text::table table(3);
280*11be35a1SLionel Sambuc
281*11be35a1SLionel Sambuc text::table_formatter formatter;
282*11be35a1SLionel Sambuc formatter.set_separator(" | ");
283*11be35a1SLionel Sambuc formatter.set_column_width(0, 23);
284*11be35a1SLionel Sambuc formatter.set_column_width(1, text::table_formatter::width_refill);
285*11be35a1SLionel Sambuc
286*11be35a1SLionel Sambuc cmdline::ui_mock ui(52);
287*11be35a1SLionel Sambuc ui.out_table(table, formatter, " ");
288*11be35a1SLionel Sambuc ATF_REQUIRE(ui.out_log().empty());
289*11be35a1SLionel Sambuc }
290*11be35a1SLionel Sambuc
291*11be35a1SLionel Sambuc
292*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ui__out_table__not_empty);
ATF_TEST_CASE_BODY(ui__out_table__not_empty)293*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ui__out_table__not_empty)
294*11be35a1SLionel Sambuc {
295*11be35a1SLionel Sambuc text::table table(3);
296*11be35a1SLionel Sambuc {
297*11be35a1SLionel Sambuc text::table_row row;
298*11be35a1SLionel Sambuc row.push_back("First");
299*11be35a1SLionel Sambuc row.push_back("Second");
300*11be35a1SLionel Sambuc row.push_back("Third");
301*11be35a1SLionel Sambuc table.add_row(row);
302*11be35a1SLionel Sambuc }
303*11be35a1SLionel Sambuc {
304*11be35a1SLionel Sambuc text::table_row row;
305*11be35a1SLionel Sambuc row.push_back("Fourth with some text");
306*11be35a1SLionel Sambuc row.push_back("Fifth with some more text");
307*11be35a1SLionel Sambuc row.push_back("Sixth foo");
308*11be35a1SLionel Sambuc table.add_row(row);
309*11be35a1SLionel Sambuc }
310*11be35a1SLionel Sambuc
311*11be35a1SLionel Sambuc text::table_formatter formatter;
312*11be35a1SLionel Sambuc formatter.set_separator(" | ");
313*11be35a1SLionel Sambuc formatter.set_column_width(0, 23);
314*11be35a1SLionel Sambuc formatter.set_column_width(1, text::table_formatter::width_refill);
315*11be35a1SLionel Sambuc
316*11be35a1SLionel Sambuc cmdline::ui_mock ui(52);
317*11be35a1SLionel Sambuc ui.out_table(table, formatter, " ");
318*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(4, ui.out_log().size());
319*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(" First | Second | Third",
320*11be35a1SLionel Sambuc ui.out_log()[0]);
321*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(" Fourth with some text | Fifth with | Sixth foo",
322*11be35a1SLionel Sambuc ui.out_log()[1]);
323*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(" | some more | ",
324*11be35a1SLionel Sambuc ui.out_log()[2]);
325*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(" | text | ",
326*11be35a1SLionel Sambuc ui.out_log()[3]);
327*11be35a1SLionel Sambuc }
328*11be35a1SLionel Sambuc
329*11be35a1SLionel Sambuc
330*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(print_error);
ATF_TEST_CASE_BODY(print_error)331*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(print_error)
332*11be35a1SLionel Sambuc {
333*11be35a1SLionel Sambuc cmdline::init("error-program");
334*11be35a1SLionel Sambuc cmdline::ui_mock ui;
335*11be35a1SLionel Sambuc cmdline::print_error(&ui, "The error");
336*11be35a1SLionel Sambuc ATF_REQUIRE(ui.out_log().empty());
337*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1, ui.err_log().size());
338*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("error-program: E: The error.", ui.err_log()[0]);
339*11be35a1SLionel Sambuc }
340*11be35a1SLionel Sambuc
341*11be35a1SLionel Sambuc
342*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(print_info);
ATF_TEST_CASE_BODY(print_info)343*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(print_info)
344*11be35a1SLionel Sambuc {
345*11be35a1SLionel Sambuc cmdline::init("info-program");
346*11be35a1SLionel Sambuc cmdline::ui_mock ui;
347*11be35a1SLionel Sambuc cmdline::print_info(&ui, "The info");
348*11be35a1SLionel Sambuc ATF_REQUIRE(ui.out_log().empty());
349*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1, ui.err_log().size());
350*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("info-program: I: The info.", ui.err_log()[0]);
351*11be35a1SLionel Sambuc }
352*11be35a1SLionel Sambuc
353*11be35a1SLionel Sambuc
354*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(print_warning);
ATF_TEST_CASE_BODY(print_warning)355*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(print_warning)
356*11be35a1SLionel Sambuc {
357*11be35a1SLionel Sambuc cmdline::init("warning-program");
358*11be35a1SLionel Sambuc cmdline::ui_mock ui;
359*11be35a1SLionel Sambuc cmdline::print_warning(&ui, "The warning");
360*11be35a1SLionel Sambuc ATF_REQUIRE(ui.out_log().empty());
361*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1, ui.err_log().size());
362*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("warning-program: W: The warning.", ui.err_log()[0]);
363*11be35a1SLionel Sambuc }
364*11be35a1SLionel Sambuc
365*11be35a1SLionel Sambuc
ATF_INIT_TEST_CASES(tcs)366*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
367*11be35a1SLionel Sambuc {
368*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ui__screen_width__columns_set__no_tty);
369*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ui__screen_width__columns_set__tty);
370*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ui__screen_width__columns_empty__no_tty);
371*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ui__screen_width__columns_empty__tty);
372*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ui__screen_width__columns_invalid__no_tty);
373*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ui__screen_width__columns_invalid__tty);
374*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ui__screen_width__tty_is_file);
375*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ui__screen_width__cached);
376*11be35a1SLionel Sambuc
377*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ui__err);
378*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ui__out);
379*11be35a1SLionel Sambuc
380*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ui__out_wrap__no_refill);
381*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ui__out_wrap__refill);
382*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ui__out_tag_wrap__no_refill);
383*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ui__out_tag_wrap__refill__repeat);
384*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ui__out_tag_wrap__refill__no_repeat);
385*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ui__out_tag_wrap__tag_too_long);
386*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ui__out_table__empty);
387*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ui__out_table__not_empty);
388*11be35a1SLionel Sambuc
389*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, print_error);
390*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, print_info);
391*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, print_warning);
392*11be35a1SLionel Sambuc }
393