1// Copyright 2011 Google Inc.
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 "engine/drivers/list_tests.hpp"
30
31#include "engine/exceptions.hpp"
32#include "engine/filters.hpp"
33#include "engine/kyuafile.hpp"
34#include "engine/test_program.hpp"
35#include "utils/optional.ipp"
36
37namespace fs = utils::fs;
38namespace list_tests = engine::drivers::list_tests;
39
40using utils::none;
41using utils::optional;
42
43
44namespace {
45
46
47/// Lists a single test program.
48///
49/// \param program The test program to print.
50/// \param filters [in,out] The filters used to select which test cases to
51///     print.  These filters are updated on output to mark which of them
52///     actually matched a test case.
53/// \param hooks The runtime hooks.
54static void
55list_test_program(const engine::test_program& program,
56                  engine::filters_state& filters,
57                  list_tests::base_hooks& hooks)
58{
59    const engine::test_cases_vector test_cases = program.test_cases();
60
61    for (engine::test_cases_vector::const_iterator iter = test_cases.begin();
62         iter != test_cases.end(); iter++) {
63        const engine::test_case_ptr tc = *iter;
64
65        if (filters.match_test_case(program.relative_path(), tc->name()))
66            hooks.got_test_case(*tc);
67    }
68}
69
70
71}  // anonymous namespace
72
73
74/// Pure abstract destructor.
75list_tests::base_hooks::~base_hooks(void)
76{
77}
78
79
80/// Executes the operation.
81///
82/// \param kyuafile_path The path to the Kyuafile to be loaded.
83/// \param build_root If not none, path to the built test programs.
84/// \param raw_filters The test case filters as provided by the user.
85/// \param hooks The hooks for this execution.
86///
87/// \returns A structure with all results computed by this driver.
88list_tests::result
89list_tests::drive(const fs::path& kyuafile_path,
90                  const optional< fs::path > build_root,
91                  const std::set< engine::test_filter >& raw_filters,
92                  base_hooks& hooks)
93{
94    const engine::kyuafile kyuafile = engine::kyuafile::load(
95        kyuafile_path, build_root);
96    filters_state filters(raw_filters);
97
98    for (test_programs_vector::const_iterator iter =
99         kyuafile.test_programs().begin();
100         iter != kyuafile.test_programs().end(); iter++) {
101        const test_program_ptr& test_program = *iter;
102
103        if (!filters.match_test_program(test_program->relative_path()))
104            continue;
105
106        list_test_program(*test_program, filters, hooks);
107    }
108
109    return result(filters.unused());
110}
111