Skip to content

Commit 263e2c0

Browse files
committed
Setup tests for running headless
1 parent 51319e9 commit 263e2c0

4 files changed

Lines changed: 100 additions & 20 deletions

File tree

CMakeLists.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ project(Qedit)
33

44
set(CMAKE_CXX_STANDARD 17)
55

6+
# Platform-specific filesystem library
7+
if(APPLE)
8+
# On macOS, filesystem is part of the standard library
9+
set(FILESYSTEM_LIB "")
10+
else()
11+
# On Linux, we need to link against stdc++fs
12+
set(FILESYSTEM_LIB stdc++fs)
13+
endif()
14+
615
# Include FetchContent for downloading Catch2
716
include(FetchContent)
817

@@ -24,20 +33,29 @@ add_executable(Qedit
2433
${SOURCES}
2534
src/EditorCommands.h
2635
)
36+
if(FILESYSTEM_LIB)
37+
target_link_libraries(Qedit PRIVATE ${FILESYSTEM_LIB})
38+
endif()
2739

2840
# Config test executable
2941
add_executable(config_test
3042
lib/config_test.cpp
3143
lib/Config.cpp
3244
)
45+
if(FILESYSTEM_LIB)
46+
target_link_libraries(config_test PRIVATE ${FILESYSTEM_LIB})
47+
endif()
3348

3449
# Editor test executable
3550
add_executable(editor_test
3651
tests/editor_test.cpp
3752
${SOURCES}
3853
)
3954
target_link_libraries(editor_test PRIVATE Catch2::Catch2WithMain)
40-
target_compile_definitions(editor_test PRIVATE TESTING)
55+
if(FILESYSTEM_LIB)
56+
target_link_libraries(editor_test PRIVATE ${FILESYSTEM_LIB})
57+
endif()
58+
target_compile_definitions(editor_test PRIVATE CATCH_CONFIG_FAST_COMPILE)
4159

4260
# Enable testing
4361
enable_testing()

src/QEditor.cpp

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <iostream>
66
#include <thread>
77
#include <sys/ioctl.h>
8+
#include <filesystem>
89

910
#include "EditorCommands.h"
1011
#include "../lib/EditorError.h"
@@ -14,8 +15,18 @@ namespace {
1415
}
1516

1617
Editor::Editor() {
17-
enableRawMode();
18-
updateWindowSize();
18+
#ifdef TESTING
19+
if (!skipTerminalSetup) {
20+
#endif
21+
enableRawMode();
22+
updateWindowSize();
23+
#ifdef TESTING
24+
} else {
25+
// Set default terminal size for testing
26+
screenRows = 24;
27+
screenCols = 80;
28+
}
29+
#endif
1930

2031
// Load configuration
2132
config.parse();
@@ -32,14 +43,20 @@ Editor::Editor() {
3243
filename = "";
3344
commandBuffer = "";
3445

35-
// Save terminal state and switch to alternate screen
36-
std::cout << "\x1b[?1049h" << std::flush;
37-
// Clear the alternate screen completely
38-
std::cout << "\x1b[2J" << std::flush;
39-
// Move to home position
40-
std::cout << "\x1b[H" << std::flush;
41-
// Disable line wrapping
42-
std::cout << "\x1b[?7l" << std::flush;
46+
#ifdef TESTING
47+
if (!skipTerminalSetup) {
48+
#endif
49+
// Save terminal state and switch to alternate screen
50+
std::cout << "\x1b[?1049h" << std::flush;
51+
// Clear the alternate screen completely
52+
std::cout << "\x1b[2J" << std::flush;
53+
// Move to home position
54+
std::cout << "\x1b[H" << std::flush;
55+
// Disable line wrapping
56+
std::cout << "\x1b[?7l" << std::flush;
57+
#ifdef TESTING
58+
}
59+
#endif
4360
}
4461

4562
Editor::~Editor() {
@@ -539,13 +556,36 @@ void Editor::deleteLine() {
539556
}
540557

541558
void Editor::updateWindowSize() {
559+
#ifdef TESTING
560+
if (skipTerminalSetup) {
561+
// Use default size for testing
562+
screenRows = 24;
563+
screenCols = 80;
564+
return;
565+
}
566+
#endif
567+
568+
// Check if we're in a terminal
569+
if (!isatty(STDOUT_FILENO)) {
570+
// Not in a terminal, use default size
571+
screenRows = 24;
572+
screenCols = 80;
573+
return;
574+
}
575+
542576
winsize ws{};
543577
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1) {
544-
throw QEditor::TerminalError("Failed to get window size");
578+
// If we can't get window size, use default
579+
screenRows = 24;
580+
screenCols = 80;
581+
return;
545582
}
546583

547584
if (ws.ws_row < 1 || ws.ws_col < 1) {
548-
throw QEditor::TerminalSizeError(ws.ws_row, ws.ws_col);
585+
// Invalid window size, use default
586+
screenRows = 24;
587+
screenCols = 80;
588+
return;
549589
}
550590

551591
screenRows = ws.ws_row;

src/QEditor.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <string>
77
#include <vector>
88
#include <chrono>
9+
#include <csignal>
910
#include "../lib/Config.h"
1011

1112
enum Mode { VIEW, EDIT, COMMAND };
@@ -49,8 +50,7 @@ class Editor {
4950
static void enableRawMode();
5051
static void disableRawMode();
5152

52-
#ifdef TESTING
53-
// Test helper methods
53+
// Test helper methods - always available
5454
[[nodiscard]] bool isInEditMode() const { return mode == EDIT; }
5555
[[nodiscard]] bool isInNormalMode() const { return mode == VIEW; }
5656
[[nodiscard]] bool isInCommandMode() const { return mode == COMMAND; }
@@ -67,7 +67,7 @@ class Editor {
6767
[[nodiscard]] size_t getScreenRows() const { return screenRows; }
6868
[[nodiscard]] size_t getScreenCols() const { return screenCols; }
6969

70-
// Test-only methods
70+
// Test-only methods - always available
7171
void setCursorPosition(size_t x, size_t y) {
7272
cur_x = x;
7373
cur_y = y;
@@ -77,6 +77,16 @@ class Editor {
7777
cur_x = 0;
7878
cur_y = 0;
7979
}
80+
81+
#ifdef TESTING
82+
// Test-specific terminal handling
83+
void skipTerminalInit() {
84+
skipTerminalSetup = true;
85+
}
86+
void setMockTerminalSize(size_t rows, size_t cols) {
87+
screenRows = rows;
88+
screenCols = cols;
89+
}
8090
#endif
8191

8292
private:
@@ -100,6 +110,9 @@ class Editor {
100110
size_t TAB_WIDTH = 4; // Now can be configured
101111
bool showLineNumbers = false; // Default to not showing line numbers
102112
bool running = true;
113+
#ifdef TESTING
114+
bool skipTerminalSetup = false;
115+
#endif
103116

104117
std::string commandBuffer;
105118
std::vector<std::string> buffer;

tests/editor_test.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
#define CATCH_CONFIG_MAIN
2+
#define TESTING // Define TESTING before including QEditor.h
23
#include <catch2/catch_test_macros.hpp>
34
#include "../src/QEditor.h"
45

5-
TEST_CASE("Basic editor initialization", "[editor]") {
6+
// Helper function to create a test-ready editor
7+
Editor createTestEditor() {
68
Editor editor;
9+
editor.skipTerminalInit();
10+
editor.setMockTerminalSize(24, 80);
11+
return editor;
12+
}
13+
14+
TEST_CASE("Basic editor initialization", "[editor]") {
15+
Editor editor = createTestEditor();
716
REQUIRE(editor.isInNormalMode());
817
REQUIRE(editor.getBuffer().empty());
918
REQUIRE(editor.getCursorX() == 0);
1019
REQUIRE(editor.getCursorY() == 0);
1120
}
1221

1322
TEST_CASE("Editor mode switching", "[editor]") {
14-
Editor editor;
23+
Editor editor = createTestEditor();
1524

1625
SECTION("Start in normal mode") {
1726
REQUIRE(editor.isInNormalMode());
@@ -33,15 +42,15 @@ TEST_CASE("Editor mode switching", "[editor]") {
3342
}
3443

3544
TEST_CASE("Editor buffer operations", "[editor]") {
36-
Editor editor;
45+
Editor editor = createTestEditor();
3746

3847
SECTION("Empty buffer") {
3948
REQUIRE(editor.getBuffer().empty());
4049
}
4150
}
4251

4352
TEST_CASE("Editor cursor navigation", "[editor]") {
44-
Editor editor;
53+
Editor editor = createTestEditor();
4554

4655
// Set up a 3x3 grid of text for testing navigation
4756
editor.editMode();

0 commit comments

Comments
 (0)