Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/catch2/catch_list.cpp
blob: 91f581877a249323a59f7ab51ac4680d3b1ee7d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 *  Created by Phil on 5/11/2010.
 *  Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
 *
 *  Distributed under the Boost Software License, Version 1.0. (See accompanying
 *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 */

#include <catch2/catch_list.h>

#include <catch2/catch_interfaces_registry_hub.h>
#include <catch2/catch_interfaces_reporter.h>
#include <catch2/catch_interfaces_testcase.h>

#include <catch2/catch_context.h>
#include <catch2/catch_stream.h>
#include <catch2/catch_text.h>

#include <catch2/catch_console_colour.h>
#include <catch2/catch_test_spec_parser.h>
#include <catch2/catch_tostring.h>
#include <catch2/catch_string_manip.h>

#include <limits>
#include <algorithm>
#include <set>

namespace Catch {
    namespace {

        void listTests(IStreamingReporter& reporter, Config const& config) {
            TestSpec testSpec = config.testSpec();
            auto matchedTestCases = filterTests(getAllTestCasesSorted(config), testSpec, config);
            reporter.listTests(matchedTestCases, config);
        }

        void listTags(IStreamingReporter& reporter, Config const& config) {
            TestSpec testSpec = config.testSpec();
            std::vector<TestCaseHandle> matchedTestCases = filterTests(getAllTestCasesSorted(config), testSpec, config);

            std::map<StringRef, TagInfo> tagCounts;
            for (auto const& testCase : matchedTestCases) {
                for (auto const& tagName : testCase.getTestCaseInfo().tags) {
                    auto it = tagCounts.find(tagName.lowerCased);
                    if (it == tagCounts.end())
                        it = tagCounts.insert(std::make_pair(tagName.lowerCased, TagInfo())).first;
                    it->second.add(tagName.original);
                }
            }

            std::vector<TagInfo> infos; infos.reserve(tagCounts.size());
            for (auto const& tagc : tagCounts) {
                infos.push_back(std::move(tagc.second));
            }

            reporter.listTags(infos, config);
        }

        void listReporters(IStreamingReporter& reporter, Config const& config) {
            std::vector<ReporterDescription> descriptions;

            IReporterRegistry::FactoryMap const& factories = getRegistryHub().getReporterRegistry().getFactories();
            descriptions.reserve(factories.size());
            for (auto const& fac : factories) {
                descriptions.push_back({ fac.first, fac.second->getDescription() });
            }

            reporter.listReporters(descriptions, config);
        }

    } // end anonymous namespace

    void TagInfo::add( StringRef spelling ) {
        ++count;
        spellings.insert( spelling );
    }

    std::string TagInfo::all() const {
        // 2 per tag for brackets '[' and ']'
        size_t size =  spellings.size() * 2;
        for (auto const& spelling : spellings) {
            size += spelling.size();
        }

        std::string out; out.reserve(size);
        for (auto const& spelling : spellings) {
            out += '[';
            out += spelling;
            out += ']';
        }
        return out;
    }

    bool list( IStreamingReporter& reporter, std::shared_ptr<Config> const& config ) {
        bool listed = false;
        getCurrentMutableContext().setConfig( config );
        if (config->listTests()) {
            listed = true;
            listTests(reporter, *config);
        }
        if (config->listTags()) {
            listed = true;
            listTags(reporter, *config);
        }
        if (config->listReporters()) {
            listed = true;
            listReporters(reporter, *config);
        }
        return listed;
    }

} // end namespace Catch