Path: blob/main/contrib/kyua/utils/cmdline/ui_mock.cpp
48178 views
// Copyright 2010 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "utils/cmdline/ui_mock.hpp"2930#include <iostream>3132#include "utils/optional.ipp"3334using utils::cmdline::ui_mock;35using utils::none;36using utils::optional;373839/// Constructs a new mock UI.40///41/// \param screen_width_ The width of the screen to use for testing purposes.42/// Defaults to 0 to prevent uncontrolled wrapping on our tests.43ui_mock::ui_mock(const std::size_t screen_width_) :44_screen_width(screen_width_)45{46}474849/// Writes a line to stderr and records it for further inspection.50///51/// \param message The line to print and record, without the trailing newline52/// character.53/// \param newline Whether to append a newline to the message or not.54void55ui_mock::err(const std::string& message, const bool newline)56{57if (newline)58std::cerr << message << "\n";59else {60std::cerr << message << "\n";61std::cerr.flush();62}63_err_log.push_back(message);64}656667/// Writes a line to stdout and records it for further inspection.68///69/// \param message The line to print and record, without the trailing newline70/// character.71/// \param newline Whether to append a newline to the message or not.72void73ui_mock::out(const std::string& message, const bool newline)74{75if (newline)76std::cout << message << "\n";77else {78std::cout << message << "\n";79std::cout.flush();80}81_out_log.push_back(message);82}838485/// Queries the width of the screen.86///87/// \return Always none, as we do not want to depend on line wrapping in our88/// tests.89optional< std::size_t >90ui_mock::screen_width(void) const91{92return _screen_width > 0 ? optional< std::size_t >(_screen_width) : none;93}949596/// Gets all the lines written to stderr.97///98/// \return The printed lines.99const std::vector< std::string >&100ui_mock::err_log(void) const101{102return _err_log;103}104105106/// Gets all the lines written to stdout.107///108/// \return The printed lines.109const std::vector< std::string >&110ui_mock::out_log(void) const111{112return _out_log;113}114115116