Skip to content

Commit

Permalink
test(parser_test.cpp): test loading from file
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamska1008 committed Aug 31, 2024
1 parent 4b24834 commit 78d8696
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
8 changes: 7 additions & 1 deletion include/myxml/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ namespace myxml

class IOError : public std::exception
{

private:
std::string message;

public:
IOError(std::string);

virtual const char *what() const noexcept override;
};
}
10 changes: 10 additions & 0 deletions src/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ namespace myxml
: ParseError("End of input")
{
}

IOError::IOError(std::string message)
: message(message)
{
}

const char *IOError::what() const noexcept
{
return message.c_str();
}
}
8 changes: 4 additions & 4 deletions src/xmlfile.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// TODO: support windows

#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fmt/core.h>

#include "myxml/xmlfile.hpp"
#include "myxml/error.hpp"
Expand All @@ -22,18 +22,18 @@ namespace myxml
xfile->fd = open(fpath.data(), O_RDONLY);
if (xfile->fd == -1)
{
throw IOError();
throw IOError(fmt::format("Failed to open file: {}", fpath));
}
struct stat fileInfo;
if (fstat(xfile->fd, &fileInfo) == -1)
{
throw IOError();
throw IOError(fmt::format("Failed to get info of file: {}", fpath));
}
xfile->fileSize = fileInfo.st_size;
void *mappedRegion = mmap(nullptr, xfile->fileSize, PROT_READ, MAP_PRIVATE, xfile->fd, 0);
if (mappedRegion == MAP_FAILED)
{
throw IOError();
throw IOError(fmt::format("Failed to map memory for file: {}", fpath));
}
xfile->inner = static_cast<char *>(mappedRegion);
return xfile;
Expand Down
4 changes: 1 addition & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ 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)
add_test(NAME parser_test COMMAND parser_test WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
add_test(NAME exportable_test COMMAND exportable_test)
add_test(NAME document_test COMMAND document_test)
add_test(NAME buffer_test COMMAND buffer_test)

set_tests_properties(buffer_test PROPERTIES WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/tests)
4 changes: 3 additions & 1 deletion tests/parser_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>
#include <catch2/catch_test_macros.hpp>
#include <filesystem>
#include "myxml/parser.hpp"
#include "myxml/xmlfile.hpp"

Expand Down Expand Up @@ -224,6 +225,7 @@ TEST_CASE("Parsing simple xml elements", "[parser]")

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

0 comments on commit 78d8696

Please sign in to comment.