summaryrefslogtreecommitdiffstats
path: root/lldb/unittests/Utility/CompletionRequestTest.cpp
blob: 58dfab0da508a800e683b5697f9924b19a07b3d8 (plain)
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
//===-- CompletionRequestTest.cpp -------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#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 int arg_index = 1;
  const int arg_cursor_pos = 1;
  const int match_start = 2345;
  const int match_max_return = 12345;
  StringList matches;

  CompletionRequest request(command, cursor_pos, match_start, match_max_return,
                            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.GetCursorCharPosition(), arg_cursor_pos);
  EXPECT_EQ(request.GetMatchStartPoint(), match_start);
  EXPECT_EQ(request.GetMaxReturnElements(), match_max_return);
  EXPECT_EQ(request.GetWordComplete(), false);

  EXPECT_EQ(request.GetPartialParsedLine().GetArgumentCount(), 2u);
  EXPECT_STREQ(request.GetPartialParsedLine().GetArgumentAtIndex(1), "b");
}

TEST(CompletionRequest, DuplicateFiltering) {
  std::string command = "a bad c";
  const unsigned cursor_pos = 3;
  StringList matches;

  CompletionRequest request(command, cursor_pos, 0, 0, matches);

  EXPECT_EQ(0U, request.GetNumberOfMatches());

  // Add foo twice
  request.AddCompletion("foo");
  EXPECT_EQ(1U, request.GetNumberOfMatches());
  EXPECT_EQ(1U, matches.GetSize());
  EXPECT_STREQ("foo", matches.GetStringAtIndex(0));

  request.AddCompletion("foo");
  EXPECT_EQ(1U, request.GetNumberOfMatches());
  EXPECT_EQ(1U, matches.GetSize());
  EXPECT_STREQ("foo", matches.GetStringAtIndex(0));

  // Add bar twice
  request.AddCompletion("bar");
  EXPECT_EQ(2U, request.GetNumberOfMatches());
  EXPECT_EQ(2U, matches.GetSize());
  EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
  EXPECT_STREQ("bar", matches.GetStringAtIndex(1));

  request.AddCompletion("bar");
  EXPECT_EQ(2U, request.GetNumberOfMatches());
  EXPECT_EQ(2U, matches.GetSize());
  EXPECT_STREQ("foo", matches.GetStringAtIndex(0));
  EXPECT_STREQ("bar", matches.GetStringAtIndex(1));

  // Add foo again.
  request.AddCompletion("foo");
  EXPECT_EQ(2U, request.GetNumberOfMatches());
  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");
  EXPECT_EQ(3U, request.GetNumberOfMatches());
  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, TestCompletionOwnership) {
  std::string command = "a bad c";
  const unsigned cursor_pos = 3;
  StringList matches;

  CompletionRequest request(command, cursor_pos, 0, 0, matches);

  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';

  EXPECT_EQ(1U, request.GetNumberOfMatches());
  EXPECT_STREQ("bar", matches.GetStringAtIndex(0));
}
OpenPOWER on IntegriCloud