1 //===-- 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"
13 using namespace lldb_private;
14 
15 TEST(CompletionRequest, Constructor) {
16   std::string command = "a bad c";
17   const unsigned cursor_pos = 3;
18   const int arg_index = 1;
19   const int arg_cursor_pos = 1;
20   const int match_start = 2345;
21   const int match_max_return = 12345;
22   StringList matches;
23   CompletionResult result;
24 
25   CompletionRequest request(command, cursor_pos, match_start, match_max_return,
26                             result);
27   result.GetMatches(matches);
28 
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);
35   EXPECT_EQ(request.GetWordComplete(), false);
36 
37   EXPECT_EQ(request.GetPartialParsedLine().GetArgumentCount(), 2u);
38   EXPECT_STREQ(request.GetPartialParsedLine().GetArgumentAtIndex(1), "b");
39 }
40 
41 TEST(CompletionRequest, DuplicateFiltering) {
42   std::string command = "a bad c";
43   const unsigned cursor_pos = 3;
44   StringList matches;
45 
46   CompletionResult result;
47   CompletionRequest request(command, cursor_pos, 0, 0, result);
48   result.GetMatches(matches);
49 
50   EXPECT_EQ(0U, request.GetNumberOfMatches());
51 
52   // Add foo twice
53   request.AddCompletion("foo");
54   result.GetMatches(matches);
55 
56   EXPECT_EQ(1U, request.GetNumberOfMatches());
57   EXPECT_EQ(1U, matches.GetSize());
58   EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
59 
60   request.AddCompletion("foo");
61   result.GetMatches(matches);
62 
63   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");
69   result.GetMatches(matches);
70 
71   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");
77   result.GetMatches(matches);
78 
79   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");
86   result.GetMatches(matches);
87 
88   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");
95   result.GetMatches(matches);
96 
97   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 
104 TEST(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 
180 TEST(CompletionRequest, TestCompletionOwnership) {
181   std::string command = "a bad c";
182   const unsigned cursor_pos = 3;
183   StringList matches;
184 
185   CompletionResult result;
186   CompletionRequest request(command, cursor_pos, 0, 0, result);
187 
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 
194   result.GetMatches(matches);
195   EXPECT_EQ(1U, request.GetNumberOfMatches());
196   EXPECT_STREQ("bar", matches.GetStringAtIndex(0));
197 }
198