From fa5f84006c1663207c43bb5d1bd3c40a51159fa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=89=E5=AE=87=E6=B8=85=E9=9F=B3?= <2422523675@qq.com> Date: Sat, 25 Feb 2023 17:16:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CoreTest/CoreTest.pro | 42 ++++++++++ CoreTest/configtest.cpp | 172 ++++++++++++++++++++++++++++++++++++++++ CoreTest/configtest.h | 20 +++++ CoreTest/main.cpp | 13 +++ 4 files changed, 247 insertions(+) create mode 100644 CoreTest/CoreTest.pro create mode 100644 CoreTest/configtest.cpp create mode 100644 CoreTest/configtest.h create mode 100644 CoreTest/main.cpp diff --git a/CoreTest/CoreTest.pro b/CoreTest/CoreTest.pro new file mode 100644 index 0000000..dfda0f9 --- /dev/null +++ b/CoreTest/CoreTest.pro @@ -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 diff --git a/CoreTest/configtest.cpp b/CoreTest/configtest.cpp new file mode 100644 index 0000000..d7547c6 --- /dev/null +++ b/CoreTest/configtest.cpp @@ -0,0 +1,172 @@ +#include "configtest.h" +#include +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 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 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; +} + + + + + + + + + + diff --git a/CoreTest/configtest.h b/CoreTest/configtest.h new file mode 100644 index 0000000..2db6311 --- /dev/null +++ b/CoreTest/configtest.h @@ -0,0 +1,20 @@ +#ifndef CONFIGTEST_H +#define CONFIGTEST_H +#include +#include + +class ConfigTest : public QObject +{ + Q_OBJECT +public: + ConfigTest(); + +private slots: + void testReload(); + void testException(); + void testConfig(); + void testList(); + void testMap(); +}; + +#endif // CONFIGTEST_H diff --git a/CoreTest/main.cpp b/CoreTest/main.cpp new file mode 100644 index 0000000..127c57c --- /dev/null +++ b/CoreTest/main.cpp @@ -0,0 +1,13 @@ +#include +#include "configtest.h" + + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + ConfigTest t0; + QTest::qExec(&t0, argc, argv); + + return a.exec(); +}