Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions googletest/src/gtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include <map>
#include <ostream> // NOLINT
#include <sstream>
#include <unordered_set>
#include <vector>

#include "gtest/gtest-assertion-result.h"
Expand Down Expand Up @@ -727,20 +728,33 @@ static bool PatternMatchesString(const std::string& name_str,

namespace {

bool IsGlobPattern(const std::string& pattern) {
return std::any_of(pattern.begin(), pattern.end(),
[](const char c) { return c == '?' || c == '*'; });
}

class UnitTestFilter {
public:
UnitTestFilter() = default;

// Constructs a filter from a string of patterns separated by `:`.
explicit UnitTestFilter(const std::string& filter) {
// By design "" filter matches "" string.
SplitString(filter, ':', &patterns_);
std::vector<std::string> all_patterns;
SplitString(filter, ':', &all_patterns);
const auto exact_match_patterns_begin = std::partition(
all_patterns.begin(), all_patterns.end(), &IsGlobPattern);

glob_patterns_.reserve(exact_match_patterns_begin - all_patterns.begin());
std::move(all_patterns.begin(), exact_match_patterns_begin, std::inserter(glob_patterns_, glob_patterns_.begin()));
std::move(exact_match_patterns_begin, all_patterns.end(), std::inserter(exact_match_patterns_, exact_match_patterns_.begin()));
}

// Returns true if and only if name matches at least one of the patterns in
// the filter.
bool MatchesName(const std::string& name) const {
return std::any_of(patterns_.begin(), patterns_.end(),
return exact_match_patterns_.count(name) ||
std::any_of(glob_patterns_.begin(), glob_patterns_.end(),
[&name](const std::string& pattern) {
return PatternMatchesString(
name, pattern.c_str(),
Expand All @@ -749,7 +763,8 @@ class UnitTestFilter {
}

private:
std::vector<std::string> patterns_;
std::vector<std::string> glob_patterns_;
std::unordered_set<std::string> exact_match_patterns_;
};

class PositiveAndNegativeUnitTestFilter {
Expand Down