xref: /netbsd-src/external/bsd/kyua-cli/dist/utils/cmdline/ui_mock.cpp (revision 6b3a42af15b5e090c339512c790dd68f3d11a9d8)
1*6b3a42afSjmmv // Copyright 2010 Google Inc.
2*6b3a42afSjmmv // All rights reserved.
3*6b3a42afSjmmv //
4*6b3a42afSjmmv // Redistribution and use in source and binary forms, with or without
5*6b3a42afSjmmv // modification, are permitted provided that the following conditions are
6*6b3a42afSjmmv // met:
7*6b3a42afSjmmv //
8*6b3a42afSjmmv // * Redistributions of source code must retain the above copyright
9*6b3a42afSjmmv //   notice, this list of conditions and the following disclaimer.
10*6b3a42afSjmmv // * Redistributions in binary form must reproduce the above copyright
11*6b3a42afSjmmv //   notice, this list of conditions and the following disclaimer in the
12*6b3a42afSjmmv //   documentation and/or other materials provided with the distribution.
13*6b3a42afSjmmv // * Neither the name of Google Inc. nor the names of its contributors
14*6b3a42afSjmmv //   may be used to endorse or promote products derived from this software
15*6b3a42afSjmmv //   without specific prior written permission.
16*6b3a42afSjmmv //
17*6b3a42afSjmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*6b3a42afSjmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*6b3a42afSjmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*6b3a42afSjmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*6b3a42afSjmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*6b3a42afSjmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*6b3a42afSjmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*6b3a42afSjmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*6b3a42afSjmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*6b3a42afSjmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*6b3a42afSjmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*6b3a42afSjmmv 
29*6b3a42afSjmmv #include "utils/cmdline/ui_mock.hpp"
30*6b3a42afSjmmv 
31*6b3a42afSjmmv #include <iostream>
32*6b3a42afSjmmv 
33*6b3a42afSjmmv #include "utils/optional.ipp"
34*6b3a42afSjmmv 
35*6b3a42afSjmmv using utils::cmdline::ui_mock;
36*6b3a42afSjmmv using utils::none;
37*6b3a42afSjmmv using utils::optional;
38*6b3a42afSjmmv 
39*6b3a42afSjmmv 
40*6b3a42afSjmmv /// Constructs a new mock UI.
41*6b3a42afSjmmv ///
42*6b3a42afSjmmv /// \param screen_width_ The width of the screen to use for testing purposes.
43*6b3a42afSjmmv ///     Defaults to 0 to prevent uncontrolled wrapping on our tests.
ui_mock(const std::size_t screen_width_)44*6b3a42afSjmmv ui_mock::ui_mock(const std::size_t screen_width_) :
45*6b3a42afSjmmv     _screen_width(screen_width_)
46*6b3a42afSjmmv {
47*6b3a42afSjmmv }
48*6b3a42afSjmmv 
49*6b3a42afSjmmv 
50*6b3a42afSjmmv /// Writes a line to stderr and records it for further inspection.
51*6b3a42afSjmmv ///
52*6b3a42afSjmmv /// \param message The line to print and record, without the trailing newline
53*6b3a42afSjmmv ///     character.
54*6b3a42afSjmmv /// \param newline Whether to append a newline to the message or not.
55*6b3a42afSjmmv void
err(const std::string & message,const bool newline)56*6b3a42afSjmmv ui_mock::err(const std::string& message, const bool newline)
57*6b3a42afSjmmv {
58*6b3a42afSjmmv     if (newline)
59*6b3a42afSjmmv         std::cerr << message << "\n";
60*6b3a42afSjmmv     else {
61*6b3a42afSjmmv         std::cerr << message << "\n";
62*6b3a42afSjmmv         std::cerr.flush();
63*6b3a42afSjmmv     }
64*6b3a42afSjmmv     _err_log.push_back(message);
65*6b3a42afSjmmv }
66*6b3a42afSjmmv 
67*6b3a42afSjmmv 
68*6b3a42afSjmmv /// Writes a line to stdout and records it for further inspection.
69*6b3a42afSjmmv ///
70*6b3a42afSjmmv /// \param message The line to print and record, without the trailing newline
71*6b3a42afSjmmv ///     character.
72*6b3a42afSjmmv /// \param newline Whether to append a newline to the message or not.
73*6b3a42afSjmmv void
out(const std::string & message,const bool newline)74*6b3a42afSjmmv ui_mock::out(const std::string& message, const bool newline)
75*6b3a42afSjmmv {
76*6b3a42afSjmmv     if (newline)
77*6b3a42afSjmmv         std::cout << message << "\n";
78*6b3a42afSjmmv     else {
79*6b3a42afSjmmv         std::cout << message << "\n";
80*6b3a42afSjmmv         std::cout.flush();
81*6b3a42afSjmmv     }
82*6b3a42afSjmmv     _out_log.push_back(message);
83*6b3a42afSjmmv }
84*6b3a42afSjmmv 
85*6b3a42afSjmmv 
86*6b3a42afSjmmv /// Queries the width of the screen.
87*6b3a42afSjmmv ///
88*6b3a42afSjmmv /// \return Always none, as we do not want to depend on line wrapping in our
89*6b3a42afSjmmv /// tests.
90*6b3a42afSjmmv optional< std::size_t >
screen_width(void) const91*6b3a42afSjmmv ui_mock::screen_width(void) const
92*6b3a42afSjmmv {
93*6b3a42afSjmmv     return _screen_width > 0 ? optional< std::size_t >(_screen_width) : none;
94*6b3a42afSjmmv }
95*6b3a42afSjmmv 
96*6b3a42afSjmmv 
97*6b3a42afSjmmv /// Gets all the lines written to stderr.
98*6b3a42afSjmmv ///
99*6b3a42afSjmmv /// \return The printed lines.
100*6b3a42afSjmmv const std::vector< std::string >&
err_log(void) const101*6b3a42afSjmmv ui_mock::err_log(void) const
102*6b3a42afSjmmv {
103*6b3a42afSjmmv     return _err_log;
104*6b3a42afSjmmv }
105*6b3a42afSjmmv 
106*6b3a42afSjmmv 
107*6b3a42afSjmmv /// Gets all the lines written to stdout.
108*6b3a42afSjmmv ///
109*6b3a42afSjmmv /// \return The printed lines.
110*6b3a42afSjmmv const std::vector< std::string >&
out_log(void) const111*6b3a42afSjmmv ui_mock::out_log(void) const
112*6b3a42afSjmmv {
113*6b3a42afSjmmv     return _out_log;
114*6b3a42afSjmmv }
115