From 1a6111e3f02b0a9cff0f81fb524b4dfb7d69854b Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Tue, 4 Nov 2025 21:59:42 +0800 Subject: Move xml to base. --- test/CMakeLists.txt | 1 - test/base/CMakeLists.txt | 1 + test/base/xml/ParserTest.cpp | 116 +++++++++++++++++++++++++++++++++++++++++++ test/xml/CMakeLists.txt | 6 --- test/xml/ParserTest.cpp | 116 ------------------------------------------- 5 files changed, 117 insertions(+), 123 deletions(-) create mode 100644 test/base/xml/ParserTest.cpp delete mode 100644 test/xml/CMakeLists.txt delete mode 100644 test/xml/ParserTest.cpp (limited to 'test') diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index cd51b2f3..22f66e40 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -17,6 +17,5 @@ endfunction() add_subdirectory(base) add_subdirectory(platform) -add_subdirectory(xml) add_subdirectory(toml) add_subdirectory(ui) diff --git a/test/base/CMakeLists.txt b/test/base/CMakeLists.txt index 39895c3e..b36bbd23 100644 --- a/test/base/CMakeLists.txt +++ b/test/base/CMakeLists.txt @@ -6,6 +6,7 @@ add_executable(CruBaseTest StringUtilTest.cpp SubProcessTest.cpp TimerTest.cpp + xml/ParserTest.cpp ) target_link_libraries(CruBaseTest PRIVATE CruBase CruTestBase) diff --git a/test/base/xml/ParserTest.cpp b/test/base/xml/ParserTest.cpp new file mode 100644 index 00000000..f7544f02 --- /dev/null +++ b/test/base/xml/ParserTest.cpp @@ -0,0 +1,116 @@ +#include "cru/base/xml/XmlNode.h" +#include "cru/base/xml/XmlParser.h" + +#include + +using namespace cru::xml; + +TEST_CASE("CruXmlParserTest Simple", "[xml]") { + XmlParser parser(""); + auto n = parser.Parse(); + REQUIRE(n->GetTag() == "root"); + REQUIRE(n->GetAttributes().empty() == true); + REQUIRE(n->GetChildCount() == 0); + delete n; +} + +TEST_CASE("CruXmlParserTest SimpleWithAttribute", "[xml]") { + XmlParser parser(""); + auto n = parser.Parse(); + REQUIRE(n->GetTag() == "root"); + REQUIRE(n->GetAttributeValue("a1") == "v1"); + REQUIRE(n->GetAttributeValue("a2") == "v2"); + REQUIRE(n->GetChildCount() == 0); + delete n; +} + +TEST_CASE("CruXmlParserTest SimpleSelfClosing", "[xml]") { + XmlParser parser(""); + auto n = parser.Parse(); + REQUIRE(n->GetTag() == "root"); + REQUIRE(n->GetAttributeValue("a1") == "v1"); + REQUIRE(n->GetAttributeValue("a2") == "v2"); + REQUIRE(n->GetChildCount() == 0); + delete n; +} + +TEST_CASE("CruXmlParserTest NestedElement", "[xml]") { + XmlParser parser( + ""); + auto n = parser.Parse(); + REQUIRE(n->GetChildren().size() == 2); + REQUIRE(n->GetChildAt(0)->AsElement()->GetTag() == "c1"); + REQUIRE(n->GetChildAt(1)->AsElement()->GetTag() == "c2"); + REQUIRE(n->GetChildAt(0)->AsElement()->GetChildCount() == 1); + REQUIRE(n->GetChildAt(0)->AsElement()->GetChildAt(0)->AsElement()->GetTag() == + "d1"); + REQUIRE(n->GetChildAt(1)->AsElement()->GetChildCount() == 2); + REQUIRE(n->GetChildAt(1)->AsElement()->GetChildAt(0)->AsElement()->GetTag() == + "d2"); + REQUIRE(n->GetChildAt(1)->AsElement()->GetChildAt(1)->AsElement()->GetTag() == + "d3"); + delete n; +} + +TEST_CASE("CruXmlParserTest SimpleText", "[xml]") { + XmlParser parser("text"); + auto n = parser.Parse(); + REQUIRE(n->GetChildCount() == 1); + REQUIRE(n->GetChildAt(0)->AsText()->GetText() == "text"); + delete n; +} + +TEST_CASE("CruXmlParserTest Whitespace", "[xml]") { + XmlParser parser("\t\t\n\t\t\ttext test\n\t\t\t\t"); + auto n = parser.Parse(); + REQUIRE(n->GetChildCount() == 1); + REQUIRE(n->GetChildAt(0)->AsText()->GetText() == "text test"); + delete n; +} + +TEST_CASE("CruXmlParserTest Comment", "[xml]") { + XmlParser parser(""); + auto n = parser.Parse(); + REQUIRE(n->IsCommentNode()); + REQUIRE(n->AsComment()->GetText() == "comment"); + delete n; +} + +TEST_CASE("CruXmlParserTest Complex", "[xml]") { + XmlParser parser( + R"( + + + + + + + t1 + t2 + text test + + t2 + + + + )"); + auto n = parser.Parse(); + REQUIRE(n->GetAttributeValue("a1") == "v1"); + REQUIRE(n->GetChildCount() == 3); + REQUIRE(n->GetChildAt(0)->AsElement()->GetTag() == "c1"); + REQUIRE(n->GetChildAt(0)->AsElement()->GetChildCount() == 1); + auto c2 = n->GetChildAt(1)->AsElement(); + REQUIRE(c2->GetTag() == "c2"); + REQUIRE(c2->GetAttributeValue("a2") == "v2"); + REQUIRE(c2->GetAttributeValue("a3") == "v3"); + REQUIRE(c2->GetChildAt(0)->AsText()->GetText() == "t1"); + auto d2 = c2->GetChildAt(1)->AsElement(); + REQUIRE(d2->GetTag() == "d2"); + REQUIRE(d2->GetAttributeValue("a4") == "v4"); + REQUIRE(c2->GetChildAt(2)->AsText()->GetText() == "text test"); + REQUIRE(c2->GetChildAt(3)->AsElement()->GetTag() == "d3"); + REQUIRE(c2->GetChildAt(4)->AsText()->GetText() == "t2"); + REQUIRE(n->GetChildAt(2)->IsCommentNode()); + REQUIRE(n->GetChildAt(2)->AsComment()->GetText() == "comment"); + delete n; +} diff --git a/test/xml/CMakeLists.txt b/test/xml/CMakeLists.txt deleted file mode 100644 index 07bfa52b..00000000 --- a/test/xml/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -add_executable(CruXmlTest - ParserTest.cpp -) -target_link_libraries(CruXmlTest PRIVATE CruXml CruTestBase) - -cru_catch_discover_tests(CruXmlTest) diff --git a/test/xml/ParserTest.cpp b/test/xml/ParserTest.cpp deleted file mode 100644 index 0d3ab1d7..00000000 --- a/test/xml/ParserTest.cpp +++ /dev/null @@ -1,116 +0,0 @@ -#include "cru/xml/XmlNode.h" -#include "cru/xml/XmlParser.h" - -#include - -using namespace cru::xml; - -TEST_CASE("CruXmlParserTest Simple", "[xml]") { - XmlParser parser(""); - auto n = parser.Parse(); - REQUIRE(n->GetTag() == "root"); - REQUIRE(n->GetAttributes().empty() == true); - REQUIRE(n->GetChildCount() == 0); - delete n; -} - -TEST_CASE("CruXmlParserTest SimpleWithAttribute", "[xml]") { - XmlParser parser(""); - auto n = parser.Parse(); - REQUIRE(n->GetTag() == "root"); - REQUIRE(n->GetAttributeValue("a1") == "v1"); - REQUIRE(n->GetAttributeValue("a2") == "v2"); - REQUIRE(n->GetChildCount() == 0); - delete n; -} - -TEST_CASE("CruXmlParserTest SimpleSelfClosing", "[xml]") { - XmlParser parser(""); - auto n = parser.Parse(); - REQUIRE(n->GetTag() == "root"); - REQUIRE(n->GetAttributeValue("a1") == "v1"); - REQUIRE(n->GetAttributeValue("a2") == "v2"); - REQUIRE(n->GetChildCount() == 0); - delete n; -} - -TEST_CASE("CruXmlParserTest NestedElement", "[xml]") { - XmlParser parser( - ""); - auto n = parser.Parse(); - REQUIRE(n->GetChildren().size() == 2); - REQUIRE(n->GetChildAt(0)->AsElement()->GetTag() == "c1"); - REQUIRE(n->GetChildAt(1)->AsElement()->GetTag() == "c2"); - REQUIRE(n->GetChildAt(0)->AsElement()->GetChildCount() == 1); - REQUIRE(n->GetChildAt(0)->AsElement()->GetChildAt(0)->AsElement()->GetTag() == - "d1"); - REQUIRE(n->GetChildAt(1)->AsElement()->GetChildCount() == 2); - REQUIRE(n->GetChildAt(1)->AsElement()->GetChildAt(0)->AsElement()->GetTag() == - "d2"); - REQUIRE(n->GetChildAt(1)->AsElement()->GetChildAt(1)->AsElement()->GetTag() == - "d3"); - delete n; -} - -TEST_CASE("CruXmlParserTest SimpleText", "[xml]") { - XmlParser parser("text"); - auto n = parser.Parse(); - REQUIRE(n->GetChildCount() == 1); - REQUIRE(n->GetChildAt(0)->AsText()->GetText() == "text"); - delete n; -} - -TEST_CASE("CruXmlParserTest Whitespace", "[xml]") { - XmlParser parser("\t\t\n\t\t\ttext test\n\t\t\t\t"); - auto n = parser.Parse(); - REQUIRE(n->GetChildCount() == 1); - REQUIRE(n->GetChildAt(0)->AsText()->GetText() == "text test"); - delete n; -} - -TEST_CASE("CruXmlParserTest Comment", "[xml]") { - XmlParser parser(""); - auto n = parser.Parse(); - REQUIRE(n->IsCommentNode()); - REQUIRE(n->AsComment()->GetText() == "comment"); - delete n; -} - -TEST_CASE("CruXmlParserTest Complex", "[xml]") { - XmlParser parser( - R"( - - - - - - - t1 - t2 - text test - - t2 - - - - )"); - auto n = parser.Parse(); - REQUIRE(n->GetAttributeValue("a1") == "v1"); - REQUIRE(n->GetChildCount() == 3); - REQUIRE(n->GetChildAt(0)->AsElement()->GetTag() == "c1"); - REQUIRE(n->GetChildAt(0)->AsElement()->GetChildCount() == 1); - auto c2 = n->GetChildAt(1)->AsElement(); - REQUIRE(c2->GetTag() == "c2"); - REQUIRE(c2->GetAttributeValue("a2") == "v2"); - REQUIRE(c2->GetAttributeValue("a3") == "v3"); - REQUIRE(c2->GetChildAt(0)->AsText()->GetText() == "t1"); - auto d2 = c2->GetChildAt(1)->AsElement(); - REQUIRE(d2->GetTag() == "d2"); - REQUIRE(d2->GetAttributeValue("a4") == "v4"); - REQUIRE(c2->GetChildAt(2)->AsText()->GetText() == "text test"); - REQUIRE(c2->GetChildAt(3)->AsElement()->GetTag() == "d3"); - REQUIRE(c2->GetChildAt(4)->AsText()->GetText() == "t2"); - REQUIRE(n->GetChildAt(2)->IsCommentNode()); - REQUIRE(n->GetChildAt(2)->AsComment()->GetText() == "comment"); - delete n; -} -- cgit v1.2.3