Files
asteria-qt/tests/formats/TxtReaderTest.cpp
2026-10-31 00:15:55 +01:00

361 lines
11 KiB
C++

#include <QObject>
#include <QSignalSpy>
#include <QElapsedTimer>
#include <QTemporaryDir>
#include <QTemporaryFile>
#include <QTest>
#include "backend/formats/TxtReader.h"
#include "backend/reading/layout/ChunkLayoutEngine.h"
class TxtReaderTest : public QObject {
Q_OBJECT
private slots:
void testOpenNonExistentFile();
void testOpenEmptyFile();
void testStandardBlankLineParagraphs();
void testSceneBreaks();
void testUnwrappedTextWithoutBlankLines();
void testGutenbergMetadataExtraction();
void testMetadataOnlyMode();
void testUtf8WithBom();
void testUtf16Le();
void testWindows1251WithoutBom();
void testElochkaFileIfExists();
void testBulbaFile();
void testLuideFile();
void testBondarFile();
void testArinFile();
void testTonkiyFile();
};
void TxtReaderTest::testOpenNonExistentFile()
{
TxtReader reader;
QVERIFY(!reader.open(QStringLiteral("/path/to/nonexistent_file.txt")));
QVERIFY(!reader.lastError().isEmpty());
}
void TxtReaderTest::testOpenEmptyFile()
{
QTemporaryFile tempFile;
QVERIFY(tempFile.open());
tempFile.close();
TxtReader reader;
QVERIFY(reader.open(tempFile.fileName()));
QCOMPARE(reader.mode(), ReadingMode::Text);
QVERIFY(reader.chunks().isEmpty());
QVERIFY(!reader.contentHash().isEmpty());
}
void TxtReaderTest::testStandardBlankLineParagraphs()
{
QTemporaryFile tempFile;
QVERIFY(tempFile.open());
const QByteArray content =
"This is the first line of the story,\n"
"and this is the second line of the same paragraph.\n"
"\n"
"Here begins the second paragraph, which\n"
"also spans across multiple wrapped lines.\n";
tempFile.write(content);
tempFile.close();
TxtReader reader;
QVERIFY(reader.open(tempFile.fileName()));
const auto chunks = reader.chunks();
QVERIFY(!chunks.isEmpty());
// Count paragraphs across all chunks
int paragraphCount = 0;
for (const auto &chunk : chunks) {
for (const auto &block : chunk.blocks) {
if (block.kind == BlockItem::Kind::Paragraph) {
++paragraphCount;
QCOMPARE(block.align, TextAlign::Justify);
QCOMPARE(block.textIndent, 32.0);
}
}
}
QCOMPARE(paragraphCount, 2);
}
void TxtReaderTest::testSceneBreaks()
{
QTemporaryFile tempFile;
QVERIFY(tempFile.open());
const QByteArray content =
"First scene paragraph.\n"
"\n"
"\n"
"Second scene paragraph after double blank lines.\n";
tempFile.write(content);
tempFile.close();
TxtReader reader;
QVERIFY(reader.open(tempFile.fileName()));
const auto chunks = reader.chunks();
QVERIFY(!chunks.isEmpty());
bool foundSceneBreakContainer = false;
for (const auto &chunk : chunks) {
for (const auto &block : chunk.blocks) {
if (block.kind == BlockItem::Kind::ContainerStart) {
foundSceneBreakContainer = true;
}
}
}
QVERIFY(foundSceneBreakContainer);
}
void TxtReaderTest::testUnwrappedTextWithoutBlankLines()
{
QTemporaryFile tempFile;
QVERIFY(tempFile.open());
const QByteArray content =
"Single line paragraph one.\n"
"Single line paragraph two.\n"
"Single line paragraph three.\n";
tempFile.write(content);
tempFile.close();
TxtReader reader;
QVERIFY(reader.open(tempFile.fileName()));
const auto chunks = reader.chunks();
QVERIFY(!chunks.isEmpty());
int paragraphCount = 0;
for (const auto &chunk : chunks) {
for (const auto &block : chunk.blocks) {
if (block.kind == BlockItem::Kind::Paragraph) {
++paragraphCount;
}
}
}
QCOMPARE(paragraphCount, 3);
}
void TxtReaderTest::testGutenbergMetadataExtraction()
{
QTemporaryFile tempFile;
QVERIFY(tempFile.open());
const QByteArray content =
"The Project Gutenberg eBook of Wonderland\n"
"\n"
"Title: Alice's Adventures in Wonderland\n"
"Author: Lewis Carroll\n"
"\n"
"CHAPTER I. Down the Rabbit-Hole\n";
tempFile.write(content);
tempFile.close();
TxtReader reader;
QVERIFY(reader.open(tempFile.fileName()));
QCOMPARE(reader.title(), QStringLiteral("Alice's Adventures in Wonderland"));
QCOMPARE(reader.author(), QStringLiteral("Lewis Carroll"));
}
void TxtReaderTest::testMetadataOnlyMode()
{
QTemporaryFile tempFile;
QVERIFY(tempFile.open());
const QByteArray content =
"Title: Metamorphosis\n"
"Author: Franz Kafka\n"
"\n"
"One morning, when Gregor Samsa woke from troubled dreams...\n";
tempFile.write(content);
tempFile.close();
TxtReader reader;
QVERIFY(reader.loadMetadata(tempFile.fileName()));
QCOMPARE(reader.title(), QStringLiteral("Metamorphosis"));
QCOMPARE(reader.author(), QStringLiteral("Franz Kafka"));
QVERIFY(reader.chunks().isEmpty());
QVERIFY(!reader.contentHash().isEmpty());
}
void TxtReaderTest::testUtf8WithBom()
{
QTemporaryFile tempFile;
QVERIFY(tempFile.open());
QByteArray content;
content.append("\xEF\xBB\xBF"); // UTF-8 BOM
content.append("Привет, мир!\n\nВторой абзац книги.\n");
tempFile.write(content);
tempFile.close();
TxtReader reader;
QVERIFY(reader.open(tempFile.fileName()));
const auto chunks = reader.chunks();
QVERIFY(!chunks.isEmpty());
QString firstParagraphText;
for (const auto &chunk : chunks) {
for (const auto &block : chunk.blocks) {
if (block.kind == BlockItem::Kind::Paragraph && !block.inlines.isEmpty()) {
firstParagraphText = block.inlines.first().text;
break;
}
}
if (!firstParagraphText.isEmpty()) break;
}
QCOMPARE(firstParagraphText, QString::fromUtf8("Привет, мир!"));
}
void TxtReaderTest::testUtf16Le()
{
QTemporaryFile tempFile;
QVERIFY(tempFile.open());
const QString text = QStringLiteral("Hello in UTF-16LE.\n\nSecond paragraph.\n");
QByteArray content;
content.append("\xFF\xFE", 2); // UTF-16LE BOM
content.append(reinterpret_cast<const char*>(text.utf16()), text.size() * sizeof(char16_t));
tempFile.write(content);
tempFile.close();
TxtReader reader;
QVERIFY(reader.open(tempFile.fileName()));
const auto chunks = reader.chunks();
QVERIFY(!chunks.isEmpty());
QString firstParagraphText;
for (const auto &chunk : chunks) {
for (const auto &block : chunk.blocks) {
if (block.kind == BlockItem::Kind::Paragraph && !block.inlines.isEmpty()) {
firstParagraphText = block.inlines.first().text;
break;
}
}
if (!firstParagraphText.isEmpty()) break;
}
QCOMPARE(firstParagraphText, QStringLiteral("Hello in UTF-16LE."));
}
void TxtReaderTest::testWindows1251WithoutBom()
{
QTemporaryFile tempFile;
QVERIFY(tempFile.open());
// "Версия для программистов\n\nЛес.елочка.create();\n" in windows-1251
const QByteArray win1251 =
"\xC2\xE5\xF0\xF1\xE8\xFF\x20\xE4\xEB\xFF\x20\xEF\xF0\xEE\xE3\xF0\xE0\xEC\xEC\xE8\xF1\xF2\xEE\xE2\n\n"
"\xCB\xE5\xF1\x2E\xE5\xEB\xEE\xF7\xEA\xE0\x2E\x63\x72\x65\x61\x74\x65\x28\x29\x3B\n";
tempFile.write(win1251);
tempFile.close();
TxtReader reader;
QVERIFY(reader.open(tempFile.fileName()));
const auto chunks = reader.chunks();
QVERIFY(!chunks.isEmpty());
QString firstParagraphText;
for (const auto &chunk : chunks) {
for (const auto &block : chunk.blocks) {
if (block.kind == BlockItem::Kind::Paragraph && !block.inlines.isEmpty()) {
firstParagraphText = block.inlines.first().text;
break;
}
}
if (!firstParagraphText.isEmpty()) break;
}
QCOMPARE(firstParagraphText, QString::fromUtf8("Версия для программистов"));
QVERIFY(!firstParagraphText.contains(QChar::ReplacementCharacter));
}
void TxtReaderTest::testElochkaFileIfExists()
{
const QString path = QStringLiteral("/Users/agel/OpenCloud/Personal/Books/elochka.txt");
if (!QFile::exists(path)) return;
TxtReader reader;
QVERIFY(reader.open(path));
const auto chunks = reader.chunks();
QVERIFY(!chunks.isEmpty());
QString firstParagraphText;
for (const auto &chunk : chunks) {
for (const auto &block : chunk.blocks) {
if (block.kind == BlockItem::Kind::Paragraph && !block.inlines.isEmpty()) {
firstParagraphText = block.inlines.first().text;
break;
}
}
if (!firstParagraphText.isEmpty()) break;
}
QCOMPARE(firstParagraphText, QString::fromUtf8("Версия для программистов"));
QVERIFY(!firstParagraphText.contains(QChar::ReplacementCharacter));
}
void TxtReaderTest::testBulbaFile()
{
const QString path = QStringLiteral("/Users/agel/OpenCloud/Personal/Books/bulba nataliyaохотники за диковинками.txt");
if (!QFile::exists(path)) return;
TxtReader reader;
QVERIFY(!reader.open(path));
QVERIFY(reader.lastError().contains(QStringLiteral("corrupted, binary, or encrypted data")));
}
void TxtReaderTest::testLuideFile()
{
const QString path = QStringLiteral("/Users/agel/OpenCloud/Personal/Books/Луиде записки практикующего адвоката.txt");
if (!QFile::exists(path)) return;
TxtReader reader;
QVERIFY(!reader.open(path));
QVERIFY(reader.lastError().contains(QStringLiteral("corrupted, binary, or encrypted data")));
}
void TxtReaderTest::testBondarFile()
{
const QString path = QStringLiteral("/Users/agel/OpenCloud/Personal/Books/бондарь ирина последний дракон.txt");
if (!QFile::exists(path)) return;
TxtReader reader;
QVERIFY(!reader.open(path));
QVERIFY(reader.lastError().contains(QStringLiteral("corrupted, binary, or encrypted data")));
}
void TxtReaderTest::testArinFile()
{
const QString path = QStringLiteral("/Users/agel/OpenCloud/Personal/Books/арин когда сила в твоей крови.txt");
if (!QFile::exists(path)) return;
TxtReader reader;
QVERIFY(reader.open(path));
QVERIFY(!reader.chunks().isEmpty());
QVERIFY(!reader.title().isEmpty());
}
void TxtReaderTest::testTonkiyFile()
{
const QString path = QStringLiteral("/Users/agel/OpenCloud/Personal/Books/Тонкий японистский юмор.txt");
if (!QFile::exists(path)) return;
TxtReader reader;
QVERIFY(reader.open(path));
QVERIFY(!reader.chunks().isEmpty());
QVERIFY(!reader.title().isEmpty());
}
QTEST_MAIN(TxtReaderTest)
#include "TxtReaderTest.moc"