Add unit test for QXlsx::Document
This commit is contained in:
@@ -19,5 +19,9 @@ int main()
|
|||||||
xlsx.save();
|
xlsx.save();
|
||||||
//![2]
|
//![2]
|
||||||
|
|
||||||
|
QXlsx::Document xlsx2;
|
||||||
|
xlsx2.addWorksheet();
|
||||||
|
xlsx2.saveAs("Book2.xlsx");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -6,6 +6,6 @@ SUBDIRS=\
|
|||||||
relationships \
|
relationships \
|
||||||
propscore \
|
propscore \
|
||||||
propsapp \
|
propsapp \
|
||||||
readdocument \
|
document \
|
||||||
sharedstrings \
|
sharedstrings \
|
||||||
styles
|
styles
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
QT += testlib xlsx xlsx-private
|
QT += testlib xlsx # xlsx-private
|
||||||
CONFIG += testcase
|
CONFIG += testcase
|
||||||
DEFINES += XLSX_TEST
|
DEFINES += XLSX_TEST
|
||||||
|
|
||||||
TARGET = tst_readdocumenttest
|
TARGET = tst_document
|
||||||
CONFIG += console
|
CONFIG += console
|
||||||
CONFIG -= app_bundle
|
CONFIG -= app_bundle
|
||||||
|
|
||||||
TEMPLATE = app
|
TEMPLATE = app
|
||||||
|
|
||||||
|
|
||||||
SOURCES += tst_readdocumenttest.cpp
|
SOURCES += tst_documenttest.cpp
|
||||||
DEFINES += SRCDIR=\\\"$$PWD/\\\"
|
DEFINES += SRCDIR=\\\"$$PWD/\\\"
|
||||||
@@ -0,0 +1,161 @@
|
|||||||
|
#include "xlsxdocument.h"
|
||||||
|
#include "xlsxcell.h"
|
||||||
|
#include "xlsxformat.h"
|
||||||
|
#include <QString>
|
||||||
|
#include <QtTest>
|
||||||
|
|
||||||
|
QTXLSX_USE_NAMESPACE
|
||||||
|
|
||||||
|
class DocumentTest : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
DocumentTest();
|
||||||
|
|
||||||
|
private Q_SLOTS:
|
||||||
|
void testDocumentProperty();
|
||||||
|
void testReadWriteString();
|
||||||
|
void testReadWriteNumeric();
|
||||||
|
void testReadWriteBool();
|
||||||
|
void testReadWriteBlank();
|
||||||
|
void testReadWriteFormula();
|
||||||
|
};
|
||||||
|
|
||||||
|
DocumentTest::DocumentTest()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void DocumentTest::testDocumentProperty()
|
||||||
|
{
|
||||||
|
Document xlsx1;
|
||||||
|
xlsx1.setDocumentProperty("creator", "Debao");
|
||||||
|
xlsx1.setDocumentProperty("company", "Test");
|
||||||
|
xlsx1.saveAs("test.xlsx");
|
||||||
|
|
||||||
|
Document xlsx2("test.xlsx");
|
||||||
|
QCOMPARE(xlsx2.documentProperty("creator"), QString("Debao"));
|
||||||
|
QCOMPARE(xlsx2.documentProperty("company"), QString("Test"));
|
||||||
|
|
||||||
|
QFile::remove("test.xlsx");
|
||||||
|
}
|
||||||
|
|
||||||
|
void DocumentTest::testReadWriteString()
|
||||||
|
{
|
||||||
|
Document xlsx1;
|
||||||
|
xlsx1.write("A1", "Hello Qt!");
|
||||||
|
Format *format = xlsx1.createFormat();
|
||||||
|
format->setFontColor(Qt::blue);
|
||||||
|
format->setBorderStyle(Format::BorderDashDotDot);
|
||||||
|
format->setFillPattern(Format::PatternSolid);
|
||||||
|
xlsx1.write("A2", "Hello Qt again!", format);
|
||||||
|
xlsx1.saveAs("test.xlsx");
|
||||||
|
|
||||||
|
Document xlsx2("test.xlsx");
|
||||||
|
QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::String);
|
||||||
|
QCOMPARE(xlsx2.cellAt("A1")->value().toString(), QString("Hello Qt!"));
|
||||||
|
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::String);
|
||||||
|
QCOMPARE(xlsx2.cellAt("A2")->value().toString(), QString("Hello Qt again!"));
|
||||||
|
QVERIFY(xlsx2.cellAt("A2")->format()!=0);
|
||||||
|
QCOMPARE(*xlsx2.cellAt("A2")->format(), *format);
|
||||||
|
|
||||||
|
QFile::remove("test.xlsx");
|
||||||
|
}
|
||||||
|
|
||||||
|
void DocumentTest::testReadWriteNumeric()
|
||||||
|
{
|
||||||
|
Document xlsx1;
|
||||||
|
xlsx1.write("A1", 123);
|
||||||
|
Format *format = xlsx1.createFormat();
|
||||||
|
format->setFontColor(Qt::blue);
|
||||||
|
format->setBorderStyle(Format::BorderDashDotDot);
|
||||||
|
format->setFillPattern(Format::PatternSolid);
|
||||||
|
format->setNumberFormatIndex(10);
|
||||||
|
xlsx1.write("A2", 12345, format);
|
||||||
|
xlsx1.saveAs("test.xlsx");
|
||||||
|
|
||||||
|
Document xlsx2("test.xlsx");
|
||||||
|
QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Numeric);
|
||||||
|
QCOMPARE(xlsx2.cellAt("A1")->value().toDouble(), 123.0);
|
||||||
|
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Numeric);
|
||||||
|
QCOMPARE(xlsx2.cellAt("A2")->value().toDouble(), 12345.0);
|
||||||
|
QVERIFY(xlsx2.cellAt("A2")->format()!=0);
|
||||||
|
QCOMPARE(*xlsx2.cellAt("A2")->format(), *format);
|
||||||
|
|
||||||
|
QFile::remove("test.xlsx");
|
||||||
|
}
|
||||||
|
|
||||||
|
void DocumentTest::testReadWriteBool()
|
||||||
|
{
|
||||||
|
Document xlsx1;
|
||||||
|
xlsx1.write("A1", true);
|
||||||
|
Format *format = xlsx1.createFormat();
|
||||||
|
format->setFontColor(Qt::blue);
|
||||||
|
format->setBorderStyle(Format::BorderDashDotDot);
|
||||||
|
format->setFillPattern(Format::PatternSolid);
|
||||||
|
xlsx1.write("A2", false, format);
|
||||||
|
xlsx1.saveAs("test.xlsx");
|
||||||
|
|
||||||
|
Document xlsx2("test.xlsx");
|
||||||
|
QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Boolean);
|
||||||
|
QCOMPARE(xlsx2.cellAt("A1")->value().toBool(), true);
|
||||||
|
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Boolean);
|
||||||
|
QCOMPARE(xlsx2.cellAt("A2")->value().toBool(), false);
|
||||||
|
QVERIFY(xlsx2.cellAt("A2")->format()!=0);
|
||||||
|
QCOMPARE(*xlsx2.cellAt("A2")->format(), *format);
|
||||||
|
|
||||||
|
QFile::remove("test.xlsx");
|
||||||
|
}
|
||||||
|
|
||||||
|
void DocumentTest::testReadWriteBlank()
|
||||||
|
{
|
||||||
|
Document xlsx1;
|
||||||
|
xlsx1.write("A1", QVariant());
|
||||||
|
Format *format = xlsx1.createFormat();
|
||||||
|
format->setFontColor(Qt::blue);
|
||||||
|
format->setBorderStyle(Format::BorderDashDotDot);
|
||||||
|
format->setFillPattern(Format::PatternSolid);
|
||||||
|
xlsx1.write("A2", QVariant(), format);
|
||||||
|
xlsx1.saveAs("test.xlsx");
|
||||||
|
|
||||||
|
Document xlsx2("test.xlsx");
|
||||||
|
QVERIFY(xlsx2.cellAt("A1"));
|
||||||
|
QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Blank);
|
||||||
|
QVERIFY(!xlsx2.cellAt("A1")->value().isValid());
|
||||||
|
QVERIFY(xlsx2.cellAt("A2"));
|
||||||
|
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Blank);
|
||||||
|
QVERIFY(!xlsx2.cellAt("A2")->value().isValid());
|
||||||
|
QVERIFY(xlsx2.cellAt("A2")->format()!=0);
|
||||||
|
QCOMPARE(*xlsx2.cellAt("A2")->format(), *format);
|
||||||
|
|
||||||
|
QFile::remove("test.xlsx");
|
||||||
|
}
|
||||||
|
|
||||||
|
void DocumentTest::testReadWriteFormula()
|
||||||
|
{
|
||||||
|
Document xlsx1;
|
||||||
|
xlsx1.write("A1", "=11+22");
|
||||||
|
Format *format = xlsx1.createFormat();
|
||||||
|
format->setFontColor(Qt::blue);
|
||||||
|
format->setBorderStyle(Format::BorderDashDotDot);
|
||||||
|
format->setFillPattern(Format::PatternSolid);
|
||||||
|
xlsx1.write("A2", "=22+33", format);
|
||||||
|
xlsx1.saveAs("test.xlsx");
|
||||||
|
|
||||||
|
|
||||||
|
Document xlsx2("test.xlsx");
|
||||||
|
QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Formula);
|
||||||
|
// QCOMPARE(xlsx2.cellAt("A1")->value().toDouble(), 0.0);
|
||||||
|
QCOMPARE(xlsx2.cellAt("A1")->formula(), QStringLiteral("11+22"));
|
||||||
|
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Formula);
|
||||||
|
// QCOMPARE(xlsx2.cellAt("A2")->value().toDouble(), 0.0);
|
||||||
|
QCOMPARE(xlsx2.cellAt("A2")->formula(), QStringLiteral("22+33"));
|
||||||
|
QVERIFY(xlsx2.cellAt("A2")->format()!=0);
|
||||||
|
QCOMPARE(*xlsx2.cellAt("A2")->format(), *format);
|
||||||
|
|
||||||
|
QFile::remove("test.xlsx");
|
||||||
|
}
|
||||||
|
|
||||||
|
QTEST_APPLESS_MAIN(DocumentTest)
|
||||||
|
|
||||||
|
#include "tst_documenttest.moc"
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
#include "xlsxdocument.h"
|
|
||||||
#include <QString>
|
|
||||||
#include <QtTest>
|
|
||||||
|
|
||||||
class ReadDocumentTest : public QObject
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
|
|
||||||
public:
|
|
||||||
ReadDocumentTest();
|
|
||||||
|
|
||||||
private Q_SLOTS:
|
|
||||||
void testDocProps();
|
|
||||||
};
|
|
||||||
|
|
||||||
ReadDocumentTest::ReadDocumentTest()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void ReadDocumentTest::testDocProps()
|
|
||||||
{
|
|
||||||
QXlsx::Document doc1;
|
|
||||||
doc1.setDocumentProperty("creator", "Debao");
|
|
||||||
doc1.setDocumentProperty("company", "Test");
|
|
||||||
doc1.saveAs("test.xlsx");
|
|
||||||
|
|
||||||
QXlsx::Document doc2("test.xlsx");
|
|
||||||
QCOMPARE(doc2.documentProperty("creator"), QString("Debao"));
|
|
||||||
QCOMPARE(doc2.documentProperty("company"), QString("Test"));
|
|
||||||
|
|
||||||
QFile::remove("test.xlsx");
|
|
||||||
}
|
|
||||||
|
|
||||||
QTEST_APPLESS_MAIN(ReadDocumentTest)
|
|
||||||
|
|
||||||
#include "tst_readdocumenttest.moc"
|
|
||||||
Reference in New Issue
Block a user