summaryrefslogtreecommitdiff
path: root/unittests/LineEditor/LineEditor.cpp
blob: cb115bd7dedb27a7662c7cf1bc6ad680ebb24346 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//===-- LineEditor.cpp ----------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/LineEditor/LineEditor.h"
#include "llvm/Support/Path.h"
#include "gtest/gtest.h"

using namespace llvm;

class LineEditorTest : public testing::Test {
public:
  SmallString<64> HistPath;
  LineEditor *LE;

  LineEditorTest() {
    init();
  }

  void init() {
    sys::fs::createTemporaryFile("temp", "history", HistPath);
    ASSERT_FALSE(HistPath.empty());
    LE = new LineEditor("test", HistPath);
  }

  ~LineEditorTest() {
    delete LE;
    sys::fs::remove(HistPath.str());
  }
};

TEST_F(LineEditorTest, HistorySaveLoad) {
  LE->saveHistory();
  LE->loadHistory();
}

struct TestListCompleter {
  std::vector<LineEditor::Completion> Completions;

  TestListCompleter(const std::vector<LineEditor::Completion> &Completions)
      : Completions(Completions) {}

  std::vector<LineEditor::Completion> operator()(StringRef Buffer,
                                                 size_t Pos) const {
    EXPECT_TRUE(Buffer.empty());
    EXPECT_EQ(0u, Pos);
    return Completions;
  }
};

TEST_F(LineEditorTest, ListCompleters) {
  std::vector<LineEditor::Completion> Comps;

  Comps.push_back(LineEditor::Completion("foo", "int foo()"));
  LE->setListCompleter(TestListCompleter(Comps));
  LineEditor::CompletionAction CA = LE->getCompletionAction("", 0);
  EXPECT_EQ(LineEditor::CompletionAction::AK_Insert, CA.Kind);
  EXPECT_EQ("foo", CA.Text);

  Comps.push_back(LineEditor::Completion("bar", "int bar()"));
  LE->setListCompleter(TestListCompleter(Comps));
  CA = LE->getCompletionAction("", 0);
  EXPECT_EQ(LineEditor::CompletionAction::AK_ShowCompletions, CA.Kind);
  ASSERT_EQ(2u, CA.Completions.size());
  ASSERT_EQ("int foo()", CA.Completions[0]);
  ASSERT_EQ("int bar()", CA.Completions[1]);

  Comps.clear();
  Comps.push_back(LineEditor::Completion("fee", "int fee()"));
  Comps.push_back(LineEditor::Completion("fi", "int fi()"));
  Comps.push_back(LineEditor::Completion("foe", "int foe()"));
  Comps.push_back(LineEditor::Completion("fum", "int fum()"));
  LE->setListCompleter(TestListCompleter(Comps));
  CA = LE->getCompletionAction("", 0);
  EXPECT_EQ(LineEditor::CompletionAction::AK_Insert, CA.Kind);
  EXPECT_EQ("f", CA.Text);
}