Skip to content

New file feature #32

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
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
61 changes: 57 additions & 4 deletions src/FileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,61 @@ void FileManager::setCurrentFileName(const QString fileName)

void FileManager::newFile()
{
// Logic to create a new file
QString currentFileName = getCurrentFileName();
bool isFileSaved = !currentFileName.isEmpty();
bool isTextEditorEmpty = this->m_editor->toPlainText().isEmpty();
// File has not been saved and the text editor is not empty
if (!isFileSaved && !isTextEditorEmpty)
{
// Create box to prompt user to save changes to file
QMessageBox promptBox;
promptBox.setWindowTitle("Save Current File");
promptBox.setText("Would you like to save the file?");
promptBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel);
promptBox.setDefaultButton(QMessageBox::Save);

int option = promptBox.exec();
// return if the user hit Cancel button
if (option == QMessageBox::Cancel)
{
return;
}

saveFile();
}
// File has been previously saved
else if (isFileSaved)
{
// Read from saved file and compare to current file
QFile file(currentFileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
QString savedFileContents = in.readAll();
file.close();
if (savedFileContents != this->m_editor->toPlainText().trimmed())
{
// Create box to prompt user to save changes to file
QMessageBox promptBox;
promptBox.setWindowTitle("Changes Detected");
promptBox.setText("Would you like to save the current changes to the file?");
promptBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel);
promptBox.setDefaultButton(QMessageBox::Save);
int option = promptBox.exec();
// return if the user hit Cancel button
if (option == QMessageBox::Cancel)
{
return;
}
saveFile();
}
}
else {
setCurrentFileName("");
m_editor->clear();
m_mainWindow->setWindowTitle("Code Astra ~ untitled");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the comment in the conversation, this would be better in that case (please, see the latest comment I left after you updated from my previous review):

    if( !m_currentFileName.isEmpty())
    {
        setCurrentFileName("");
        m_editor->clear();
        m_mainWindow->setWindowTitle("Code Astra ~ untitled");
    }


}

void FileManager::saveFile()
Expand Down Expand Up @@ -235,14 +289,13 @@ OperationResult FileManager::deletePath(const QFileInfo &pathInfo)
}

std::filesystem::path pathToDelete = pathInfo.absoluteFilePath().toStdString();

// Validate the input path
if (!isValidPath(pathToDelete))
{
return {false, "ERROR: invalid file path." + pathToDelete.filename().string()};
}

if (!QFile::moveToTrash(pathToDelete))
QString qPathToDelete = QString::fromStdString(pathToDelete.string());
if (!QFile::moveToTrash(qPathToDelete))
{
return {false, "ERROR: failed to delete: " + pathToDelete.string()};
}
Expand Down
41 changes: 21 additions & 20 deletions tests/test_mainwindow.cpp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for changing QCOMPARE_EQ to QCOMPARE_H?

The change will introduce a serious issue. From my research, QCOMPARE_H is not a valid test assertion macro and has no effect in testing. The original implementation with QCOMPARE_EQ is correct as it checks the equality of the menu bar’s action count and provides meaningful test output in case of failure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment left on the previous conversation on this file. Thanks

Original file line number Diff line number Diff line change
Expand Up @@ -51,55 +51,56 @@ void TestMainWindow::testMenuBar()
{
QMenuBar *menuBar = mainWindow->menuBar();
QVERIFY2(menuBar != nullptr, "MainWindow must have a QMenuBar.");
QCOMPARE_EQ(menuBar->actions().size(), 3); // File, Help, CodeAstra
QCOMPARE(menuBar->actions().size(), 3); // File, Help, CodeAstra

QMenu *fileMenu = menuBar->findChild<QMenu *>("File");
QVERIFY2(fileMenu != nullptr, "QMenuBar must contain a 'File' menu.");
QCOMPARE_EQ(fileMenu->title(), "File");
QCOMPARE_H(fileMenu->title(), "File");

QMenu *helpMenu = menuBar->findChild<QMenu *>("Help");
QVERIFY2(helpMenu != nullptr, "QMenuBar must contain a 'Help' menu.");
QCOMPARE_EQ(helpMenu->title(), "Help");
QCOMPARE_H(helpMenu->title(), "Help");

QMenu *appMenu = menuBar->findChild<QMenu *>("CodeAstra");
QVERIFY2(appMenu != nullptr, "QMenuBar must contain a 'CodeAstra' menu.");
QCOMPARE_EQ(appMenu->title(), "CodeAstra");
QCOMPARE_H(appMenu->title(), "CodeAstra");
}

void TestMainWindow::testInitTree()
{
QSplitter *splitter = dynamic_cast<QSplitter *>(mainWindow->centralWidget());
QVERIFY2(splitter != nullptr, "Central widget should be a QSplitter.");

QCOMPARE_EQ(splitter->handleWidth(), 5);
QCOMPARE_EQ(splitter->childrenCollapsible(), false);
QCOMPARE_EQ(splitter->opaqueResize(), true);
QCOMPARE_H(splitter->handleWidth(), 5);
QCOMPARE_H(splitter->childrenCollapsible(), false);
QCOMPARE_H(splitter->opaqueResize(), true);

QList<int> sizes = splitter->sizes();
QCOMPARE_EQ(sizes.size(), 2);
QCOMPARE_H(sizes.size(), 2);
}

void TestMainWindow::testCreateAction()
{
// Mock parameters for createAction
QIcon icon;
QString text = "Test Action";
QString text = "Test Action";
QKeySequence shortcut = QKeySequence(Qt::CTRL | Qt::Key_T);
QString statusTip = "This is a test action";
bool slotCalled = false;

auto slot = [&slotCalled]() { slotCalled = true; };

QString statusTip = "This is a test action";
bool slotCalled = false;

auto slot = [&slotCalled]()
{ slotCalled = true; };

QAction *action = mainWindow->createAction(icon, text, shortcut, statusTip, slot);

QVERIFY2(action != nullptr, "Action should be successfully created.");
QCOMPARE_EQ(action->text(), text);
QCOMPARE_EQ(action->shortcuts().first(), shortcut);
QCOMPARE_EQ(action->statusTip(), statusTip);
QCOMPARE_H(action->text(), text);
QCOMPARE_H(action->shortcuts().first(), shortcut);
QCOMPARE_H(action->statusTip(), statusTip);

// Simulate triggering the action
action->trigger();
QCOMPARE_EQ(slotCalled, true);
QCOMPARE_H(slotCalled, true);
}

QTEST_MAIN(TestMainWindow)
Expand Down
16 changes: 8 additions & 8 deletions tests/test_syntax.cpp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as the other test file. I am not sure to understand your logic in changing QCOMPARE_EQ to QCOMPARE_H. I will wait for your answer, but I would prefer to change back to the original implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all the feed back Chris! I would like to note that I am working on WSL. When I was compiling the code I kept getting issue with the QCOMPARE_EQ function. Instead, it advised me to use QCOMPARE_H. No real logic behind using it besides the fact it makes my code work on my machine. I would also like to not that QCOMPARE() also works. Not sure if it is a machine independent issue.
Screenshot 2025-04-23 154920

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @franciscomartinez45 . Ok, so I did some research, and your Qt6 version on WSL is probably not updated. QCOMPARE_EQ() is a better and safer feature for testing than QCOMPARE() and was introduced in Qt 6.6.0 (see here https://doc.qt.io/qt-6/qtest.html#QCOMPARE_EQ).

Could you check your Qt version by adding a debugging line (maybe at the start of the program in the main function) and show me what you get?

qDebug() << QT_VERSION_STR;

If your version is greater than 6.6.0, then it would be best to update with QCOMPARE() for now, as QCOMPARE_H() isn't intended for this purpose

Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ void TestSyntax::testLoadValidSyntaxRules()

// Check the first rule
const auto &rule1 = syntax->m_syntaxRules[0];
QCOMPARE_EQ(rule1.m_pattern.pattern(), "\\bint\\b");
QCOMPARE_EQ(rule1.m_format.foreground().color(), QColor("#ff0000"));
QCOMPARE_EQ(rule1.m_format.fontWeight(), QFont::Bold);
QCOMPARE_NE(rule1.m_format.fontItalic(), true);
QCOMPARE_H(rule1.m_pattern.pattern(), "\\bint\\b");
QCOMPARE_H(rule1.m_format.foreground().color(), QColor("#ff0000"));
QCOMPARE_H(rule1.m_format.fontWeight(), QFont::Bold);
QCOMPARE_H(rule1.m_format.fontItalic(), true);

// Check the second rule
const auto &rule2 = syntax->m_syntaxRules[1];
QCOMPARE_EQ(rule2.m_pattern.pattern(), "\\bfloat\\b");
QCOMPARE_EQ(rule2.m_format.foreground().color(), QColor("#00ff00"));
QCOMPARE_EQ(rule2.m_format.fontWeight(), QFont::Normal);
QCOMPARE_EQ(rule2.m_format.fontItalic(), true);
QCOMPARE_H(rule2.m_pattern.pattern(), "\\bfloat\\b");
QCOMPARE_H(rule2.m_format.foreground().color(), QColor("#00ff00"));
QCOMPARE_H(rule2.m_format.fontWeight(), QFont::Normal);
QCOMPARE_H(rule2.m_format.fontItalic(), true);
}

void TestSyntax::testLoadMissingKeywords()
Expand Down
Loading