Add unit test for Worksheet class
This commit is contained in:
@@ -110,7 +110,6 @@ QString Workbook::defaultDateFormat() const
|
||||
{
|
||||
Q_D(const Workbook);
|
||||
return d->defaultDateFormat;
|
||||
|
||||
}
|
||||
|
||||
void Workbook::setDefaultDateFormat(const QString &format)
|
||||
|
||||
@@ -206,6 +206,8 @@ Worksheet::Worksheet(const QString &name, Workbook *workbook) :
|
||||
d_ptr(new WorksheetPrivate(this))
|
||||
{
|
||||
d_ptr->name = name;
|
||||
if (!workbook) //For unit test propose only. Ignore the memery leak.
|
||||
workbook = new Workbook;
|
||||
d_ptr->workbook = workbook;
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ class QIODevice;
|
||||
class QDateTime;
|
||||
class QUrl;
|
||||
class QImage;
|
||||
class WorksheetTest;
|
||||
|
||||
QT_BEGIN_NAMESPACE_XLSX
|
||||
class Package;
|
||||
@@ -72,11 +73,6 @@ public:
|
||||
void setRightToLeft(bool enable);
|
||||
void setZeroValuesHidden(bool enable);
|
||||
|
||||
void saveToXmlFile(QIODevice *device);
|
||||
QByteArray saveToXmlData();
|
||||
bool loadFromXmlFile(QIODevice *device);
|
||||
bool loadFromXmlData(const QByteArray &data);
|
||||
|
||||
QString sheetName() const;
|
||||
void setSheetName(const QString &sheetName);
|
||||
|
||||
@@ -84,7 +80,13 @@ public:
|
||||
private:
|
||||
friend class Package;
|
||||
friend class Workbook;
|
||||
Worksheet(const QString &sheetName, Workbook *book=0);
|
||||
friend class WorksheetTest;
|
||||
Worksheet(const QString &sheetName, Workbook *book);
|
||||
|
||||
void saveToXmlFile(QIODevice *device);
|
||||
QByteArray saveToXmlData();
|
||||
bool loadFromXmlFile(QIODevice *device);
|
||||
bool loadFromXmlData(const QByteArray &data);
|
||||
|
||||
virtual bool isChartsheet() const;
|
||||
bool isHidden() const;
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
TEMPLATE=subdirs
|
||||
SUBDIRS=\
|
||||
utility \
|
||||
mergecell \
|
||||
worksheet \
|
||||
zipreader \
|
||||
relationships \
|
||||
propscore \
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
#include <QBuffer>
|
||||
#include <QtTest>
|
||||
|
||||
#include "xlsxworksheet.h"
|
||||
#include "xlsxdocument.h"
|
||||
|
||||
class MergeCellTest : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MergeCellTest();
|
||||
|
||||
private Q_SLOTS:
|
||||
void testWithoutMerge();
|
||||
void testMerge();
|
||||
void testUnMerge();
|
||||
};
|
||||
|
||||
MergeCellTest::MergeCellTest()
|
||||
{
|
||||
}
|
||||
|
||||
void MergeCellTest::testWithoutMerge()
|
||||
{
|
||||
QXlsx::Document xlsx;
|
||||
xlsx.write("B1", "Hello");
|
||||
|
||||
QByteArray xmldata;
|
||||
QBuffer buffer(&xmldata);
|
||||
buffer.open(QIODevice::WriteOnly);
|
||||
xlsx.currentWorksheet()->saveToXmlFile(&buffer);
|
||||
|
||||
QVERIFY2(!xmldata.contains("<mergeCell"), "");
|
||||
}
|
||||
|
||||
void MergeCellTest::testMerge()
|
||||
{
|
||||
QXlsx::Document xlsx;
|
||||
xlsx.write("B1", "Test Merged Cell");
|
||||
xlsx.mergeCells("B1:B5");
|
||||
|
||||
QByteArray xmldata;
|
||||
QBuffer buffer(&xmldata);
|
||||
buffer.open(QIODevice::WriteOnly);
|
||||
xlsx.currentWorksheet()->saveToXmlFile(&buffer);
|
||||
|
||||
QVERIFY2(xmldata.contains("<mergeCells count=\"1\"><mergeCell ref=\"B1:B5\"/></mergeCells>"), "");
|
||||
}
|
||||
|
||||
void MergeCellTest::testUnMerge()
|
||||
{
|
||||
QXlsx::Document xlsx;
|
||||
xlsx.write("B1", "Test Merged Cell");
|
||||
xlsx.mergeCells("B1:B5");
|
||||
xlsx.unmergeCells("B1:B5");
|
||||
|
||||
QByteArray xmldata;
|
||||
QBuffer buffer(&xmldata);
|
||||
buffer.open(QIODevice::WriteOnly);
|
||||
xlsx.currentWorksheet()->saveToXmlFile(&buffer);
|
||||
|
||||
QVERIFY2(!xmldata.contains("<mergeCell"), "");
|
||||
}
|
||||
|
||||
QTEST_APPLESS_MAIN(MergeCellTest)
|
||||
|
||||
#include "tst_mergecelltest.moc"
|
||||
@@ -0,0 +1,56 @@
|
||||
#include <QBuffer>
|
||||
#include <QtTest>
|
||||
|
||||
#include "xlsxworksheet.h"
|
||||
|
||||
class WorksheetTest : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
WorksheetTest();
|
||||
|
||||
private Q_SLOTS:
|
||||
void testEmptySheet();
|
||||
void testMerge();
|
||||
void testUnMerge();
|
||||
};
|
||||
|
||||
WorksheetTest::WorksheetTest()
|
||||
{
|
||||
}
|
||||
|
||||
void WorksheetTest::testEmptySheet()
|
||||
{
|
||||
QXlsx::Worksheet sheet("", 0);
|
||||
sheet.write("B1", 123);
|
||||
QByteArray xmldata = sheet.saveToXmlData();
|
||||
|
||||
QVERIFY2(!xmldata.contains("<mergeCell"), "");
|
||||
}
|
||||
|
||||
void WorksheetTest::testMerge()
|
||||
{
|
||||
QXlsx::Worksheet sheet("", 0);
|
||||
sheet.write("B1", 123);
|
||||
sheet.mergeCells("B1:B5");
|
||||
QByteArray xmldata = sheet.saveToXmlData();
|
||||
|
||||
QVERIFY2(xmldata.contains("<mergeCells count=\"1\"><mergeCell ref=\"B1:B5\"/></mergeCells>"), "");
|
||||
}
|
||||
|
||||
void WorksheetTest::testUnMerge()
|
||||
{
|
||||
QXlsx::Worksheet sheet("", 0);
|
||||
sheet.write("B1", 123);
|
||||
sheet.mergeCells("B1:B5");
|
||||
sheet.unmergeCells("B1:B5");
|
||||
|
||||
QByteArray xmldata = sheet.saveToXmlData();
|
||||
|
||||
QVERIFY2(!xmldata.contains("<mergeCell"), "");
|
||||
}
|
||||
|
||||
QTEST_APPLESS_MAIN(WorksheetTest)
|
||||
|
||||
#include "tst_worksheet.moc"
|
||||
@@ -8,11 +8,11 @@ QT += testlib xlsx xlsx-private
|
||||
CONFIG += testcase
|
||||
DEFINES += XLSX_TEST
|
||||
|
||||
TARGET = tst_mergecelltest
|
||||
TARGET = tst_worksheet
|
||||
CONFIG += console
|
||||
CONFIG -= app_bundle
|
||||
|
||||
TEMPLATE = app
|
||||
|
||||
SOURCES += tst_mergecelltest.cpp
|
||||
SOURCES += tst_worksheet.cpp
|
||||
DEFINES += SRCDIR=\\\"$$PWD/\\\"
|
||||
Reference in New Issue
Block a user