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 #include "lldb/DataFormatters/FormatClasses.h"
11
12 #include "gtest/gtest.h"
13
14 using namespace lldb;
15 using namespace lldb_private;
16
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.
CandidateFromTypeName(const char * type_name)19 FormattersMatchCandidate CandidateFromTypeName(const char *type_name) {
20 return FormattersMatchCandidate(ConstString(type_name), nullptr, TypeImpl(),
21 FormattersMatchCandidate::Flags());
22 }
23
24 // All the prefixes that the exact name matching will strip from the type.
25 static const std::vector<std::string> exact_name_prefixes = {
26 "", // no prefix.
27 "class ", "struct ", "union ", "enum ",
28 };
29
30 // TypeMatcher that uses a exact type name string that needs to be matched.
TEST(TypeMatcherTests,ExactName)31 TEST(TypeMatcherTests, ExactName) {
32 for (const std::string &prefix : exact_name_prefixes) {
33 SCOPED_TRACE("Prefix: " + prefix);
34
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")));
41
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")));
51 }
52 }
53
54 // TypeMatcher that uses a regex to match a type name.
TEST(TypeMatcherTests,RegexName)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")));
59
60 // FIXME: This isn't consistent with the 'exact' type name matches above.
61 EXPECT_FALSE(matcher.Matches(CandidateFromTypeName("class abc")));
62
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("")));
73 }
74
75 // TypeMatcher that only searches the type name.
TEST(TypeMatcherTests,RegexMatchPart)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")));
86
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("")));
93 }
94
95 // GetMatchString for exact type name matchers.
TEST(TypeMatcherTests,GetMatchStringExactName)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]");
100 }
101
102 // GetMatchString for regex matchers.
TEST(TypeMatcherTests,GetMatchStringRegex)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]");
107 }
108
109 // GetMatchString for regex matchers.
TEST(TypeMatcherTests,CreatedBySameMatchString)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));
115
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));
120
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));
125
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));
130
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));
135
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));
140
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));
145
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));
150
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));
155 }
156
157 // Test CreatedBySameMatchString with stripped exact name prefixes.
TEST(TypeMatcherTests,CreatedBySameMatchStringExactNamePrefixes)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(""));
163
164 EXPECT_TRUE(with_prefix.CreatedBySameMatchString(with_prefix));
165 EXPECT_TRUE(without_prefix.CreatedBySameMatchString(without_prefix));
166 }
167 }
168