From 1fcf0ae58f0a4358d6f7904b787e7fa9658b59ac Mon Sep 17 00:00:00 2001 From: Lieven Hey Date: Wed, 17 Apr 2024 10:06:55 +0200 Subject: [PATCH] add search test to tst_models --- tests/modeltests/tst_models.cpp | 84 +++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/tests/modeltests/tst_models.cpp b/tests/modeltests/tst_models.cpp index 5e4cee09..e8ad3960 100644 --- a/tests/modeltests/tst_models.cpp +++ b/tests/modeltests/tst_models.cpp @@ -11,10 +11,13 @@ #include #include #include +#include +#include #include #include #include "../testutils.h" +#include "search.h" #include #include @@ -473,6 +476,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.flush(); + + 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(), + QStringLiteral("Line 3")); + + QCOMPARE(model.data(model.index(5, SourceCodeModel::SourceCodeColumn), Qt::DisplayRole).value(), + 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"), Direction::Forward, i); + auto result = searchSpy.takeFirst(); + QCOMPARE(result.at(0).value(), model.index(3, SourceCodeModel::SourceCodeColumn)); + } + + // Check wrap around + for (int i = 1; i < 4; i++) { + QSignalSpy endReached(&model, &SourceCodeModel::searchEndReached); + model.find(QStringLiteral("Line 3"), 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"), Direction::Forward, i); + auto result = searchSpy.takeFirst(); + QCOMPARE(result.at(0).value().isValid(), false); + } + + // test backward search + for (int i = 4; i > 0; i--) { + model.find(QStringLiteral("Line 7"), Direction::Backward, i); + auto result = searchSpy.takeFirst(); + QCOMPARE(result.at(0).value(), 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"), 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"), Direction::Backward, i); + auto result = searchSpy.takeFirst(); + QCOMPARE(result.at(0).value().isValid(), false); + } + } + void testEventModel() { Data::EventResults events;