1 //===-- FormattersContainerTests.cpp --------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/DataFormatters/FormattersContainer.h"
10 
11 #include "gtest/gtest.h"
12 
13 using namespace lldb;
14 using namespace lldb_private;
15 
16 // All the prefixes that the exact name matching will strip from the type.
17 static const std::vector<std::string> exact_name_prefixes = {
18     "", // no prefix.
19     "class ", "struct ", "union ", "enum ",
20 };
21 
22 // TypeMatcher that uses a exact type name string that needs to be matched.
TEST(TypeMatcherTests,ExactName)23 TEST(TypeMatcherTests, ExactName) {
24   for (const std::string &prefix : exact_name_prefixes) {
25     SCOPED_TRACE("Prefix: " + prefix);
26 
27     TypeMatcher matcher(ConstString(prefix + "Name"));
28     EXPECT_TRUE(matcher.Matches(ConstString("class Name")));
29     EXPECT_TRUE(matcher.Matches(ConstString("struct Name")));
30     EXPECT_TRUE(matcher.Matches(ConstString("union Name")));
31     EXPECT_TRUE(matcher.Matches(ConstString("enum Name")));
32     EXPECT_TRUE(matcher.Matches(ConstString("Name")));
33 
34     EXPECT_FALSE(matcher.Matches(ConstString("Name ")));
35     EXPECT_FALSE(matcher.Matches(ConstString("ame")));
36     EXPECT_FALSE(matcher.Matches(ConstString("Nam")));
37     EXPECT_FALSE(matcher.Matches(ConstString("am")));
38     EXPECT_FALSE(matcher.Matches(ConstString("a")));
39     EXPECT_FALSE(matcher.Matches(ConstString(" ")));
40     EXPECT_FALSE(matcher.Matches(ConstString("class N")));
41     EXPECT_FALSE(matcher.Matches(ConstString("class ")));
42     EXPECT_FALSE(matcher.Matches(ConstString("class")));
43   }
44 }
45 
46 // TypeMatcher that uses a regex to match a type name.
TEST(TypeMatcherTests,RegexName)47 TEST(TypeMatcherTests, RegexName) {
48   TypeMatcher matcher(RegularExpression("^a[a-z]c$"));
49   EXPECT_TRUE(matcher.Matches(ConstString("abc")));
50   EXPECT_TRUE(matcher.Matches(ConstString("azc")));
51 
52   // FIXME: This isn't consistent with the 'exact' type name matches above.
53   EXPECT_FALSE(matcher.Matches(ConstString("class abc")));
54 
55   EXPECT_FALSE(matcher.Matches(ConstString("abbc")));
56   EXPECT_FALSE(matcher.Matches(ConstString(" abc")));
57   EXPECT_FALSE(matcher.Matches(ConstString("abc ")));
58   EXPECT_FALSE(matcher.Matches(ConstString(" abc ")));
59   EXPECT_FALSE(matcher.Matches(ConstString("XabcX")));
60   EXPECT_FALSE(matcher.Matches(ConstString("ac")));
61   EXPECT_FALSE(matcher.Matches(ConstString("a[a-z]c")));
62   EXPECT_FALSE(matcher.Matches(ConstString("aAc")));
63   EXPECT_FALSE(matcher.Matches(ConstString("ABC")));
64   EXPECT_FALSE(matcher.Matches(ConstString("")));
65 }
66 
67 // TypeMatcher that only searches the type name.
TEST(TypeMatcherTests,RegexMatchPart)68 TEST(TypeMatcherTests, RegexMatchPart) {
69   TypeMatcher matcher(RegularExpression("a[a-z]c"));
70   EXPECT_TRUE(matcher.Matches(ConstString("class abc")));
71   EXPECT_TRUE(matcher.Matches(ConstString("abc")));
72   EXPECT_TRUE(matcher.Matches(ConstString(" abc ")));
73   EXPECT_TRUE(matcher.Matches(ConstString("azc")));
74   EXPECT_TRUE(matcher.Matches(ConstString("abc ")));
75   EXPECT_TRUE(matcher.Matches(ConstString(" abc ")));
76   EXPECT_TRUE(matcher.Matches(ConstString(" abc")));
77   EXPECT_TRUE(matcher.Matches(ConstString("XabcX")));
78 
79   EXPECT_FALSE(matcher.Matches(ConstString("abbc")));
80   EXPECT_FALSE(matcher.Matches(ConstString("ac")));
81   EXPECT_FALSE(matcher.Matches(ConstString("a[a-z]c")));
82   EXPECT_FALSE(matcher.Matches(ConstString("aAc")));
83   EXPECT_FALSE(matcher.Matches(ConstString("ABC")));
84   EXPECT_FALSE(matcher.Matches(ConstString("")));
85 }
86 
87 // GetMatchString for exact type name matchers.
TEST(TypeMatcherTests,GetMatchStringExactName)88 TEST(TypeMatcherTests, GetMatchStringExactName) {
89   EXPECT_EQ(TypeMatcher(ConstString("aa")).GetMatchString(), "aa");
90   EXPECT_EQ(TypeMatcher(ConstString("")).GetMatchString(), "");
91   EXPECT_EQ(TypeMatcher(ConstString("[a]")).GetMatchString(), "[a]");
92 }
93 
94 // GetMatchString for regex matchers.
TEST(TypeMatcherTests,GetMatchStringRegex)95 TEST(TypeMatcherTests, GetMatchStringRegex) {
96   EXPECT_EQ(TypeMatcher(RegularExpression("aa")).GetMatchString(), "aa");
97   EXPECT_EQ(TypeMatcher(RegularExpression("")).GetMatchString(), "");
98   EXPECT_EQ(TypeMatcher(RegularExpression("[a]")).GetMatchString(), "[a]");
99 }
100 
101 // GetMatchString for regex matchers.
TEST(TypeMatcherTests,CreatedBySameMatchString)102 TEST(TypeMatcherTests, CreatedBySameMatchString) {
103   TypeMatcher empty_str(ConstString(""));
104   TypeMatcher empty_regex(RegularExpression(""));
105   EXPECT_TRUE(empty_str.CreatedBySameMatchString(empty_str));
106   EXPECT_TRUE(empty_str.CreatedBySameMatchString(empty_regex));
107 
108   TypeMatcher a_str(ConstString("a"));
109   TypeMatcher a_regex(RegularExpression("a"));
110   EXPECT_TRUE(a_str.CreatedBySameMatchString(a_str));
111   EXPECT_TRUE(a_str.CreatedBySameMatchString(a_regex));
112 
113   TypeMatcher digit_str(ConstString("[0-9]"));
114   TypeMatcher digit_regex(RegularExpression("[0-9]"));
115   EXPECT_TRUE(digit_str.CreatedBySameMatchString(digit_str));
116   EXPECT_TRUE(digit_str.CreatedBySameMatchString(digit_regex));
117 
118   EXPECT_FALSE(empty_str.CreatedBySameMatchString(a_str));
119   EXPECT_FALSE(empty_str.CreatedBySameMatchString(a_regex));
120   EXPECT_FALSE(empty_str.CreatedBySameMatchString(digit_str));
121   EXPECT_FALSE(empty_str.CreatedBySameMatchString(digit_regex));
122 
123   EXPECT_FALSE(empty_regex.CreatedBySameMatchString(a_str));
124   EXPECT_FALSE(empty_regex.CreatedBySameMatchString(a_regex));
125   EXPECT_FALSE(empty_regex.CreatedBySameMatchString(digit_str));
126   EXPECT_FALSE(empty_regex.CreatedBySameMatchString(digit_regex));
127 
128   EXPECT_FALSE(a_str.CreatedBySameMatchString(empty_str));
129   EXPECT_FALSE(a_str.CreatedBySameMatchString(empty_regex));
130   EXPECT_FALSE(a_str.CreatedBySameMatchString(digit_str));
131   EXPECT_FALSE(a_str.CreatedBySameMatchString(digit_regex));
132 
133   EXPECT_FALSE(a_regex.CreatedBySameMatchString(empty_str));
134   EXPECT_FALSE(a_regex.CreatedBySameMatchString(empty_regex));
135   EXPECT_FALSE(a_regex.CreatedBySameMatchString(digit_str));
136   EXPECT_FALSE(a_regex.CreatedBySameMatchString(digit_regex));
137 
138   EXPECT_FALSE(digit_str.CreatedBySameMatchString(empty_str));
139   EXPECT_FALSE(digit_str.CreatedBySameMatchString(empty_regex));
140   EXPECT_FALSE(digit_str.CreatedBySameMatchString(a_str));
141   EXPECT_FALSE(digit_str.CreatedBySameMatchString(a_regex));
142 
143   EXPECT_FALSE(digit_regex.CreatedBySameMatchString(empty_str));
144   EXPECT_FALSE(digit_regex.CreatedBySameMatchString(empty_regex));
145   EXPECT_FALSE(digit_regex.CreatedBySameMatchString(a_str));
146   EXPECT_FALSE(digit_regex.CreatedBySameMatchString(a_regex));
147 }
148 
149 // Test CreatedBySameMatchString with stripped exact name prefixes.
TEST(TypeMatcherTests,CreatedBySameMatchStringExactNamePrefixes)150 TEST(TypeMatcherTests, CreatedBySameMatchStringExactNamePrefixes) {
151   for (const std::string &prefix : exact_name_prefixes) {
152     SCOPED_TRACE("Prefix: " + prefix);
153     TypeMatcher with_prefix(ConstString(prefix + "Name"));
154     TypeMatcher without_prefix(RegularExpression(""));
155 
156     EXPECT_TRUE(with_prefix.CreatedBySameMatchString(with_prefix));
157     EXPECT_TRUE(without_prefix.CreatedBySameMatchString(without_prefix));
158   }
159 }
160