added txt support
This commit is contained in:
1 parent
64a70bc4cf
commit
21656a3e9c
10 files changed
+441
-120
No files matched your search
@@ -15,7 +15,8 @@ public:
|
||||
return {index, {}, {}};
|
||||
return {index,
|
||||
QStringLiteral("Chapter %1").arg(index),
|
||||
QStringLiteral("<p>Content %1</p>").arg(index)};
|
||||
{QStringLiteral("<p>Chunk A of %1</p>").arg(index),
|
||||
QStringLiteral("<p>Chunk B of %1</p>").arg(index)}};
|
||||
}
|
||||
|
||||
QList<TocEntry> tableOfContents() override { return {}; }
|
||||
@@ -32,8 +33,8 @@ private slots:
|
||||
ReadingModel model;
|
||||
QCOMPARE(model.totalChapters(), 0);
|
||||
QCOMPARE(model.progress(), 0.0);
|
||||
QVERIFY(model.chapterContent().isEmpty());
|
||||
QVERIFY(model.chapterTitle().isEmpty());
|
||||
QCOMPARE(model.rowCount(QModelIndex()), 0);
|
||||
}
|
||||
|
||||
void setReaderLoadsFirstChapter()
|
||||
@@ -45,7 +46,39 @@ private slots:
|
||||
QCOMPARE(model.currentChapter(), 0);
|
||||
QCOMPARE(model.totalChapters(), 3);
|
||||
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 0"));
|
||||
QCOMPARE(model.chapterContent(), QStringLiteral("<p>Content 0</p>"));
|
||||
}
|
||||
|
||||
void rowCountMatchesChunks()
|
||||
{
|
||||
ReadingModel model;
|
||||
FakeReader reader;
|
||||
model.setReader(&reader);
|
||||
|
||||
QCOMPARE(model.rowCount(QModelIndex()), 2);
|
||||
}
|
||||
|
||||
void dataReturnsChunkContent()
|
||||
{
|
||||
ReadingModel model;
|
||||
FakeReader reader;
|
||||
model.setReader(&reader);
|
||||
|
||||
QModelIndex idx0 = model.index(0, 0);
|
||||
QModelIndex idx1 = model.index(1, 0);
|
||||
QCOMPARE(model.data(idx0, ReadingModel::ContentRole).toString(),
|
||||
QStringLiteral("<p>Chunk A of 0</p>"));
|
||||
QCOMPARE(model.data(idx1, ReadingModel::ContentRole).toString(),
|
||||
QStringLiteral("<p>Chunk B of 0</p>"));
|
||||
}
|
||||
|
||||
void dataInvalidForOutOfRange()
|
||||
{
|
||||
ReadingModel model;
|
||||
FakeReader reader;
|
||||
model.setReader(&reader);
|
||||
|
||||
QModelIndex badIdx = model.index(99, 0);
|
||||
QVERIFY(!model.data(badIdx, ReadingModel::ContentRole).isValid());
|
||||
}
|
||||
|
||||
void nextChapter()
|
||||
@@ -60,6 +93,9 @@ private slots:
|
||||
QCOMPARE(model.currentChapter(), 1);
|
||||
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 1"));
|
||||
QCOMPARE(spy.count(), 1);
|
||||
QCOMPARE(model.rowCount(QModelIndex()), 2);
|
||||
QCOMPARE(model.data(model.index(0, 0), ReadingModel::ContentRole).toString(),
|
||||
QStringLiteral("<p>Chunk A of 1</p>"));
|
||||
}
|
||||
|
||||
void nextChapterAtEnd()
|
||||
@@ -112,7 +148,9 @@ private slots:
|
||||
|
||||
model.goToChapter(2);
|
||||
QCOMPARE(model.currentChapter(), 2);
|
||||
QCOMPARE(model.chapterContent(), QStringLiteral("<p>Content 2</p>"));
|
||||
QCOMPARE(model.rowCount(QModelIndex()), 2);
|
||||
QCOMPARE(model.data(model.index(0, 0), ReadingModel::ContentRole).toString(),
|
||||
QStringLiteral("<p>Chunk A of 2</p>"));
|
||||
}
|
||||
|
||||
void goToChapterOutOfRange()
|
||||
@@ -154,8 +192,31 @@ private slots:
|
||||
|
||||
QCOMPARE(model.totalChapters(), 0);
|
||||
QCOMPARE(model.progress(), 0.0);
|
||||
QVERIFY(model.chapterContent().isEmpty());
|
||||
QVERIFY(model.chapterTitle().isEmpty());
|
||||
QCOMPARE(model.rowCount(QModelIndex()), 0);
|
||||
}
|
||||
|
||||
void navigationResetsModel()
|
||||
{
|
||||
ReadingModel model;
|
||||
FakeReader reader;
|
||||
model.setReader(&reader);
|
||||
|
||||
QCOMPARE(model.rowCount(QModelIndex()), 2);
|
||||
|
||||
// Navigate and verify model is reset with new chapter's chunks
|
||||
model.nextChapter();
|
||||
QCOMPARE(model.rowCount(QModelIndex()), 2);
|
||||
QCOMPARE(model.data(model.index(0, 0), ReadingModel::ContentRole).toString(),
|
||||
QStringLiteral("<p>Chunk A of 1</p>"));
|
||||
}
|
||||
|
||||
void roleNamesContainsContent()
|
||||
{
|
||||
ReadingModel model;
|
||||
QHash<int, QByteArray> roles = model.roleNames();
|
||||
QVERIFY(roles.contains(ReadingModel::ContentRole));
|
||||
QCOMPARE(roles.value(ReadingModel::ContentRole), QByteArray("content"));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -26,12 +26,14 @@ private slots:
|
||||
|
||||
Chapter ch = reader.loadChapter(0);
|
||||
QCOMPARE(ch.index, 0);
|
||||
QVERIFY(!ch.contentHtml.isEmpty());
|
||||
QVERIFY(ch.contentHtml.contains("Books are a uniquely portable magic"));
|
||||
QVERIFY(!ch.contentChunks.isEmpty());
|
||||
// Small file should produce a single chunk
|
||||
QCOMPARE(ch.contentChunks.size(), 1);
|
||||
QVERIFY(ch.contentChunks.first().contains("Books are a uniquely portable magic"));
|
||||
// Verify Cyrillic content survived
|
||||
QVERIFY(ch.contentHtml.contains(QString::fromUtf8("Искусство чтения")));
|
||||
QVERIFY(ch.contentChunks.first().contains(QString::fromUtf8("Искусство чтения")));
|
||||
// Verify HTML paragraph structure
|
||||
QVERIFY(ch.contentHtml.contains("<p>"));
|
||||
QVERIFY(ch.contentChunks.first().contains("<p>"));
|
||||
}
|
||||
|
||||
void tableOfContentsEmpty()
|
||||
@@ -61,7 +63,7 @@ private slots:
|
||||
reader.open(fixturePath("sample.txt"));
|
||||
|
||||
Chapter ch = reader.loadChapter(1);
|
||||
QVERIFY(ch.contentHtml.isEmpty());
|
||||
QVERIFY(ch.contentChunks.isEmpty());
|
||||
}
|
||||
|
||||
void openCp1251File()
|
||||
@@ -71,8 +73,94 @@ private slots:
|
||||
QCOMPARE(reader.chapterCount(), 1);
|
||||
|
||||
Chapter ch = reader.loadChapter(0);
|
||||
QVERIFY(ch.contentHtml.contains(QString::fromUtf8("Привет, мир!")));
|
||||
QVERIFY(ch.contentHtml.contains(QString::fromUtf8("Русская литература")));
|
||||
QVERIFY(!ch.contentChunks.isEmpty());
|
||||
QString allContent = ch.contentChunks.join(QString());
|
||||
QVERIFY(allContent.contains(QString::fromUtf8("Привет, мир!")));
|
||||
QVERIFY(allContent.contains(QString::fromUtf8("Русская литература")));
|
||||
}
|
||||
|
||||
void largeFileProducesMultipleChunks()
|
||||
{
|
||||
// Generate a large file with paragraphs
|
||||
QTemporaryFile tmpFile;
|
||||
tmpFile.setAutoRemove(true);
|
||||
QVERIFY(tmpFile.open());
|
||||
|
||||
// Write ~20KB of paragraphed text (well above 4096 chunk size)
|
||||
QString paragraph = QStringLiteral("This is a paragraph of text that has some content. ");
|
||||
paragraph = paragraph.repeated(10); // ~500 chars per paragraph
|
||||
for (int i = 0; i < 50; ++i) {
|
||||
tmpFile.write(paragraph.toUtf8());
|
||||
tmpFile.write("\n\n");
|
||||
}
|
||||
tmpFile.close();
|
||||
|
||||
TxtReader reader;
|
||||
QVERIFY(reader.open(tmpFile.fileName()));
|
||||
|
||||
Chapter ch = reader.loadChapter(0);
|
||||
QVERIFY(ch.contentChunks.size() > 1);
|
||||
// Each chunk should have <p> tags
|
||||
for (const QString &chunk : ch.contentChunks) {
|
||||
QVERIFY(chunk.contains("<p>"));
|
||||
}
|
||||
}
|
||||
|
||||
void wallOfTextProducesMultipleChunks()
|
||||
{
|
||||
// Generate a large file with no blank lines
|
||||
QTemporaryFile tmpFile;
|
||||
tmpFile.setAutoRemove(true);
|
||||
QVERIFY(tmpFile.open());
|
||||
|
||||
// Write ~20KB of text with single newlines only
|
||||
QString line = QStringLiteral("This is a line of continuous text without blank lines between them. ");
|
||||
line = line.repeated(5); // ~340 chars per line
|
||||
for (int i = 0; i < 80; ++i) {
|
||||
tmpFile.write(line.toUtf8());
|
||||
tmpFile.write("\n");
|
||||
}
|
||||
tmpFile.close();
|
||||
|
||||
TxtReader reader;
|
||||
QVERIFY(reader.open(tmpFile.fileName()));
|
||||
|
||||
Chapter ch = reader.loadChapter(0);
|
||||
QVERIFY(ch.contentChunks.size() > 1);
|
||||
// Each chunk should be standalone HTML
|
||||
for (const QString &chunk : ch.contentChunks) {
|
||||
QVERIFY(chunk.contains("<p>"));
|
||||
}
|
||||
}
|
||||
|
||||
void eachChunkIsStandaloneHtml()
|
||||
{
|
||||
// Generate a large file
|
||||
QTemporaryFile tmpFile;
|
||||
tmpFile.setAutoRemove(true);
|
||||
QVERIFY(tmpFile.open());
|
||||
|
||||
QString paragraph = QStringLiteral("Test paragraph content here. ");
|
||||
paragraph = paragraph.repeated(15);
|
||||
for (int i = 0; i < 40; ++i) {
|
||||
tmpFile.write(paragraph.toUtf8());
|
||||
tmpFile.write("\n\n");
|
||||
}
|
||||
tmpFile.close();
|
||||
|
||||
TxtReader reader;
|
||||
QVERIFY(reader.open(tmpFile.fileName()));
|
||||
|
||||
Chapter ch = reader.loadChapter(0);
|
||||
QVERIFY(ch.contentChunks.size() > 1);
|
||||
for (const QString &chunk : ch.contentChunks) {
|
||||
// Every chunk must start with <p> and end with </p>
|
||||
QString trimmed = chunk.trimmed();
|
||||
QVERIFY2(trimmed.startsWith("<p>"),
|
||||
qPrintable("Chunk doesn't start with <p>: " + trimmed.left(50)));
|
||||
QVERIFY2(trimmed.endsWith("</p>"),
|
||||
qPrintable("Chunk doesn't end with </p>: " + trimmed.right(50)));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user