1 //===-- FormattersContainerTests.cpp --------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/DataFormatters/FormattersContainer.h"
10 #include "lldb/DataFormatters/FormatClasses.h"
12 #include "gtest/gtest.h"
15 using namespace lldb_private
;
17 // Creates a dummy candidate with just a type name in order to test the string
18 // matching (exact name match and regex match) paths.
19 FormattersMatchCandidate
CandidateFromTypeName(const char *type_name
) {
20 return FormattersMatchCandidate(ConstString(type_name
), nullptr, TypeImpl(),
21 FormattersMatchCandidate::Flags());
24 // All the prefixes that the exact name matching will strip from the type.
25 static const std::vector
<std::string
> exact_name_prefixes
= {
27 "class ", "struct ", "union ", "enum ",
30 // TypeMatcher that uses a exact type name string that needs to be matched.
31 TEST(TypeMatcherTests
, ExactName
) {
32 for (const std::string
&prefix
: exact_name_prefixes
) {
33 SCOPED_TRACE("Prefix: " + prefix
);
35 TypeMatcher
matcher(ConstString(prefix
+ "Name"));
36 EXPECT_TRUE(matcher
.Matches(CandidateFromTypeName("class Name")));
37 EXPECT_TRUE(matcher
.Matches(CandidateFromTypeName("struct Name")));
38 EXPECT_TRUE(matcher
.Matches(CandidateFromTypeName("union Name")));
39 EXPECT_TRUE(matcher
.Matches(CandidateFromTypeName("enum Name")));
40 EXPECT_TRUE(matcher
.Matches(CandidateFromTypeName("Name")));
42 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("Name ")));
43 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("ame")));
44 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("Nam")));
45 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("am")));
46 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("a")));
47 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName(" ")));
48 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("class N")));
49 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("class ")));
50 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("class")));
54 // TypeMatcher that uses a regex to match a type name.
55 TEST(TypeMatcherTests
, RegexName
) {
56 TypeMatcher
matcher(RegularExpression("^a[a-z]c$"));
57 EXPECT_TRUE(matcher
.Matches(CandidateFromTypeName("abc")));
58 EXPECT_TRUE(matcher
.Matches(CandidateFromTypeName("azc")));
60 // FIXME: This isn't consistent with the 'exact' type name matches above.
61 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("class abc")));
63 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("abbc")));
64 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName(" abc")));
65 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("abc ")));
66 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName(" abc ")));
67 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("XabcX")));
68 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("ac")));
69 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("a[a-z]c")));
70 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("aAc")));
71 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("ABC")));
72 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("")));
75 // TypeMatcher that only searches the type name.
76 TEST(TypeMatcherTests
, RegexMatchPart
) {
77 TypeMatcher
matcher(RegularExpression("a[a-z]c"));
78 EXPECT_TRUE(matcher
.Matches(CandidateFromTypeName("class abc")));
79 EXPECT_TRUE(matcher
.Matches(CandidateFromTypeName("abc")));
80 EXPECT_TRUE(matcher
.Matches(CandidateFromTypeName(" abc ")));
81 EXPECT_TRUE(matcher
.Matches(CandidateFromTypeName("azc")));
82 EXPECT_TRUE(matcher
.Matches(CandidateFromTypeName("abc ")));
83 EXPECT_TRUE(matcher
.Matches(CandidateFromTypeName(" abc ")));
84 EXPECT_TRUE(matcher
.Matches(CandidateFromTypeName(" abc")));
85 EXPECT_TRUE(matcher
.Matches(CandidateFromTypeName("XabcX")));
87 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("abbc")));
88 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("ac")));
89 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("a[a-z]c")));
90 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("aAc")));
91 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("ABC")));
92 EXPECT_FALSE(matcher
.Matches(CandidateFromTypeName("")));
95 // GetMatchString for exact type name matchers.
96 TEST(TypeMatcherTests
, GetMatchStringExactName
) {
97 EXPECT_EQ(TypeMatcher(ConstString("aa")).GetMatchString(), "aa");
98 EXPECT_EQ(TypeMatcher(ConstString("")).GetMatchString(), "");
99 EXPECT_EQ(TypeMatcher(ConstString("[a]")).GetMatchString(), "[a]");
102 // GetMatchString for regex matchers.
103 TEST(TypeMatcherTests
, GetMatchStringRegex
) {
104 EXPECT_EQ(TypeMatcher(RegularExpression("aa")).GetMatchString(), "aa");
105 EXPECT_EQ(TypeMatcher(RegularExpression("")).GetMatchString(), "");
106 EXPECT_EQ(TypeMatcher(RegularExpression("[a]")).GetMatchString(), "[a]");
109 // GetMatchString for regex matchers.
110 TEST(TypeMatcherTests
, CreatedBySameMatchString
) {
111 TypeMatcher
empty_str(ConstString(""));
112 TypeMatcher
empty_regex(RegularExpression(""));
113 EXPECT_TRUE(empty_str
.CreatedBySameMatchString(empty_str
));
114 EXPECT_TRUE(empty_str
.CreatedBySameMatchString(empty_regex
));
116 TypeMatcher
a_str(ConstString("a"));
117 TypeMatcher
a_regex(RegularExpression("a"));
118 EXPECT_TRUE(a_str
.CreatedBySameMatchString(a_str
));
119 EXPECT_TRUE(a_str
.CreatedBySameMatchString(a_regex
));
121 TypeMatcher
digit_str(ConstString("[0-9]"));
122 TypeMatcher
digit_regex(RegularExpression("[0-9]"));
123 EXPECT_TRUE(digit_str
.CreatedBySameMatchString(digit_str
));
124 EXPECT_TRUE(digit_str
.CreatedBySameMatchString(digit_regex
));
126 EXPECT_FALSE(empty_str
.CreatedBySameMatchString(a_str
));
127 EXPECT_FALSE(empty_str
.CreatedBySameMatchString(a_regex
));
128 EXPECT_FALSE(empty_str
.CreatedBySameMatchString(digit_str
));
129 EXPECT_FALSE(empty_str
.CreatedBySameMatchString(digit_regex
));
131 EXPECT_FALSE(empty_regex
.CreatedBySameMatchString(a_str
));
132 EXPECT_FALSE(empty_regex
.CreatedBySameMatchString(a_regex
));
133 EXPECT_FALSE(empty_regex
.CreatedBySameMatchString(digit_str
));
134 EXPECT_FALSE(empty_regex
.CreatedBySameMatchString(digit_regex
));
136 EXPECT_FALSE(a_str
.CreatedBySameMatchString(empty_str
));
137 EXPECT_FALSE(a_str
.CreatedBySameMatchString(empty_regex
));
138 EXPECT_FALSE(a_str
.CreatedBySameMatchString(digit_str
));
139 EXPECT_FALSE(a_str
.CreatedBySameMatchString(digit_regex
));
141 EXPECT_FALSE(a_regex
.CreatedBySameMatchString(empty_str
));
142 EXPECT_FALSE(a_regex
.CreatedBySameMatchString(empty_regex
));
143 EXPECT_FALSE(a_regex
.CreatedBySameMatchString(digit_str
));
144 EXPECT_FALSE(a_regex
.CreatedBySameMatchString(digit_regex
));
146 EXPECT_FALSE(digit_str
.CreatedBySameMatchString(empty_str
));
147 EXPECT_FALSE(digit_str
.CreatedBySameMatchString(empty_regex
));
148 EXPECT_FALSE(digit_str
.CreatedBySameMatchString(a_str
));
149 EXPECT_FALSE(digit_str
.CreatedBySameMatchString(a_regex
));
151 EXPECT_FALSE(digit_regex
.CreatedBySameMatchString(empty_str
));
152 EXPECT_FALSE(digit_regex
.CreatedBySameMatchString(empty_regex
));
153 EXPECT_FALSE(digit_regex
.CreatedBySameMatchString(a_str
));
154 EXPECT_FALSE(digit_regex
.CreatedBySameMatchString(a_regex
));
157 // Test CreatedBySameMatchString with stripped exact name prefixes.
158 TEST(TypeMatcherTests
, CreatedBySameMatchStringExactNamePrefixes
) {
159 for (const std::string
&prefix
: exact_name_prefixes
) {
160 SCOPED_TRACE("Prefix: " + prefix
);
161 TypeMatcher
with_prefix(ConstString(prefix
+ "Name"));
162 TypeMatcher
without_prefix(RegularExpression(""));
164 EXPECT_TRUE(with_prefix
.CreatedBySameMatchString(with_prefix
));
165 EXPECT_TRUE(without_prefix
.CreatedBySameMatchString(without_prefix
));