#include #include #include "xlsxworksheet.h" #include "xlsxcell.h" #include "xlsxcellrange.h" #include "xlsxdatavalidation.h" #include "private/xlsxworksheet_p.h" #include "private/xlsxxmlreader_p.h" #include "private/xlsxsharedstrings_p.h" class WorksheetTest : public QObject { Q_OBJECT public: WorksheetTest(); private Q_SLOTS: void testEmptySheet(); void testDimension(); void testSheetView(); void testWriteCells(); void testWriteHyperlinks(); void testWriteDataValidations(); void testMerge(); void testUnMerge(); void testReadSheetData(); void testReadColsInfo(); void testReadRowsInfo(); void testReadMergeCells(); void testReadDataValidations(); }; WorksheetTest::WorksheetTest() { } void WorksheetTest::testEmptySheet() { QXlsx::Worksheet sheet("", 1, 0); sheet.write("B1", 123); QByteArray xmldata = sheet.saveToXmlData(); QVERIFY2(!xmldata.contains("123"), "numeric"); QVERIFY2(xmldata.contains("0"), "string"); QVERIFY2(xmldata.contains("Hello inline"), "inline string"); QVERIFY2(xmldata.contains("1"), "boolean"); QVERIFY2(xmldata.contains("44+330"), "formula"); QVERIFY2(xmldata.contains("44+3377"), "formula"); QCOMPARE(sheet.d_ptr->sharedStrings()->getSharedString(0), QStringLiteral("Hello")); } void WorksheetTest::testWriteHyperlinks() { QXlsx::Worksheet sheet("", 1, 0); sheet.write("A1", QUrl::fromUserInput("http://qt-project.org")); sheet.write("B1", QUrl::fromUserInput("http://qt-project.org/abc")); sheet.write("C1", QUrl::fromUserInput("http://qt-project.org/abc.html#test")); sheet.write("D1", QUrl::fromUserInput("mailto:xyz@debao.me")); sheet.write("E1", QUrl::fromUserInput("mailto:xyz@debao.me?subject=Test")); QByteArray xmldata = sheet.saveToXmlData(); QVERIFY2(xmldata.contains(""), "simple"); QVERIFY2(xmldata.contains(""), "url with path"); QVERIFY2(xmldata.contains(""), "url with location"); QVERIFY2(xmldata.contains(""), "mail"); QVERIFY2(xmldata.contains(""), "mail with subject"); QCOMPARE(sheet.d_ptr->sharedStrings()->getSharedString(0), QStringLiteral("http://qt-project.org")); QCOMPARE(sheet.d_ptr->sharedStrings()->getSharedString(1), QStringLiteral("http://qt-project.org/abc")); QCOMPARE(sheet.d_ptr->sharedStrings()->getSharedString(2), QStringLiteral("http://qt-project.org/abc.html#test")); QCOMPARE(sheet.d_ptr->sharedStrings()->getSharedString(3), QStringLiteral("xyz@debao.me")); QCOMPARE(sheet.d_ptr->sharedStrings()->getSharedString(4), QStringLiteral("xyz@debao.me?subject=Test")); } void WorksheetTest::testWriteDataValidations() { QXlsx::Worksheet sheet("", 1, 0); QXlsx::DataValidation validation(QXlsx::DataValidation::Whole); validation.setFormula1("10"); validation.setFormula2("100"); validation.addCell("A1"); validation.addCell("C2:C4"); sheet.addDataValidation(validation); QByteArray xmldata = sheet.saveToXmlData(); QVERIFY(xmldata.contains("10100")); } void WorksheetTest::testMerge() { QXlsx::Worksheet sheet("", 1, 0); sheet.write("B1", 123); sheet.mergeCells("B1:B5"); QByteArray xmldata = sheet.saveToXmlData(); QVERIFY2(xmldata.contains(""), ""); } void WorksheetTest::testUnMerge() { QXlsx::Worksheet sheet("", 1, 0); sheet.write("B1", 123); sheet.mergeCells("B1:B5"); sheet.unmergeCells("B1:B5"); QByteArray xmldata = sheet.saveToXmlData(); QVERIFY2(!xmldata.contains("" "0" "44+3377" "44+3377" "" "" "12345" "inline test string" "1/0#DIV/0!" "" ""; QXlsx::XmlStreamReader reader(xmlData); reader.readNextStartElement();//current node is sheetData QXlsx::Worksheet sheet("", 1, 0); sheet.d_ptr->sharedStrings()->addSharedString("Hello"); sheet.d_ptr->readSheetData(reader); QCOMPARE(sheet.d_ptr->cellTable.size(), 2); //A1 QCOMPARE(sheet.cellAt("A1")->dataType(), QXlsx::Cell::String); QCOMPARE(sheet.cellAt("A1")->value().toString(), QStringLiteral("Hello")); //B1 QCOMPARE(sheet.cellAt("B1")->dataType(), QXlsx::Cell::Formula); QCOMPARE(sheet.cellAt("B1")->value().toInt(), 77); QCOMPARE(sheet.cellAt("B1")->formula(), QStringLiteral("44+33")); //C1 QCOMPARE(sheet.cellAt("C1")->dataType(), QXlsx::Cell::Formula); QCOMPARE(sheet.cellAt("C1")->value().toInt(), 77); QCOMPARE(sheet.cellAt("C1")->formula(), QStringLiteral("44+33")); //B3 QCOMPARE(sheet.cellAt("B3")->dataType(), QXlsx::Cell::Numeric); QCOMPARE(sheet.cellAt("B3")->value().toInt(), 12345); //C3 QCOMPARE(sheet.cellAt("C3")->dataType(), QXlsx::Cell::InlineString); QCOMPARE(sheet.cellAt("C3")->value().toString(), QStringLiteral("inline test string")); //E3 QCOMPARE(sheet.cellAt("E3")->dataType(), QXlsx::Cell::Error); QCOMPARE(sheet.cellAt("E3")->value().toString(), QStringLiteral("#DIV/0!")); } void WorksheetTest::testReadColsInfo() { const QByteArray xmlData = "" "" ""; QXlsx::XmlStreamReader reader(xmlData); reader.readNextStartElement();//current node is cols QXlsx::Worksheet sheet("", 1, 0); sheet.d_ptr->readColumnsInfo(reader); QCOMPARE(sheet.d_ptr->colsInfo.size(), 1); QCOMPARE(sheet.d_ptr->colsInfo[0]->width, 5.0); } void WorksheetTest::testReadRowsInfo() { const QByteArray xmlData = "" "" "0" "" "" "12345" "" ""; QXlsx::XmlStreamReader reader(xmlData); reader.readNextStartElement();//current node is sheetData QXlsx::Worksheet sheet("", 1, 0); sheet.d_ptr->readSheetData(reader); QCOMPARE(sheet.d_ptr->rowsInfo.size(), 1); QCOMPARE(sheet.d_ptr->rowsInfo[3]->height, 40.0); } void WorksheetTest::testReadMergeCells() { const QByteArray xmlData = ""; QXlsx::XmlStreamReader reader(xmlData); reader.readNextStartElement();//current node is mergeCells QXlsx::Worksheet sheet("", 1, 0); sheet.d_ptr->readMergeCells(reader); QCOMPARE(sheet.d_ptr->merges.size(), 2); QCOMPARE(sheet.d_ptr->merges[0].toString(), QStringLiteral("B1:B5")); } void WorksheetTest::testReadDataValidations() { const QByteArray xmlData = "" "10100" "10100" ""; QXlsx::XmlStreamReader reader(xmlData); reader.readNextStartElement();//current node is dataValidations QXlsx::Worksheet sheet("", 1, 0); sheet.d_ptr->readDataValidations(reader); QCOMPARE(sheet.d_ptr->dataValidationsList.size(), 2); QCOMPARE(sheet.d_ptr->dataValidationsList[0].validationType(), QXlsx::DataValidation::Whole); } QTEST_APPLESS_MAIN(WorksheetTest) #include "tst_worksheet.moc"