Skip to content

Commit

Permalink
test for XmlReader
Browse files Browse the repository at this point in the history
  • Loading branch information
ctapmex committed Oct 17, 2024
1 parent 83ef34f commit 2d8be84
Show file tree
Hide file tree
Showing 19 changed files with 303 additions and 80 deletions.
18 changes: 13 additions & 5 deletions src/colorer/xml/libxml2/LibXmlInputSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,22 @@ void LibXmlInputSource::initZipSource(const UnicodeString& path, const UnicodeSt
PathInJar LibXmlInputSource::getFullPathsToZip(const UnicodeString& path, const UnicodeString* base)
{
if (path.startsWith(jar)) {
const auto path_idx = path.lastIndexOf('!');
auto local_path = colorer::Environment::expandSpecialEnvironment(path);
const auto path_idx = local_path.lastIndexOf('!');
if (path_idx == -1) {
throw InputSourceException("Bad jar uri format: " + path);
throw InputSourceException("Bad jar uri format: " + local_path);
}

UnicodeString path_to_jar;
if (local_path.compare(path) == 0 ) {
path_to_jar = colorer::Environment::getAbsolutePath(
base ? *base : u"", UnicodeString(local_path, jar.length(), path_idx - jar.length()));
}else {
path_to_jar = colorer::Environment::getAbsolutePath(
u"", UnicodeString(local_path, jar.length(), path_idx - jar.length()));
}

const UnicodeString path_to_jar = colorer::Environment::getAbsolutePath(
base ? *base : u"", UnicodeString(path, jar.length(), path_idx - jar.length()));
const UnicodeString path_in_jar(path, path_idx + 1);
const UnicodeString path_in_jar(local_path, path_idx + 1);

const UnicodeString full_path = jar + path_to_jar + u"!" + path_in_jar;
return {full_path, path_to_jar, path_in_jar};
Expand Down
4 changes: 2 additions & 2 deletions src/colorer/xml/libxml2/LibXmlReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
class LibXmlReader
{
public:
explicit LibXmlReader(const UnicodeString& source_file);
explicit LibXmlReader(const XmlInputSource& source);

~LibXmlReader();
Expand All @@ -24,9 +23,10 @@ class LibXmlReader
}

private:
xmlDocPtr xmldoc {nullptr};

xmlDocPtr xmldoc {nullptr};

explicit LibXmlReader(const UnicodeString& source_file);
static void getAttributes(const xmlNode* node, std::unordered_map<UnicodeString, UnicodeString>& data);
void getChildren(xmlNode* node, XMLNode& result);
bool populateNode(xmlNode* node, XMLNode& result);
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ set(unit_tests_SRC
test_environment.cpp
test_hrcparsing.cpp
test_xmlinputsource.cpp
test_xmlreader.cpp
test_common.h
TestLogger.h
)

add_executable(unit_tests ${unit_tests_SRC})
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/TestLogger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef TESTLOGGER_H
#define TESTLOGGER_H

#include <iostream>
#include "colorer/common/Logger.h"

class TestLogger : public Logger
{
public:
static constexpr std::string_view LogLevelStr[] {"off", "error", "warning", "info", "debug", "trace"};

TestLogger() { current_level = LogLevel::LOG_WARN; }

~TestLogger() override = default;

void log(Logger::LogLevel level, const char* /*filename_in*/, int /*line_in*/, const char* /*funcname_in*/,
const char* message) override
{
if (level > current_level) {
return;
}
log_messages.push_back(message);
//std::cerr << message << '\n';
}

bool message_exists() const { return !log_messages.empty(); }
bool message_print(bool waiting_exist = false) const
{
if (!waiting_exist && message_exists()) {
print_messages();
}
return !log_messages.empty();
}
void clean_messages() { log_messages.clear(); }
void print_messages() const
{
for (const auto& msg : log_messages) {
std::cerr << msg << '\n';
}
}

std::vector<std::string> log_messages;

private:
Logger::LogLevel current_level = Logger::LOG_OFF;
};

#endif // TESTLOGGER_H
14 changes: 14 additions & 0 deletions tests/unit/data/catalog-allpacked-env.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog [
<!ENTITY hrd "jar:common-allpacked.zip!hrd">
<!ENTITY catalog-console SYSTEM "jar:$CUR_DIR/data/common-allpacked.zip!hrd/catalog-console.xml">
]>
<catalog xmlns="http://colorer.github.io/schema/v1/catalog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://colorer.github.io/schema/v1/catalog https://colorer.github.io/schema/v1/catalog.xsd">
<hrc-sets>
<location link="jar:common.zip!hrc/proto.hrc" />
<location link="auto" />
</hrc-sets>
<hrd-sets>
&catalog-console;
</hrd-sets>
</catalog>
14 changes: 14 additions & 0 deletions tests/unit/data/catalog-allpacked.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog [
<!ENTITY hrd "jar:common-allpacked.zip!hrd">
<!ENTITY catalog-console SYSTEM "jar:common-allpacked.zip!hrd/catalog-console.xml">
]>
<catalog xmlns="http://colorer.github.io/schema/v1/catalog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://colorer.github.io/schema/v1/catalog https://colorer.github.io/schema/v1/catalog.xsd">
<hrc-sets>
<location link="jar:common.zip!hrc/proto.hrc" />
<location link="auto" />
</hrc-sets>
<hrd-sets>
&catalog-console;
</hrd-sets>
</catalog>
14 changes: 14 additions & 0 deletions tests/unit/data/catalog-env.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog [
<!ENTITY hrd "hrd">
<!ENTITY catalog-console SYSTEM "env:$CUR_DIR/data/hrd/catalog-console.xml">
]>
<catalog xmlns="http://colorer.github.io/schema/v1/catalog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://colorer.github.io/schema/v1/catalog https://colorer.github.io/schema/v1/catalog.xsd">
<hrc-sets>
<location link="hrc/proto.hrc" />
<location link="hrc/auto" />
</hrc-sets>
<hrd-sets>
&catalog-console;
</hrd-sets>
</catalog>
14 changes: 14 additions & 0 deletions tests/unit/data/catalog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog [
<!ENTITY hrd "hrd">
<!ENTITY catalog-console SYSTEM "hrd/catalog-console.xml">
]>
<catalog xmlns="http://colorer.github.io/schema/v1/catalog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://colorer.github.io/schema/v1/catalog https://colorer.github.io/schema/v1/catalog.xsd">
<hrc-sets>
<location link="hrc/proto.hrc" />
<location link="hrc/auto" />
</hrc-sets>
<hrd-sets>
&catalog-console;
</hrd-sets>
</catalog>
Binary file added tests/unit/data/common-allpacked.zip
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/unit/data/hrd/catalog-console.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<hrd class="console" name="default" description="Aqua on blue">
<location link="&hrd;/console/default.hrd"/>
</hrd>
Empty file added tests/unit/data/test1.xml
Empty file.
Empty file added tests/unit/data/test1.zip
Empty file.
Empty file added tests/unit/data/test2.xml
Empty file.
12 changes: 12 additions & 0 deletions tests/unit/data/type_cue.hrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<hrc>
<prototype name="cue" group="other" description="Cue sheet">
<filename>/\.cue$/</filename>
<parameters>
<param value='1' description="lines for 'firstline'"/>
<param name='firstlinebytes' value='5' description="bytes for 'firstline'"/>
</parameters>
</prototype>
<type name="cue">
</type>
</hrc>
8 changes: 8 additions & 0 deletions tests/unit/test_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef TEST_COMMON_H
#define TEST_COMMON_H

#include "TestLogger.h"

extern std::unique_ptr<TestLogger> logger;

#endif //TEST_COMMON_H
24 changes: 1 addition & 23 deletions tests/unit/test_hrcparsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,11 @@

namespace fs = std::filesystem;

TEST_CASE("Create files")
{
auto file1_str = R"(<?xml version="1.0" encoding="UTF-8"?>
<hrc>
<prototype name="cue" group="other" description="Cue sheet">
<filename>/\.cue$/</filename>
<parameters>
<param value='1' description="lines for 'firstline'"/>
<param name='firstlinebytes' value='5' description="bytes for 'firstline'"/>
</parameters>
</prototype>
<type name="cue">
</type>
</hrc>
)";
auto temp_path = fs::current_path();
auto work_dir = temp_path / "test1.hrc";
std::ofstream file_handler;
file_handler.open(work_dir.c_str());
file_handler << file1_str;
file_handler.close();
}

TEST_CASE("Load hrc")
{
auto temp_path = fs::current_path();
auto work_dir = temp_path / "test1.hrc";
auto work_dir = temp_path / "data/type_cue.hrc";
auto uwork_dir = UnicodeString(work_dir.c_str());
XmlInputSource file1(uwork_dir, nullptr);
HrcLibrary lib;
Expand Down
10 changes: 6 additions & 4 deletions tests/unit/test_main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#define CATCH_CONFIG_MAIN

#include <catch2/catch.hpp>
#include "colorer/Common.h"
#include "test_common.h"

std::unique_ptr<TestLogger> logger;


TEST_CASE("All test cases reside in other .cpp files") {

TEST_CASE("All test cases reside in other .cpp files")
{
logger = std::make_unique<TestLogger>();
Log::registerLogger(*logger);
}
Loading

0 comments on commit 2d8be84

Please sign in to comment.