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 
24   CompletionRequest request(command, cursor_pos, match_start, match_max_return,
25                             matches);
26 
27   EXPECT_STREQ(request.GetRawLine().str().c_str(), command.c_str());
28   EXPECT_EQ(request.GetRawCursorPos(), cursor_pos);
29   EXPECT_EQ(request.GetCursorIndex(), arg_index);
30   EXPECT_EQ(request.GetCursorCharPosition(), arg_cursor_pos);
31   EXPECT_EQ(request.GetMatchStartPoint(), match_start);
32   EXPECT_EQ(request.GetMaxReturnElements(), match_max_return);
33   EXPECT_EQ(request.GetWordComplete(), false);
34 
35   EXPECT_EQ(request.GetPartialParsedLine().GetArgumentCount(), 2u);
36   EXPECT_STREQ(request.GetPartialParsedLine().GetArgumentAtIndex(1), "b");
37 }
38 
39 TEST(CompletionRequest, DuplicateFiltering) {
40   std::string command = "a bad c";
41   const unsigned cursor_pos = 3;
42   StringList matches;
43 
44   CompletionRequest request(command, cursor_pos, 0, 0, matches);
45 
46   EXPECT_EQ(0U, request.GetNumberOfMatches());
47 
48   // Add foo twice
49   request.AddCompletion("foo");
50   EXPECT_EQ(1U, request.GetNumberOfMatches());
51   EXPECT_EQ(1U, matches.GetSize());
52   EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
53 
54   request.AddCompletion("foo");
55   EXPECT_EQ(1U, request.GetNumberOfMatches());
56   EXPECT_EQ(1U, matches.GetSize());
57   EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
58 
59   // Add bar twice
60   request.AddCompletion("bar");
61   EXPECT_EQ(2U, request.GetNumberOfMatches());
62   EXPECT_EQ(2U, matches.GetSize());
63   EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
64   EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
65 
66   request.AddCompletion("bar");
67   EXPECT_EQ(2U, request.GetNumberOfMatches());
68   EXPECT_EQ(2U, matches.GetSize());
69   EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
70   EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
71 
72   // Add foo again.
73   request.AddCompletion("foo");
74   EXPECT_EQ(2U, request.GetNumberOfMatches());
75   EXPECT_EQ(2U, matches.GetSize());
76   EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
77   EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
78 
79   // Add something with an existing prefix
80   request.AddCompletion("foobar");
81   EXPECT_EQ(3U, request.GetNumberOfMatches());
82   EXPECT_EQ(3U, matches.GetSize());
83   EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
84   EXPECT_STREQ("bar", matches.GetStringAtIndex(1));
85   EXPECT_STREQ("foobar", matches.GetStringAtIndex(2));
86 }
87 
88 TEST(CompletionRequest, TestCompletionOwnership) {
89   std::string command = "a bad c";
90   const unsigned cursor_pos = 3;
91   StringList matches;
92 
93   CompletionRequest request(command, cursor_pos, 0, 0, matches);
94 
95   std::string Temporary = "bar";
96   request.AddCompletion(Temporary);
97   // Manipulate our completion. The request should have taken a copy, so that
98   // shouldn't influence anything.
99   Temporary[0] = 'f';
100 
101   EXPECT_EQ(1U, request.GetNumberOfMatches());
102   EXPECT_STREQ("bar", matches.GetStringAtIndex(0));
103 }
104