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