1// Copyright 2010 The Kyua Authors.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9//   notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright
11//   notice, this list of conditions and the following disclaimer in the
12//   documentation and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors
14//   may be used to endorse or promote products derived from this software
15//   without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#include "cli/cmd_list.hpp"
30
31#include <cstdlib>
32#include <utility>
33#include <vector>
34
35#include "cli/common.ipp"
36#include "drivers/list_tests.hpp"
37#include "engine/filters.hpp"
38#include "model/metadata.hpp"
39#include "model/test_case.hpp"
40#include "model/test_program.hpp"
41#include "model/types.hpp"
42#include "utils/cmdline/options.hpp"
43#include "utils/cmdline/parser.ipp"
44#include "utils/cmdline/ui.hpp"
45#include "utils/defs.hpp"
46#include "utils/format/macros.hpp"
47#include "utils/fs/path.hpp"
48
49namespace cmdline = utils::cmdline;
50namespace config = utils::config;
51namespace fs = utils::fs;
52
53
54namespace {
55
56
57/// Hooks for list_tests to print test cases as they come.
58class progress_hooks : public drivers::list_tests::base_hooks {
59    /// The ui object to which to print the test cases.
60    cmdline::ui* _ui;
61
62    /// Whether to print test case details or just their names.
63    bool _verbose;
64
65public:
66    /// Initializes the hooks.
67    ///
68    /// \param ui_ The ui object to which to print the test cases.
69    /// \param verbose_ Whether to print test case details or just their names.
70    progress_hooks(cmdline::ui* ui_, const bool verbose_) :
71        _ui(ui_),
72        _verbose(verbose_)
73    {
74    }
75
76    /// Reports a test case as soon as it is found.
77    ///
78    /// \param test_program The test program containing the test case.
79    /// \param test_case_name The name of the located test case.
80    void
81    got_test_case(const model::test_program& test_program,
82                  const std::string& test_case_name)
83    {
84        cli::detail::list_test_case(_ui, _verbose, test_program,
85                                    test_case_name);
86    }
87};
88
89
90}  // anonymous namespace
91
92
93/// Lists a single test case.
94///
95/// \param [out] ui Object to interact with the I/O of the program.
96/// \param verbose Whether to be verbose or not.
97/// \param test_program The test program containing the test case to print.
98/// \param test_case_name The name of the test case to print.
99void
100cli::detail::list_test_case(cmdline::ui* ui, const bool verbose,
101                            const model::test_program& test_program,
102                            const std::string& test_case_name)
103{
104    const model::test_case& test_case = test_program.find(test_case_name);
105
106    const std::string id = format_test_case_id(test_program, test_case_name);
107    if (!verbose) {
108        ui->out(id);
109    } else {
110        ui->out(F("%s (%s)") % id % test_program.test_suite_name());
111
112        // TODO(jmmv): Running these for every test case is probably not the
113        // fastest thing to do.
114        const model::metadata default_md = model::metadata_builder().build();
115        const model::properties_map default_props = default_md.to_properties();
116
117        const model::metadata& test_md = test_case.get_metadata();
118        const model::properties_map test_props = test_md.to_properties();
119
120        for (model::properties_map::const_iterator iter = test_props.begin();
121             iter != test_props.end(); iter++) {
122            const model::properties_map::const_iterator default_iter =
123                default_props.find((*iter).first);
124            if (default_iter == default_props.end() ||
125                (*iter).second != (*default_iter).second)
126                ui->out(F("    %s = %s") % (*iter).first % (*iter).second);
127        }
128    }
129}
130
131
132/// Default constructor for cmd_list.
133cli::cmd_list::cmd_list(void) :
134    cli_command("list", "[test-program ...]", 0, -1,
135                "Lists test cases and their meta-data")
136{
137    add_option(build_root_option);
138    add_option(kyuafile_option);
139    add_option(cmdline::bool_option('v', "verbose", "Show properties"));
140}
141
142
143/// Entry point for the "list" subcommand.
144///
145/// \param ui Object to interact with the I/O of the program.
146/// \param cmdline Representation of the command line to the subcommand.
147/// \param user_config The runtime configuration of the program.
148///
149/// \return 0 to indicate success.
150int
151cli::cmd_list::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
152                   const config::tree& user_config)
153{
154    progress_hooks hooks(ui, cmdline.has_option("verbose"));
155    const drivers::list_tests::result result = drivers::list_tests::drive(
156        kyuafile_path(cmdline), build_root_path(cmdline),
157        parse_filters(cmdline.arguments()), user_config, hooks);
158
159    return report_unused_filters(result.unused_filters, ui) ?
160        EXIT_FAILURE : EXIT_SUCCESS;
161}
162