added txt support

This commit is contained in:
Agel committed 2026-03-15 22:18:26 +01:00
1 parent 64a70bc4cf
commit 21656a3e9c
10 files changed
+441 -120

No files matched your search

+95 -7
View File
@@ -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)));
}
}
};