From 3bddc86be10f704cb16a8e35b4d0f0620978d56a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=BB=A3=E7=A0=812016?= Date: Wed, 1 Feb 2023 12:50:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9F=A5=E6=89=BE=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 ++- data/simple.xml | 10 +++++++- test/test_tinyxml2_find_node.cpp | 42 ++++++++++++++++++++++++++++++++ test/test_tinyxml2_load.cpp | 42 +++++++++++++++++++++++++++++--- test/tmp_test.cpp | 18 ++++++++++++++ 5 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 test/test_tinyxml2_find_node.cpp create mode 100644 test/tmp_test.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json index 74f6069..7614e59 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -44,6 +44,7 @@ "xstddef": "cpp", "xstring": "cpp", "xtr1common": "cpp", - "xutility": "cpp" + "xutility": "cpp", + "format": "cpp" } } \ No newline at end of file diff --git a/data/simple.xml b/data/simple.xml index 346f499..2310a0e 100644 --- a/data/simple.xml +++ b/data/simple.xml @@ -1,4 +1,12 @@ - + + 这是一段介绍. 这是一段介绍. + + + 这是一段介绍. 这是一段介绍. + + + 这是一段介绍. 这是一段介绍. + \ No newline at end of file diff --git a/test/test_tinyxml2_find_node.cpp b/test/test_tinyxml2_find_node.cpp new file mode 100644 index 0000000..990740d --- /dev/null +++ b/test/test_tinyxml2_find_node.cpp @@ -0,0 +1,42 @@ +#include +#include + +#include "doctest/doctest.h" +#include "tinyxml2.h" +#include "spdlog/spdlog.h" +#include "khl_xml_config/khl_xml_config.hpp" + +TEST_CASE("tinyxml2_find_node") +{ + std::string simpleXmlPath; + + tinyxml2::XMLDocument doc; + tinyxml2::XMLError xmlError; + + /** + * 查找节点 + */ + SUBCASE("find_node") + { + simpleXmlPath = getXmlPath("/data/simple.xml"); + xmlError = doc.LoadFile(simpleXmlPath.c_str()); + CHECK(tinyxml2::XMLError::XML_SUCCESS == xmlError); + + const char* id = "1002"; + + auto rootEl = doc.RootElement(); + auto personNode = rootEl->FirstChildElement("person"); + while( nullptr != personNode ) + { + if( strcmp(id,personNode->Attribute("id"))) + { + break; + } + + personNode = personNode->NextSiblingElement(); + } + + bool find = nullptr != personNode; + CHECK(find); + } +} \ No newline at end of file diff --git a/test/test_tinyxml2_load.cpp b/test/test_tinyxml2_load.cpp index 2ef2d98..e822a1e 100644 --- a/test/test_tinyxml2_load.cpp +++ b/test/test_tinyxml2_load.cpp @@ -1,9 +1,8 @@ -#include "doctest/doctest.h" +#include +#include "doctest/doctest.h" #include "tinyxml2.h" - #include "spdlog/spdlog.h" - #include "khl_xml_config/khl_xml_config.hpp" TEST_CASE("tinyxml2_load") @@ -32,4 +31,41 @@ TEST_CASE("tinyxml2_load") xmlError = doc.LoadFile(simpleXmlPath.c_str()); CHECK(tinyxml2::XMLError::XML_ERROR_FILE_NOT_FOUND == xmlError); } + + /** + * 使用 FILE * 加载 + */ + SUBCASE("load_by_file") + { + simpleXmlPath = getXmlPath("/data/simple.xml"); + FILE *file = fopen(simpleXmlPath.c_str(), "rb"); + bool openSuccess = NULL != file; + CHECK(openSuccess); + + xmlError = doc.LoadFile(simpleXmlPath.c_str()); + CHECK(tinyxml2::XMLError::XML_SUCCESS == xmlError); + + bool closeSuccess = 0 == fclose(file); + CHECK(closeSuccess); + } + + /** + * 从字符串加载 + */ + SUBCASE("load_by_charbuffe") + { + static const char *xml = ""; + xmlError = doc.Parse(xml); + CHECK(tinyxml2::XMLError::XML_SUCCESS == xmlError); + } + + /** + * 从字符串加载 + */ + SUBCASE("load_by_charbuffe") + { + static const char *xml = ""; + xmlError = doc.Parse(xml); + CHECK(tinyxml2::XMLError::XML_ERROR_EMPTY_DOCUMENT == xmlError); + } } \ No newline at end of file diff --git a/test/tmp_test.cpp b/test/tmp_test.cpp new file mode 100644 index 0000000..d2e2f89 --- /dev/null +++ b/test/tmp_test.cpp @@ -0,0 +1,18 @@ +#include +#include +#include + +#include "doctest/doctest.h" + +#include "spdlog/spdlog.h" + +#include "khl_xml_config/khl_xml_config.hpp" + +TEST_CASE("test_tmp") +{ + spdlog::info("test tmp"); + + char* a = "one"; + char* b = "one"; + CHECK(0 == strcmp(a,b)); +} \ No newline at end of file -- GitLab