test_editor_core.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "gtest/gtest.h"
  2. #include "lumacs/editor_core.hpp"
  3. #include "lumacs/buffer_manager.hpp"
  4. #include "lumacs/window_manager.hpp"
  5. #include "lumacs/kill_ring_manager.hpp"
  6. #include "lumacs/register_manager.hpp"
  7. #include "lumacs/macro_manager.hpp"
  8. #include "lumacs/rectangle_manager.hpp"
  9. #include <string>
  10. // Test fixture for EditorCore, initializing core subsystems
  11. struct EditorCoreTest : public ::testing::Test {
  12. lumacs::EditorCore core;
  13. void SetUp() override {
  14. // EditorCore's constructor already initializes most things.
  15. // We might want to mock some dependencies for more isolated tests later.
  16. }
  17. void TearDown() override {
  18. // Clean up
  19. }
  20. };
  21. TEST_F(EditorCoreTest, InitialSetup) {
  22. ASSERT_NE(core.buffer_manager().active_buffer(), nullptr);
  23. ASSERT_NE(core.window_manager().active_window(), nullptr);
  24. ASSERT_EQ(core.buffer().name(), "*scratch*");
  25. ASSERT_EQ(core.cursor().line, 0);
  26. ASSERT_EQ(core.cursor().column, 0);
  27. }
  28. // TEST_F(EditorCoreTest, MoveCursorRight) {
  29. // // Test assumes initial cursor is at (0,0) on an empty buffer.
  30. // // EditorCore constructor and init.lua execution might affect this.
  31. // // Let's ensure a clean state first if needed, but for now test move_right.
  32. // core.move_right();
  33. // ASSERT_EQ(core.cursor().column, 1);
  34. // }
  35. TEST_F(EditorCoreTest, NewBufferCommand) {
  36. core.new_buffer("test_new");
  37. ASSERT_EQ(core.buffer().name(), "test_new");
  38. ASSERT_EQ(core.cursor().line, 0);
  39. ASSERT_EQ(core.cursor().column, 0);
  40. }
  41. TEST_F(EditorCoreTest, SetMessage) {
  42. std::string test_message = "Hello, Lumacs!";
  43. core.set_message(test_message);
  44. ASSERT_EQ(core.last_message(), test_message);
  45. }
  46. TEST_F(EditorCoreTest, SplitWindowHorizontally) {
  47. core.split_horizontally();
  48. // After split, there should be 2 windows in the layout.
  49. std::vector<std::shared_ptr<lumacs::Window>> windows;
  50. core.window_manager().collect_windows(core.root_layout().get(), windows);
  51. ASSERT_EQ(windows.size(), 2);
  52. // The newly created window should be the active one.
  53. ASSERT_EQ(core.active_window(), windows[1]);
  54. }
  55. TEST_F(EditorCoreTest, KillLine) {
  56. core.buffer_manager().active_buffer()->insert({0,0}, "Line 1\nLine 2");
  57. core.set_cursor({0,0});
  58. core.kill_line();
  59. ASSERT_EQ(core.buffer().line(0), "Line 2");
  60. ASSERT_FALSE(core.kill_ring_manager().empty());
  61. ASSERT_EQ(core.kill_ring_manager().current(), "Line 1\n");
  62. }
  63. TEST_F(EditorCoreTest, Yank) {
  64. core.buffer_manager().active_buffer()->insert({0,0}, "some text");
  65. core.set_cursor({0,5}); // at ' '
  66. core.kill_line(); // Kills " text"
  67. core.yank(); // Yanks " text"
  68. ASSERT_EQ(core.buffer().line(0), "some text"); // Should be "some" + " text" from kill ring
  69. ASSERT_EQ(core.cursor().column, 9); // Cursor should be at end of yanked text
  70. }
  71. TEST_F(EditorCoreTest, CopyToRegisterAndInsert) {
  72. char reg_name = 'a';
  73. std::string reg_content = "Register content";
  74. core.copy_to_register(reg_name, reg_content);
  75. core.buffer_manager().active_buffer()->insert({0,0}, "Original ");
  76. core.set_cursor({0,9});
  77. core.insert_register(reg_name);
  78. ASSERT_EQ(core.buffer().line(0), "Original Register content");
  79. }
  80. TEST_F(EditorCoreTest, StartAndEndMacro) {
  81. ASSERT_FALSE(core.macro_manager().is_recording_macro());
  82. core.start_kbd_macro();
  83. ASSERT_TRUE(core.macro_manager().is_recording_macro());
  84. core.record_key_sequence("a");
  85. core.end_kbd_macro_or_call();
  86. ASSERT_FALSE(core.macro_manager().is_recording_macro());
  87. // Executing the macro will depend on process_key being called
  88. // which is hard to test without mocking keybinding_manager directly.
  89. // For now, just test recording state.
  90. }
  91. TEST_F(EditorCoreTest, KillRectangle) {
  92. core.buffer_manager().active_buffer()->insert({0,0}, "Line1\nLine2\nLine3");
  93. core.buffer_manager().active_buffer()->set_mark({0,1}); // Mark 'i'
  94. core.set_cursor({2,3}); // Cursor 'e' in Line3
  95. core.kill_rectangle();
  96. // Expected buffer state after killing rectangle from (0,1) to (2,3)
  97. // "Lne1"
  98. // "Lne2"
  99. // "Lne3"
  100. ASSERT_EQ(core.buffer().line(0), "Le1");
  101. ASSERT_EQ(core.buffer().line(1), "Le2");
  102. ASSERT_EQ(core.buffer().line(2), "Le3"); // Check if rectangle was killed by verifying we can yank it back or check if it's not empty
  103. // Since we can't access private members, we'll check side effects or public API
  104. // For now, let's just assert that the buffer changed as expected.
  105. // A more robust test would verify the content of the rectangle_manager's kill ring
  106. // or through `yank_rectangle`.
  107. // For this test, we expect the buffer to be modified
  108. ASSERT_NE(core.buffer().content(), "Line1\nLine2\nLine3");
  109. }