Skip to content

Commit

Permalink
add search test to tst_models
Browse files Browse the repository at this point in the history
  • Loading branch information
lievenhey committed Jan 22, 2024
1 parent 899632d commit 4e4d930
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions tests/modeltests/tst_models.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <QObject>
#include <QProcess>
#include <QRegularExpression>
#include <QSignalSpy>
#include <QTemporaryFile>
#include <QTest>
#include <QTextStream>

Expand Down Expand Up @@ -473,6 +475,87 @@ private slots:
28);
}

void testSourceCodeModelSearch()
{
QTemporaryFile file;
if (!file.open()) {
QSKIP("Failed to create test file");
}

for (int i = 0; i < 10; i++) {
file.write(QStringLiteral("Line %1\n").arg(i).toUtf8());
}

file.close();

DisassemblyOutput output;
output.mainSourceFileName = file.fileName();
output.realSourceFileName = file.fileName();

DisassemblyOutput::DisassemblyLine line1;
line1.fileLine = Data::FileLine {file.fileName(), 4};

DisassemblyOutput::DisassemblyLine line2;
line2.fileLine = Data::FileLine {file.fileName(), 8};

output.disassemblyLines = {line1, line2};

SourceCodeModel model(nullptr);
model.setDisassembly(output, {});

QCOMPARE(model.rowCount(), 6); // 5 lines + function name
QCOMPARE(model.data(model.index(1, SourceCodeModel::SourceCodeColumn), Qt::DisplayRole).value<QString>(),
QStringLiteral("Line 3"));

QCOMPARE(model.data(model.index(5, SourceCodeModel::SourceCodeColumn), Qt::DisplayRole).value<QString>(),
QStringLiteral("Line 7"));

// check if search works in general
QSignalSpy searchSpy(&model, &SourceCodeModel::resultFound);
for (int i = 0; i < 5; i++) {
model.find(QStringLiteral("Line 5"), SourceCodeModel::Direction::Forward, i);
auto result = searchSpy.takeFirst();
QCOMPARE(result.at(0).value<QModelIndex>(), model.index(3, SourceCodeModel::SourceCodeColumn));
}

// Check wrap around
for (int i = 1; i < 5; i++) {
QSignalSpy endReached(&model, &SourceCodeModel::searchEndReached);
model.find(QStringLiteral("Line 3"), SourceCodeModel::Direction::Forward, i);
QCOMPARE(endReached.size(), 1);
}

// check if no result found works
searchSpy.clear();
for (int i = 0; i < 5; i++) {
model.find(QStringLiteral("Line 8"), SourceCodeModel::Direction::Forward, i);
auto result = searchSpy.takeFirst();
QCOMPARE(result.at(0).value<QModelIndex>().isValid(), false);
}

// test backward search
for (int i = 4; i > 0; i--) {
model.find(QStringLiteral("Line 7"), SourceCodeModel::Direction::Backward, i);
auto result = searchSpy.takeFirst();
QCOMPARE(result.at(0).value<QModelIndex>(), model.index(5, SourceCodeModel::SourceCodeColumn));
}

// Check wrap around
for (int i = 4; i > 0; i--) {
QSignalSpy endReached(&model, &SourceCodeModel::searchEndReached);
model.find(QStringLiteral("Line 7"), SourceCodeModel::Direction::Backward, i);
QCOMPARE(endReached.size(), 1);
}

// check if no result found works
searchSpy.clear();
for (int i = 0; i < 5; i++) {
model.find(QStringLiteral("Line 8"), SourceCodeModel::Direction::Backward, i);
auto result = searchSpy.takeFirst();
QCOMPARE(result.at(0).value<QModelIndex>().isValid(), false);
}
}

void testEventModel()
{
Data::EventResults events;
Expand Down

0 comments on commit 4e4d930

Please sign in to comment.