Peter Collingbourne | c7d437c | 2014-01-31 23:46:14 +0000 | [diff] [blame] | 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" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 11 | #include "llvm/Support/FileSystem.h" |
Peter Collingbourne | c7d437c | 2014-01-31 23:46:14 +0000 | [diff] [blame] | 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 | |
| 22 | LineEditorTest() { |
| 23 | init(); |
| 24 | } |
| 25 | |
| 26 | void init() { |
| 27 | sys::fs::createTemporaryFile("temp", "history", HistPath); |
| 28 | ASSERT_FALSE(HistPath.empty()); |
| 29 | LE = new LineEditor("test", HistPath); |
| 30 | } |
| 31 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 32 | ~LineEditorTest() override { |
Peter Collingbourne | c7d437c | 2014-01-31 23:46:14 +0000 | [diff] [blame] | 33 | delete LE; |
| 34 | sys::fs::remove(HistPath.str()); |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | TEST_F(LineEditorTest, HistorySaveLoad) { |
| 39 | LE->saveHistory(); |
| 40 | LE->loadHistory(); |
| 41 | } |
| 42 | |
| 43 | struct TestListCompleter { |
| 44 | std::vector<LineEditor::Completion> Completions; |
| 45 | |
| 46 | TestListCompleter(const std::vector<LineEditor::Completion> &Completions) |
| 47 | : Completions(Completions) {} |
| 48 | |
| 49 | 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 | |
| 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 | } |