LineEditor.cpp 2.5 KB

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