完善测试用例

This commit is contained in:
玉宇清音 2023-02-25 17:16:29 +08:00
parent c901a8b10b
commit fa5f84006c
4 changed files with 247 additions and 0 deletions

42
CoreTest/CoreTest.pro Normal file
View File

@ -0,0 +1,42 @@
QT -= gui
QT += testlib
QT += xml
msvc {
QMAKE_CFLAGS += /utf-8
QMAKE_CXXFLAGS += /utf-8
}
CONFIG += c++11 console
CONFIG -= app_bundle
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
configtest.cpp \
main.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../libConfig/release/ -llibConfig
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../libConfig/debug/ -llibConfig
else:unix: LIBS += -L$$OUT_PWD/../libConfig/ -llibConfig
INCLUDEPATH += $$PWD/../libConfig
DEPENDPATH += $$PWD/../libConfig
HEADERS += \
configtest.h
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../libProjectManager/release/ -llibProjectManager
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../libProjectManager/debug/ -llibProjectManager
else:unix: LIBS += -L$$OUT_PWD/../libProjectManager/ -llibProjectManager
INCLUDEPATH += $$PWD/../libProjectManager
DEPENDPATH += $$PWD/../libProjectManager

172
CoreTest/configtest.cpp Normal file
View File

@ -0,0 +1,172 @@
#include "configtest.h"
#include <xmlconfig.h>
using namespace Config;
ConfigTest::ConfigTest()
{
}
void ConfigTest::testConfig()
{
XMLConfig xml;
xml.loadFile("./base_test.xml");
QStringList keys;
keys << "test-key" << "test-sub";
xml.setConfig(keys , "omevalue");
QVERIFY(xml.getConfig(keys) == "omevalue");
xml.deleteX(keys);
QVERIFY(xml.getConfig(keys) == "");
xml.setConfig(keys , "omevalue");
QVERIFY(xml.getConfig(keys) == "omevalue");
}
void ConfigTest::testList()
{
XMLConfig xml;
xml.loadFile("./base_test.xml");
QStringList keys;
keys << "test-key" << "test-sub";
QStringList values;
values << "v0" << "v1";
xml.setList(keys, values);
auto temp_list = xml.getList(keys);
for(auto &v : values){
QVERIFY(temp_list.contains(v));
}
xml.deleteX(keys);
QVERIFY(xml.getList(keys).count() == 0);
xml.setList(keys, values);
temp_list = xml.getList(keys);
for(auto &v : values){
QVERIFY(temp_list.contains(v));
}
}
void ConfigTest::testMap()
{
XMLConfig xml;
xml.loadFile("./base_test.xml");
QStringList keys;
keys << "key0" << "key1" << "key2" << "key3";
QHash<QString,QString> map;
map["key0"] = "value0";
map["key1"] = "value1";
map["key2"] = "value2";
map["key3"] = "value3";
xml.setMap(keys, map);
auto temp_map = xml.getMap(keys);
for(auto &v : keys){
QVERIFY(temp_map[v] == map[v]);
}
xml.deleteX(keys);
QVERIFY(xml.getMap(keys).count() == 0);
xml.setMap(keys, map);
temp_map = xml.getMap(keys);
for(auto &v : keys){
QVERIFY(temp_map[v] == map[v]);
}
}
void ConfigTest::testReload()
{
XMLConfig xml;
xml.loadFile("./base_test.xml");
QStringList keys;
keys << "test-key" << "test-sub";
xml.setConfig(keys , "omevalue");
QStringList values;
values << "v0" << "v1";
xml.setList(keys, values);
QHash<QString,QString> map;
map["key0"] = "value0";
map["key1"] = "value1";
map["key2"] = "value2";
map["key3"] = "value3";
xml.setMap(keys, map);
xml.save();
xml.loadFile("./base_test.xml");
QVERIFY(xml.getConfig(keys) == "omevalue");
auto temp_list = xml.getList(keys);
for(auto &v : values){
QVERIFY(temp_list.contains(v));
}
auto temp_map = xml.getMap(keys);
for(auto &v : keys){
QVERIFY(temp_map[v] == map[v]);
}
}
void ConfigTest::testException()
{
ParseException *temp = nullptr;
XMLConfig xml;
QFile test_f("./exists_test.xml");
if(test_f.exists())
test_f.remove();
try {
xml.loadFile("./exists_test.xml");
} catch (ParseException *x) {
}
QVERIFY2(test_f.exists(), "在指定路径生成默认配置文件失败");
QFile anchor("./exception_base_test.xml");
anchor.open(QIODevice::WriteOnly|QIODevice::NewOnly);
try{
xml.loadFile("./exception_base_test.xml");
} catch (ParseException *x) {
temp = x;
}
QVERIFY2(temp != nullptr, "测试无法打开的Exception失败未能探知");
delete temp;
temp = nullptr;
QTextStream to(&anchor);
to << "alkdjfalkfj";
to.flush();
anchor.close();
try{
xml.loadFile("./exception_base_test.xml");
} catch (ParseException *x) {
temp = x;
}
QVERIFY2(temp != nullptr, "配置文件格式错误探知失败");
delete temp;
}

20
CoreTest/configtest.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef CONFIGTEST_H
#define CONFIGTEST_H
#include <QObject>
#include <QtTest/QTest>
class ConfigTest : public QObject
{
Q_OBJECT
public:
ConfigTest();
private slots:
void testReload();
void testException();
void testConfig();
void testList();
void testMap();
};
#endif // CONFIGTEST_H

13
CoreTest/main.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <QCoreApplication>
#include "configtest.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
ConfigTest t0;
QTest::qExec(&t0, argc, argv);
return a.exec();
}