llvm-capstone/lldb/unittests/Utility/CompletionRequestTest.cpp
Raphael Isemann 243f52b58b [lldb] Cut off unused suffix in CompletionRequest::GetRawLine
The GetRawLine currently returns the full command line used
to create the CompletionRequest. So for example for "foo b[tab] --arg"
it would return the whole string instead of "foo b". Usually
completion code makes the wrong assumption that the cursor is at
the end of the line and handing out the complete line will cause
that people implement completions that also make this assumption.

This patch makes GetRawLine() return only the string until the
cursor and hides the suffix (so that the cursor is always at the
end of this string) and adds another function GetRawLineWithUnusedSuffix
that is specifically the line with the suffix that isn't used by
the CompletionRequest for argument parsing etc.

There is only one user of this new function that actually needs the
suffix and that is the expression command which needs the suffix to
detect if it is in the raw or argument part of the command (by looking
at the "--" separator).
2020-01-28 11:12:22 +01:00

277 lines
9.0 KiB
C++

//===-- CompletionRequestTest.cpp -----------------------------------------===//
//
// 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_EQ(request.GetRawLine(), "a b");
EXPECT_EQ(request.GetRawLineWithUnusedSuffix(), command);
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_EQ(request.GetRawLine(), command);
EXPECT_EQ(request.GetRawLineWithUnusedSuffix(), command);
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_EQ(request.GetRawLine(), "a b");
EXPECT_EQ(request.GetRawLineWithUnusedSuffix(), command);
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_EQ(request.GetRawLine(), "a b");
EXPECT_EQ(request.GetRawLineWithUnusedSuffix(), command);
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));
}