1*9880d681SAndroid Build Coastguard Worker //===-- LineEditor.cpp ----------------------------------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker
10*9880d681SAndroid Build Coastguard Worker #include "llvm/LineEditor/LineEditor.h"
11*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/FileSystem.h"
12*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Path.h"
13*9880d681SAndroid Build Coastguard Worker #include "gtest/gtest.h"
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker using namespace llvm;
16*9880d681SAndroid Build Coastguard Worker
17*9880d681SAndroid Build Coastguard Worker class LineEditorTest : public testing::Test {
18*9880d681SAndroid Build Coastguard Worker public:
19*9880d681SAndroid Build Coastguard Worker SmallString<64> HistPath;
20*9880d681SAndroid Build Coastguard Worker LineEditor *LE;
21*9880d681SAndroid Build Coastguard Worker
LineEditorTest()22*9880d681SAndroid Build Coastguard Worker LineEditorTest() {
23*9880d681SAndroid Build Coastguard Worker init();
24*9880d681SAndroid Build Coastguard Worker }
25*9880d681SAndroid Build Coastguard Worker
init()26*9880d681SAndroid Build Coastguard Worker void init() {
27*9880d681SAndroid Build Coastguard Worker sys::fs::createTemporaryFile("temp", "history", HistPath);
28*9880d681SAndroid Build Coastguard Worker ASSERT_FALSE(HistPath.empty());
29*9880d681SAndroid Build Coastguard Worker LE = new LineEditor("test", HistPath);
30*9880d681SAndroid Build Coastguard Worker }
31*9880d681SAndroid Build Coastguard Worker
~LineEditorTest()32*9880d681SAndroid Build Coastguard Worker ~LineEditorTest() override {
33*9880d681SAndroid Build Coastguard Worker delete LE;
34*9880d681SAndroid Build Coastguard Worker sys::fs::remove(HistPath.str());
35*9880d681SAndroid Build Coastguard Worker }
36*9880d681SAndroid Build Coastguard Worker };
37*9880d681SAndroid Build Coastguard Worker
TEST_F(LineEditorTest,HistorySaveLoad)38*9880d681SAndroid Build Coastguard Worker TEST_F(LineEditorTest, HistorySaveLoad) {
39*9880d681SAndroid Build Coastguard Worker LE->saveHistory();
40*9880d681SAndroid Build Coastguard Worker LE->loadHistory();
41*9880d681SAndroid Build Coastguard Worker }
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard Worker struct TestListCompleter {
44*9880d681SAndroid Build Coastguard Worker std::vector<LineEditor::Completion> Completions;
45*9880d681SAndroid Build Coastguard Worker
TestListCompleterTestListCompleter46*9880d681SAndroid Build Coastguard Worker TestListCompleter(const std::vector<LineEditor::Completion> &Completions)
47*9880d681SAndroid Build Coastguard Worker : Completions(Completions) {}
48*9880d681SAndroid Build Coastguard Worker
operator ()TestListCompleter49*9880d681SAndroid Build Coastguard Worker std::vector<LineEditor::Completion> operator()(StringRef Buffer,
50*9880d681SAndroid Build Coastguard Worker size_t Pos) const {
51*9880d681SAndroid Build Coastguard Worker EXPECT_TRUE(Buffer.empty());
52*9880d681SAndroid Build Coastguard Worker EXPECT_EQ(0u, Pos);
53*9880d681SAndroid Build Coastguard Worker return Completions;
54*9880d681SAndroid Build Coastguard Worker }
55*9880d681SAndroid Build Coastguard Worker };
56*9880d681SAndroid Build Coastguard Worker
TEST_F(LineEditorTest,ListCompleters)57*9880d681SAndroid Build Coastguard Worker TEST_F(LineEditorTest, ListCompleters) {
58*9880d681SAndroid Build Coastguard Worker std::vector<LineEditor::Completion> Comps;
59*9880d681SAndroid Build Coastguard Worker
60*9880d681SAndroid Build Coastguard Worker Comps.push_back(LineEditor::Completion("foo", "int foo()"));
61*9880d681SAndroid Build Coastguard Worker LE->setListCompleter(TestListCompleter(Comps));
62*9880d681SAndroid Build Coastguard Worker LineEditor::CompletionAction CA = LE->getCompletionAction("", 0);
63*9880d681SAndroid Build Coastguard Worker EXPECT_EQ(LineEditor::CompletionAction::AK_Insert, CA.Kind);
64*9880d681SAndroid Build Coastguard Worker EXPECT_EQ("foo", CA.Text);
65*9880d681SAndroid Build Coastguard Worker
66*9880d681SAndroid Build Coastguard Worker Comps.push_back(LineEditor::Completion("bar", "int bar()"));
67*9880d681SAndroid Build Coastguard Worker LE->setListCompleter(TestListCompleter(Comps));
68*9880d681SAndroid Build Coastguard Worker CA = LE->getCompletionAction("", 0);
69*9880d681SAndroid Build Coastguard Worker EXPECT_EQ(LineEditor::CompletionAction::AK_ShowCompletions, CA.Kind);
70*9880d681SAndroid Build Coastguard Worker ASSERT_EQ(2u, CA.Completions.size());
71*9880d681SAndroid Build Coastguard Worker ASSERT_EQ("int foo()", CA.Completions[0]);
72*9880d681SAndroid Build Coastguard Worker ASSERT_EQ("int bar()", CA.Completions[1]);
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard Worker Comps.clear();
75*9880d681SAndroid Build Coastguard Worker Comps.push_back(LineEditor::Completion("fee", "int fee()"));
76*9880d681SAndroid Build Coastguard Worker Comps.push_back(LineEditor::Completion("fi", "int fi()"));
77*9880d681SAndroid Build Coastguard Worker Comps.push_back(LineEditor::Completion("foe", "int foe()"));
78*9880d681SAndroid Build Coastguard Worker Comps.push_back(LineEditor::Completion("fum", "int fum()"));
79*9880d681SAndroid Build Coastguard Worker LE->setListCompleter(TestListCompleter(Comps));
80*9880d681SAndroid Build Coastguard Worker CA = LE->getCompletionAction("", 0);
81*9880d681SAndroid Build Coastguard Worker EXPECT_EQ(LineEditor::CompletionAction::AK_Insert, CA.Kind);
82*9880d681SAndroid Build Coastguard Worker EXPECT_EQ("f", CA.Text);
83*9880d681SAndroid Build Coastguard Worker }
84