169 lines
5.2 KiB
C++
169 lines
5.2 KiB
C++
#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.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.contentChunks.first().contains(QString::fromUtf8("Искусство чтения")));
|
|
// Verify HTML paragraph structure
|
|
QVERIFY(ch.contentChunks.first().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.contentChunks.isEmpty());
|
|
}
|
|
|
|
void openCp1251File()
|
|
{
|
|
TxtReader reader;
|
|
QVERIFY(reader.open(fixturePath("sample_cp1251.txt")));
|
|
QCOMPARE(reader.chapterCount(), 1);
|
|
|
|
Chapter ch = reader.loadChapter(0);
|
|
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)));
|
|
}
|
|
}
|
|
};
|
|
|
|
QTEST_MAIN(TxtReaderTest)
|
|
#include "TxtReaderTest.moc"
|