some refactor
This commit is contained in:
20 files changed
+1204
-948
No files matched your search
@@ -0,0 +1,112 @@
|
||||
#include <QtTest>
|
||||
#include "backend/formats/TextEncoding.h"
|
||||
|
||||
class TextEncodingTest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void testEmpty();
|
||||
void testUtf8NoBom();
|
||||
void testUtf8WithBom();
|
||||
void testUtf16LeWithBom();
|
||||
void testUtf16BeWithBom();
|
||||
void testDetectXmlEncoding();
|
||||
void testDetectHtml5Charset();
|
||||
void testDetectHtml4Charset();
|
||||
void testDecodeWindows1251();
|
||||
void testDecodeWithHint();
|
||||
void testDecodeWithExplicitEncoding();
|
||||
};
|
||||
|
||||
void TextEncodingTest::testEmpty()
|
||||
{
|
||||
QCOMPARE(TextEncoding::detectEncoding(QByteArray()), QString());
|
||||
QCOMPARE(TextEncoding::decode(QByteArray()), QString());
|
||||
QCOMPARE(TextEncoding::decodeWithEncoding(QByteArray(), QStringLiteral("utf-8")), QString());
|
||||
}
|
||||
|
||||
void TextEncodingTest::testUtf8NoBom()
|
||||
{
|
||||
const QByteArray bytes = "Hello, world! \xF0\x9F\x93\x96"; // book emoji
|
||||
const QString result = TextEncoding::decode(bytes);
|
||||
QCOMPARE(result, QString::fromUtf8(bytes));
|
||||
}
|
||||
|
||||
void TextEncodingTest::testUtf8WithBom()
|
||||
{
|
||||
const QByteArray bytes = "\xEF\xBB\xBFHello, BOM!";
|
||||
QCOMPARE(TextEncoding::detectEncoding(bytes), QStringLiteral("UTF-8"));
|
||||
const QString result = TextEncoding::decode(bytes);
|
||||
QCOMPARE(result, QStringLiteral("Hello, BOM!"));
|
||||
}
|
||||
|
||||
void TextEncodingTest::testUtf16LeWithBom()
|
||||
{
|
||||
// UTF-16 LE BOM + "AB" (0x0041, 0x0042)
|
||||
const QByteArray bytes = QByteArray("\xFF\xFE\x41\x00\x42\x00", 6);
|
||||
QCOMPARE(TextEncoding::detectEncoding(bytes), QStringLiteral("UTF-16LE"));
|
||||
const QString result = TextEncoding::decode(bytes);
|
||||
QCOMPARE(result, QStringLiteral("AB"));
|
||||
}
|
||||
|
||||
void TextEncodingTest::testUtf16BeWithBom()
|
||||
{
|
||||
// UTF-16 BE BOM + "AB" (0x0041, 0x0042)
|
||||
const QByteArray bytes = QByteArray("\xFE\xFF\x00\x41\x00\x42", 6);
|
||||
QCOMPARE(TextEncoding::detectEncoding(bytes), QStringLiteral("UTF-16BE"));
|
||||
const QString result = TextEncoding::decode(bytes);
|
||||
QCOMPARE(result, QStringLiteral("AB"));
|
||||
}
|
||||
|
||||
void TextEncodingTest::testDetectXmlEncoding()
|
||||
{
|
||||
const QByteArray xml1 = "<?xml version=\"1.0\" encoding=\"windows-1251\"?>\n<FictionBook>";
|
||||
QCOMPARE(TextEncoding::detectEncoding(xml1), QStringLiteral("windows-1251"));
|
||||
|
||||
const QByteArray xml2 = "<?xml version='1.0' encoding='KOI8-R'?>\n<FictionBook>";
|
||||
QCOMPARE(TextEncoding::detectEncoding(xml2), QStringLiteral("KOI8-R"));
|
||||
}
|
||||
|
||||
void TextEncodingTest::testDetectHtml5Charset()
|
||||
{
|
||||
const QByteArray html = "<html><head><meta charset=\"shift_jis\"></head></html>";
|
||||
QCOMPARE(TextEncoding::detectEncoding(html), QStringLiteral("shift_jis"));
|
||||
}
|
||||
|
||||
void TextEncodingTest::testDetectHtml4Charset()
|
||||
{
|
||||
const QByteArray html = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gbk\"></head></html>";
|
||||
QCOMPARE(TextEncoding::detectEncoding(html), QStringLiteral("gbk"));
|
||||
}
|
||||
|
||||
void TextEncodingTest::testDecodeWindows1251()
|
||||
{
|
||||
// "Привет" in windows-1251: CF F0 E8 E2 E5 F2
|
||||
const QByteArray win1251Text("\xCF\xF0\xE8\xE2\xE5\xF2");
|
||||
const QByteArray xml = "<?xml version=\"1.0\" encoding=\"windows-1251\"?>\n<title>" + win1251Text + "</title>";
|
||||
|
||||
const QString result = TextEncoding::decode(xml);
|
||||
QVERIFY(result.contains(QString::fromUtf8("Привет")));
|
||||
}
|
||||
|
||||
void TextEncodingTest::testDecodeWithHint()
|
||||
{
|
||||
// "Привет" in windows-1251 without header
|
||||
const QByteArray win1251Text("\xCF\xF0\xE8\xE2\xE5\xF2");
|
||||
const QString result = TextEncoding::decode(win1251Text, QStringLiteral("windows-1251"));
|
||||
QCOMPARE(result, QString::fromUtf8("Привет"));
|
||||
}
|
||||
|
||||
void TextEncodingTest::testDecodeWithExplicitEncoding()
|
||||
{
|
||||
const QByteArray win1251Text("\xCF\xF0\xE8\xE2\xE5\xF2");
|
||||
const QString result = TextEncoding::decodeWithEncoding(win1251Text, QStringLiteral("windows-1251"));
|
||||
QCOMPARE(result, QString::fromUtf8("Привет"));
|
||||
|
||||
// Invalid encoding returns empty
|
||||
const QString invalid = TextEncoding::decodeWithEncoding(win1251Text, QStringLiteral("invalid_encoding_xyz"));
|
||||
QVERIFY(invalid.isEmpty());
|
||||
}
|
||||
|
||||
QTEST_MAIN(TextEncodingTest)
|
||||
#include "TextEncodingTest.moc"
|
||||
Reference in new issue
Block a user