Skip to content

Init Unit Tests #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d151fe4
[Refactor] Update include file path to fix testing errors
chrisdedman Mar 9, 2025
bcca0db
Added testing to the build
chrisdedman Mar 9, 2025
e068893
[Make] Added automating testing from Make file
chrisdedman Mar 9, 2025
f165366
[Test] CMakeLists file for testing directory
chrisdedman Mar 9, 2025
d07ddc3
[Test] Added init :test: mainwindow
chrisdedman Mar 9, 2025
292c1be
Merge branch 'main' into testing
chrisdedman Mar 10, 2025
ef882ff
[Update] Added Syntax to the test build
chrisdedman Mar 10, 2025
376c020
Merge branch 'main' into testing
chrisdedman Mar 23, 2025
036bfe3
[Refactor] Update include directives to use relative paths for unit test
chrisdedman Mar 23, 2025
04ed7f4
[Refactor] Add object names to menu items in createMenuBar for improv…
chrisdedman Mar 23, 2025
c44394a
[Refactor] Rename target variables and restructure CMakeLists.txt for…
chrisdedman Mar 23, 2025
14c2589
[Refactor] Update test_mainwindow CMake configuration to link with Co…
chrisdedman Mar 23, 2025
c0b69c6
[Refactor] Update include directives to use local paths instead of re…
chrisdedman Mar 23, 2025
fa6e342
[Refactor] Improve test structure and initialization in TestMainWindow
chrisdedman Mar 23, 2025
3d54fb9
[Refactor] Update comment
chrisdedman Mar 23, 2025
3894c52
Merge branch 'main' into testing
chrisdedman Mar 23, 2025
6f1c649
[added] Add test_tree executable and update CMake configuration
chrisdedman Mar 27, 2025
4aeafd5
[Refactor] Moved getDirectoryPath method from FileManager and createA…
chrisdedman Mar 27, 2025
45fc753
[Refactor] Simplify Tree constructor by removing FileManager dependen…
chrisdedman Mar 27, 2025
0f41a6b
[added] Implement getDirectoryPath method to allow directory selection
chrisdedman Mar 27, 2025
a10d924
[added] Implement Open Project action in file menu to initialize tree…
chrisdedman Mar 27, 2025
ef2f1dd
Refactor Tree class to remove FileManager dependency and streamline i…
chrisdedman Mar 27, 2025
d463bdd
[added] Implement tests for QSplitter initialization and action creat…
chrisdedman Mar 27, 2025
03ff076
[added] Implement unit tests for Tree class to validate openFile meth…
chrisdedman Mar 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
cmake_minimum_required(VERSION 3.16)

# Project name
set(TARGET_NAME CodeAstra)
set(TARGET_NAME CodeAstraApp)
set(EXECUTABLE_NAME CodeAstra)

set(QT_MAJOR_VERSION 6)

Expand Down Expand Up @@ -47,11 +48,10 @@ endif()
set(CMAKE_PREFIX_PATH ${Qt_DIR})

# Find Qt Widgets
find_package(Qt${QT_MAJOR_VERSION} COMPONENTS Widgets REQUIRED)
find_package(Qt${QT_MAJOR_VERSION} COMPONENTS Widgets Test REQUIRED)

# Add executable and source files
add_executable(${TARGET_NAME}
src/main.cpp
# Create the CodeAstra library
add_library(${TARGET_NAME}
src/MainWindow.cpp
src/CodeEditor.cpp
src/Syntax.cpp
Expand All @@ -65,29 +65,42 @@ add_executable(${TARGET_NAME}
include/FileManager.h
)

# Create the executable for the application
add_executable(${EXECUTABLE_NAME}
src/main.cpp
)

# Link the main executable with the CodeAstra library and Qt libraries
target_link_libraries(${EXECUTABLE_NAME} PRIVATE ${TARGET_NAME} Qt${QT_MAJOR_VERSION}::Widgets)

# Add the tests subdirectory
add_subdirectory(tests)

# Qt resource files
qt_add_resources(APP_RESOURCES resources.qrc)
target_sources(${TARGET_NAME} PRIVATE ${APP_RESOURCES})
target_sources(${EXECUTABLE_NAME} PRIVATE ${APP_RESOURCES})

# Compiler flags per OS
if(MSVC)
target_compile_options(${TARGET_NAME} PRIVATE /W4 /WX)
target_compile_options(${EXECUTABLE_NAME} PRIVATE /W4 /WX)
elseif(APPLE)
target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -pedantic -Werror)
set_target_properties(${TARGET_NAME} PROPERTIES MACOSX_BUNDLE TRUE)
target_compile_options(${EXECUTABLE_NAME} PRIVATE -Wall -Wextra -pedantic -Werror)
set_target_properties(${EXECUTABLE_NAME} PROPERTIES MACOSX_BUNDLE TRUE)
else()
target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -pedantic -Werror)
target_compile_options(${EXECUTABLE_NAME} PRIVATE -Wall -Wextra -pedantic -Werror)
endif()

# Include directories
target_include_directories(${TARGET_NAME} PRIVATE ${Qt${QT_MAJOR_VERSION}_INCLUDE_DIRS})
target_include_directories(${EXECUTABLE_NAME} PRIVATE ${Qt${QT_MAJOR_VERSION}_INCLUDE_DIRS})
target_include_directories(${EXECUTABLE_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/include)

# Set output names properly for Debug and Release
set_target_properties(${TARGET_NAME} PROPERTIES
set_target_properties(${EXECUTABLE_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}"
DEBUG_OUTPUT_NAME "${TARGET_NAME}d"
RELEASE_OUTPUT_NAME ${TARGET_NAME}
DEBUG_OUTPUT_NAME "${EXECUTABLE_NAME}d"
RELEASE_OUTPUT_NAME ${EXECUTABLE_NAME}
)

# Link necessary Qt libraries
# Link necessary Qt libraries to CodeAstra library
target_link_libraries(${TARGET_NAME} PRIVATE Qt${QT_MAJOR_VERSION}::Widgets)
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,11 @@ install:
fi \
fi
@echo "$(PROJECT) installed."

test:
@for test in ./build/tests/test_*; do \
if [ -f $$test ]; then \
echo "Running $$test..."; \
$$test; \
fi; \
done
2 changes: 2 additions & 0 deletions include/FileManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public slots:
void openFile();
void loadFileInEditor(const QString &filePath);

QString getDirectoryPath() const;

private:
FileManager(CodeEditor *editor, MainWindow *mainWindow);
~FileManager();
Expand Down
8 changes: 4 additions & 4 deletions include/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class MainWindow : public QMainWindow
// of the main window, alongside the code editor
void initTree();

QAction *createAction(const QIcon &icon, const QString &text,
const QKeySequence &shortcut, const QString &statusTip,
const std::function<void()> &slot);

private slots:
void showAbout();

Expand All @@ -40,10 +44,6 @@ private slots:
void createHelpActions(QMenu *helpMenu);
void createAppActions(QMenu *appMenu);

QAction *createAction(const QIcon &icon, const QString &text,
const QKeySequence &shortcut, const QString &statusTip,
const std::function<void()> &slot);

std::unique_ptr<CodeEditor> m_editor;
std::unique_ptr<Syntax> m_syntax;
std::unique_ptr<Tree> m_tree;
Expand Down
12 changes: 5 additions & 7 deletions include/Tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,18 @@ class Tree : public QObject
Q_OBJECT

public:
explicit Tree(QSplitter *splitter, FileManager *FileManager);
explicit Tree(QSplitter *splitter);
~Tree();

private:
void showContextMenu(const QPoint &pos);
void setupModel();
void initialize(const QString &directory);
void setupModel(const QString &directory);
void setupTree();
void openFile(const QModelIndex &index);

QString getDirectoryPath() const;
private:
void showContextMenu(const QPoint &pos);

std::unique_ptr<QFileIconProvider> m_iconProvider;
std::unique_ptr<QFileSystemModel> m_model;
std::unique_ptr<QTreeView> m_tree;

FileManager * m_FileManager;
};
11 changes: 9 additions & 2 deletions src/FileManager.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "FileManager.h"
#include "CodeEditor.h"
#include "MainWindow.h"

#include <QFileDialog>
#include <QMessageBox>
#include <QTextStream>
#include <QFileInfo>
#include "CodeEditor.h"
#include "MainWindow.h"

FileManager::FileManager(CodeEditor *editor, MainWindow *mainWindow)
: m_editor(editor), m_mainWindow(mainWindow)
Expand Down Expand Up @@ -144,3 +144,10 @@ QString FileManager::getFileExtension() const

return QFileInfo(m_currentFileName).suffix().toLower();
}

QString FileManager::getDirectoryPath() const
{
return QFileDialog::getExistingDirectory(
nullptr, QObject::tr("Open Directory"), QDir::homePath(),
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
}
18 changes: 16 additions & 2 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void MainWindow::initTree()
QSplitter *splitter = new QSplitter(Qt::Horizontal, this);
setCentralWidget(splitter);

m_tree = std::make_unique<Tree>(splitter, m_fileManager);
m_tree = std::make_unique<Tree>(splitter);

splitter->addWidget(m_editor.get());
splitter->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
Expand All @@ -62,8 +62,13 @@ void MainWindow::createMenuBar()
QMenuBar *menuBar = new QMenuBar(this);

QMenu *fileMenu = menuBar->addMenu("File");
fileMenu->setObjectName("File");

QMenu *helpMenu = menuBar->addMenu("Help");
QMenu *appMenu = menuBar->addMenu("CodeAstra");
helpMenu->setObjectName("Help");

QMenu *appMenu = menuBar->addMenu("CodeAstra");
appMenu->setObjectName("CodeAstra");

createFileActions(fileMenu);
createHelpActions(helpMenu);
Expand All @@ -75,6 +80,15 @@ void MainWindow::createMenuBar()
void MainWindow::createFileActions(QMenu *fileMenu)
{
fileMenu->addAction(createAction(QIcon(), tr("&New"), QKeySequence::New, tr("Create a new file"), [this]() { m_fileManager->newFile(); }));
fileMenu->addSeparator();
fileMenu->addAction(createAction(QIcon(), tr("&Open &Project"), QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_O), tr("Open a project"), [this]()
{
QString projectPath = m_fileManager->getDirectoryPath();
if (!projectPath.isEmpty())
{
m_tree->initialize(projectPath);
}
}));
fileMenu->addAction(createAction(QIcon(), tr("&Open"), QKeySequence::Open, tr("Open an existing file"), [this]() { m_fileManager->openFile(); }));
fileMenu->addSeparator();
fileMenu->addAction(createAction(QIcon(), tr("&Save"), QKeySequence::Save, tr("Save the current file"), [this]() { m_fileManager->saveFile(); }));
Expand Down
1 change: 1 addition & 0 deletions src/Syntax.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Syntax.h"

#include <QColor>
#include <QFont>

Expand Down
25 changes: 10 additions & 15 deletions src/Tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@
#include <QTreeView>
#include <QMenu>

Tree::Tree(QSplitter *splitter, FileManager *FileManager)
Tree::Tree(QSplitter *splitter)
: QObject(splitter),
m_iconProvider(std::make_unique<QFileIconProvider>()),
m_model(std::make_unique<QFileSystemModel>()),
m_tree(std::make_unique<QTreeView>(splitter)),
m_FileManager(FileManager)
m_tree(std::make_unique<QTreeView>(splitter))
{
setupModel();
setupTree();

connect(m_tree.get(), &QTreeView::doubleClicked, this, &Tree::openFile);
}

Tree::~Tree() {}

void Tree::setupModel()
void Tree::initialize(const QString &directory)
{
m_model->setRootPath(getDirectoryPath());
setupModel(directory);
setupTree();
}

void Tree::setupModel(const QString &directory)
{
m_model->setRootPath(directory);
m_model->setIconProvider(m_iconProvider.get());
m_model->setFilter(QDir::AllEntries | QDir::Hidden | QDir::NoDotAndDotDot);
}
Expand All @@ -51,13 +53,6 @@ void Tree::setupTree()
connect(m_tree.get(), &QTreeView::customContextMenuRequested, this, &Tree::showContextMenu);
}

QString Tree::getDirectoryPath() const
{
return QFileDialog::getExistingDirectory(
nullptr, QObject::tr("Open Directory"), QDir::homePath(),
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
}

void Tree::openFile(const QModelIndex &index)
{
QString filePath = m_model->filePath(index);
Expand Down
35 changes: 35 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Enable testing
enable_testing()

# Add individual test executables
add_executable(test_mainwindow
test_mainwindow.cpp
)

# Add test_tree executable
add_executable(test_tree
test_tree.cpp
)

# Link the test with the CodeAstra library and necessary Qt libraries
target_link_libraries(test_mainwindow PRIVATE ${TARGET_NAME} Qt6::Widgets Qt6::Test)
target_link_libraries(test_tree PRIVATE ${TARGET_NAME} Qt6::Widgets Qt6::Test)

# Register each test with CTest
add_test(NAME test_mainwindow COMMAND test_mainwindow)
add_test(NAME test_tree COMMAND test_tree)

# Set the runtime output directory for the test executables
set_target_properties(test_mainwindow PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build/tests
)
set_target_properties(test_tree PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build/tests
)

set_property(SOURCE test_mainwindow.cpp PROPERTY SKIP_AUTOMOC OFF)
set_property(SOURCE test_tree.cpp PROPERTY SKIP_AUTOMOC OFF)

# Include directories for tests
target_include_directories(test_mainwindow PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_include_directories(test_tree PRIVATE ${CMAKE_SOURCE_DIR}/include)
Loading
Loading