// Copyright (C) 2020 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 #include "qtxmltosphinxtest.h" #include "qtxmltosphinx.h" #include #include #include #include using namespace Qt::StringLiterals; Q_LOGGING_CATEGORY(lcQtXmlToSphinxTest, "qt.sphinxtabletest"); // QtXmlToSphinxDocGeneratorInterface QString QtXmlToSphinxTest::expandFunction(const QString &) const { return {}; } QString QtXmlToSphinxTest::expandClass(const QString &, const QString &) const { return {}; } QString QtXmlToSphinxTest::resolveContextForMethod(const QString &, const QString &) const { return {}; } const QLoggingCategory &QtXmlToSphinxTest::loggingCategory() const { return lcQtXmlToSphinxTest(); } QtXmlToSphinxLink QtXmlToSphinxTest::resolveLink(const QtXmlToSphinxLink &link) const { return link; } QString QtXmlToSphinxTest::transformXml(const QString &xml) const { return QtXmlToSphinx(this, m_parameters, xml).result(); } void QtXmlToSphinxTest::testTable_data() { QTest::addColumn("xml"); QTest::addColumn("expected"); QTest::newRow("emptyString") << QString() << QString(); // testSimpleTable const char *xml = R"(
Header 1 Header 2
1 1 1 2 2 1 2 2
)"; const char *expected = R"( +--------+--------+ |Header 1|Header 2| +========+========+ |1 1 |1 2 | +--------+--------+ |2 1 |2 2 | +--------+--------+ )"; QTest::newRow("testSimpleTable") << QString::fromLatin1(xml) << QString::fromLatin1(expected); // testRowSpan xml = R"(
Header 1 Header 2
I'm a big text! 2 1 2 2
)"; expected = R"( +---------------+--------+ |Header 1 |Header 2| +===============+========+ |I'm a big text! | +---------------+--------+ |2 1 |2 2 | +---------------+--------+ )"; QTest::newRow("testColSpan") << QString::fromLatin1(xml) << QString::fromLatin1(expected); // testRowSpan xml = R"(
Header 1 Header 2
1.1 1.2 2 2
)"; expected = R"( +--------+--------+ |Header 1|Header 2| +========+========+ |1.1 |1.2 | + +--------+ | |2 2 | +--------+--------+ )"; QTest::newRow("testRowSpan") << QString::fromLatin1(xml) << QString::fromLatin1(expected); // testComplexTable xml = R"(
Header 1 Header 2 Header 3
1.1 1.2 2 2 2 3
)"; expected = R"( +--------+--------+--------+ |Header 1|Header 2|Header 3| +========+========+========+ |1.1 |1.2 | + +--------+--------+ | |2 2 |2 3 | +--------+--------+--------+ )"; QTest::newRow("testComplexTable") << QString::fromLatin1(xml) << QString::fromLatin1(expected); // testRowSpan2 xml = R"(
h1 h2 h3 h4
A B C D E F E F E F E F E F
)"; expected = R"( +--+--+--+--+ |h1|h2|h3|h4| +==+==+==+==+ |A |B |C |D | + + +--+--+ | | |E |F | + + +--+--+ | | |E |F | + + +--+--+ | | |E |F | + + +--+--+ | | |E |F | + + +--+--+ | | |E |F | +--+--+--+--+ )"; QTest::newRow("testRowSpan2") << QString::fromLatin1(xml) << QString::fromLatin1(expected); // testNestedList xml = R"( I11 I21 I12 I22
)"; expected = R"( +---------+---------+ | * I11| * I12| | * I21| * I22| +---------+---------+ )"; QTest::newRow("testNestedList") << QString::fromLatin1(xml) << QString::fromLatin1(expected); // testBrokenTable xml = R"(
Header 1 Header 2
1.1 1.2 2 2 2 3 2 4 2 5 3 1 3 2 3 3
)"; expected = R"( +--------+------------+ |Header 1|Header 2 | +========+============+ |1.1 |1.2 | +--------+------------+ |2 2 2 3 2 4 2 5| +--------+------------+ |3 1 |3 2 3 3 | +--------+------------+ )"; QTest::newRow("testBrokenTable") << QString::fromLatin1(xml) << QString::fromLatin1(expected); } void QtXmlToSphinxTest::testTable() { QFETCH(QString, xml); QFETCH(QString, expected); const QString actual = transformXml(xml); QEXPECT_FAIL("testBrokenTable", "testBrokenTable fails", Continue); QCOMPARE(actual, expected); } using TablePtr = std::shared_ptr; Q_DECLARE_METATYPE(TablePtr); void QtXmlToSphinxTest::testTableFormatting_data() { using TableCell = QtXmlToSphinx::TableCell; QTest::addColumn("table"); QTest::addColumn("expected"); TablePtr table(new QtXmlToSphinx::Table); table->appendRow({TableCell("item11"), TableCell("item12")}); table->appendRow({TableCell(""), TableCell("item22")}); table->normalize(); const char *expected = R"(+------+------+ |item11|item12| +------+------+ | |item22| +------+------+ )"; QTest::newRow("normal") << table << QString::fromLatin1(expected); table.reset(new QtXmlToSphinx::Table); table->appendRow({TableCell("item11"), TableCell("item12\nline2")}); table->appendRow({TableCell(""), TableCell("item22\nline2\nline3")}); table->normalize(); expected = R"(+------+------+ |item11|item12| | |line2 | +------+------+ | |item22| | |line2 | | |line3 | +------+------+ )"; QTest::newRow("multi-line") << table << QString::fromLatin1(expected); } void QtXmlToSphinxTest::testTableFormatting() { QFETCH(TablePtr, table); QFETCH(QString, expected); StringStream str; table->format(str); const QString actual = str.toString(); QCOMPARE(actual, expected); } void QtXmlToSphinxTest::testTableFormattingIoDevice_data() { testTableFormatting_data(); } void QtXmlToSphinxTest::testTableFormattingIoDevice() { QFETCH(TablePtr, table); QFETCH(QString, expected); QByteArray byteArray; { TextStream str(&byteArray); table->format(str); } const QString actual = QString::fromUtf8(byteArray); QCOMPARE(actual, expected); } void QtXmlToSphinxTest::testSnippetExtraction_data() { QTest::addColumn("file"); QTest::addColumn("id"); QTest::addColumn("expected"); const char *fileCpp = R"(bla // ![snip1] snip1_line1 // ![snip1] // ![snip2] snip2_line1 snip2_line2 // ![snip2] // ![snip3] )"; constexpr auto id = "snip2"_L1; const QString expected = uR"(snip2_line1 snip2_line2 )"_s; const char *filePython = R"(bla # ![snip1] snip1_line1 # ![snip1] # ![snip2] snip2_line1 snip2_line2 # ![snip2] # ![snip3] )"; QTest::newRow("c++") << QByteArray(fileCpp) << id << expected; QTest::newRow("Python") << QByteArray(filePython) << id << expected; } void QtXmlToSphinxTest::testSnippetExtraction() { QFETCH(QByteArray, file); QFETCH(QLatin1StringView, id); QFETCH(QString, expected); QBuffer buffer(&file); QVERIFY(buffer.open(QIODevice::ReadOnly)); QString errorMessage; QString actual = QtXmlToSphinx::readSnippet(buffer, id, &errorMessage); QVERIFY2(errorMessage.isEmpty(), qPrintable(errorMessage)); QCOMPARE(actual, expected); } QTEST_APPLESS_MAIN( QtXmlToSphinxTest)