136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines//===-- LineEditor.cpp ----------------------------------------------------===//
236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines//
336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines//                     The LLVM Compiler Infrastructure
436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines//
536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// This file is distributed under the University of Illinois Open Source
636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// License. See LICENSE.TXT for details.
736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines//
836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines//===----------------------------------------------------------------------===//
936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
1036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/LineEditor/LineEditor.h"
11dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#include "llvm/Support/FileSystem.h"
1236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/Support/Path.h"
1336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "gtest/gtest.h"
1436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
1536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesusing namespace llvm;
1636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
1736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesclass LineEditorTest : public testing::Test {
1836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinespublic:
1936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  SmallString<64> HistPath;
2036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  LineEditor *LE;
2136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
2236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  LineEditorTest() {
2336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    init();
2436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
2536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
2636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void init() {
2736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    sys::fs::createTemporaryFile("temp", "history", HistPath);
2836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    ASSERT_FALSE(HistPath.empty());
2936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    LE = new LineEditor("test", HistPath);
3036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
3136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
320c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  ~LineEditorTest() override {
3336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    delete LE;
3436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    sys::fs::remove(HistPath.str());
3536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
3636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines};
3736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
3836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesTEST_F(LineEditorTest, HistorySaveLoad) {
3936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  LE->saveHistory();
4036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  LE->loadHistory();
4136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines}
4236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
4336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesstruct TestListCompleter {
4436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::vector<LineEditor::Completion> Completions;
4536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
4636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  TestListCompleter(const std::vector<LineEditor::Completion> &Completions)
4736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      : Completions(Completions) {}
4836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
4936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::vector<LineEditor::Completion> operator()(StringRef Buffer,
5036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                                 size_t Pos) const {
5136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    EXPECT_TRUE(Buffer.empty());
5236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    EXPECT_EQ(0u, Pos);
5336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    return Completions;
5436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
5536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines};
5636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
5736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesTEST_F(LineEditorTest, ListCompleters) {
5836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::vector<LineEditor::Completion> Comps;
5936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
6036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  Comps.push_back(LineEditor::Completion("foo", "int foo()"));
6136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  LE->setListCompleter(TestListCompleter(Comps));
6236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  LineEditor::CompletionAction CA = LE->getCompletionAction("", 0);
6336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  EXPECT_EQ(LineEditor::CompletionAction::AK_Insert, CA.Kind);
6436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  EXPECT_EQ("foo", CA.Text);
6536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
6636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  Comps.push_back(LineEditor::Completion("bar", "int bar()"));
6736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  LE->setListCompleter(TestListCompleter(Comps));
6836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  CA = LE->getCompletionAction("", 0);
6936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  EXPECT_EQ(LineEditor::CompletionAction::AK_ShowCompletions, CA.Kind);
7036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ASSERT_EQ(2u, CA.Completions.size());
7136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ASSERT_EQ("int foo()", CA.Completions[0]);
7236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ASSERT_EQ("int bar()", CA.Completions[1]);
7336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
7436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  Comps.clear();
7536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  Comps.push_back(LineEditor::Completion("fee", "int fee()"));
7636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  Comps.push_back(LineEditor::Completion("fi", "int fi()"));
7736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  Comps.push_back(LineEditor::Completion("foe", "int foe()"));
7836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  Comps.push_back(LineEditor::Completion("fum", "int fum()"));
7936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  LE->setListCompleter(TestListCompleter(Comps));
8036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  CA = LE->getCompletionAction("", 0);
8136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  EXPECT_EQ(LineEditor::CompletionAction::AK_Insert, CA.Kind);
8236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  EXPECT_EQ("f", CA.Text);
8336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines}
84