test_defaults_loading.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "gtest/gtest.h"
  2. #include "lumacs/editor_core.hpp"
  3. #include "lumacs/lua_api.hpp"
  4. #include "lumacs/keybinding.hpp"
  5. #include "lumacs/command_system.hpp"
  6. #include "lumacs/window.hpp"
  7. #include <memory>
  8. using namespace lumacs;
  9. class DefaultsLoadingTest : public ::testing::Test {
  10. protected:
  11. void SetUp() override {
  12. core = std::make_unique<EditorCore>();
  13. // Load the REAL defaults (this is what main.cpp does)
  14. core->lua_api()->load_init_file();
  15. // Ensure buffer is ready and attached to window
  16. core->new_buffer("*scratch*");
  17. // Ensure we have an active window (Core constructor makes one, new_buffer assigns buffer to it)
  18. if (!core->active_window()) {
  19. // This shouldn't happen given EditorCore ctor logic, but safe check
  20. }
  21. core->set_cursor({0,0});
  22. }
  23. void TearDown() override {
  24. core.reset();
  25. }
  26. std::unique_ptr<EditorCore> core;
  27. };
  28. TEST_F(DefaultsLoadingTest, SelfInsertFromDefaults) {
  29. std::string input_key = "h";
  30. // 1. Process key (should be unbound in defaults)
  31. // Wait, 'h' is unbound. fallback triggers self-insert-command.
  32. // KeyBindingManager returns Unbound.
  33. auto result = core->keybinding_manager().process_key(input_key);
  34. EXPECT_EQ(result.type, KeyResult::Unbound);
  35. // 2. Simulate GtkEditor fallback logic
  36. if (result.type == KeyResult::Unbound) {
  37. if (input_key.length() == 1) {
  38. // This executes the LUA version of self-insert-command defined in defaults.hpp
  39. auto cmd_result = core->command_system().execute("self-insert-command", {input_key});
  40. EXPECT_EQ(cmd_result.status, CommandStatus::Success) << "Message: " << cmd_result.message;
  41. }
  42. }
  43. // 3. Verify state
  44. EXPECT_EQ(core->buffer().content(), "h");
  45. }
  46. TEST_F(DefaultsLoadingTest, NextLineCommand) {
  47. // Setup: 2 lines
  48. core->buffer().insert({0,0}, "Line 1\nLine 2");
  49. core->set_cursor({0,0});
  50. // Press C-n
  51. // "C-n" -> "next-line" -> editor:move_down()
  52. auto result = core->keybinding_manager().process_key("C-n");
  53. EXPECT_EQ(result.type, KeyResult::Executed);
  54. // Check cursor position. Should be line 1.
  55. EXPECT_EQ(core->cursor().line, 1);
  56. }
  57. TEST_F(DefaultsLoadingTest, ReturnKeyNewline) {
  58. core->buffer().insert({0,0}, "Line1");
  59. core->set_cursor({0,5}); // End of line
  60. // Press Return
  61. // "Return" -> "insert-newline"
  62. auto result = core->keybinding_manager().process_key("Return");
  63. EXPECT_EQ(result.type, KeyResult::Executed);
  64. // Check content
  65. EXPECT_EQ(core->buffer().content(), "Line1\n");
  66. // Check cursor (should be line 1, col 0)
  67. EXPECT_EQ(core->cursor().line, 1);
  68. EXPECT_EQ(core->cursor().column, 0);
  69. }