Improve format and error handling

This commit is contained in:
Agel committed 2026-04-08 20:41:14 +02:00
1 parent 503324e7e3
commit 86e44e5d2a
23 files changed
+138 -49

No files matched your search

+28 -5
View File
@@ -1,6 +1,16 @@
#include <QtTest>
#include "backend/ReaderFactory.h"
#include "backend/FormatReader.h"
#include "backend/formats/TxtReader.h"
#include "backend/formats/EpubReader.h"
static ReaderFactory makeFactory()
{
return ReaderFactory({
[] { return std::make_unique<TxtReader>(); },
[] { return std::make_unique<EpubReader>(); },
});
}
class ReaderFactoryTest : public QObject {
Q_OBJECT
@@ -8,33 +18,46 @@ class ReaderFactoryTest : public QObject {
private slots:
void createTxtReader()
{
auto reader = ReaderFactory::create("file.txt");
auto factory = makeFactory();
auto reader = factory.create("file.txt");
QVERIFY(reader != nullptr);
}
void createTxtReaderUpperCase()
{
auto reader = ReaderFactory::create("file.TXT");
auto factory = makeFactory();
auto reader = factory.create("file.TXT");
QVERIFY(reader != nullptr);
}
void createUnknownFormat()
{
auto reader = ReaderFactory::create("file.xyz");
auto factory = makeFactory();
auto reader = factory.create("file.xyz");
QVERIFY(reader == nullptr);
}
void createNoExtension()
{
auto reader = ReaderFactory::create("noext");
auto factory = makeFactory();
auto reader = factory.create("noext");
QVERIFY(reader == nullptr);
}
void createEmptyPath()
{
auto reader = ReaderFactory::create("");
auto factory = makeFactory();
auto reader = factory.create("");
QVERIFY(reader == nullptr);
}
void supportedExtensions()
{
auto factory = makeFactory();
const QStringList exts = factory.supportedExtensions();
QVERIFY(exts.contains(QStringLiteral("txt")));
QVERIFY(exts.contains(QStringLiteral("epub")));
}
};
QTEST_MAIN(ReaderFactoryTest)
+2
View File
@@ -32,6 +32,7 @@ public:
QString coverImagePath() override { return {}; }
QByteArray contentHash() override { return QByteArray("fakehash"); }
bool loadMetadata(const QString &) override { return true; }
QString fileType() const override { return QStringLiteral("txt"); }
};
class SingleChapterReader : public FormatReader {
@@ -47,6 +48,7 @@ public:
QString coverImagePath() override { return {}; }
QByteArray contentHash() override { return QByteArray("singlehash"); }
bool loadMetadata(const QString &) override { return true; }
QString fileType() const override { return QStringLiteral("txt"); }
};
class ReadingModelTest : public QObject {
+11 -2
View File
@@ -6,6 +6,9 @@
#include "backend/AppState.h"
#include "backend/IFileDialogService.h"
#include "backend/ReaderFactory.h"
#include "backend/formats/TxtReader.h"
#include "backend/formats/EpubReader.h"
#include "backend/state/BookPositionStore.h"
#include "backend/book/Book.h"
@@ -14,7 +17,7 @@ class FakeDialogService : public IFileDialogService {
Q_OBJECT
public:
explicit FakeDialogService(QObject *parent = nullptr) : IFileDialogService(parent) {}
void open() override { /* no-op */ }
void open(const QStringList &) override { /* no-op */ }
void triggerFile(const QString &path) { emit fileSelected(path); }
};
@@ -46,6 +49,7 @@ private:
AppState *appState = nullptr;
BookPositionStore *store = nullptr;
FakeDialogService *dialog = nullptr;
ReaderFactory *factory = nullptr;
Book *book = nullptr;
Ctx() {
@@ -55,11 +59,16 @@ private:
appState = new AppState(*s);
store = new BookPositionStore(*appState);
dialog = new FakeDialogService;
book = new Book(*dialog, *store);
factory = new ReaderFactory({
[] { return std::make_unique<TxtReader>(); },
[] { return std::make_unique<EpubReader>(); },
});
book = new Book(*dialog, *store, *factory);
}
~Ctx() {
delete book;
delete dialog;
delete factory;
delete store;
delete appState;
delete s;