Skip to content

Commit

Permalink
test: doc_test
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamska1008 committed Sep 5, 2024
1 parent d386cce commit e023542
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 45 deletions.
8 changes: 4 additions & 4 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ using namespace myxml;
// `std::string xml`
document doc = document::parse(xml);
// or
document doc = document::load(path);
doc = document::load(path);
// get root elem
optional<element> elem = doc.root().first_elem();
std::optional<element> elem = doc.root().first_elem();
// or directly query elem in root
optional<element> elem = doc.first_elem();
elem = doc.first_elem();
// or query by name
optional<element> elem = doc.first_elem("elem");
elem = doc.first_elem("elem");
```

### Element
Expand Down
4 changes: 1 addition & 3 deletions include/myxml/document.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ namespace myxml
void set_root(std::shared_ptr<element_impl> root);

/* Query */
const declaration &get_declaration() const;
declaration &get_declaration();
const element &get_root() const;
element &get_root();
element &root();
element first_elem(std::string_view);
element first_elem();
text first_text();
Expand Down
12 changes: 1 addition & 11 deletions src/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,12 @@ namespace myxml
this->_root = root;
}

const declaration &document::get_declaration() const
{
return this->_decl;
}

declaration &document::get_declaration()
{
return this->_decl;
}

const element &document::get_root() const
{
return this->_root;
}

element &document::get_root()
element &document::root()
{
return this->_root;
}
Expand Down
35 changes: 13 additions & 22 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
include(CTest)

add_executable(element_test element_test.cpp)
add_executable(parser_test parser_test.cpp)
add_executable(printable_test printable_test.cpp)
add_executable(document_test document_test.cpp)
add_executable(buffer_test buffer_test.cpp)
function(add_custom_test test_name)
add_executable(${test_name} ${test_name}.cpp)
target_link_libraries(${test_name} Catch2::Catch2WithMain myxml)
target_compile_features(${test_name} PRIVATE cxx_std_17)
target_compile_features(${test_name} PRIVATE cxx_std_17)
add_test(NAME ${test_name} COMMAND ${test_name} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
endfunction()

target_link_libraries(element_test Catch2::Catch2WithMain myxml)
target_link_libraries(parser_test Catch2::Catch2WithMain myxml)
target_link_libraries(printable_test Catch2::Catch2WithMain myxml)
target_link_libraries(document_test Catch2::Catch2WithMain myxml)
target_link_libraries(buffer_test Catch2::Catch2WithMain myxml)

target_compile_features(element_test PRIVATE cxx_std_17)
target_compile_features(parser_test PRIVATE cxx_std_17)
target_compile_features(printable_test PRIVATE cxx_std_17)
target_compile_features(document_test PRIVATE cxx_std_17)
target_compile_features(buffer_test PRIVATE cxx_std_17)

add_test(NAME element_test COMMAND element_test)
add_test(NAME parser_test COMMAND parser_test WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
add_test(NAME printable_test COMMAND printable_test)
add_test(NAME document_test COMMAND document_test)
add_test(NAME buffer_test COMMAND buffer_test)
add_custom_test(element_test)
add_custom_test(parser_test)
add_custom_test(printable_test)
add_custom_test(document_test)
add_custom_test(buffer_test)
add_custom_test(doc_test)
23 changes: 23 additions & 0 deletions tests/doc_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// All example codes in document
#include <optional>
#include <catch2/catch_test_macros.hpp>
#include "myxml/document.hpp"
#include "myxml/element.hpp"

TEST_CASE("docs/examples", "[doc]")
{
SECTION("1")
{
using namespace myxml;
std::string xml = "<root/>";
document doc = document::parse(xml);
std::string path = "tests/data/example.xml";
doc = document::load(path);
// get root elem
std::optional<element> elem = doc.root().first_elem();
// or directly query elem in root
elem = doc.first_elem();
// or query by name
elem = doc.first_elem("elem");
}
}
9 changes: 5 additions & 4 deletions tests/document_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

TEST_CASE("Simple document", "[document]")
{
using namespace myxml;
SECTION("No decl")
{
std::string input = R"(<root>
<child>Value</child>
</root>)";
auto doc = myxml::document::parse(input);
REQUIRE(doc.get_root().name() == "root");
auto doc = document::parse(input);
REQUIRE(doc.root().name() == "root");
REQUIRE(doc.first_elem("child").name() == "child");
REQUIRE(doc.first_elem("child").first_text().str() == "Value");
}
Expand All @@ -21,7 +22,7 @@ TEST_CASE("Simple document", "[document]")
<child>Value</child>
</root>
)";
auto doc = myxml::document::parse(input);
auto doc = document::parse(input);
REQUIRE(doc.get_declaration().version == "1.0");
REQUIRE(doc.get_declaration().encoding == "UTF-8");
}
Expand All @@ -31,5 +32,5 @@ TEST_CASE("Custom String Literal", "[document]")
{
using namespace myxml::literals;
auto doc = "<root></root>"_doc;
REQUIRE(doc.get_root().name() == "root");
REQUIRE(doc.root().name() == "root");
}
1 change: 0 additions & 1 deletion tests/parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ TEST_CASE("Parsing simple xml elements", "[parser]")

SECTION("Simple File Buffer")
{
std::cout << std::filesystem::current_path() << std::endl;
auto doc = document::load("tests/data/example.xml");
}
}

0 comments on commit e023542

Please sign in to comment.