added txt support

This commit is contained in:
Agel committed 2026-03-15 21:33:18 +01:00
1 parent 3c0037cfc9
commit 64a70bc4cf
22 files changed
+800 -186

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
#include <QtTest>
#include <QSignalSpy>
#include <QUrl>
#include "backend/BookController.h"
#include "backend/ReadingModel.h"
class BookControllerTest : public QObject {
Q_OBJECT
private:
QString fixturePath(const QString &filename) const
{
return QStringLiteral(FIXTURES_DIR) + "/" + filename;
}
private slots:
void openValidBook()
{
BookController ctrl;
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QCOMPARE(readySpy.count(), 1);
QVERIFY(ctrl.hasBook());
QCOMPARE(ctrl.bookTitle(), QStringLiteral("sample"));
}
void openInvalidPath()
{
BookController ctrl;
QSignalSpy errorSpy(&ctrl, &BookController::bookError);
ctrl.openBook(QUrl::fromLocalFile("/tmp/nonexistent_asteria_file.txt"));
QCOMPARE(errorSpy.count(), 1);
QVERIFY(!ctrl.errorMessage().isEmpty());
QVERIFY(!ctrl.hasBook());
}
void openUnsupportedFormat()
{
BookController ctrl;
QSignalSpy errorSpy(&ctrl, &BookController::bookError);
ctrl.openBook(QUrl::fromLocalFile("/tmp/file.xyz"));
QCOMPARE(errorSpy.count(), 1);
QCOMPARE(ctrl.errorMessage(), QStringLiteral("No format readers available"));
QVERIFY(!ctrl.hasBook());
}
void closeBook()
{
BookController ctrl;
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QVERIFY(ctrl.hasBook());
QSignalSpy changedSpy(&ctrl, &BookController::bookChanged);
ctrl.closeBook();
QVERIFY(!ctrl.hasBook());
QVERIFY(ctrl.bookTitle().isEmpty());
QVERIFY(ctrl.bookAuthor().isEmpty());
QCOMPARE(changedSpy.count(), 1);
}
void openBookSetsReadingModel()
{
BookController ctrl;
ReadingModel model;
ctrl.setReadingModel(&model);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
QVERIFY(model.totalChapters() > 0);
}
void loadingTransitions()
{
BookController ctrl;
QSignalSpy loadingSpy(&ctrl, &BookController::loadingChanged);
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
// loading goes true then false → 2 emissions
QCOMPARE(loadingSpy.count(), 2);
// After completion, loading should be false
QVERIFY(!ctrl.loading());
}
};
QTEST_MAIN(BookControllerTest)
#include "BookControllerTest.moc"
+41
View File
@@ -0,0 +1,41 @@
#include <QtTest>
#include "backend/ReaderFactory.h"
#include "backend/FormatReader.h"
class ReaderFactoryTest : public QObject {
Q_OBJECT
private slots:
void createTxtReader()
{
auto reader = ReaderFactory::create("file.txt");
QVERIFY(reader != nullptr);
}
void createTxtReaderUpperCase()
{
auto reader = ReaderFactory::create("file.TXT");
QVERIFY(reader != nullptr);
}
void createUnknownFormat()
{
auto reader = ReaderFactory::create("file.xyz");
QVERIFY(reader == nullptr);
}
void createNoExtension()
{
auto reader = ReaderFactory::create("noext");
QVERIFY(reader == nullptr);
}
void createEmptyPath()
{
auto reader = ReaderFactory::create("");
QVERIFY(reader == nullptr);
}
};
QTEST_MAIN(ReaderFactoryTest)
#include "ReaderFactoryTest.moc"
+163
View File
@@ -0,0 +1,163 @@
#include <QtTest>
#include <QSignalSpy>
#include "backend/ReadingModel.h"
#include "backend/FormatReader.h"
class FakeReader : public FormatReader {
public:
bool open(const QString &) override { return true; }
QString lastError() override { return {}; }
int chapterCount() override { return 3; }
Chapter loadChapter(int index) override
{
if (index < 0 || index >= 3)
return {index, {}, {}};
return {index,
QStringLiteral("Chapter %1").arg(index),
QStringLiteral("<p>Content %1</p>").arg(index)};
}
QList<TocEntry> tableOfContents() override { return {}; }
QString title() override { return QStringLiteral("Fake Book"); }
QString author() override { return QStringLiteral("Fake Author"); }
};
class ReadingModelTest : public QObject {
Q_OBJECT
private slots:
void initialState()
{
ReadingModel model;
QCOMPARE(model.totalChapters(), 0);
QCOMPARE(model.progress(), 0.0);
QVERIFY(model.chapterContent().isEmpty());
QVERIFY(model.chapterTitle().isEmpty());
}
void setReaderLoadsFirstChapter()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QCOMPARE(model.currentChapter(), 0);
QCOMPARE(model.totalChapters(), 3);
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 0"));
QCOMPARE(model.chapterContent(), QStringLiteral("<p>Content 0</p>"));
}
void nextChapter()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
model.nextChapter();
QCOMPARE(model.currentChapter(), 1);
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 1"));
QCOMPARE(spy.count(), 1);
}
void nextChapterAtEnd()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.goToChapter(2);
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
model.nextChapter();
QCOMPARE(model.currentChapter(), 2);
QCOMPARE(spy.count(), 0);
}
void previousChapter()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.goToChapter(1);
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
model.previousChapter();
QCOMPARE(model.currentChapter(), 0);
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 0"));
QCOMPARE(spy.count(), 1);
}
void previousChapterAtStart()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
model.previousChapter();
QCOMPARE(model.currentChapter(), 0);
QCOMPARE(spy.count(), 0);
}
void goToChapter()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.goToChapter(2);
QCOMPARE(model.currentChapter(), 2);
QCOMPARE(model.chapterContent(), QStringLiteral("<p>Content 2</p>"));
}
void goToChapterOutOfRange()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
model.goToChapter(-1);
QCOMPARE(model.currentChapter(), 0);
QCOMPARE(spy.count(), 0);
model.goToChapter(99);
QCOMPARE(model.currentChapter(), 0);
QCOMPARE(spy.count(), 0);
}
void progressCalculation()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
// Chapter 0 of 3 → 1/3
QCOMPARE(model.progress(), 1.0 / 3.0);
model.goToChapter(2);
// Chapter 2 of 3 → 3/3 = 1.0
QCOMPARE(model.progress(), 1.0);
}
void setReaderNull()
{
ReadingModel model;
FakeReader reader;
model.setReader(&reader);
model.setReader(nullptr);
QCOMPARE(model.totalChapters(), 0);
QCOMPARE(model.progress(), 0.0);
QVERIFY(model.chapterContent().isEmpty());
QVERIFY(model.chapterTitle().isEmpty());
}
};
QTEST_MAIN(ReadingModelTest)
#include "ReadingModelTest.moc"
+13
View File
@@ -0,0 +1,13 @@
The Art of Reading
Books are a uniquely portable magic. They transport us to worlds unknown,
introduce us to people we would never otherwise meet, and allow us to
experience lives vastly different from our own.
Reading is not just a pastime — it is a journey of the mind.
Искусство чтения
Книги — это уникальная портативная магия. Они переносят нас в неизведанные
миры, знакомят с людьми, которых мы никогда бы не встретили, и позволяют
пережить жизни, совершенно отличные от нашей собственной.
+2
View File
@@ -0,0 +1,2 @@
Привет, мир! Это тестовый файл в кодировке Windows-1251.
Русская литература — великое наследие.
+80
View File
@@ -0,0 +1,80 @@
#include <QtTest>
#include "backend/formats/TxtReader.h"
class TxtReaderTest : public QObject {
Q_OBJECT
private:
QString fixturePath(const QString &filename) const
{
return QStringLiteral(FIXTURES_DIR) + "/" + filename;
}
private slots:
void openValidFile()
{
TxtReader reader;
QVERIFY(reader.open(fixturePath("sample.txt")));
QCOMPARE(reader.chapterCount(), 1);
QCOMPARE(reader.title(), QStringLiteral("sample"));
}
void loadChapter()
{
TxtReader reader;
reader.open(fixturePath("sample.txt"));
Chapter ch = reader.loadChapter(0);
QCOMPARE(ch.index, 0);
QVERIFY(!ch.contentHtml.isEmpty());
QVERIFY(ch.contentHtml.contains("Books are a uniquely portable magic"));
// Verify Cyrillic content survived
QVERIFY(ch.contentHtml.contains(QString::fromUtf8("Искусство чтения")));
// Verify HTML paragraph structure
QVERIFY(ch.contentHtml.contains("<p>"));
}
void tableOfContentsEmpty()
{
TxtReader reader;
reader.open(fixturePath("sample.txt"));
QVERIFY(reader.tableOfContents().isEmpty());
}
void authorEmpty()
{
TxtReader reader;
reader.open(fixturePath("sample.txt"));
QVERIFY(reader.author().isEmpty());
}
void openNonexistentFile()
{
TxtReader reader;
QVERIFY(!reader.open("/tmp/does_not_exist_asteria.txt"));
QVERIFY(!reader.lastError().isEmpty());
}
void loadChapterOutOfRange()
{
TxtReader reader;
reader.open(fixturePath("sample.txt"));
Chapter ch = reader.loadChapter(1);
QVERIFY(ch.contentHtml.isEmpty());
}
void openCp1251File()
{
TxtReader reader;
QVERIFY(reader.open(fixturePath("sample_cp1251.txt")));
QCOMPARE(reader.chapterCount(), 1);
Chapter ch = reader.loadChapter(0);
QVERIFY(ch.contentHtml.contains(QString::fromUtf8("Привет, мир!")));
QVERIFY(ch.contentHtml.contains(QString::fromUtf8("Русская литература")));
}
};
QTEST_MAIN(TxtReaderTest)
#include "TxtReaderTest.moc"