-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test/fix: Add test for pattern language exports (except HTML) + fix H…
…TML formatter (#107)
- Loading branch information
Showing
6 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters