xref: /llvm-project/llvm/unittests/LineEditor/LineEditor.cpp (revision 2946cd701067404b99c39fb29dc9c74bd7193eb3)
1c7d437c1SPeter Collingbourne //===-- LineEditor.cpp ----------------------------------------------------===//
2c7d437c1SPeter Collingbourne //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6c7d437c1SPeter Collingbourne //
7c7d437c1SPeter Collingbourne //===----------------------------------------------------------------------===//
8c7d437c1SPeter Collingbourne 
9c7d437c1SPeter Collingbourne #include "llvm/LineEditor/LineEditor.h"
10d59664f4SBenjamin Kramer #include "llvm/Support/FileSystem.h"
11c7d437c1SPeter Collingbourne #include "llvm/Support/Path.h"
12c7d437c1SPeter Collingbourne #include "gtest/gtest.h"
13c7d437c1SPeter Collingbourne 
14c7d437c1SPeter Collingbourne using namespace llvm;
15c7d437c1SPeter Collingbourne 
16c7d437c1SPeter Collingbourne class LineEditorTest : public testing::Test {
17c7d437c1SPeter Collingbourne public:
18c7d437c1SPeter Collingbourne   SmallString<64> HistPath;
19c7d437c1SPeter Collingbourne   LineEditor *LE;
20c7d437c1SPeter Collingbourne 
LineEditorTest()21c7d437c1SPeter Collingbourne   LineEditorTest() {
22c7d437c1SPeter Collingbourne     init();
23c7d437c1SPeter Collingbourne   }
24c7d437c1SPeter Collingbourne 
init()25c7d437c1SPeter Collingbourne   void init() {
26c7d437c1SPeter Collingbourne     sys::fs::createTemporaryFile("temp", "history", HistPath);
27c7d437c1SPeter Collingbourne     ASSERT_FALSE(HistPath.empty());
28c7d437c1SPeter Collingbourne     LE = new LineEditor("test", HistPath);
29c7d437c1SPeter Collingbourne   }
30c7d437c1SPeter Collingbourne 
~LineEditorTest()31f817c1cbSAlexander Kornienko   ~LineEditorTest() override {
32c7d437c1SPeter Collingbourne     delete LE;
33c7d437c1SPeter Collingbourne     sys::fs::remove(HistPath.str());
34c7d437c1SPeter Collingbourne   }
35c7d437c1SPeter Collingbourne };
36c7d437c1SPeter Collingbourne 
TEST_F(LineEditorTest,HistorySaveLoad)37c7d437c1SPeter Collingbourne TEST_F(LineEditorTest, HistorySaveLoad) {
38c7d437c1SPeter Collingbourne   LE->saveHistory();
39c7d437c1SPeter Collingbourne   LE->loadHistory();
40c7d437c1SPeter Collingbourne }
41c7d437c1SPeter Collingbourne 
42c7d437c1SPeter Collingbourne struct TestListCompleter {
43c7d437c1SPeter Collingbourne   std::vector<LineEditor::Completion> Completions;
44c7d437c1SPeter Collingbourne 
TestListCompleterTestListCompleter45c7d437c1SPeter Collingbourne   TestListCompleter(const std::vector<LineEditor::Completion> &Completions)
46c7d437c1SPeter Collingbourne       : Completions(Completions) {}
47c7d437c1SPeter Collingbourne 
operator ()TestListCompleter48c7d437c1SPeter Collingbourne   std::vector<LineEditor::Completion> operator()(StringRef Buffer,
49c7d437c1SPeter Collingbourne                                                  size_t Pos) const {
50c7d437c1SPeter Collingbourne     EXPECT_TRUE(Buffer.empty());
51c7d437c1SPeter Collingbourne     EXPECT_EQ(0u, Pos);
52c7d437c1SPeter Collingbourne     return Completions;
53c7d437c1SPeter Collingbourne   }
54c7d437c1SPeter Collingbourne };
55c7d437c1SPeter Collingbourne 
TEST_F(LineEditorTest,ListCompleters)56c7d437c1SPeter Collingbourne TEST_F(LineEditorTest, ListCompleters) {
57c7d437c1SPeter Collingbourne   std::vector<LineEditor::Completion> Comps;
58c7d437c1SPeter Collingbourne 
59c7d437c1SPeter Collingbourne   Comps.push_back(LineEditor::Completion("foo", "int foo()"));
60c7d437c1SPeter Collingbourne   LE->setListCompleter(TestListCompleter(Comps));
61c7d437c1SPeter Collingbourne   LineEditor::CompletionAction CA = LE->getCompletionAction("", 0);
62c7d437c1SPeter Collingbourne   EXPECT_EQ(LineEditor::CompletionAction::AK_Insert, CA.Kind);
63c7d437c1SPeter Collingbourne   EXPECT_EQ("foo", CA.Text);
64c7d437c1SPeter Collingbourne 
65c7d437c1SPeter Collingbourne   Comps.push_back(LineEditor::Completion("bar", "int bar()"));
66c7d437c1SPeter Collingbourne   LE->setListCompleter(TestListCompleter(Comps));
67c7d437c1SPeter Collingbourne   CA = LE->getCompletionAction("", 0);
68c7d437c1SPeter Collingbourne   EXPECT_EQ(LineEditor::CompletionAction::AK_ShowCompletions, CA.Kind);
69c7d437c1SPeter Collingbourne   ASSERT_EQ(2u, CA.Completions.size());
70c7d437c1SPeter Collingbourne   ASSERT_EQ("int foo()", CA.Completions[0]);
71c7d437c1SPeter Collingbourne   ASSERT_EQ("int bar()", CA.Completions[1]);
72c7d437c1SPeter Collingbourne 
73c7d437c1SPeter Collingbourne   Comps.clear();
74c7d437c1SPeter Collingbourne   Comps.push_back(LineEditor::Completion("fee", "int fee()"));
75c7d437c1SPeter Collingbourne   Comps.push_back(LineEditor::Completion("fi", "int fi()"));
76c7d437c1SPeter Collingbourne   Comps.push_back(LineEditor::Completion("foe", "int foe()"));
77c7d437c1SPeter Collingbourne   Comps.push_back(LineEditor::Completion("fum", "int fum()"));
78c7d437c1SPeter Collingbourne   LE->setListCompleter(TestListCompleter(Comps));
79c7d437c1SPeter Collingbourne   CA = LE->getCompletionAction("", 0);
80c7d437c1SPeter Collingbourne   EXPECT_EQ(LineEditor::CompletionAction::AK_Insert, CA.Kind);
81c7d437c1SPeter Collingbourne   EXPECT_EQ("f", CA.Text);
82c7d437c1SPeter Collingbourne }
83