// Copyright 2010 Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/// Fake command implementation that crashes during its execution.
class cmd_mock_crash : public cli::cli_command {
public:
/// Constructs a new mock command.
///
/// All command parameters are set to irrelevant values.
cmd_mock_crash(void) :
cli::cli_command("mock_error", "", 0, 0, "Mock command that crashes")
{
}
/// Runs the mock command.
///
/// \param unused_ui Object to interact with the I/O of the program.
/// \param unused_cmdline Representation of the command line to the
/// subcommand.
/// \param unused_user_config The runtime configuration of the program.
///
/// \return Nothing because this function always aborts.
int
run(cmdline::ui* UTILS_UNUSED_PARAM(ui),
const cmdline::parsed_cmdline& UTILS_UNUSED_PARAM(cmdline),
const config::tree& UTILS_UNUSED_PARAM(user_config))
{
std::abort();
}
};
/// Fake command implementation that throws an exception during its execution.
class cmd_mock_error : public cli::cli_command {
/// Whether the command raises an exception captured by the parent or not.
///
/// If this is true, the command will raise a std::runtime_error exception
/// or a subclass of it. The main program is in charge of capturing these
/// and reporting them appropriately. If false, this raises another
/// exception that does not inherit from std::runtime_error.
bool _unhandled;
public:
/// Constructs a new mock command.
///
/// \param unhandled If true, make run raise an exception not catched by the
/// main program.
cmd_mock_error(const bool unhandled) :
cli::cli_command("mock_error", "", 0, 0,
"Mock command that raises an error"),
_unhandled(unhandled)
{
}
/// Runs the mock command.
///
/// \param unused_ui Object to interact with the I/O of the program.
/// \param unused_cmdline Representation of the command line to the
/// subcommand.
/// \param unused_user_config The runtime configuration of the program.
///
/// \return Nothing because this function always aborts.
///
/// \throw std::logic_error If _unhandled is true.
/// \throw std::runtime_error If _unhandled is false.
int
run(cmdline::ui* UTILS_UNUSED_PARAM(ui),
const cmdline::parsed_cmdline& UTILS_UNUSED_PARAM(cmdline),
const config::tree& UTILS_UNUSED_PARAM(user_config))
{
if (_unhandled)
throw std::logic_error("This is unhandled");
else
throw std::runtime_error("Runtime error");
}
};
/// Fake command implementation that prints messages during its execution.
class cmd_mock_write : public cli::cli_command {
public:
/// Constructs a new mock command.
///
/// All command parameters are set to irrelevant values.
cmd_mock_write(void) : cli::cli_command(
"mock_write", "", 0, 0, "Mock command that prints output")
{
}
/// Runs the mock command.
///
/// \param ui Object to interact with the I/O of the program.
/// \param unused_cmdline Representation of the command line to the
/// subcommand.
/// \param unused_user_config The runtime configuration of the program.
///
/// \return Nothing because this function always aborts.
int
run(cmdline::ui* ui,
const cmdline::parsed_cmdline& UTILS_UNUSED_PARAM(cmdline),
const config::tree& UTILS_UNUSED_PARAM(user_config))
{
ui->out("stdout message from subcommand");
ui->err("stderr message from subcommand");
return EXIT_FAILURE;
}
};