test_editor_core.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.buffer().insert({0,0}, "test");
  33. core.set_cursor({0,0});
  34. core.move_right();
  35. ASSERT_EQ(core.cursor().column, 1);
  36. }
  37. TEST_F(EditorCoreTest, NewBufferCommand) {
  38. core.new_buffer("test_new");
  39. ASSERT_EQ(core.buffer().name(), "test_new");
  40. ASSERT_EQ(core.cursor().line, 0);
  41. ASSERT_EQ(core.cursor().column, 0);
  42. }
  43. TEST_F(EditorCoreTest, SetMessage) {
  44. std::string test_message = "Hello, Lumacs!";
  45. core.set_message(test_message);
  46. ASSERT_EQ(core.last_message(), test_message);
  47. }
  48. TEST_F(EditorCoreTest, SplitWindowHorizontally) {
  49. core.split_horizontally();
  50. // After split, there should be 2 windows in the layout.
  51. std::vector<std::shared_ptr<lumacs::Window>> windows;
  52. core.window_manager().collect_windows(core.root_layout().get(), windows);
  53. ASSERT_EQ(windows.size(), 2);
  54. // The newly created window should be the active one.
  55. ASSERT_EQ(core.active_window(), windows[1]);
  56. }
  57. TEST_F(EditorCoreTest, KillLine) {
  58. core.buffer_manager().active_buffer()->insert({0,0}, "Line 1\nLine 2");
  59. core.set_cursor({0,0});
  60. core.kill_line();
  61. ASSERT_EQ(core.buffer().line(0), "Line 2");
  62. ASSERT_FALSE(core.kill_ring_manager().empty());
  63. ASSERT_EQ(core.kill_ring_manager().current(), "Line 1\n");
  64. }
  65. TEST_F(EditorCoreTest, Yank) {
  66. core.buffer_manager().active_buffer()->insert({0,0}, "some text");
  67. core.set_cursor({0,5}); // at ' '
  68. core.kill_line(); // Kills " text"
  69. core.yank(); // Yanks " text"
  70. ASSERT_EQ(core.buffer().line(0), "some text"); // Should be "some" + " text" from kill ring
  71. ASSERT_EQ(core.cursor().column, 9); // Cursor should be at end of yanked text
  72. }
  73. TEST_F(EditorCoreTest, CopyToRegisterAndInsert) {
  74. char reg_name = 'a';
  75. std::string reg_content = "Register content";
  76. core.copy_to_register(reg_name, reg_content);
  77. core.buffer_manager().active_buffer()->insert({0,0}, "Original ");
  78. core.set_cursor({0,9});
  79. core.insert_register(reg_name);
  80. ASSERT_EQ(core.buffer().line(0), "Original Register content");
  81. }
  82. TEST_F(EditorCoreTest, StartAndEndMacro) {
  83. ASSERT_FALSE(core.macro_manager().is_recording_macro());
  84. core.start_kbd_macro();
  85. ASSERT_TRUE(core.macro_manager().is_recording_macro());
  86. core.record_key_sequence("a");
  87. core.end_kbd_macro_or_call();
  88. ASSERT_FALSE(core.macro_manager().is_recording_macro());
  89. // Executing the macro will depend on process_key being called
  90. // which is hard to test without mocking keybinding_manager directly.
  91. // For now, just test recording state.
  92. }
  93. TEST_F(EditorCoreTest, KillRectangle) {
  94. core.buffer_manager().active_buffer()->insert({0,0}, "Line1\nLine2\nLine3");
  95. core.buffer_manager().active_buffer()->set_mark({0,1}); // Mark 'i'
  96. core.set_cursor({2,3}); // Cursor 'e' in Line3
  97. core.kill_rectangle();
  98. // Expected buffer state after killing rectangle from (0,1) to (2,3)
  99. // "Lne1"
  100. // "Lne2"
  101. // "Lne3"
  102. ASSERT_EQ(core.buffer().line(0), "Le1");
  103. ASSERT_EQ(core.buffer().line(1), "Le2");
  104. 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
  105. // Since we can't access private members, we'll check side effects or public API
  106. // For now, let's just assert that the buffer changed as expected.
  107. // A more robust test would verify the content of the rectangle_manager's kill ring
  108. // or through `yank_rectangle`.
  109. // For this test, we expect the buffer to be modified
  110. ASSERT_NE(core.buffer().content(), "Line1\nLine2\nLine3");
  111. }