Skip to content

Commit

Permalink
test/fix: Add test for pattern language exports (except HTML) + fix H…
Browse files Browse the repository at this point in the history
…TML formatter (#107)
  • Loading branch information
iTrooz authored May 21, 2024
1 parent b96d430 commit 850e296
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 2 deletions.
11 changes: 10 additions & 1 deletion generators/include/pl/formatters/formatter_html.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ namespace pl::gen::fmt {
for (const auto *pattern : patterns) {
if (pattern->getVisibility() == ptrn::Visibility::Hidden) continue;

content += ::fmt::format(R"html({} {} | {}<br>)html", pattern->getFormattedName(), pattern->getVariableName(), pattern->toString());
// TODO do that in pattern->toString() directly ?
std::string result;
for (char c : pattern->toString()) {
if (std::isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')
result += c;
else
result += ::fmt::format("%{:02X}", c);
}

content += ::fmt::format(R"html({} {} | {}<br>)html", pattern->getFormattedName(), pattern->getVariableName(), result);
}

if (!content.empty())
Expand Down
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set(AVAILABLE_TESTS
Attributes
Pragmas
PragmasFail
Format
)


Expand All @@ -43,7 +44,7 @@ add_executable(pattern_language_tests
# ---- No need to change anything from here downwards unless you know what you're doing ---- #

target_include_directories(pattern_language_tests PRIVATE include)
target_link_libraries(pattern_language_tests PRIVATE libpl fmt::fmt-header-only)
target_link_libraries(pattern_language_tests PRIVATE libpl libpl-gen fmt::fmt-header-only)

set_target_properties(pattern_language_tests PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

Expand Down
16 changes: 16 additions & 0 deletions tests/files/export/json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"data": {
"s": "%89PNG%0D%0A%1A%0A%00",
"ua": 0,
"ub": 3328,
"uc": 1380206665,
"ud": 873070592,
"ue": 16573246628824624646,
"uf": 159330415869275869250811929192955402058,
"sa": -68,
"sb": -11044,
"sc": -25165923,
"sd": 29773251444219,
"se": -1463797564129820304
}
}
14 changes: 14 additions & 0 deletions tests/files/export/yaml.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
data:
s: "\x89PNG\r \x1A \x00"
ua: 0
ub: 3328
uc: 1380206665
ud: 873070592
ue: 16573246628824624646
uf: 159330415869275869250811929192955402058
sa: -68
sb: -11044
sc: -25165923
sd: 29773251444219
se: -1463797564129820304
77 changes: 77 additions & 0 deletions tests/include/test_patterns/test_pattern_format.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#pragma once

#include "test_pattern.hpp"
#include <pl/pattern_language.hpp>
#include <pl/formatters.hpp>
#include <wolv/io/file.hpp>

namespace pl::test {

class TestPatternFormat : public TestPattern {
public:
TestPatternFormat() : TestPattern("Format", Mode::Succeeding) {
}
~TestPatternFormat() override = default;

[[nodiscard]] std::string getSourceCode() const override {
return R"test(
struct MyStruct {
char s[];
u8 ua;
u16 ub;
u32 uc;
u48 ud;
u64 ue;
u128 uf;
s8 sa;
s16 sb;
s32 sc;
s48 sd;
s64 se;
// s128 sf;
};
MyStruct data @ 0x0;
)test";
}

[[nodiscard]] bool runChecks(const std::vector<std::shared_ptr<ptrn::Pattern>> &patterns) const override {
auto formatters = pl::gen::fmt::createFormatters();

// do this to ensure new formatters will be tested (or are least considered)
if (formatters.size() != 3) {
throw std::runtime_error(fmt::format("Expected 3 formatters: JSON, Yaml, HTML. Got {}. If you are adding a new formatter, please add a test for it", formatters.size()));
}

for(auto &formatter : formatters) {
if (formatter->getName() == "html") {
continue; // disable test for html formatter because there is a lot of metadata information, which may often change without indicating a problem
}

auto actualResultBytes = formatter->format(*this->m_runtime);
std::string actualResult(actualResultBytes.begin(), actualResultBytes.end());

std::string inputFilename = "../tests/files/export/"+formatter->getName()+"."+formatter->getFileExtension();
wolv::io::File inputFile(inputFilename, wolv::io::File::Mode::Read);
if (!inputFile.isValid()) {
throw std::runtime_error(fmt::format("Could not open file {}", inputFilename));
}
std::string expectedResult = inputFile.readString();

if (formatter->getName() == "json" || formatter->getName() == "yaml") {
// trim strings
actualResult = wolv::util::trim(actualResult);
expectedResult = wolv::util::trim(expectedResult);
}

if (actualResult != expectedResult) {
throw std::runtime_error(fmt::format("Formatter {} returned unexpected result.\nExpected: {}\nActual: {}", formatter->getName(), expectedResult, actualResult));
}
}

return true;
}

};

}
2 changes: 2 additions & 0 deletions tests/source/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "test_patterns/test_pattern_import.hpp"
#include "test_patterns/test_pattern_pragmas.hpp"
#include "test_patterns/test_pattern_pragmas_fail.hpp"
#include "test_patterns/test_pattern_format.hpp"

std::array Tests = {
TEST(Placement),
Expand Down Expand Up @@ -53,4 +54,5 @@ std::array Tests = {
TEST(StructInheritance),
TEST(Pragmas),
TEST(PragmasFail),
TEST(Format),
};

0 comments on commit 850e296

Please sign in to comment.