/* * Copyright (C) 2008-2014 The Communi Project * * This test is free, and not covered by the BSD license. There is no * restriction applied to their modification, redistribution, using and so on. * You can study them, modify them, use them in your own program - either * completely or partially. */ #include "irc.h" #include "irctextformat.h" #include "ircpalette.h" #include class tst_IrcTextFormat : public QObject { Q_OBJECT private slots: void testDefaults(); void testPlainText_data(); void testPlainText(); void testHtml_data(); void testHtml(); void testUrls_data(); void testUrls(); }; void tst_IrcTextFormat::testDefaults() { IrcTextFormat format; QVERIFY(format.palette()); QVERIFY(!format.urlPattern().isEmpty()); QCOMPARE(format.spanFormat(), IrcTextFormat::SpanStyle); } void tst_IrcTextFormat::testPlainText_data() { QTest::addColumn("input"); QTest::addColumn("output"); QTest::newRow("bold") << "\02bold\x0f" << "bold"; QTest::newRow("line-through") << "\x13line-through\x0f" << "line-through"; QTest::newRow("underline") << "\x15underline\x0f" << "underline"; QTest::newRow("inverse") << "\x16inverse\x0f" << "inverse"; QTest::newRow("italic") << "\x1ditalic\x0f" << "italic"; QTest::newRow("underline") << "\x1funderline\x0f" << "underline"; IrcTextFormat format; QVERIFY(format.palette()); IrcPalette* palette = format.palette(); for (int i = Irc::White; i <= Irc::LightGray; ++i) { QString color = palette->colorName(i); QTest::newRow(color.toUtf8()) << QString("\x03%1%2\x0f").arg(i).arg(color) << color; } QTest::newRow("dummy \\x03") << "foo\x03 \02bold\x0f bar\x03" << "foo bold bar"; QTest::newRow("extra \\x0f") << "foo\x0f \02bold\x0f bar\x0f" << "foo bold bar"; QTest::newRow("background") << QString("foo \x03%1,%1red\x0f on \x03%1,%1red\x03 bar").arg(Irc::Red) << "foo red on red bar"; } void tst_IrcTextFormat::testPlainText() { QFETCH(QString, input); QFETCH(QString, output); IrcTextFormat format; QCOMPARE(format.toPlainText(input), output); format.parse(input); QCOMPARE(format.plainText(), output); } void tst_IrcTextFormat::testHtml_data() { qRegisterMetaType(); QTest::addColumn("span"); QTest::addColumn("input"); QTest::addColumn("output"); QTest::newRow("style=bold") << IrcTextFormat::SpanStyle << "foo \02bold\x0f and \02bold\02 bar" << "foo bold and bold bar"; QTest::newRow("class=bold") << IrcTextFormat::SpanClass << "foo \02bold\x0f and \02bold\02 bar" << "foo bold and bold bar"; QTest::newRow("style=line-through") << IrcTextFormat::SpanStyle << "foo \x13line\x0f and \x13through\x13 bar" << "foo line and through bar"; QTest::newRow("class=line-through") << IrcTextFormat::SpanClass << "foo \x13line\x0f and \x13through\x13 bar" << "foo line and through bar"; QTest::newRow("style=underline1") << IrcTextFormat::SpanStyle << "foo \x15under\x0f and \x15line\x15 bar" << "foo under and line bar"; QTest::newRow("style=inverse") << IrcTextFormat::SpanStyle << "foo \x16inverse\x0f and \x16inverse\x16 bar" << "foo inverse and inverse bar"; QTest::newRow("class=inverse") << IrcTextFormat::SpanClass << "foo \x16inverse\x0f and \x16inverse\x16 bar" << "foo inverse and inverse bar"; QTest::newRow("style=italic") << IrcTextFormat::SpanStyle << "foo \x1ditalic\x0f and \x1ditalic\x1d bar" << "foo italic and italic bar"; QTest::newRow("class=italic") << IrcTextFormat::SpanClass << "foo \x1ditalic\x0f and \x1ditalic\x1d bar" << "foo italic and italic bar"; QTest::newRow("style=underline2") << IrcTextFormat::SpanStyle << "foo \x1funder\x0f and \x1fline\x1f bar" << "foo under and line bar"; IrcTextFormat format; QVERIFY(format.palette()); IrcPalette* palette = format.palette(); for (int i = Irc::White; i <= Irc::LightGray; ++i) { QString color = palette->colorName(i); QTest::newRow(QString("style=%1").arg(color).toUtf8()) << IrcTextFormat::SpanStyle << QString("foo \x03%1%2\x0f and \x03%1%2\x03 bar").arg(i).arg(color) << QString("foo %1 and %1 bar").arg(color); QTest::newRow(QString("class=%1").arg(color).toUtf8()) << IrcTextFormat::SpanClass << QString("foo \x03%1%2\x0f and \x03%1%2\x03 bar").arg(i).arg(color) << QString("foo %1 and %1 bar").arg(color); } QTest::newRow("extra \\x0f") << IrcTextFormat::SpanStyle << "foo\x0f \02bold\x0f bar\x0f" << "foo bold bar"; QTest::newRow("style=background") << IrcTextFormat::SpanStyle << QString("foo \x03%1,%1red\x0f on \x03%1,%1red\x03 bar").arg(Irc::Red) << "foo red on red bar"; QTest::newRow("class=background") << IrcTextFormat::SpanClass << QString("foo \x03%1,%1red\x0f on \x03%1,%1red\x03 bar").arg(Irc::Red) << "foo red on red bar"; } void tst_IrcTextFormat::testHtml() { QFETCH(IrcTextFormat::SpanFormat, span); QFETCH(QString, input); QFETCH(QString, output); IrcTextFormat format; format.setSpanFormat(span); QCOMPARE(format.toHtml(input), output); format.parse(input); QCOMPARE(format.html(), output); } Q_DECLARE_METATYPE(QList) void tst_IrcTextFormat::testUrls_data() { qRegisterMetaType >(); QTest::addColumn("pattern"); QTest::addColumn("input"); QTest::addColumn("output"); QTest::addColumn >("urls"); QString defaultPattern = IrcTextFormat().urlPattern(); QTest::newRow("www.fi") << defaultPattern << "www.fi" << "www.fi" << (QList() << QUrl("http://www.fi")); QTest::newRow("ftp.funet.fi") << defaultPattern << "ftp.funet.fi" << "ftp.funet.fi" << (QList() << QUrl("ftp://ftp.funet.fi")); QTest::newRow("jpnurmi@gmail.com") << defaultPattern << "jpnurmi@gmail.com" << "jpnurmi@gmail.com" << (QList() << QUrl("mailto:jpnurmi@gmail.com")); QTest::newRow("quote") << defaultPattern << "http://en.wikipedia.org/wiki/Shamir's_Secret_Sharing" << "http://en.wikipedia.org/wiki/Shamir's_Secret_Sharing" << (QList() << QUrl("http://en.wikipedia.org/wiki/Shamir's_Secret_Sharing")); QTest::newRow("percent") << defaultPattern << "http://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing" << "http://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing" << (QList() << QUrl("http://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing")); QTest::newRow("parentheses") << defaultPattern << "http://en.wikipedia.org/wiki/Qt_(software)" << "http://en.wikipedia.org/wiki/Qt_(software)" << (QList() << QUrl("http://en.wikipedia.org/wiki/Qt_(software)")); QTest::newRow("hash & comma") << defaultPattern << "https://codereview.qt-project.org/#change,1" << "https://codereview.qt-project.org/#change,1" << (QList() << QUrl("https://codereview.qt-project.org/#change,1")); QTest::newRow("equal & question & ampersand") << defaultPattern << "https://www.google.no/imghp?hl=en&tab=wi" << "https://www.google.no/imghp?hl=en&tab=wi" << (QList() << QUrl("https://www.google.no/imghp?hl=en&tab=wi")); QTest::newRow("github commits") << defaultPattern << "https://github.com/communi/libcommuni/compare/ebf3c8ea47dc...19d66ddcb122" << "https://github.com/communi/libcommuni/compare/ebf3c8ea47dc...19d66ddcb122" << (QList() << QUrl("https://github.com/communi/libcommuni/compare/ebf3c8ea47dc...19d66ddcb122")); QTest::newRow("multiple") << defaultPattern << "www.fi ftp.funet.fi jpnurmi@gmail.com" << "www.fi ftp.funet.fi jpnurmi@gmail.com" << (QList() << QUrl("http://www.fi") << QUrl("ftp://ftp.funet.fi") << QUrl("mailto:jpnurmi@gmail.com")); QTest::newRow("empty pattern") << QString() << "www.fi ftp.funet.fi jpnurmi@gmail.com" << "www.fi ftp.funet.fi jpnurmi@gmail.com" << QList(); QTest::newRow("info") << defaultPattern << QString("[freenode-info] if you're at a conference and other people are having trouble connecting, please mention it to staff: http://freenode.net/faq.shtml#gettinghelp") << QString("[freenode-info] if you're at a conference and other people are having trouble connecting, please mention it to staff: http://freenode.net/faq.shtml#gettinghelp") << (QList() << QUrl("http://freenode.net/faq.shtml#gettinghelp")); QTest::newRow("topic") << defaultPattern << QString("Communi 1.2.2 - IRC framework || Home: http://communi.github.com || Docs: http://communi.github.com/doc || MeeGo: http://store.ovi.com/content/219150") << QString("Communi 1.2.2 - IRC framework || Home: http://communi.github.com || Docs: http://communi.github.com/doc || MeeGo: http://store.ovi.com/content/219150") << (QList() << QUrl("http://communi.github.com") << QUrl("http://communi.github.com/doc") << QUrl("http://store.ovi.com/content/219150")); QTest::newRow("commit") << defaultPattern << QString("[communi-desktop] jpnurmi pushed 2 new commits to master: https://github.com/communi/communi-desktop/compare/257ca915a490...8832bfe8d0b8") << QString("[communi-desktop] jpnurmi pushed 2 new commits to master: https://github.com/communi/communi-desktop/compare/257ca915a490...8832bfe8d0b8") << (QList() << QUrl("https://github.com/communi/communi-desktop/compare/257ca915a490...8832bfe8d0b8")); QTest::newRow("with protocol") << defaultPattern << QString("aa http://www.fi bb ftp://ftp.funet.fi cc") << QString("aa http://www.fi bb ftp://ftp.funet.fi cc") << (QList() << QUrl("http://www.fi") << QUrl("ftp://ftp.funet.fi")); QTest::newRow("without protocol") << defaultPattern << QString("aa www.fi bb ftp.funet.fi cc jpnurmi@gmail.com dd") << QString("aa www.fi bb ftp.funet.fi cc jpnurmi@gmail.com dd") << (QList() << QUrl("http://www.fi") << QUrl("ftp://ftp.funet.fi") << QUrl("mailto:jpnurmi@gmail.com")); } void tst_IrcTextFormat::testUrls() { QFETCH(QString, pattern); QFETCH(QString, input); QFETCH(QString, output); QFETCH(QList, urls); IrcTextFormat format; format.setUrlPattern(pattern); QCOMPARE(format.urlPattern(), pattern); QCOMPARE(format.toHtml(input), output); format.parse(input); QCOMPARE(format.html(), output); QCOMPARE(format.urls(), urls); } QTEST_MAIN(tst_IrcTextFormat) #include "tst_irctextformat.moc"