1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
| //===-- CompletionRequestTest.cpp -------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "gtest/gtest.h"
#include "lldb/Utility/CompletionRequest.h"
using namespace lldb_private;
TEST(CompletionRequest, Constructor) {
std::string command = "a bad c";
const unsigned cursor_pos = 3;
const size_t arg_index = 1;
StringList matches;
CompletionResult result;
CompletionRequest request(command, cursor_pos, result);
result.GetMatches(matches);
EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str());
EXPECT_EQ(request.GetRawCursorPos(), cursor_pos);
EXPECT_EQ(request.GetCursorIndex(), arg_index);
EXPECT_EQ(request.GetParsedLine().GetArgumentCount(), 2u);
EXPECT_EQ(request.GetCursorArgumentPrefix().str(), "b");
}
TEST(CompletionRequest, FakeLastArg) {
// We insert an empty fake argument into the argument list when the
// cursor is after a space.
std::string command = "a bad c ";
const unsigned cursor_pos = command.size();
CompletionResult result;
CompletionRequest request(command, cursor_pos, result);
EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str());
EXPECT_EQ(request.GetRawCursorPos(), cursor_pos);
EXPECT_EQ(request.GetCursorIndex(), 3U);
EXPECT_EQ(request.GetParsedLine().GetArgumentCount(), 4U);
EXPECT_EQ(request.GetCursorArgumentPrefix().str(), "");
}
TEST(CompletionRequest, TryCompleteCurrentArgGood) {
std::string command = "a bad c";
StringList matches, descriptions;
CompletionResult result;
CompletionRequest request(command, 3, result);
request.TryCompleteCurrentArg("boo", "car");
result.GetMatches(matches);
result.GetDescriptions(descriptions);
EXPECT_EQ(1U, result.GetResults().size());
EXPECT_STREQ("boo", matches.GetStringAtIndex(0U));
EXPECT_EQ(1U, descriptions.GetSize());
EXPECT_STREQ("car", descriptions.GetStringAtIndex(0U));
}
TEST(CompletionRequest, TryCompleteCurrentArgBad) {
std::string command = "a bad c";
CompletionResult result;
CompletionRequest request(command, 3, result);
request.TryCompleteCurrentArg("car", "card");
EXPECT_EQ(0U, result.GetResults().size());
}
TEST(CompletionRequest, TryCompleteCurrentArgMode) {
std::string command = "a bad c";
CompletionResult result;
CompletionRequest request(command, 3, result);
request.TryCompleteCurrentArg<CompletionMode::Partial>("bar", "bard");
EXPECT_EQ(1U, result.GetResults().size());
EXPECT_EQ(CompletionMode::Partial, result.GetResults()[0].GetMode());
}
TEST(CompletionRequest, ShiftArguments) {
std::string command = "a bad c";
const unsigned cursor_pos = 3;
const size_t arg_index = 1;
StringList matches;
CompletionResult result;
CompletionRequest request(command, cursor_pos, result);
result.GetMatches(matches);
EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str());
EXPECT_EQ(request.GetRawCursorPos(), cursor_pos);
EXPECT_EQ(request.GetCursorIndex(), arg_index);
EXPECT_EQ(request.GetParsedLine().GetArgumentCount(), 2u);
EXPECT_STREQ(request.GetParsedLine().GetArgumentAtIndex(1), "b");
// Shift away the 'a' argument.
request.ShiftArguments();
// The raw line/cursor stays identical.
EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str());
EXPECT_EQ(request.GetRawCursorPos(), cursor_pos);
// Partially parsed line and cursor should be updated.
EXPECT_EQ(request.GetCursorIndex(), arg_index - 1U);
EXPECT_EQ(request.GetParsedLine().GetArgumentCount(), 1u);
EXPECT_EQ(request.GetCursorArgumentPrefix().str(), "b");
}
TEST(CompletionRequest, DuplicateFiltering) {
std::string command = "a bad c";
const unsigned cursor_pos = 3;
StringList matches;
CompletionResult result;
CompletionRequest request(command, cursor_pos, result);
result.GetMatches(matches);
EXPECT_EQ(0U, result.GetNumberOfResults());
// Add foo twice
request.AddCompletion("foo");
result.GetMatches(matches);
EXPECT_EQ(1U, result.GetNumberOfResults());
EXPECT_EQ(1U, matches.GetSize());
EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
request.AddCompletion("foo");
result.GetMatches(matches);
EXPECT_EQ(1U, result.GetNumberOfResults());
EXPECT_EQ(1U, matches.GetSize());
EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
// Add bar twice
request.AddCompletion("bar");
result.GetMatches(matches);
EXPECT_EQ(2U, result.GetNumberOfResults());
EXPECT_EQ(2U, matches.GetSize());
EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
request.AddCompletion("bar");
result.GetMatches(matches);
EXPECT_EQ(2U, result.GetNumberOfResults());
EXPECT_EQ(2U, matches.GetSize());
EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
// Add foo again.
request.AddCompletion("foo");
result.GetMatches(matches);
EXPECT_EQ(2U, result.GetNumberOfResults());
EXPECT_EQ(2U, matches.GetSize());
EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
// Add something with an existing prefix
request.AddCompletion("foobar");
result.GetMatches(matches);
EXPECT_EQ(3U, result.GetNumberOfResults());
EXPECT_EQ(3U, matches.GetSize());
EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
EXPECT_STREQ("foobar", matches.GetStringAtIndex(2));
}
TEST(CompletionRequest, DuplicateFilteringWithComments) {
std::string command = "a bad c";
const unsigned cursor_pos = 3;
StringList matches, descriptions;
CompletionResult result;
CompletionRequest request(command, cursor_pos, result);
result.GetMatches(matches);
result.GetDescriptions(descriptions);
EXPECT_EQ(0U, result.GetNumberOfResults());
// Add foo twice with same comment
request.AddCompletion("foo", "comment");
result.GetMatches(matches);
result.GetDescriptions(descriptions);
EXPECT_EQ(1U, result.GetNumberOfResults());
EXPECT_EQ(1U, matches.GetSize());
EXPECT_EQ(1U, descriptions.GetSize());
EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
EXPECT_STREQ("comment", descriptions.GetStringAtIndex(0));
request.AddCompletion("foo", "comment");
result.GetMatches(matches);
result.GetDescriptions(descriptions);
EXPECT_EQ(1U, result.GetNumberOfResults());
EXPECT_EQ(1U, matches.GetSize());
EXPECT_EQ(1U, descriptions.GetSize());
EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
EXPECT_STREQ("comment", descriptions.GetStringAtIndex(0));
// Add bar twice with different comments
request.AddCompletion("bar", "comment");
result.GetMatches(matches);
result.GetDescriptions(descriptions);
EXPECT_EQ(2U, result.GetNumberOfResults());
EXPECT_EQ(2U, matches.GetSize());
EXPECT_EQ(2U, descriptions.GetSize());
EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
request.AddCompletion("bar", "another comment");
result.GetMatches(matches);
result.GetDescriptions(descriptions);
EXPECT_EQ(3U, result.GetNumberOfResults());
EXPECT_EQ(3U, matches.GetSize());
EXPECT_EQ(3U, descriptions.GetSize());
EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
EXPECT_STREQ("comment", descriptions.GetStringAtIndex(0));
EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
EXPECT_STREQ("comment", descriptions.GetStringAtIndex(1));
EXPECT_STREQ("bar", matches.GetStringAtIndex(2));
EXPECT_STREQ("another comment", descriptions.GetStringAtIndex(2));
// Add foo again with no comment
request.AddCompletion("foo");
result.GetMatches(matches);
result.GetDescriptions(descriptions);
EXPECT_EQ(4U, result.GetNumberOfResults());
EXPECT_EQ(4U, matches.GetSize());
EXPECT_EQ(4U, descriptions.GetSize());
EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
EXPECT_STREQ("comment", descriptions.GetStringAtIndex(0));
EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
EXPECT_STREQ("comment", descriptions.GetStringAtIndex(1));
EXPECT_STREQ("bar", matches.GetStringAtIndex(2));
EXPECT_STREQ("another comment", descriptions.GetStringAtIndex(2));
EXPECT_STREQ("foo", matches.GetStringAtIndex(3));
EXPECT_STREQ("", descriptions.GetStringAtIndex(3));
}
TEST(CompletionRequest, TestCompletionOwnership) {
std::string command = "a bad c";
const unsigned cursor_pos = 3;
StringList matches;
CompletionResult result;
CompletionRequest request(command, cursor_pos, result);
std::string Temporary = "bar";
request.AddCompletion(Temporary);
// Manipulate our completion. The request should have taken a copy, so that
// shouldn't influence anything.
Temporary[0] = 'f';
result.GetMatches(matches);
EXPECT_EQ(1U, result.GetNumberOfResults());
EXPECT_STREQ("bar", matches.GetStringAtIndex(0));
}
|