Ocean
Loading...
Searching...
No Matches
TestSelector.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_TEST_TEST_SELECTOR_H
9#define META_OCEAN_TEST_TEST_SELECTOR_H
10
11#include "ocean/test/Test.h"
12
14
15#include <string>
16#include <unordered_set>
17
18namespace Ocean
19{
20
21namespace Test
22{
23
24/**
25 * This class implements a test selector that parses test function strings and determines which tests should be executed.
26 * The selector supports hierarchical test names with dot notation (e.g., "frameconverter.rgb24.normal") and wildcard patterns.
27 *
28 * Usage examples:
29 * - Empty string: runs all tests
30 * - "frameconverter": runs the frameconverter test and all its sub-tests
31 * - "frameconverter*": runs all tests starting with "frameconverter"
32 * - "frameconverterrgb24.rgb24tobgr24": runs only that specific sub-test
33 * - "test1,test2,test3": runs test1, test2, and test3
34 *
35 * Hierarchical matching example:
36 * @code
37 * TestSelector selector("datatype.char,frame");
38 *
39 * if (TestSelector subSelector = selector.shouldRun("datatype"))
40 * {
41 * // subSelector now contains only "char" (prefix "datatype." is stripped)
42 * if (subSelector.shouldRun("char")) // This will match!
43 * {
44 * // Run char test
45 * }
46 * }
47 * @endcode
48 * @ingroup test
49 */
50class OCEAN_TEST_EXPORT TestSelector
51{
52 protected:
53
54 /// Definition of an unordered set holding test patterns.
55 using Patterns = std::unordered_set<std::string>;
56
57 public:
58
59 /**
60 * Creates a new test selector from a comma-separated list of test function names.
61 * Test names are converted to lowercase automatically.
62 * @param testFunctions Comma-separated list of test names, wildcards, or hierarchical test identifiers; empty string means run all tests
63 */
64 explicit TestSelector(const std::string& testFunctions = std::string());
65
66 /**
67 * Determines whether a test with the given name should be executed and returns a sub-selector for hierarchical filtering.
68 * @param testName The name of the test to check (case-insensitive), must be valid
69 * @return A TestSelector that can be cast to bool (true if test should run) and contains stripped sub-patterns
70 */
71 TestSelector shouldRun(const std::string& testName) const;
72
73 /**
74 * Bool conversion operator to check if this selector indicates the test should run.
75 * @return True if the test should execute, false otherwise
76 */
77 explicit operator bool() const;
78
79 /**
80 * Returns whether all tests should be executed.
81 * @return True if no specific patterns were provided (empty string), false otherwise
82 */
83 inline bool runAll() const;
84
85 private:
86
87 /**
88 * Creates a sub-selector for hierarchical test filtering.
89 * @param patterns The patterns for this selector
90 * @param shouldExecute Whether tests matching this selector should execute
91 */
92 TestSelector(Patterns&& patterns, bool shouldExecute);
93
94 /**
95 * Checks if a pattern matches a test name.
96 * @param pattern The pattern to match against (may contain wildcards), must be valid
97 * @param testName The test name to check, must be valid
98 * @return True if the pattern matches the test name
99 */
100 static bool matches(const std::string& pattern, const std::string& testName);
101
102 private:
103
104 /// Set of test patterns parsed from the input string
106
107 /// Whether tests matching this selector should execute
108 bool shouldExecute_ = true;
109};
110
111inline bool TestSelector::runAll() const
112{
113 return patterns_.empty();
114}
115
116/**
117 * Writes a test selector scope to a stream.
118 * Outputs "Entire" if all tests should run, "Partial" if only selected tests should run.
119 * @param stream The stream to write to
120 * @param selector The test selector to write
121 * @return Reference to the stream
122 */
123inline std::ostream& operator<<(std::ostream& stream, const TestSelector& selector)
124{
125 if (selector.runAll())
126 {
127 stream << "Entire";
128 }
129 else
130 {
131 stream << "Partial";
132 }
133
134 return stream;
135}
136
137/**
138 * Writes a test selector scope to a message object.
139 * Outputs "Entire" if all tests should run, "Partial" if only selected tests should run.
140 * @param messageObject The message object to write to
141 * @param selector The test selector to write
142 * @return Reference to the message object
143 * @tparam tActive True to write the message; false to suppress it
144 */
145template <bool tActive>
146MessageObject<tActive>& operator<<(MessageObject<tActive>& messageObject, const TestSelector& selector)
147{
148 if (selector.runAll())
149 {
150 messageObject << "Entire";
151 }
152 else
153 {
154 messageObject << "Partial";
155 }
156
157 return messageObject;
158}
159
160/**
161 * Writes a test selector scope to a message object (rvalue reference version).
162 * Outputs "Entire" if all tests should run, "Partial" if only selected tests should run.
163 * @param messageObject The message object to write to
164 * @param selector The test selector to write
165 * @return Reference to the message object
166 * @tparam tActive True to write the message; false to suppress it
167 */
168template <bool tActive>
169MessageObject<tActive>& operator<<(MessageObject<tActive>&& messageObject, const TestSelector& selector)
170{
171 if (selector.runAll())
172 {
173 messageObject << "Entire";
174 }
175 else
176 {
177 messageObject << "Partial";
178 }
179
180 return messageObject;
181}
182
183}
184
185}
186
187#endif // META_OCEAN_TEST_TEST_SELECTOR_H
Messenger object, one object for each message.
Definition Messenger.h:448
This class implements a test selector that parses test function strings and determines which tests sh...
Definition TestSelector.h:51
Patterns patterns_
Set of test patterns parsed from the input string.
Definition TestSelector.h:105
TestSelector shouldRun(const std::string &testName) const
Determines whether a test with the given name should be executed and returns a sub-selector for hiera...
bool runAll() const
Returns whether all tests should be executed.
Definition TestSelector.h:111
std::unordered_set< std::string > Patterns
Definition of an unordered set holding test patterns.
Definition TestSelector.h:55
TestSelector(Patterns &&patterns, bool shouldExecute)
Creates a sub-selector for hierarchical test filtering.
TestSelector(const std::string &testFunctions=std::string())
Creates a new test selector from a comma-separated list of test function names.
static bool matches(const std::string &pattern, const std::string &testName)
Checks if a pattern matches a test name.
std::ostream & operator<<(std::ostream &stream, const TestResult &testResult)
Writes a test result to a stream.
Definition TestResult.h:200
The namespace covering the entire Ocean framework.
Definition Accessor.h:15