blob: bdf3c4d056fb642c4236e133bd952ae025b082a4 [file] [log] [blame]
Raphael Isemann2443bbd2018-07-02 21:29:56 +00001//===-- CompletionRequestTest.cpp -------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "gtest/gtest.h"
11
12#include "lldb/Utility/CompletionRequest.h"
13using namespace lldb_private;
14
15TEST(CompletionRequest, Constructor) {
Raphael Isemanna2e76c02018-07-13 18:28:14 +000016 std::string command = "a bad c";
17 const unsigned cursor_pos = 3;
Raphael Isemann2443bbd2018-07-02 21:29:56 +000018 const int arg_index = 1;
Raphael Isemanna2e76c02018-07-13 18:28:14 +000019 const int arg_cursor_pos = 1;
Raphael Isemann2443bbd2018-07-02 21:29:56 +000020 const int match_start = 2345;
21 const int match_max_return = 12345;
Raphael Isemann2443bbd2018-07-02 21:29:56 +000022 StringList matches;
Raphael Isemann7f888292018-09-13 21:26:00 +000023 CompletionResult result;
Raphael Isemanna2e76c02018-07-13 18:28:14 +000024
25 CompletionRequest request(command, cursor_pos, match_start, match_max_return,
Raphael Isemann7f888292018-09-13 21:26:00 +000026 result);
27 result.GetMatches(matches);
Raphael Isemann2443bbd2018-07-02 21:29:56 +000028
29 EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str());
30 EXPECT_EQ(request.GetRawCursorPos(), cursor_pos);
31 EXPECT_EQ(request.GetCursorIndex(), arg_index);
32 EXPECT_EQ(request.GetCursorCharPosition(), arg_cursor_pos);
33 EXPECT_EQ(request.GetMatchStartPoint(), match_start);
34 EXPECT_EQ(request.GetMaxReturnElements(), match_max_return);
Raphael Isemanna2e76c02018-07-13 18:28:14 +000035 EXPECT_EQ(request.GetWordComplete(), false);
36
37 EXPECT_EQ(request.GetPartialParsedLine().GetArgumentCount(), 2u);
38 EXPECT_STREQ(request.GetPartialParsedLine().GetArgumentAtIndex(1), "b");
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +000039}
Raphael Isemanna2e76c02018-07-13 18:28:14 +000040
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +000041TEST(CompletionRequest, DuplicateFiltering) {
42 std::string command = "a bad c";
43 const unsigned cursor_pos = 3;
44 StringList matches;
45
Raphael Isemann7f888292018-09-13 21:26:00 +000046 CompletionResult result;
47 CompletionRequest request(command, cursor_pos, 0, 0, result);
48 result.GetMatches(matches);
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +000049
50 EXPECT_EQ(0U, request.GetNumberOfMatches());
51
52 // Add foo twice
53 request.AddCompletion("foo");
Raphael Isemann7f888292018-09-13 21:26:00 +000054 result.GetMatches(matches);
55
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +000056 EXPECT_EQ(1U, request.GetNumberOfMatches());
57 EXPECT_EQ(1U, matches.GetSize());
58 EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
59
60 request.AddCompletion("foo");
Raphael Isemann7f888292018-09-13 21:26:00 +000061 result.GetMatches(matches);
62
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +000063 EXPECT_EQ(1U, request.GetNumberOfMatches());
64 EXPECT_EQ(1U, matches.GetSize());
65 EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
66
67 // Add bar twice
68 request.AddCompletion("bar");
Raphael Isemann7f888292018-09-13 21:26:00 +000069 result.GetMatches(matches);
70
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +000071 EXPECT_EQ(2U, request.GetNumberOfMatches());
72 EXPECT_EQ(2U, matches.GetSize());
73 EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
74 EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
75
76 request.AddCompletion("bar");
Raphael Isemann7f888292018-09-13 21:26:00 +000077 result.GetMatches(matches);
78
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +000079 EXPECT_EQ(2U, request.GetNumberOfMatches());
80 EXPECT_EQ(2U, matches.GetSize());
81 EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
82 EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
83
84 // Add foo again.
85 request.AddCompletion("foo");
Raphael Isemann7f888292018-09-13 21:26:00 +000086 result.GetMatches(matches);
87
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +000088 EXPECT_EQ(2U, request.GetNumberOfMatches());
89 EXPECT_EQ(2U, matches.GetSize());
90 EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
91 EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
92
93 // Add something with an existing prefix
94 request.AddCompletion("foobar");
Raphael Isemann7f888292018-09-13 21:26:00 +000095 result.GetMatches(matches);
96
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +000097 EXPECT_EQ(3U, request.GetNumberOfMatches());
98 EXPECT_EQ(3U, matches.GetSize());
99 EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
100 EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
101 EXPECT_STREQ("foobar", matches.GetStringAtIndex(2));
102}
103
Raphael Isemann7f888292018-09-13 21:26:00 +0000104TEST(CompletionRequest, DuplicateFilteringWithComments) {
105 std::string command = "a bad c";
106 const unsigned cursor_pos = 3;
107 StringList matches, descriptions;
108
109 CompletionResult result;
110 CompletionRequest request(command, cursor_pos, 0, 0, result);
111 result.GetMatches(matches);
112 result.GetDescriptions(descriptions);
113
114 EXPECT_EQ(0U, request.GetNumberOfMatches());
115
116 // Add foo twice with same comment
117 request.AddCompletion("foo", "comment");
118 result.GetMatches(matches);
119 result.GetDescriptions(descriptions);
120
121 EXPECT_EQ(1U, request.GetNumberOfMatches());
122 EXPECT_EQ(1U, matches.GetSize());
123 EXPECT_EQ(1U, descriptions.GetSize());
124 EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
125 EXPECT_STREQ("comment", descriptions.GetStringAtIndex(0));
126
127 request.AddCompletion("foo", "comment");
128 result.GetMatches(matches);
129 result.GetDescriptions(descriptions);
130
131 EXPECT_EQ(1U, request.GetNumberOfMatches());
132 EXPECT_EQ(1U, matches.GetSize());
133 EXPECT_EQ(1U, descriptions.GetSize());
134 EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
135 EXPECT_STREQ("comment", descriptions.GetStringAtIndex(0));
136
137 // Add bar twice with different comments
138 request.AddCompletion("bar", "comment");
139 result.GetMatches(matches);
140 result.GetDescriptions(descriptions);
141
142 EXPECT_EQ(2U, request.GetNumberOfMatches());
143 EXPECT_EQ(2U, matches.GetSize());
144 EXPECT_EQ(2U, descriptions.GetSize());
145 EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
146 EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
147
148 request.AddCompletion("bar", "another comment");
149 result.GetMatches(matches);
150 result.GetDescriptions(descriptions);
151
152 EXPECT_EQ(3U, request.GetNumberOfMatches());
153 EXPECT_EQ(3U, matches.GetSize());
154 EXPECT_EQ(3U, descriptions.GetSize());
155 EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
156 EXPECT_STREQ("comment", descriptions.GetStringAtIndex(0));
157 EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
158 EXPECT_STREQ("comment", descriptions.GetStringAtIndex(1));
159 EXPECT_STREQ("bar", matches.GetStringAtIndex(2));
160 EXPECT_STREQ("another comment", descriptions.GetStringAtIndex(2));
161
162 // Add foo again with no comment
163 request.AddCompletion("foo");
164 result.GetMatches(matches);
165 result.GetDescriptions(descriptions);
166
167 EXPECT_EQ(4U, request.GetNumberOfMatches());
168 EXPECT_EQ(4U, matches.GetSize());
169 EXPECT_EQ(4U, descriptions.GetSize());
170 EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
171 EXPECT_STREQ("comment", descriptions.GetStringAtIndex(0));
172 EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
173 EXPECT_STREQ("comment", descriptions.GetStringAtIndex(1));
174 EXPECT_STREQ("bar", matches.GetStringAtIndex(2));
175 EXPECT_STREQ("another comment", descriptions.GetStringAtIndex(2));
176 EXPECT_STREQ("foo", matches.GetStringAtIndex(3));
177 EXPECT_STREQ("", descriptions.GetStringAtIndex(3));
178}
179
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000180TEST(CompletionRequest, TestCompletionOwnership) {
181 std::string command = "a bad c";
182 const unsigned cursor_pos = 3;
183 StringList matches;
184
Raphael Isemann7f888292018-09-13 21:26:00 +0000185 CompletionResult result;
186 CompletionRequest request(command, cursor_pos, 0, 0, result);
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000187
188 std::string Temporary = "bar";
189 request.AddCompletion(Temporary);
190 // Manipulate our completion. The request should have taken a copy, so that
191 // shouldn't influence anything.
192 Temporary[0] = 'f';
193
Raphael Isemann7f888292018-09-13 21:26:00 +0000194 result.GetMatches(matches);
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000195 EXPECT_EQ(1U, request.GetNumberOfMatches());
196 EXPECT_STREQ("bar", matches.GetStringAtIndex(0));
Raphael Isemann2443bbd2018-07-02 21:29:56 +0000197}