test_macro_manager.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "gtest/gtest.h"
  2. #include "lumacs/macro_manager.hpp"
  3. #include "lumacs/editor_core.hpp"
  4. #include <string>
  5. #include <vector>
  6. #include <memory>
  7. using namespace lumacs;
  8. // Fixture for MacroManager tests
  9. class MacroManagerTest : public ::testing::Test {
  10. protected:
  11. EditorCore core; // MacroManager's constructor requires an EditorCore&
  12. std::unique_ptr<MacroManager> macro_manager;
  13. void SetUp() override {
  14. macro_manager = std::make_unique<MacroManager>(core);
  15. }
  16. void TearDown() override {
  17. // Nothing specific needed
  18. }
  19. };
  20. TEST_F(MacroManagerTest, InitialState) {
  21. ASSERT_FALSE(macro_manager->is_recording_macro());
  22. ASSERT_TRUE(macro_manager->get_last_macro().empty());
  23. }
  24. TEST_F(MacroManagerTest, StartAndEndRecording) {
  25. macro_manager->start_kbd_macro();
  26. ASSERT_TRUE(macro_manager->is_recording_macro());
  27. macro_manager->record_key_sequence("a");
  28. macro_manager->record_key_sequence("b");
  29. macro_manager->record_key_sequence("C-c");
  30. macro_manager->end_kbd_macro_or_call();
  31. ASSERT_FALSE(macro_manager->is_recording_macro());
  32. ASSERT_FALSE(macro_manager->get_last_macro().empty());
  33. ASSERT_EQ(macro_manager->get_last_macro().size(), 3);
  34. ASSERT_EQ(macro_manager->get_last_macro()[0], "a");
  35. ASSERT_EQ(macro_manager->get_last_macro()[1], "b");
  36. ASSERT_EQ(macro_manager->get_last_macro()[2], "C-c");
  37. }
  38. TEST_F(MacroManagerTest, CallLastMacroWithoutRecording) {
  39. // Should not crash, and should not execute anything
  40. macro_manager->end_kbd_macro_or_call(); // Call when not recording and no last macro
  41. ASSERT_FALSE(macro_manager->is_recording_macro());
  42. ASSERT_TRUE(macro_manager->get_last_macro().empty());
  43. }
  44. TEST_F(MacroManagerTest, CallLastMacroExecutesKeys) {
  45. // This test is harder to verify directly in a unit test.
  46. // Executing the macro involves calling core.process_key, which
  47. // affects the editor's state (buffer, cursor).
  48. // For now, we can only verify the MacroManager's internal state.
  49. // A proper test would require mocking EditorCore::process_key or
  50. // making an integration test.
  51. macro_manager->start_kbd_macro();
  52. macro_manager->record_key_sequence("h");
  53. macro_manager->record_key_sequence("e");
  54. macro_manager->record_key_sequence("l");
  55. macro_manager->record_key_sequence("l");
  56. macro_manager->record_key_sequence("o");
  57. macro_manager->end_kbd_macro_or_call(); // This stores the macro
  58. // Calling the last macro will execute the recorded sequence.
  59. // We expect the macro to be executed.
  60. // However, without mocking core.process_key, we can only test MacroManager's state.
  61. // The previous test already verified the macro is recorded.
  62. // Let's ensure calling it doesn't crash.
  63. macro_manager->end_kbd_macro_or_call(); // This calls the last recorded macro
  64. ASSERT_FALSE(macro_manager->is_recording_macro()); // Still not recording
  65. ASSERT_FALSE(macro_manager->get_last_macro().empty()); // Macro should still be there
  66. }
  67. TEST_F(MacroManagerTest, ConsecutiveMacroRecording) {
  68. macro_manager->start_kbd_macro();
  69. macro_manager->record_key_sequence("a");
  70. macro_manager->end_kbd_macro_or_call();
  71. ASSERT_EQ(macro_manager->get_last_macro().size(), 1);
  72. ASSERT_EQ(macro_manager->get_last_macro()[0], "a");
  73. macro_manager->start_kbd_macro();
  74. macro_manager->record_key_sequence("b");
  75. macro_manager->record_key_sequence("c");
  76. macro_manager->end_kbd_macro_or_call();
  77. ASSERT_EQ(macro_manager->get_last_macro().size(), 2);
  78. ASSERT_EQ(macro_manager->get_last_macro()[0], "b");
  79. ASSERT_EQ(macro_manager->get_last_macro()[1], "c");
  80. }
  81. TEST_F(MacroManagerTest, MacroRecordingDuringMacroExecution) {
  82. // This tests Emacs' behavior: if you start recording during a macro call,
  83. // the executed keys are recorded into the new macro.
  84. // This requires mocking EditorCore's process_key.
  85. // For now, simple check:
  86. macro_manager->start_kbd_macro();
  87. macro_manager->record_key_sequence("a");
  88. macro_manager->end_kbd_macro_or_call(); // Macro: [a]
  89. // Simulate calling the macro, and during its call, start recording another
  90. // This can't be fully tested without a way to intercept process_key calls
  91. // and inject start_kbd_macro().
  92. // We'll rely on integration tests for this.
  93. }