Files
clang-p2996/lldb/unittests/Utility/NameMatchesTest.cpp
Pavel Labath c4a3395103 Fix a couple of corner cases in NameMatches
Summary:
I originally set out to move the NameMatches closer to the relevant
function and add some unit tests. However, in the process I've found a
couple of bugs in the implementation:
- the early exits where not always correct:
  - (test==pattern) does not mean the match will always suceed because
    of regular expressions
  - pattern.empty() does not mean the match will fail because the "" is
    a valid prefix of any string

So I cleaned up those and added some tests. The only tricky part here
was that regcomp() implementation on darwin did not recognise the empty
string as a regular expression and returned an REG_EMPTY error instead.
The simples fix here seemed to be to replace the empty expression with
an equivalent non-empty one.

Reviewers: clayborg, zturner

Subscribers: mgorny, lldb-commits

Differential Revision: https://reviews.llvm.org/D30094

llvm-svn: 295651
2017-02-20 11:35:33 +00:00

59 lines
2.4 KiB
C++

//===-- NameMatchesTest.cpp -------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/Utility/NameMatches.h"
#include "gtest/gtest.h"
using namespace lldb_private;
TEST(NameMatchesTest, Ignore) {
EXPECT_TRUE(NameMatches("foo", NameMatch::Ignore, "bar"));
}
TEST(NameMatchesTest, Equals) {
EXPECT_TRUE(NameMatches("foo", NameMatch::Equals, "foo"));
EXPECT_FALSE(NameMatches("foo", NameMatch::Equals, "bar"));
}
TEST(NameMatchesTest, Contains) {
EXPECT_TRUE(NameMatches("foobar", NameMatch::Contains, "foo"));
EXPECT_TRUE(NameMatches("foobar", NameMatch::Contains, "oob"));
EXPECT_TRUE(NameMatches("foobar", NameMatch::Contains, "bar"));
EXPECT_TRUE(NameMatches("foobar", NameMatch::Contains, "foobar"));
EXPECT_TRUE(NameMatches("", NameMatch::Contains, ""));
EXPECT_FALSE(NameMatches("", NameMatch::Contains, "foo"));
EXPECT_FALSE(NameMatches("foobar", NameMatch::Contains, "baz"));
}
TEST(NameMatchesTest, StartsWith) {
EXPECT_TRUE(NameMatches("foo", NameMatch::StartsWith, "f"));
EXPECT_TRUE(NameMatches("foo", NameMatch::StartsWith, ""));
EXPECT_TRUE(NameMatches("", NameMatch::StartsWith, ""));
EXPECT_FALSE(NameMatches("foo", NameMatch::StartsWith, "b"));
EXPECT_FALSE(NameMatches("", NameMatch::StartsWith, "b"));
}
TEST(NameMatchesTest, EndsWith) {
EXPECT_TRUE(NameMatches("foo", NameMatch::EndsWith, "o"));
EXPECT_TRUE(NameMatches("foo", NameMatch::EndsWith, ""));
EXPECT_TRUE(NameMatches("", NameMatch::EndsWith, ""));
EXPECT_FALSE(NameMatches("foo", NameMatch::EndsWith, "b"));
EXPECT_FALSE(NameMatches("", NameMatch::EndsWith, "b"));
}
TEST(NameMatchesTest, RegularExpression) {
EXPECT_TRUE(NameMatches("foobar", NameMatch::RegularExpression, "foo"));
EXPECT_TRUE(NameMatches("foobar", NameMatch::RegularExpression, "f[oa]o"));
EXPECT_TRUE(NameMatches("foo", NameMatch::RegularExpression, ""));
EXPECT_TRUE(NameMatches("", NameMatch::RegularExpression, ""));
EXPECT_FALSE(NameMatches("foo", NameMatch::RegularExpression, "b"));
EXPECT_FALSE(NameMatches("", NameMatch::RegularExpression, "b"));
EXPECT_FALSE(NameMatches("^a", NameMatch::RegularExpression, "^a"));
}